Android: Simplify the jni code in QtAndroidInput

Let the QJNI classes manager the jni environment and caching of jni
handles.

Change-Id: I8c238375026adf449d6e6e2b521caa6cd63a0fb4
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@theqtcompany.com>
This commit is contained in:
Christian Strømme 2014-10-30 19:43:32 +01:00 committed by Christian Stromme
parent 7a3a3a5694
commit 4835808287

View File

@ -48,10 +48,6 @@ using namespace QtAndroid;
namespace QtAndroidInput namespace QtAndroidInput
{ {
static jmethodID m_showSoftwareKeyboardMethodID = 0;
static jmethodID m_resetSoftwareKeyboardMethodID = 0;
static jmethodID m_hideSoftwareKeyboardMethodID = 0;
static jmethodID m_updateSelectionMethodID = 0;
static bool m_ignoreMouseEvents = false; static bool m_ignoreMouseEvents = false;
static bool m_softwareKeyboardVisible = false; static bool m_softwareKeyboardVisible = false;
@ -62,25 +58,23 @@ namespace QtAndroidInput
void updateSelection(int selStart, int selEnd, int candidatesStart, int candidatesEnd) void updateSelection(int selStart, int selEnd, int candidatesStart, int candidatesEnd)
{ {
AttachedJNIEnv env;
if (!env.jniEnv)
return;
#ifdef QT_DEBUG_ANDROID_IM_PROTOCOL #ifdef QT_DEBUG_ANDROID_IM_PROTOCOL
qDebug() << ">>> UPDATESELECTION" << selStart << selEnd << candidatesStart << candidatesEnd; qDebug() << ">>> UPDATESELECTION" << selStart << selEnd << candidatesStart << candidatesEnd;
#endif #endif
env.jniEnv->CallStaticVoidMethod(applicationClass(), m_updateSelectionMethodID, QJNIObjectPrivate::callStaticMethod<void>(applicationClass(),
selStart, selEnd, candidatesStart, candidatesEnd); "updateSelection",
"(IIII)V",
selStart,
selEnd,
candidatesStart,
candidatesEnd);
} }
void showSoftwareKeyboard(int left, int top, int width, int height, int inputHints) void showSoftwareKeyboard(int left, int top, int width, int height, int inputHints)
{ {
AttachedJNIEnv env; QJNIObjectPrivate::callStaticMethod<void>(applicationClass(),
if (!env.jniEnv) "showSoftwareKeyboard",
return; "(IIIII)V",
env.jniEnv->CallStaticVoidMethod(applicationClass(),
m_showSoftwareKeyboardMethodID,
left, left,
top, top,
width, width,
@ -93,11 +87,7 @@ namespace QtAndroidInput
void resetSoftwareKeyboard() void resetSoftwareKeyboard()
{ {
AttachedJNIEnv env; QJNIObjectPrivate::callStaticMethod<void>(applicationClass(), "resetSoftwareKeyboard");
if (!env.jniEnv)
return;
env.jniEnv->CallStaticVoidMethod(applicationClass(), m_resetSoftwareKeyboardMethodID);
#ifdef QT_DEBUG_ANDROID_IM_PROTOCOL #ifdef QT_DEBUG_ANDROID_IM_PROTOCOL
qDebug() << "@@@ RESETSOFTWAREKEYBOARD"; qDebug() << "@@@ RESETSOFTWAREKEYBOARD";
#endif #endif
@ -105,11 +95,7 @@ namespace QtAndroidInput
void hideSoftwareKeyboard() void hideSoftwareKeyboard()
{ {
AttachedJNIEnv env; QJNIObjectPrivate::callStaticMethod<void>(applicationClass(), "hideSoftwareKeyboard");
if (!env.jniEnv)
return;
env.jniEnv->CallStaticVoidMethod(applicationClass(), m_hideSoftwareKeyboardMethodID);
#ifdef QT_DEBUG_ANDROID_IM_PROTOCOL #ifdef QT_DEBUG_ANDROID_IM_PROTOCOL
qDebug() << "@@@ HIDESOFTWAREKEYBOARD"; qDebug() << "@@@ HIDESOFTWAREKEYBOARD";
#endif #endif
@ -722,13 +708,6 @@ namespace QtAndroidInput
{"keyboardVisibilityChanged", "(Z)V", (void *)keyboardVisibilityChanged} {"keyboardVisibilityChanged", "(Z)V", (void *)keyboardVisibilityChanged}
}; };
#define GET_AND_CHECK_STATIC_METHOD(VAR, CLASS, METHOD_NAME, METHOD_SIGNATURE) \
VAR = env->GetStaticMethodID(CLASS, METHOD_NAME, METHOD_SIGNATURE); \
if (!VAR) { \
__android_log_print(ANDROID_LOG_FATAL, qtTagText(), methodErrorMsgFmt(), METHOD_NAME, METHOD_SIGNATURE); \
return false; \
}
bool registerNatives(JNIEnv *env) bool registerNatives(JNIEnv *env)
{ {
jclass appClass = QtAndroid::applicationClass(); jclass appClass = QtAndroid::applicationClass();
@ -738,10 +717,6 @@ namespace QtAndroidInput
return false; return false;
} }
GET_AND_CHECK_STATIC_METHOD(m_showSoftwareKeyboardMethodID, appClass, "showSoftwareKeyboard", "(IIIII)V");
GET_AND_CHECK_STATIC_METHOD(m_resetSoftwareKeyboardMethodID, appClass, "resetSoftwareKeyboard", "()V");
GET_AND_CHECK_STATIC_METHOD(m_hideSoftwareKeyboardMethodID, appClass, "hideSoftwareKeyboard", "()V");
GET_AND_CHECK_STATIC_METHOD(m_updateSelectionMethodID, appClass, "updateSelection", "(IIII)V");
return true; return true;
} }
} }