aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBogDan Vatra <bogdan@kdab.com>2016-02-04 17:16:05 +0200
committerBogDan Vatra <bogdan@kdab.com>2016-02-05 06:21:30 +0000
commitf0fefa8766cb3cec38f39c31bb01860bea88dcbc (patch)
tree98ba6d4a9156f68e29538eca9115df7a9d010573 /src
parent023d83792cd3f4cdffb9ae3a5464e4e8b654ddab (diff)
Helper functions needed to run Runnables on Android UI thread easily.
Add two function to allow the users to easily run (a)synchronously Runnables from any thread directly to Andoroid UI thread. These functions are useful to create java controls and to access their methods, which must done on Android UI thread. Change-Id: Iec5437321e6136cc90268cc7ecf091f82fc4cdd3 Reviewed-by: Christian Stromme <christian.stromme@theqtcompany.com>
Diffstat (limited to 'src')
-rw-r--r--src/androidextras/android/qandroidfunctions.cpp47
-rw-r--r--src/androidextras/android/qandroidfunctions.h5
2 files changed, 52 insertions, 0 deletions
diff --git a/src/androidextras/android/qandroidfunctions.cpp b/src/androidextras/android/qandroidfunctions.cpp
index a4d55c1..5c99642 100644
--- a/src/androidextras/android/qandroidfunctions.cpp
+++ b/src/androidextras/android/qandroidfunctions.cpp
@@ -41,6 +41,7 @@
#include "qandroidactivityresultreceiver.h"
#include "qandroidactivityresultreceiver_p.h"
+#include <QtCore/private/qjni_p.h>
#include <QtCore/private/qjnihelpers_p.h>
QT_BEGIN_NAMESPACE
@@ -151,4 +152,50 @@ void QtAndroid::startIntentSender(const QAndroidJniObject &intentSender,
}
+/*!
+ \since 5.7
+ \fn void QtAndroid::runOnAndroidThread(const Runnable &runnable)
+
+ Posts the given \a runnable on the android thread.
+ The \a runnable will be queued and executed on the Android UI thread, unless it called on the
+ Android UI thread, in which case the runnable will be executed immediately.
+
+ This function is useful to set asynchronously properties of objects that must be set on on Android UI thread.
+*/
+void QtAndroid::runOnAndroidThread(const QtAndroid::Runnable &runnable)
+{
+ QtAndroidPrivate::runOnAndroidThread(runnable, QJNIEnvironmentPrivate());
+}
+
+/*!
+ \since 5.7
+ \fn void QtAndroid::runOnAndroidThreadSync(const Runnable &runnable, int timeoutMs)
+
+ Posts the \a runnable on the Android UI thread and waits until the runnable is executed,
+ or until \a timeoutMs has passed
+
+ This function is useful to create objects, or get properties on Android UI thread:
+
+ \code
+ QAndroidJniObject javaControl;
+ QtAndroid::runOnAndroidThreadSync([&javaControl](){
+
+ // create our Java control on Android UI thread.
+ javaControl = QAndroidJniObject("android/webkit/WebView",
+ "(Landroid/content/Context;)V",
+ QtAndroid::androidActivity().object<jobject>());
+ javaControl.callMethod<void>("setWebViewClient",
+ "(Landroid/webkit/WebViewClient;)V",
+ QAndroidJniObject("android/webkit/WebViewClient").object());
+ });
+
+ // Continue the execution normally
+ qDebug() << javaControl.isValid();
+ \endcode
+*/
+void QtAndroid::runOnAndroidThreadSync(const QtAndroid::Runnable &runnable, int timeoutMs)
+{
+ QtAndroidPrivate::runOnAndroidThreadSync(runnable, QJNIEnvironmentPrivate(), timeoutMs);
+}
+
QT_END_NAMESPACE
diff --git a/src/androidextras/android/qandroidfunctions.h b/src/androidextras/android/qandroidfunctions.h
index dc1d38a..8fde117 100644
--- a/src/androidextras/android/qandroidfunctions.h
+++ b/src/androidextras/android/qandroidfunctions.h
@@ -47,6 +47,8 @@
#include <QtAndroidExtras/qandroidextrasglobal.h>
#include <QtAndroidExtras/qandroidjniobject.h>
+#include <functional>
+
QT_BEGIN_NAMESPACE
class QAndroidActivityResultReceiver;
@@ -62,6 +64,9 @@ namespace QtAndroid
int receiverRequestCode,
QAndroidActivityResultReceiver *resultReceiver = 0);
+ typedef std::function<void()> Runnable;
+ Q_ANDROIDEXTRAS_EXPORT void runOnAndroidThread(const Runnable &runnable);
+ Q_ANDROIDEXTRAS_EXPORT void runOnAndroidThreadSync(const Runnable &runnable, int timeoutMs = INT_MAX);
}
QT_END_NAMESPACE