Avoid updating editbox if hardware keyboard connected which discards all input

This commit is contained in:
Benau 2020-03-15 13:58:51 +08:00
parent 7174bcb741
commit 932f0f888e
2 changed files with 63 additions and 6 deletions

View File

@ -7,6 +7,7 @@ import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
@ -80,7 +81,7 @@ public class SuperTuxKartActivity extends NativeActivity
@Override
public void afterTextChanged(Editable edit)
{
if (m_stk_edittext != null)
if (!isHardwareKeyboardConnected() && m_stk_edittext != null)
m_stk_edittext.updateSTKEditBox();
}
});
@ -146,7 +147,7 @@ public class SuperTuxKartActivity extends NativeActivity
// Called when user change cursor / select all text in native android
// keyboard
boolean ret = super.dispatchKeyEvent(event);
if (m_stk_edittext != null)
if (!isHardwareKeyboardConnected() && m_stk_edittext != null)
m_stk_edittext.updateSTKEditBox();
return ret;
}
@ -265,4 +266,10 @@ public class SuperTuxKartActivity extends NativeActivity
return new String[0];
}
}
// ------------------------------------------------------------------------
public boolean isHardwareKeyboardConnected()
{
return getResources().getConfiguration()
.keyboard == Configuration.KEYBOARD_QWERTY;
}
}

View File

@ -1727,11 +1727,61 @@ bool CIrrDeviceAndroid::isGyroscopeAvailable()
bool CIrrDeviceAndroid::hasHardwareKeyboard() const
{
// This can happen when hosting server in android
if (!Android)
return true;
int32_t keyboard = AConfiguration_getKeyboard(Android->config);
return (keyboard == ACONFIGURATION_KEYBOARD_QWERTY);
return false;
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 attach current thread in isHardwareKeyboardConnected.", ELL_DEBUG);
return false;
}
jobject native_activity = Android->activity->clazz;
jclass class_native_activity = env->GetObjectClass(native_activity);
if (class_native_activity == NULL)
{
os::Printer::log("isHardwareKeyboardConnected unable to find object class.", ELL_ERROR);
if (was_detached)
{
Android->activity->vm->DetachCurrentThread();
}
return false;
}
jmethodID method_id = env->GetMethodID(class_native_activity, "isHardwareKeyboardConnected", "()Z");
if (method_id == NULL)
{
os::Printer::log("isHardwareKeyboardConnected unable to find method id.", ELL_ERROR);
if (was_detached)
{
Android->activity->vm->DetachCurrentThread();
}
return false;
}
bool ret = env->CallBooleanMethod(native_activity, method_id);
if (was_detached)
{
Android->activity->vm->DetachCurrentThread();
}
return ret;
}
} // end namespace irr