Android: Add runOnAndroidThreadSync

This is a convenient function which waits (timoutMS) for the runnable to be executed.

Change-Id: I2c0f262d3795c29961e8182b7ffc35a2c6f8a1f6
Reviewed-by: Christian Stromme <christian.stromme@theqtcompany.com>
This commit is contained in:
BogDan Vatra 2016-01-27 10:29:32 +02:00
parent 7696ff89c9
commit 4bf741066d
2 changed files with 12 additions and 0 deletions

View File

@ -41,6 +41,7 @@
#include "qmutex.h"
#include "qlist.h"
#include "qsemaphore.h"
#include "qsharedpointer.h"
#include "qvector.h"
#include <QtCore/qrunnable.h>
@ -338,6 +339,16 @@ void QtAndroidPrivate::runOnAndroidThread(const QtAndroidPrivate::Runnable &runn
env->CallStaticVoidMethod(g_jNativeClass, g_runPendingCppRunnablesMethodID);
}
void QtAndroidPrivate::runOnAndroidThreadSync(const QtAndroidPrivate::Runnable &runnable, JNIEnv *env, int timeoutMs)
{
QSharedPointer<QSemaphore> sem(new QSemaphore);
runOnAndroidThread([&runnable, sem]{
runnable();
sem->release();
}, env);
sem->tryAcquire(1, timeoutMs);
}
void QtAndroidPrivate::registerGenericMotionEventListener(QtAndroidPrivate::GenericMotionEventListener *listener)
{
QMutexLocker locker(&g_genericMotionEventListeners()->mutex);

View File

@ -105,6 +105,7 @@ namespace QtAndroidPrivate
jobject classLoader();
Q_CORE_EXPORT jint androidSdkVersion();
Q_CORE_EXPORT void runOnAndroidThread(const Runnable &runnable, JNIEnv *env);
Q_CORE_EXPORT void runOnAndroidThreadSync(const Runnable &runnable, JNIEnv *env, int timeoutMs = INT_MAX);
Q_CORE_EXPORT void runOnUiThread(QRunnable *runnable, JNIEnv *env);
Q_CORE_EXPORT void handleActivityResult(jint requestCode, jint resultCode, jobject data);