Move show and hide keyboard to java memeber function

This commit is contained in:
Benau 2019-05-18 15:35:25 +08:00
parent 0e1c0d1541
commit ede56a3cf8
2 changed files with 42 additions and 47 deletions

View File

@ -1,7 +1,9 @@
package org.supertuxkart.stk_dbg;
import android.app.NativeActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.inputmethod.InputMethodManager;
import android.view.KeyEvent;
public class SuperTuxKartActivity extends NativeActivity
@ -33,4 +35,20 @@ public class SuperTuxKartActivity extends NativeActivity
}
return super.dispatchKeyEvent(event);
}
public void showKeyboard()
{
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(getWindow().getDecorView(),
InputMethodManager.SHOW_FORCED);
}
public void hideKeyboard()
{
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(
getWindow().getDecorView().getWindowToken(), 0);
}
}

View File

@ -1375,72 +1375,49 @@ void CIrrDeviceAndroid::showKeyboard(bool show)
{
bool was_detached = false;
JNIEnv* env = NULL;
jint status = Android->activity->vm->GetEnv((void**)&env, JNI_VERSION_1_6);
if (status == JNI_EDETACHED)
{
JavaVMAttachArgs args;
args.version = JNI_VERSION_1_6;
args.name = "NativeThread";
args.group = NULL;
status = Android->activity->vm->AttachCurrentThread(&env, &args);
was_detached = true;
}
if (status != JNI_OK)
{
os::Printer::log("Cannot show keyboard.", ELL_DEBUG);
os::Printer::log("Cannot attach current thread in showKeyboard.", ELL_DEBUG);
return;
}
jobject activity_obj = Android->activity->clazz;
jclass activity_class = env->GetObjectClass(activity_obj);
jclass context = env->FindClass("android/content/Context");
jfieldID im_service_field = env->GetStaticFieldID(context,
"INPUT_METHOD_SERVICE", "Ljava/lang/String;");
jobject im_service_obj = env->GetStaticObjectField(context,
im_service_field);
jclass im_manager = env->FindClass(
"android/view/inputmethod/InputMethodManager");
jmethodID get_system_service = env->GetMethodID( activity_class,
"getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
jobject im_manager_obj = env->CallObjectMethod(activity_obj,
get_system_service, im_service_obj);
jobject native_activity = Android->activity->clazz;
jclass class_native_activity = env->GetObjectClass(native_activity);
jmethodID get_window = env->GetMethodID(activity_class, "getWindow",
"()Landroid/view/Window;");
jobject window_obj = env->CallObjectMethod(activity_obj, get_window);
jclass window = env->FindClass("android/view/Window");
jmethodID get_decor_view = env->GetMethodID(window, "getDecorView",
"()Landroid/view/View;");
jobject decor_view_obj = env->CallObjectMethod(window_obj, get_decor_view);
if (show)
if (class_native_activity == NULL)
{
jmethodID show_soft_input = env->GetMethodID(im_manager,
"showSoftInput", "(Landroid/view/View;I)Z");
jint flags = 0;
jboolean result = env->CallBooleanMethod(im_manager_obj,
show_soft_input, decor_view_obj, flags);
}
else
{
jclass view = env->FindClass("android/view/View");
jmethodID get_window_token = env->GetMethodID(view, "getWindowToken",
"()Landroid/os/IBinder;");
jobject token = env->CallObjectMethod(decor_view_obj, get_window_token);
jmethodID hide_soft_input = env->GetMethodID(im_manager,
"hideSoftInputFromWindow", "(Landroid/os/IBinder;I)Z");
jint flags = 0;
jboolean result = env->CallBooleanMethod(im_manager_obj,
hide_soft_input, token, flags);
os::Printer::log("showKeyboard unable to find object class.", ELL_ERROR);
if (was_detached)
{
Android->activity->vm->DetachCurrentThread();
}
return;
}
jmethodID method_id = env->GetMethodID(class_native_activity, show ? "showKeyboard" : "hideKeyboard", "()V");
if (method_id == NULL)
{
os::Printer::log("showKeyboard unable to find method id.", ELL_ERROR);
if (was_detached)
{
Android->activity->vm->DetachCurrentThread();
}
return;
}
env->CallVoidMethod(native_activity, method_id);
if (was_detached)
{
Android->activity->vm->DetachCurrentThread();