4#include <QtCore/qcoreapplication_platform.h>
6#include <QtCore/private/qnativeinterface_p.h>
7#include <QtCore/private/qjnihelpers_p.h>
8#include <QtCore/qjniobject.h>
9#if QT_CONFIG(future) && !defined(QT_NO_QOBJECT)
10#include <QtCore/qfuture.h>
11#include <QtCore/qfuturewatcher.h>
12#include <QtCore/qpromise.h>
13#include <QtCore/qtimer.h>
14#include <QtCore/qthreadpool.h>
21#if QT_CONFIG(future) && !defined(QT_NO_QOBJECT)
36
37
38
39
40
41
42
43
44
45
49
50
51
52
53
54
55
56
57QtJniTypes::Context QNativeInterface::QAndroidApplication::context()
59 return QtAndroidPrivate::context();
63
64
65
66
67
68
69
70bool QNativeInterface::QAndroidApplication::isActivityContext()
72 return QtAndroidPrivate::activity().isValid();
76
77
78
79
80
81
82int QNativeInterface::QAndroidApplication::sdkVersion()
84 return QtAndroidPrivate::androidSdkVersion();
88
89
90
91
92
93
94
95
96void QNativeInterface::QAndroidApplication::hideSplashScreen(
int duration)
98 QtAndroidPrivate::activity().callMethod<
void>(
"hideSplashScreen", duration);
102
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#if QT_CONFIG(future) && !defined(QT_NO_QOBJECT)
162QFuture<QVariant> QNativeInterface::QAndroidApplication::runOnAndroidMainThread(
163 const std::function<QVariant()> &runnable,
164 const QDeadlineTimer timeout)
166 auto promise = std::make_shared<QPromise<QVariant>>();
167 QFuture<QVariant> future = promise->future();
170 if (!timeout.isForever()) {
171 QThreadPool::globalInstance()->start([=]()
mutable {
173 QTimer::singleShot(timeout.remainingTime(), &loop, [&]() {
179 QFutureWatcher<QVariant> watcher;
180 QObject::connect(&watcher, &QFutureWatcher<QVariant>::finished, &loop, [&]() {
183 QObject::connect(&watcher, &QFutureWatcher<QVariant>::canceled, &loop, [&]() {
186 watcher.setFuture(future);
191 QThreadPool::globalInstance()->releaseThread();
192 const auto sg = qScopeGuard([] {
193 QThreadPool::globalInstance()->reserveThread();
199 QMutexLocker locker(&g_pendingRunnablesMutex);
200#ifdef __cpp_aggregate_paren_init
201 g_pendingRunnables->emplace_back(runnable, std::move(promise));
203 g_pendingRunnables->push_back({runnable, std::move(promise)});
207 QJniObject::callStaticMethod<
void>(qtNativeClassName,
208 "runPendingCppRunnablesOnAndroidThread",
214static void runPendingCppRunnables(JNIEnv *, jobject )
218 QMutexLocker locker(&g_pendingRunnablesMutex);
219 if (g_pendingRunnables->empty())
222 PendingRunnable r = std::move(g_pendingRunnables->front());
223 g_pendingRunnables->pop_front();
227 if (!r.promise->isCanceled())
228 r.promise->addResult(r.function());
234bool QtAndroidPrivate::registerNativeInterfaceNatives(QJniEnvironment &env)
236#if QT_CONFIG(future) && !defined(QT_NO_QOBJECT)
237 const JNINativeMethod methods = {
"runPendingCppRunnables",
"()V", (
void *)runPendingCppRunnables};
238 return env.registerNativeMethods(qtNativeClassName, &methods, 1);
Native interface to a core application on Android.