Android: Add findClass() function to the QJNIEnvironmentPrivate class.

The static QJNIEnvironmentPrivate::findClass() function exposes the
cache and the class finding code in qjni.

Change-Id: I42043dc993cf9cace042faf763f2a647ba79d97f
Reviewed-by: Paul Olav Tvete <paul.tvete@digia.com>
Reviewed-by: BogDan Vatra <bogdan@kde.org>
This commit is contained in:
Christian Strømme 2014-08-06 17:24:52 +02:00 committed by Christian Stromme
parent 8b0d9a16db
commit afece6e496
6 changed files with 110 additions and 76 deletions

View File

@ -75,28 +75,58 @@ static inline bool exceptionCheckAndClear(JNIEnv *env)
typedef QHash<QString, jclass> JClassHash; typedef QHash<QString, jclass> JClassHash;
Q_GLOBAL_STATIC(JClassHash, cachedClasses) Q_GLOBAL_STATIC(JClassHash, cachedClasses)
static jclass getCachedClass(JNIEnv *env, const char *className) static QString toDotEncodedClassName(const char *className)
{
return QString::fromLatin1(className).replace(QLatin1Char('/'), QLatin1Char('.'));
}
static jclass getCachedClass(const QString &classDotEnc)
{ {
jclass clazz = 0;
QString classDotEnc = QString::fromLatin1(className).replace(QLatin1Char('/'), QLatin1Char('.'));
QHash<QString, jclass>::iterator it = cachedClasses->find(classDotEnc); QHash<QString, jclass>::iterator it = cachedClasses->find(classDotEnc);
if (it == cachedClasses->end()) {
QJNIObjectPrivate classLoader = QtAndroidPrivate::classLoader();
if (!classLoader.isValid())
return 0;
QJNIObjectPrivate stringName = QJNIObjectPrivate::fromString(classDotEnc); if (it == cachedClasses->end())
QJNIObjectPrivate classObject = classLoader.callObjectMethod("loadClass", return 0;
"(Ljava/lang/String;)Ljava/lang/Class;",
stringName.object());
if (!exceptionCheckAndClear(env) && classObject.isValid()) return it.value();
clazz = static_cast<jclass>(env->NewGlobalRef(classObject.object())); }
cachedClasses->insert(classDotEnc, clazz); static jclass findClass(const char *className, JNIEnv *env)
} else { {
clazz = it.value(); const QString &classDotEnc = toDotEncodedClassName(className);
jclass clazz = getCachedClass(classDotEnc);
if (clazz != 0)
return clazz;
jclass fclazz = env->FindClass(className);
if (!exceptionCheckAndClear(env)) {
clazz = static_cast<jclass>(env->NewGlobalRef(fclazz));
env->DeleteLocalRef(fclazz);
} }
cachedClasses->insert(classDotEnc, clazz);
return clazz;
}
static jclass loadClass(const char *className, JNIEnv *env)
{
const QString &classDotEnc = toDotEncodedClassName(className);
jclass clazz = getCachedClass(classDotEnc);
if (clazz != 0)
return clazz;
QJNIObjectPrivate classLoader = QtAndroidPrivate::classLoader();
if (!classLoader.isValid())
return 0;
QJNIObjectPrivate stringName = QJNIObjectPrivate::fromString(classDotEnc);
QJNIObjectPrivate classObject = classLoader.callObjectMethod("loadClass",
"(Ljava/lang/String;)Ljava/lang/Class;",
stringName.object());
if (!exceptionCheckAndClear(env) && classObject.isValid())
clazz = static_cast<jclass>(env->NewGlobalRef(classObject.object()));
cachedClasses->insert(classDotEnc, clazz);
return clazz; return clazz;
} }
@ -192,6 +222,18 @@ JNIEnv *QJNIEnvironmentPrivate::operator->()
return jniEnv; return jniEnv;
} }
jclass QJNIEnvironmentPrivate::findClass(const char *className, JNIEnv *env)
{
jclass clazz = 0;
if (env != 0)
clazz = ::findClass(className, env);
if (clazz == 0)
clazz = loadClass(className, QJNIEnvironmentPrivate());
return clazz;
}
QJNIEnvironmentPrivate::operator JNIEnv* () const QJNIEnvironmentPrivate::operator JNIEnv* () const
{ {
return jniEnv; return jniEnv;
@ -228,7 +270,7 @@ QJNIObjectPrivate::QJNIObjectPrivate(const char *className)
: d(new QJNIObjectData()) : d(new QJNIObjectData())
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
d->m_jclass = getCachedClass(env, className); d->m_jclass = loadClass(className, env);
d->m_own_jclass = false; d->m_own_jclass = false;
if (d->m_jclass) { if (d->m_jclass) {
// get default constructor // get default constructor
@ -247,7 +289,7 @@ QJNIObjectPrivate::QJNIObjectPrivate(const char *className, const char *sig, ...
: d(new QJNIObjectData()) : d(new QJNIObjectData())
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
d->m_jclass = getCachedClass(env, className); d->m_jclass = loadClass(className, env);
d->m_own_jclass = false; d->m_own_jclass = false;
if (d->m_jclass) { if (d->m_jclass) {
jmethodID constructorId = getCachedMethodID(env, d->m_jclass, "<init>", sig); jmethodID constructorId = getCachedMethodID(env, d->m_jclass, "<init>", sig);
@ -268,7 +310,7 @@ QJNIObjectPrivate::QJNIObjectPrivate(const char *className, const char *sig, va_
: d(new QJNIObjectData()) : d(new QJNIObjectData())
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
d->m_jclass = getCachedClass(env, className); d->m_jclass = loadClass(className, env);
d->m_own_jclass = false; d->m_own_jclass = false;
if (d->m_jclass) { if (d->m_jclass) {
jmethodID constructorId = getCachedMethodID(env, d->m_jclass, "<init>", sig); jmethodID constructorId = getCachedMethodID(env, d->m_jclass, "<init>", sig);
@ -610,7 +652,7 @@ void QJNIObjectPrivate::callStaticMethod<void>(const char *className,
va_list args) va_list args)
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) { if (clazz) {
jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true);
if (id) { if (id) {
@ -664,7 +706,7 @@ jboolean QJNIObjectPrivate::callStaticMethod<jboolean>(const char *className,
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jboolean res = 0; jboolean res = 0;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) { if (clazz) {
jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true);
if (id) { if (id) {
@ -725,7 +767,7 @@ jbyte QJNIObjectPrivate::callStaticMethod<jbyte>(const char *className,
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jbyte res = 0; jbyte res = 0;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) { if (clazz) {
jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true);
if (id) { if (id) {
@ -786,7 +828,7 @@ jchar QJNIObjectPrivate::callStaticMethod<jchar>(const char *className,
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jchar res = 0; jchar res = 0;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) { if (clazz) {
jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true);
if (id) { if (id) {
@ -847,7 +889,7 @@ jshort QJNIObjectPrivate::callStaticMethod<jshort>(const char *className,
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jshort res = 0; jshort res = 0;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) { if (clazz) {
jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true);
if (id) { if (id) {
@ -908,7 +950,7 @@ jint QJNIObjectPrivate::callStaticMethod<jint>(const char *className,
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jint res = 0; jint res = 0;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) { if (clazz) {
jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true);
if (id) { if (id) {
@ -969,7 +1011,7 @@ jlong QJNIObjectPrivate::callStaticMethod<jlong>(const char *className,
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jlong res = 0; jlong res = 0;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) { if (clazz) {
jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true);
if (id) { if (id) {
@ -1030,7 +1072,7 @@ jfloat QJNIObjectPrivate::callStaticMethod<jfloat>(const char *className,
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jfloat res = 0.f; jfloat res = 0.f;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) { if (clazz) {
jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true);
if (id) { if (id) {
@ -1091,7 +1133,7 @@ jdouble QJNIObjectPrivate::callStaticMethod<jdouble>(const char *className,
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jdouble res = 0.; jdouble res = 0.;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) { if (clazz) {
jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true);
if (id) { if (id) {
@ -1336,7 +1378,7 @@ QJNIObjectPrivate QJNIObjectPrivate::callStaticObjectMethod(const char *classNam
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jobject res = 0; jobject res = 0;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) { if (clazz) {
jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true);
if (id) { if (id) {
@ -1507,7 +1549,7 @@ jboolean QJNIObjectPrivate::getStaticField<jboolean>(const char *className, cons
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jboolean res = 0; jboolean res = 0;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) if (clazz)
res = getStaticField<jboolean>(clazz, fieldName); res = getStaticField<jboolean>(clazz, fieldName);
@ -1531,7 +1573,7 @@ jbyte QJNIObjectPrivate::getStaticField<jbyte>(const char *className, const char
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jbyte res = 0; jbyte res = 0;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) if (clazz)
res = getStaticField<jbyte>(clazz, fieldName); res = getStaticField<jbyte>(clazz, fieldName);
@ -1555,7 +1597,7 @@ jchar QJNIObjectPrivate::getStaticField<jchar>(const char *className, const char
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jchar res = 0; jchar res = 0;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) if (clazz)
res = getStaticField<jchar>(clazz, fieldName); res = getStaticField<jchar>(clazz, fieldName);
@ -1579,7 +1621,7 @@ jshort QJNIObjectPrivate::getStaticField<jshort>(const char *className, const ch
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jshort res = 0; jshort res = 0;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) if (clazz)
res = getStaticField<jshort>(clazz, fieldName); res = getStaticField<jshort>(clazz, fieldName);
@ -1603,7 +1645,7 @@ jint QJNIObjectPrivate::getStaticField<jint>(const char *className, const char *
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jint res = 0; jint res = 0;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) if (clazz)
res = getStaticField<jint>(clazz, fieldName); res = getStaticField<jint>(clazz, fieldName);
@ -1627,7 +1669,7 @@ jlong QJNIObjectPrivate::getStaticField<jlong>(const char *className, const char
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jlong res = 0; jlong res = 0;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) if (clazz)
res = getStaticField<jlong>(clazz, fieldName); res = getStaticField<jlong>(clazz, fieldName);
@ -1651,7 +1693,7 @@ jfloat QJNIObjectPrivate::getStaticField<jfloat>(const char *className, const ch
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jfloat res = 0.f; jfloat res = 0.f;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) if (clazz)
res = getStaticField<jfloat>(clazz, fieldName); res = getStaticField<jfloat>(clazz, fieldName);
@ -1675,7 +1717,7 @@ jdouble QJNIObjectPrivate::getStaticField<jdouble>(const char *className, const
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jdouble res = 0.; jdouble res = 0.;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) if (clazz)
res = getStaticField<jdouble>(clazz, fieldName); res = getStaticField<jdouble>(clazz, fieldName);
@ -1705,7 +1747,7 @@ QJNIObjectPrivate QJNIObjectPrivate::getStaticObjectField(const char *className,
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
QJNIObjectPrivate res; QJNIObjectPrivate res;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) if (clazz)
res = getStaticObjectField(clazz, fieldName, sig); res = getStaticObjectField(clazz, fieldName, sig);
@ -1941,7 +1983,7 @@ void QJNIObjectPrivate::setStaticField<jboolean>(const char *className,
jboolean value) jboolean value)
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) if (clazz)
setStaticField<jboolean>(clazz, fieldName, value); setStaticField<jboolean>(clazz, fieldName, value);
} }
@ -1963,7 +2005,7 @@ void QJNIObjectPrivate::setStaticField<jbyte>(const char *className,
jbyte value) jbyte value)
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) if (clazz)
setStaticField<jbyte>(clazz, fieldName, value); setStaticField<jbyte>(clazz, fieldName, value);
} }
@ -1985,7 +2027,7 @@ void QJNIObjectPrivate::setStaticField<jchar>(const char *className,
jchar value) jchar value)
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) if (clazz)
setStaticField<jchar>(clazz, fieldName, value); setStaticField<jchar>(clazz, fieldName, value);
} }
@ -2007,7 +2049,7 @@ void QJNIObjectPrivate::setStaticField<jshort>(const char *className,
jshort value) jshort value)
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) if (clazz)
setStaticField<jshort>(clazz, fieldName, value); setStaticField<jshort>(clazz, fieldName, value);
} }
@ -2029,7 +2071,7 @@ void QJNIObjectPrivate::setStaticField<jint>(const char *className,
jint value) jint value)
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) if (clazz)
setStaticField<jint>(clazz, fieldName, value); setStaticField<jint>(clazz, fieldName, value);
} }
@ -2051,7 +2093,7 @@ void QJNIObjectPrivate::setStaticField<jlong>(const char *className,
jlong value) jlong value)
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) if (clazz)
setStaticField<jlong>(clazz, fieldName, value); setStaticField<jlong>(clazz, fieldName, value);
} }
@ -2073,7 +2115,7 @@ void QJNIObjectPrivate::setStaticField<jfloat>(const char *className,
jfloat value) jfloat value)
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) if (clazz)
setStaticField<jfloat>(clazz, fieldName, value); setStaticField<jfloat>(clazz, fieldName, value);
} }
@ -2095,7 +2137,7 @@ void QJNIObjectPrivate::setStaticField<jdouble>(const char *className,
jdouble value) jdouble value)
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) if (clazz)
setStaticField<jdouble>(clazz, fieldName, value); setStaticField<jdouble>(clazz, fieldName, value);
} }
@ -2119,7 +2161,7 @@ void QJNIObjectPrivate::setStaticField<jobject>(const char *className,
jobject value) jobject value)
{ {
QJNIEnvironmentPrivate env; QJNIEnvironmentPrivate env;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
if (clazz) if (clazz)
setStaticField<jobject>(clazz, fieldName, sig, value); setStaticField<jobject>(clazz, fieldName, sig, value);
} }
@ -2150,7 +2192,7 @@ bool QJNIObjectPrivate::isClassAvailable(const char *className)
if (!env) if (!env)
return false; return false;
jclass clazz = getCachedClass(env, className); jclass clazz = loadClass(className, env);
return (clazz != 0); return (clazz != 0);
} }

View File

@ -58,6 +58,7 @@ public:
~QJNIEnvironmentPrivate(); ~QJNIEnvironmentPrivate();
JNIEnv *operator->(); JNIEnv *operator->();
operator JNIEnv*() const; operator JNIEnv*() const;
static jclass findClass(const char *className, JNIEnv *env = 0);
private: private:
friend class QAndroidJniEnvironment; friend class QAndroidJniEnvironment;

View File

@ -162,14 +162,6 @@ namespace QtAndroid
return m_javaVM; return m_javaVM;
} }
jclass findClass(const QString &className, JNIEnv *env)
{
return static_cast<jclass>(env->CallObjectMethod(m_classLoaderObject,
m_loadClassMethodID,
env->NewString(reinterpret_cast<const jchar *>(className.constData()),
jsize(className.length()))));
}
AAssetManager *assetManager() AAssetManager *assetManager()
{ {
return m_assetManager; return m_assetManager;

View File

@ -70,7 +70,6 @@ namespace QtAndroid
int desktopHeightPixels(); int desktopHeightPixels();
double scaledDensity(); double scaledDensity();
JavaVM *javaVM(); JavaVM *javaVM();
jclass findClass(const QString &className, JNIEnv *env);
AAssetManager *assetManager(); AAssetManager *assetManager();
jclass applicationClass(); jclass applicationClass();
jobject activity(); jobject activity();

View File

@ -45,6 +45,7 @@
#include <qthread.h> #include <qthread.h>
#include <qinputmethod.h> #include <qinputmethod.h>
#include <qwindow.h> #include <qwindow.h>
#include <QtCore/private/qjni_p.h>
#include <QTextCharFormat> #include <QTextCharFormat>
@ -53,8 +54,8 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
static QAndroidInputContext *m_androidInputContext = 0; static QAndroidInputContext *m_androidInputContext = 0;
static char const *const QtNativeInputConnectionClassName = "org.qtproject.qt5.android.QtNativeInputConnection"; static char const *const QtNativeInputConnectionClassName = "org/qtproject/qt5/android/QtNativeInputConnection";
static char const *const QtExtractedTextClassName = "org.qtproject.qt5.android.QtExtractedText"; static char const *const QtExtractedTextClassName = "org/qtproject/qt5/android/QtExtractedText";
static jclass m_extractedTextClass = 0; static jclass m_extractedTextClass = 0;
static jmethodID m_classConstructorMethodID = 0; static jmethodID m_classConstructorMethodID = 0;
static jfieldID m_partialEndOffsetFieldID = 0; static jfieldID m_partialEndOffsetFieldID = 0;
@ -334,11 +335,7 @@ static JNINativeMethod methods[] = {
QAndroidInputContext::QAndroidInputContext() QAndroidInputContext::QAndroidInputContext()
: QPlatformInputContext(), m_composingTextStart(-1), m_blockUpdateSelection(false), m_batchEditNestingLevel(0), m_focusObject(0) : QPlatformInputContext(), m_composingTextStart(-1), m_blockUpdateSelection(false), m_batchEditNestingLevel(0), m_focusObject(0)
{ {
QtAndroid::AttachedJNIEnv env; jclass clazz = QJNIEnvironmentPrivate::findClass(QtNativeInputConnectionClassName);
if (!env.jniEnv)
return;
jclass clazz = QtAndroid::findClass(QtNativeInputConnectionClassName, env.jniEnv);
if (clazz == NULL) { if (clazz == NULL) {
qCritical() << "Native registration unable to find class '" qCritical() << "Native registration unable to find class '"
<< QtNativeInputConnectionClassName << QtNativeInputConnectionClassName
@ -346,14 +343,15 @@ QAndroidInputContext::QAndroidInputContext()
return; return;
} }
if (env.jniEnv->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])) < 0) { QJNIEnvironmentPrivate env;
if (env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])) < 0) {
qCritical() << "RegisterNatives failed for '" qCritical() << "RegisterNatives failed for '"
<< QtNativeInputConnectionClassName << QtNativeInputConnectionClassName
<< "'"; << "'";
return; return;
} }
clazz = QtAndroid::findClass(QtExtractedTextClassName, env.jniEnv); clazz = QJNIEnvironmentPrivate::findClass(QtExtractedTextClassName);
if (clazz == NULL) { if (clazz == NULL) {
qCritical() << "Native registration unable to find class '" qCritical() << "Native registration unable to find class '"
<< QtExtractedTextClassName << QtExtractedTextClassName
@ -361,44 +359,44 @@ QAndroidInputContext::QAndroidInputContext()
return; return;
} }
m_extractedTextClass = static_cast<jclass>(env.jniEnv->NewGlobalRef(clazz)); m_extractedTextClass = static_cast<jclass>(env->NewGlobalRef(clazz));
m_classConstructorMethodID = env.jniEnv->GetMethodID(m_extractedTextClass, "<init>", "()V"); m_classConstructorMethodID = env->GetMethodID(m_extractedTextClass, "<init>", "()V");
if (m_classConstructorMethodID == NULL) { if (m_classConstructorMethodID == NULL) {
qCritical() << "GetMethodID failed"; qCritical() << "GetMethodID failed";
return; return;
} }
m_partialEndOffsetFieldID = env.jniEnv->GetFieldID(m_extractedTextClass, "partialEndOffset", "I"); m_partialEndOffsetFieldID = env->GetFieldID(m_extractedTextClass, "partialEndOffset", "I");
if (m_partialEndOffsetFieldID == NULL) { if (m_partialEndOffsetFieldID == NULL) {
qCritical() << "Can't find field partialEndOffset"; qCritical() << "Can't find field partialEndOffset";
return; return;
} }
m_partialStartOffsetFieldID = env.jniEnv->GetFieldID(m_extractedTextClass, "partialStartOffset", "I"); m_partialStartOffsetFieldID = env->GetFieldID(m_extractedTextClass, "partialStartOffset", "I");
if (m_partialStartOffsetFieldID == NULL) { if (m_partialStartOffsetFieldID == NULL) {
qCritical() << "Can't find field partialStartOffset"; qCritical() << "Can't find field partialStartOffset";
return; return;
} }
m_selectionEndFieldID = env.jniEnv->GetFieldID(m_extractedTextClass, "selectionEnd", "I"); m_selectionEndFieldID = env->GetFieldID(m_extractedTextClass, "selectionEnd", "I");
if (m_selectionEndFieldID == NULL) { if (m_selectionEndFieldID == NULL) {
qCritical() << "Can't find field selectionEnd"; qCritical() << "Can't find field selectionEnd";
return; return;
} }
m_selectionStartFieldID = env.jniEnv->GetFieldID(m_extractedTextClass, "selectionStart", "I"); m_selectionStartFieldID = env->GetFieldID(m_extractedTextClass, "selectionStart", "I");
if (m_selectionStartFieldID == NULL) { if (m_selectionStartFieldID == NULL) {
qCritical() << "Can't find field selectionStart"; qCritical() << "Can't find field selectionStart";
return; return;
} }
m_startOffsetFieldID = env.jniEnv->GetFieldID(m_extractedTextClass, "startOffset", "I"); m_startOffsetFieldID = env->GetFieldID(m_extractedTextClass, "startOffset", "I");
if (m_startOffsetFieldID == NULL) { if (m_startOffsetFieldID == NULL) {
qCritical() << "Can't find field startOffset"; qCritical() << "Can't find field startOffset";
return; return;
} }
m_textFieldID = env.jniEnv->GetFieldID(m_extractedTextClass, "text", "Ljava/lang/String;"); m_textFieldID = env->GetFieldID(m_extractedTextClass, "text", "Ljava/lang/String;");
if (m_textFieldID == NULL) { if (m_textFieldID == NULL) {
qCritical() << "Can't find field text"; qCritical() << "Can't find field text";
return; return;

View File

@ -44,6 +44,8 @@ QT_BEGIN_NAMESPACE
namespace QtAndroidDialogHelpers { namespace QtAndroidDialogHelpers {
static jclass g_messageDialogHelperClass = 0; static jclass g_messageDialogHelperClass = 0;
static const char QtMessageHandlerHelperClassName[] = "org/qtproject/qt5/android/QtMessageDialogHelper";
QAndroidPlatformMessageDialogHelper::QAndroidPlatformMessageDialogHelper() QAndroidPlatformMessageDialogHelper::QAndroidPlatformMessageDialogHelper()
:m_buttonId(-1) :m_buttonId(-1)
,m_javaMessageDialog(g_messageDialogHelperClass, "(Landroid/app/Activity;)V", QtAndroid::activity()) ,m_javaMessageDialog(g_messageDialogHelperClass, "(Landroid/app/Activity;)V", QtAndroid::activity())
@ -148,10 +150,10 @@ static JNINativeMethod methods[] = {
bool registerNatives(JNIEnv *env) bool registerNatives(JNIEnv *env)
{ {
jclass clazz = QtAndroid::findClass("org.qtproject.qt5.android.QtMessageDialogHelper", env); jclass clazz = QJNIEnvironmentPrivate::findClass(QtMessageHandlerHelperClassName, env);
if (!clazz) { if (!clazz) {
__android_log_print(ANDROID_LOG_FATAL, QtAndroid::qtTagText(), QtAndroid::classErrorMsgFmt() __android_log_print(ANDROID_LOG_FATAL, QtAndroid::qtTagText(), QtAndroid::classErrorMsgFmt()
, "org/qtproject/qt5/android/QtMessageDialogHelper"); , QtMessageHandlerHelperClassName);
return false; return false;
} }
g_messageDialogHelperClass = static_cast<jclass>(env->NewGlobalRef(clazz)); g_messageDialogHelperClass = static_cast<jclass>(env->NewGlobalRef(clazz));