Android: Add runOnUiThread() function

Enables QRunnables to be run on the UI thread. For now this function
is only intended for internal consumption.

Change-Id: I5e2abb06104219a9dd55b3308113056e4da5fa07
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@theqtcompany.com>
This commit is contained in:
Christian Strømme 2014-10-30 19:44:11 +01:00 committed by Christian Stromme
parent c998200282
commit 7a3a3a5694
3 changed files with 53 additions and 0 deletions

View File

@ -191,6 +191,16 @@ public class QtNative
}
}
private static void runQtOnUiThread(final long id)
{
runAction(new Runnable() {
@Override
public void run() {
QtNative.onAndroidUiThread(id);
}
});
}
public static boolean startApplication(String params,
String environment,
String mainLibrary,
@ -618,4 +628,6 @@ public class QtNative
// activity methods
public static native void onActivityResult(int requestCode, int resultCode, Intent data);
public static native void onAndroidUiThread(long id);
}

View File

@ -34,6 +34,7 @@
#include "qjnihelpers_p.h"
#include "qmutex.h"
#include "qlist.h"
#include <QtCore/qrunnable.h>
QT_BEGIN_NAMESPACE
@ -41,6 +42,19 @@ static JavaVM *g_javaVM = Q_NULLPTR;
static jobject g_jActivity = Q_NULLPTR;
static jobject g_jClassLoader = Q_NULLPTR;
static jint g_androidSdkVersion = 0;
static jclass g_jNativeClass = Q_NULLPTR;
static jmethodID g_runQtOnUiThreadMethodID = Q_NULLPTR;
static void onAndroidUiThread(JNIEnv *, jclass, jlong thiz)
{
QRunnable *runnable = reinterpret_cast<QRunnable *>(thiz);
if (runnable == 0)
return;
runnable->run();
if (runnable->autoDelete())
delete runnable;
}
namespace {
class ActivityResultListeners
@ -140,6 +154,22 @@ jint QtAndroidPrivate::initJNI(JavaVM *vm, JNIEnv *env)
env->DeleteLocalRef(activity);
g_javaVM = vm;
static const JNINativeMethod methods[] = {
{"onAndroidUiThread", "(J)V", reinterpret_cast<void *>(onAndroidUiThread)}
};
const bool regOk = (env->RegisterNatives(jQtNative, methods, sizeof(methods) / sizeof(methods[0])) == JNI_OK);
if (!regOk && exceptionCheck(env))
return JNI_ERR;
g_runQtOnUiThreadMethodID = env->GetStaticMethodID(jQtNative,
"runQtOnUiThread",
"(J)V");
g_jNativeClass = static_cast<jclass>(env->NewGlobalRef(jQtNative));
env->DeleteLocalRef(jQtNative);
return JNI_OK;
}
@ -164,4 +194,12 @@ jint QtAndroidPrivate::androidSdkVersion()
return g_androidSdkVersion;
}
void QtAndroidPrivate::runOnUiThread(QRunnable *runnable, JNIEnv *env)
{
Q_ASSERT(runnable != 0);
env->CallStaticVoidMethod(g_jNativeClass, g_runQtOnUiThreadMethodID, reinterpret_cast<jlong>(runnable));
if (exceptionCheck(env) && runnable != 0 && runnable->autoDelete())
delete runnable;
}
QT_END_NAMESPACE

View File

@ -50,6 +50,8 @@
QT_BEGIN_NAMESPACE
class QRunnable;
namespace QtAndroidPrivate
{
class Q_CORE_EXPORT ActivityResultListener
@ -64,6 +66,7 @@ namespace QtAndroidPrivate
Q_CORE_EXPORT jint initJNI(JavaVM *vm, JNIEnv *env);
jobject classLoader();
Q_CORE_EXPORT jint androidSdkVersion();
Q_CORE_EXPORT void runOnUiThread(QRunnable *runnable, JNIEnv *env);
Q_CORE_EXPORT void handleActivityResult(jint requestCode, jint resultCode, jobject data);
Q_CORE_EXPORT void registerActivityResultListener(ActivityResultListener *listener);