5#include <QtCore/qcoreapplication_platform.h>
7#include <QtCore/private/qnativeinterface_p.h>
8#include <QtCore/private/qjnihelpers_p.h>
9#include <QtCore/qjniobject.h>
10#if QT_CONFIG(future) && !defined(QT_NO_QOBJECT)
11#include <QtCore/qfuture.h>
12#include <QtCore/qfuturewatcher.h>
13#include <QtCore/qpromise.h>
14#include <QtCore/qtimer.h>
15#include <QtCore/qthreadpool.h>
22#if QT_CONFIG(future) && !defined(QT_NO_QOBJECT)
37
38
39
40
41
42
43
44
45
46
50
51
52
53
54
55
56
57
58QtJniTypes::Context QNativeInterface::QAndroidApplication::context()
60 return QtAndroidPrivate::context();
64
65
66
67
68
69
70
71bool QNativeInterface::QAndroidApplication::isActivityContext()
73 return QtAndroidPrivate::activity().isValid();
77
78
79
80
81
82
83int QNativeInterface::QAndroidApplication::sdkVersion()
85 return QtAndroidPrivate::androidSdkVersion();
89
90
91
92
93
94
95
96
97void QNativeInterface::QAndroidApplication::hideSplashScreen(
int duration)
99 QtAndroidPrivate::activity().callMethod<
void>(
"hideSplashScreen", duration);
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162#if QT_CONFIG(future) && !defined(QT_NO_QOBJECT)
163QFuture<QVariant> QNativeInterface::QAndroidApplication::runOnAndroidMainThread(
164 const std::function<QVariant()> &runnable,
165 const QDeadlineTimer timeout)
167 auto promise = std::make_shared<QPromise<QVariant>>();
168 QFuture<QVariant> future = promise->future();
171 if (!timeout.isForever()) {
172 QThreadPool::globalInstance()->start([=]()
mutable {
174 QTimer::singleShot(timeout.remainingTime(), &loop, [&]() {
180 QFutureWatcher<QVariant> watcher;
181 QObject::connect(&watcher, &QFutureWatcher<QVariant>::finished, &loop, [&]() {
184 QObject::connect(&watcher, &QFutureWatcher<QVariant>::canceled, &loop, [&]() {
187 watcher.setFuture(future);
192 QThreadPool::globalInstance()->releaseThread();
193 const auto sg = qScopeGuard([] {
194 QThreadPool::globalInstance()->reserveThread();
200 QMutexLocker locker(&g_pendingRunnablesMutex);
201#ifdef __cpp_aggregate_paren_init
202 g_pendingRunnables->emplace_back(runnable, std::move(promise));
204 g_pendingRunnables->push_back({runnable, std::move(promise)});
208 QJniObject::callStaticMethod<
void>(qtNativeClassName,
209 "runPendingCppRunnablesOnAndroidThread",
215static void runPendingCppRunnables(JNIEnv *, jobject )
219 QMutexLocker locker(&g_pendingRunnablesMutex);
220 if (g_pendingRunnables->empty())
223 PendingRunnable r = std::move(g_pendingRunnables->front());
224 g_pendingRunnables->pop_front();
228 if (!r.promise->isCanceled())
229 r.promise->addResult(r.function());
235bool QtAndroidPrivate::registerNativeInterfaceNatives(QJniEnvironment &env)
237#if QT_CONFIG(future) && !defined(QT_NO_QOBJECT)
238 const JNINativeMethod methods = {
"runPendingCppRunnables",
"()V", (
void *)runPendingCppRunnables};
239 return env.registerNativeMethods(qtNativeClassName, &methods, 1);
Native interface to a core application on Android.