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
56QtJniTypes::Context QNativeInterface::QAndroidApplication::context()
58 return QtAndroidPrivate::context();
62
63
64
65
66
67
68
69bool QNativeInterface::QAndroidApplication::isActivityContext()
71 return QtAndroidPrivate::activity().isValid();
75
76
77
78
79
80
81int QNativeInterface::QAndroidApplication::sdkVersion()
83 return QtAndroidPrivate::androidSdkVersion();
87
88
89
90
91
92
93
94
95void QNativeInterface::QAndroidApplication::hideSplashScreen(
int duration)
97 QtAndroidPrivate::activity().callMethod<
void>(
"hideSplashScreen", duration);
101
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#if QT_CONFIG(future) && !defined(QT_NO_QOBJECT)
161QFuture<QVariant> QNativeInterface::QAndroidApplication::runOnAndroidMainThread(
162 const std::function<QVariant()> &runnable,
163 const QDeadlineTimer timeout)
165 auto promise = std::make_shared<QPromise<QVariant>>();
166 QFuture<QVariant> future = promise->future();
169 if (!timeout.isForever()) {
170 QThreadPool::globalInstance()->start([=]()
mutable {
172 QTimer::singleShot(timeout.remainingTime(), &loop, [&]() {
178 QFutureWatcher<QVariant> watcher;
179 QObject::connect(&watcher, &QFutureWatcher<QVariant>::finished, &loop, [&]() {
182 QObject::connect(&watcher, &QFutureWatcher<QVariant>::canceled, &loop, [&]() {
185 watcher.setFuture(future);
190 QThreadPool::globalInstance()->releaseThread();
191 const auto sg = qScopeGuard([] {
192 QThreadPool::globalInstance()->reserveThread();
198 QMutexLocker locker(&g_pendingRunnablesMutex);
199#ifdef __cpp_aggregate_paren_init
200 g_pendingRunnables->emplace_back(runnable, std::move(promise));
202 g_pendingRunnables->push_back({runnable, std::move(promise)});
206 QJniObject::callStaticMethod<
void>(qtNativeClassName,
207 "runPendingCppRunnablesOnAndroidThread",
213static void runPendingCppRunnables(JNIEnv *, jobject )
217 QMutexLocker locker(&g_pendingRunnablesMutex);
218 if (g_pendingRunnables->empty())
221 PendingRunnable r = std::move(g_pendingRunnables->front());
222 g_pendingRunnables->pop_front();
226 if (!r.promise->isCanceled())
227 r.promise->addResult(r.function());
233bool QtAndroidPrivate::registerNativeInterfaceNatives(QJniEnvironment &env)
235#if QT_CONFIG(future) && !defined(QT_NO_QOBJECT)
236 const JNINativeMethod methods = {
"runPendingCppRunnables",
"()V", (
void *)runPendingCppRunnables};
237 return env.registerNativeMethods(qtNativeClassName, &methods, 1);
Native interface to a core application on Android.