summaryrefslogtreecommitdiffstats
path: root/src/corelib/platform/android
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/platform/android')
-rw-r--r--src/corelib/platform/android/qandroidextras.cpp303
-rw-r--r--src/corelib/platform/android/qandroidextras_p.h62
-rw-r--r--src/corelib/platform/android/qandroidnativeinterface.cpp106
3 files changed, 152 insertions, 319 deletions
diff --git a/src/corelib/platform/android/qandroidextras.cpp b/src/corelib/platform/android/qandroidextras.cpp
index d9bcc76a1a..aa0c3fd093 100644
--- a/src/corelib/platform/android/qandroidextras.cpp
+++ b/src/corelib/platform/android/qandroidextras.cpp
@@ -1,41 +1,5 @@
-/****************************************************************************
-**
-** Copyright (C) 2021 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtCore module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2021 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qandroidextras_p.h"
@@ -153,14 +117,17 @@ QAndroidBinder QAndroidParcelPrivate::readBinder() const
/*!
\class QAndroidParcel
+ \inheaderfile QtCore/private/qandroidextras_p.h
\preliminary
- \inmodule QtCore
+ \inmodule QtCorePrivate
\brief Wraps the most important methods of Android Parcel class.
The QAndroidParcel is a convenience class that wraps the most important
\l {https://developer.android.com/reference/android/os/Parcel.html}{Android Parcel}
methods.
+ \include qtcore.qdoc qtcoreprivate-usage
+
\since 6.2
*/
@@ -269,14 +236,17 @@ QJniObject QAndroidParcel::handle() const
/*!
\class QAndroidBinder
+ \inheaderfile QtCore/private/qandroidextras_p.h
\preliminary
- \inmodule QtCore
+ \inmodule QtCorePrivate
\brief Wraps the most important methods of Android Binder class.
The QAndroidBinder is a convenience class that wraps the most important
\l {https://developer.android.com/reference/android/os/Binder.html}{Android Binder}
methods.
+ \include qtcore.qdoc qtcoreprivate-usage
+
\since 6.2
*/
@@ -406,8 +376,9 @@ QJniObject QAndroidBinder::handle() const
/*!
\class QAndroidServiceConnection
+ \inheaderfile QtCore/private/qandroidextras_p.h
\preliminary
- \inmodule QtCore
+ \inmodule QtCorePrivate
\brief Wraps the most important methods of Android ServiceConnection class.
The QAndroidServiceConnection is a convenience abstract class which wraps the
@@ -416,6 +387,8 @@ QJniObject QAndroidBinder::handle() const
It is useful when you perform a QtAndroidPrivate::bindService operation.
+ \include qtcore.qdoc qtcoreprivate-usage
+
\since 6.2
*/
@@ -478,7 +451,7 @@ QJniObject QAndroidServiceConnection::handle() const
*/
-static QBasicAtomicInteger<uint> nextUniqueActivityRequestCode = Q_BASIC_ATOMIC_INITIALIZER(0);
+Q_CONSTINIT static QBasicAtomicInteger<uint> nextUniqueActivityRequestCode = Q_BASIC_ATOMIC_INITIALIZER(0);
// Get a unique activity request code.
static int uniqueActivityRequestCode()
@@ -532,13 +505,16 @@ public:
/*!
\class QAndroidActivityResultReceiver
+ \inheaderfile QtCore/private/qandroidextras_p.h
\preliminary
- \inmodule QtCore
+ \inmodule QtCorePrivate
\since 6.2
\brief Interface used for callbacks from onActivityResult() in the main Android activity.
Create a subclass of this class to be notified of the results when using the
\c QtAndroidPrivate::startActivity() and \c QtAndroidPrivate::startIntentSender() APIs.
+
+ \include qtcore.qdoc qtcoreprivate-usage
*/
/*!
@@ -627,14 +603,17 @@ public:
/*!
\class QAndroidService
+ \inheaderfile QtCore/private/qandroidextras_p.h
\preliminary
- \inmodule QtCore
+ \inmodule QtCorePrivate
\brief Wraps the most important methods of Android Service class.
The QAndroidService is a convenience class that wraps the most important
\l {https://developer.android.com/reference/android/app/Service.html}{Android Service}
methods.
+ \include qtcore.qdoc qtcoreprivate-usage
+
\since 6.2
*/
@@ -693,17 +672,59 @@ QAndroidBinder* QAndroidService::onBind(const QAndroidIntent &/*intent*/)
return nullptr;
}
+static jboolean onTransact(JNIEnv */*env*/, jclass /*cls*/, jlong id, jint code, jobject data,
+ jobject reply, jint flags)
+{
+ if (!id)
+ return false;
+
+ return reinterpret_cast<QAndroidBinder*>(id)->onTransact(
+ code, QAndroidParcel(data), QAndroidParcel(reply), QAndroidBinder::CallType(flags));
+}
+
+static void onServiceConnected(JNIEnv */*env*/, jclass /*cls*/, jlong id, jstring name,
+ jobject service)
+{
+ if (!id)
+ return;
+
+ return reinterpret_cast<QAndroidServiceConnection *>(id)->onServiceConnected(
+ QJniObject(name).toString(), QAndroidBinder(service));
+}
+
+static void onServiceDisconnected(JNIEnv */*env*/, jclass /*cls*/, jlong id, jstring name)
+{
+ if (!id)
+ return;
+
+ return reinterpret_cast<QAndroidServiceConnection *>(id)->onServiceDisconnected(
+ QJniObject(name).toString());
+}
+
+bool QtAndroidPrivate::registerExtrasNatives(QJniEnvironment &env)
+{
+ static const JNINativeMethod methods[] = {
+ {"onTransact", "(JILandroid/os/Parcel;Landroid/os/Parcel;I)Z", (void *)onTransact},
+ {"onServiceConnected", "(JLjava/lang/String;Landroid/os/IBinder;)V", (void *)onServiceConnected},
+ {"onServiceDisconnected", "(JLjava/lang/String;)V", (void *)onServiceDisconnected}
+ };
+
+ return env.registerNativeMethods("org/qtproject/qt/android/extras/QtNative", methods, 3);
+}
/*!
\class QAndroidIntent
+ \inheaderfile QtCore/private/qandroidextras_p.h
\preliminary
- \inmodule QtCore
+ \inmodule QtCorePrivate
\brief Wraps the most important methods of Android Intent class.
The QAndroidIntent is a convenience class that wraps the most important
\l {https://developer.android.com/reference/android/content/Intent.html}{Android Intent}
methods.
+ \include qtcore.qdoc qtcoreprivate-usage
+
\since 6.2
*/
@@ -824,10 +845,13 @@ QJniObject QAndroidIntent::handle() const
/*!
\namespace QtAndroidPrivate
\preliminary
- \inmodule QtCore
+ \inmodule QtCorePrivate
\since 6.2
- \brief The QtAndroid namespace provides miscellaneous functions to aid Android development.
- \inheaderfile QtAndroid
+ \brief The QtAndroidPrivate namespace provides miscellaneous functions
+ to aid Android development.
+ \inheaderfile QtCore/private/qandroidextras_p.h
+
+ \include qtcore.qdoc qtcoreprivate-usage
*/
/*!
@@ -1051,62 +1075,14 @@ QtAndroidPrivate::PermissionResult resultFromAndroid(jint value)
using PendingPermissionRequestsHash
= QHash<int, QSharedPointer<QPromise<QtAndroidPrivate::PermissionResult>>>;
Q_GLOBAL_STATIC(PendingPermissionRequestsHash, g_pendingPermissionRequests);
-static QBasicMutex g_pendingPermissionRequestsMutex;
+Q_CONSTINIT static QBasicMutex g_pendingPermissionRequestsMutex;
static int nextRequestCode()
{
- static QBasicAtomicInt counter = Q_BASIC_ATOMIC_INITIALIZER(0);
+ Q_CONSTINIT static QBasicAtomicInt counter = Q_BASIC_ATOMIC_INITIALIZER(0);
return counter.fetchAndAddRelaxed(1);
}
-static QStringList nativeStringsFromPermission(QtAndroidPrivate::PermissionType permission)
-{
- static const auto precisePerm = QStringLiteral("android.permission.ACCESS_FINE_LOCATION");
- static const auto coarsePerm = QStringLiteral("android.permission.ACCESS_COARSE_LOCATION");
- static const auto backgroundPerm =
- QStringLiteral("android.permission.ACCESS_BACKGROUND_LOCATION");
-
- switch (permission) {
- case QtAndroidPrivate::Location:
- return {coarsePerm};
- case QtAndroidPrivate::PreciseLocation:
- return {precisePerm};
- case QtAndroidPrivate::BackgroundLocation:
- // Keep the background permission first to be able to use .first()
- // in checkPermission because it takes single permission
- if (QtAndroidPrivate::androidSdkVersion() >= 29)
- return {backgroundPerm, coarsePerm};
- return {coarsePerm};
- case QtAndroidPrivate::PreciseBackgroundLocation:
- // Keep the background permission first to be able to use .first()
- // in checkPermission because it takes single permission
- if (QtAndroidPrivate::androidSdkVersion() >= 29)
- return {backgroundPerm, precisePerm};
- return {precisePerm};
- case QtAndroidPrivate::Camera:
- return {QStringLiteral("android.permission.CAMERA")};
- case QtAndroidPrivate::Microphone:
- return {QStringLiteral("android.permission.RECORD_AUDIO")};
- case QtAndroidPrivate::Bluetooth:
- return { QStringLiteral("android.permission.BLUETOOTH") };
- case QtAndroidPrivate::BodySensors:
- return {QStringLiteral("android.permission.BODY_SENSORS")};
- case QtAndroidPrivate::PhysicalActivity:
- return {QStringLiteral("android.permission.ACTIVITY_RECOGNITION")};
- case QtAndroidPrivate::Contacts:
- return {QStringLiteral("android.permission.READ_CONTACTS"),
- QStringLiteral("android.permission.WRITE_CONTACTS")};
- case QtAndroidPrivate::Storage:
- return {QStringLiteral("android.permission.READ_EXTERNAL_STORAGE"),
- QStringLiteral("android.permission.WRITE_EXTERNAL_STORAGE")};
- case QtAndroidPrivate::Calendar:
- return {QStringLiteral("android.permission.READ_CALENDAR"),
- QStringLiteral("android.permission.WRITE_CALENDAR")};
- }
-
- return {};
-}
-
/*!
\internal
@@ -1139,29 +1115,36 @@ static void sendRequestPermissionsResult(JNIEnv *env, jobject *obj, jint request
request->addResult(result, i);
}
+ QtAndroidPrivate::releaseAndroidDeadlockProtector();
request->finish();
}
QFuture<QtAndroidPrivate::PermissionResult>
requestPermissionsInternal(const QStringList &permissions)
{
- QSharedPointer<QPromise<QtAndroidPrivate::PermissionResult>> promise;
- promise.reset(new QPromise<QtAndroidPrivate::PermissionResult>());
- QFuture<QtAndroidPrivate::PermissionResult> future = promise->future();
- promise->start();
-
// No mechanism to request permission for SDK version below 23, because
// permissions defined in the manifest are granted at install time.
if (QtAndroidPrivate::androidSdkVersion() < 23) {
- for (int i = 0; i < permissions.size(); ++i)
- promise->addResult(QtAndroidPrivate::checkPermission(permissions.at(i)).result(), i);
- promise->finish();
- return future;
+ QList<QtAndroidPrivate::PermissionResult> result;
+ result.reserve(permissions.size());
+ // ### can we kick off all checkPermission()s, and whenAll() collect results?
+ for (const QString &permission : permissions)
+ result.push_back(QtAndroidPrivate::checkPermission(permission).result());
+ return QtFuture::makeReadyRangeFuture(result);
}
+ if (!QtAndroidPrivate::acquireAndroidDeadlockProtector())
+ return QtFuture::makeReadyValueFuture(QtAndroidPrivate::Denied);
+
+ QSharedPointer<QPromise<QtAndroidPrivate::PermissionResult>> promise;
+ promise.reset(new QPromise<QtAndroidPrivate::PermissionResult>());
+ QFuture<QtAndroidPrivate::PermissionResult> future = promise->future();
+ promise->start();
+
const int requestCode = nextRequestCode();
QMutexLocker locker(&g_pendingPermissionRequestsMutex);
g_pendingPermissionRequests->insert(requestCode, promise);
+ locker.unlock();
QNativeInterface::QAndroidApplication::runOnAndroidMainThread([permissions, requestCode] {
QJniEnvironment env;
@@ -1193,64 +1176,16 @@ requestPermissionsInternal(const QStringList &permissions)
QFuture<QtAndroidPrivate::PermissionResult>
QtAndroidPrivate::requestPermission(const QString &permission)
{
- // avoid the uneccessary call and response to an empty permission string
- if (permission.size() > 0)
- return requestPermissionsInternal({permission});
-
- QPromise<QtAndroidPrivate::PermissionResult> promise;
- QFuture<QtAndroidPrivate::PermissionResult> future = promise.future();
- promise.start();
- promise.addResult(QtAndroidPrivate::Denied);
- promise.finish();
- return future;
-}
-
-static bool isBackgroundLocationApi29(QtAndroidPrivate::PermissionType permission)
-{
- return QNativeInterface::QAndroidApplication::sdkVersion() >= 29
- && (permission == QtAndroidPrivate::BackgroundLocation
- || permission == QtAndroidPrivate::PreciseBackgroundLocation);
+ return requestPermissions({permission});
}
-/*!
- \preliminary
-
- Requests the \a permission and returns a QFuture representing the
- result of the request.
-
- \since 6.2
- \sa checkPermission()
-*/
QFuture<QtAndroidPrivate::PermissionResult>
-QtAndroidPrivate::requestPermission(QtAndroidPrivate::PermissionType permission)
+QtAndroidPrivate::requestPermissions(const QStringList &permissions)
{
- QSharedPointer<QPromise<QtAndroidPrivate::PermissionResult>> promise;
- promise.reset(new QPromise<QtAndroidPrivate::PermissionResult>());
- QFuture<QtAndroidPrivate::PermissionResult> future = promise->future();
- promise->start();
- const auto nativePermissions = nativeStringsFromPermission(permission);
-
- if (nativePermissions.size() > 0) {
- requestPermissionsInternal(nativePermissions).then(
- [promise, permission](QFuture<QtAndroidPrivate::PermissionResult> future) {
- auto AuthorizedCount = future.results().count(QtAndroidPrivate::Authorized);
- if (AuthorizedCount > 0) {
- if (isBackgroundLocationApi29(permission))
- promise->addResult(future.resultAt(0), 0);
- else
- promise->addResult(QtAndroidPrivate::Authorized, 0);
- } else {
- promise->addResult(QtAndroidPrivate::Denied, 0);
- }
- promise->finish();
- });
-
- return future;
- }
-
- promise->addResult(QtAndroidPrivate::Denied);
- promise->finish();
- return future;
+ // avoid the uneccessary call and response to an empty permission string
+ if (permissions.isEmpty())
+ return QtFuture::makeReadyValueFuture(QtAndroidPrivate::Denied);
+ return requestPermissionsInternal(permissions);
}
/*!
@@ -1264,60 +1199,30 @@ QtAndroidPrivate::requestPermission(QtAndroidPrivate::PermissionType permission)
QFuture<QtAndroidPrivate::PermissionResult>
QtAndroidPrivate::checkPermission(const QString &permission)
{
- QPromise<QtAndroidPrivate::PermissionResult> promise;
- QFuture<QtAndroidPrivate::PermissionResult> future = promise.future();
- promise.start();
-
- if (permission.size() > 0) {
+ QtAndroidPrivate::PermissionResult result = Denied;
+ if (!permission.isEmpty()) {
auto res = QJniObject::callStaticMethod<jint>(qtNativeClassName,
"checkSelfPermission",
"(Ljava/lang/String;)I",
QJniObject::fromString(permission).object());
- promise.addResult(resultFromAndroid(res));
- } else {
- promise.addResult(QtAndroidPrivate::Denied);
+ result = resultFromAndroid(res);
}
-
- promise.finish();
- return future;
+ return QtFuture::makeReadyValueFuture(result);
}
-/*!
- \preliminary
- Checks whether this process has the named \a permission and returns a QFuture
- representing the result of the check.
-
- \since 6.2
- \sa requestPermission()
-*/
-QFuture<QtAndroidPrivate::PermissionResult>
-QtAndroidPrivate::checkPermission(QtAndroidPrivate::PermissionType permission)
-{
- const auto nativePermissions = nativeStringsFromPermission(permission);
-
- if (nativePermissions.size() > 0)
- return checkPermission(nativePermissions.first());
-
- QPromise<QtAndroidPrivate::PermissionResult> promise;
- QFuture<QtAndroidPrivate::PermissionResult> future = promise.future();
- promise.start();
- promise.addResult(QtAndroidPrivate::Denied);
- promise.finish();
- return future;
-}
-
-bool QtAndroidPrivate::registerPermissionNatives()
+bool QtAndroidPrivate::registerPermissionNatives(QJniEnvironment &env)
{
if (QtAndroidPrivate::androidSdkVersion() < 23)
return true;
- JNINativeMethod methods[] = {
+ const JNINativeMethod methods[] = {
{"sendRequestPermissionsResult", "(I[Ljava/lang/String;[I)V",
reinterpret_cast<void *>(sendRequestPermissionsResult)
}};
- QJniEnvironment env;
return env.registerNativeMethods(qtNativeClassName, methods, 1);
}
QT_END_NAMESPACE
+
+#include "moc_qandroidextras_p.cpp"
diff --git a/src/corelib/platform/android/qandroidextras_p.h b/src/corelib/platform/android/qandroidextras_p.h
index 0d6dc2097d..efdc6cf74f 100644
--- a/src/corelib/platform/android/qandroidextras_p.h
+++ b/src/corelib/platform/android/qandroidextras_p.h
@@ -1,41 +1,5 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtCore module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef QANDROIDEXTRAS_H
#define QANDROIDEXTRAS_H
@@ -261,21 +225,6 @@ namespace QtAndroidPrivate
BindFlags flags = BindFlag::None);
#if QT_CONFIG(future)
- enum PermissionType {
- Camera,
- Microphone,
- Bluetooth,
- Location,
- PreciseLocation,
- BackgroundLocation,
- PreciseBackgroundLocation,
- BodySensors,
- PhysicalActivity,
- Contacts,
- Storage,
- Calendar
- };
-
enum PermissionResult {
Undetermined,
Authorized,
@@ -283,12 +232,9 @@ namespace QtAndroidPrivate
};
Q_CORE_EXPORT QFuture<QtAndroidPrivate::PermissionResult>
- requestPermission(QtAndroidPrivate::PermissionType permission);
- Q_CORE_EXPORT QFuture<QtAndroidPrivate::PermissionResult>
requestPermission(const QString &permission);
-
- Q_CORE_EXPORT QFuture<QtAndroidPrivate::PermissionResult>
- checkPermission(QtAndroidPrivate::PermissionType permission);
+ QFuture<QtAndroidPrivate::PermissionResult>
+ requestPermissions(const QStringList &permissions);
Q_CORE_EXPORT QFuture<QtAndroidPrivate::PermissionResult>
checkPermission(const QString &permission);
#endif
diff --git a/src/corelib/platform/android/qandroidnativeinterface.cpp b/src/corelib/platform/android/qandroidnativeinterface.cpp
index b8e2cbb424..fc3a09c78b 100644
--- a/src/corelib/platform/android/qandroidnativeinterface.cpp
+++ b/src/corelib/platform/android/qandroidnativeinterface.cpp
@@ -1,41 +1,5 @@
-/****************************************************************************
-**
-** Copyright (C) 2021 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtCore module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2021 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include <QtCore/qcoreapplication_platform.h>
@@ -43,9 +7,13 @@
#include <QtCore/private/qjnihelpers_p.h>
#include <QtCore/qjniobject.h>
#if QT_CONFIG(future) && !defined(QT_NO_QOBJECT)
-#include <QtConcurrent/QtConcurrent>
+#include <QtCore/qfuture.h>
+#include <QtCore/qfuturewatcher.h>
#include <QtCore/qpromise.h>
+#include <QtCore/qtimer.h>
+#include <QtCore/qthreadpool.h>
#include <deque>
+#include <memory>
#endif
QT_BEGIN_NAMESPACE
@@ -53,10 +21,14 @@ QT_BEGIN_NAMESPACE
#if QT_CONFIG(future) && !defined(QT_NO_QOBJECT)
static const char qtNativeClassName[] = "org/qtproject/qt/android/QtNative";
-typedef std::pair<std::function<QVariant()>, QSharedPointer<QPromise<QVariant>>> RunnablePair;
-typedef std::deque<RunnablePair> PendingRunnables;
+struct PendingRunnable {
+ std::function<QVariant()> function;
+ std::shared_ptr<QPromise<QVariant>> promise;
+};
+
+using PendingRunnables = std::deque<PendingRunnable>;
Q_GLOBAL_STATIC(PendingRunnables, g_pendingRunnables);
-static QBasicMutex g_pendingRunnablesMutex;
+Q_CONSTINIT static QBasicMutex g_pendingRunnablesMutex;
#endif
/*!
@@ -74,14 +46,14 @@ static QBasicMutex g_pendingRunnablesMutex;
QT_DEFINE_NATIVE_INTERFACE(QAndroidApplication);
/*!
- \fn jobject QNativeInterface::QAndroidApplication::context()
+ \fn QJniObject QNativeInterface::QAndroidApplication::context()
- Returns the Android context as a \c jobject. The context is an \c Activity
+ Returns the Android context as a \c QJniObject. The context is an \c Activity
if the main activity object is valid. Otherwise, the context is a \c Service.
\since 6.2
*/
-jobject QNativeInterface::QAndroidApplication::context()
+QtJniTypes::Context QNativeInterface::QAndroidApplication::context()
{
return QtAndroidPrivate::context();
}
@@ -96,7 +68,7 @@ jobject QNativeInterface::QAndroidApplication::context()
*/
bool QNativeInterface::QAndroidApplication::isActivityContext()
{
- return QtAndroidPrivate::activity();
+ return QtAndroidPrivate::activity().isValid();
}
/*!
@@ -122,8 +94,7 @@ int QNativeInterface::QAndroidApplication::sdkVersion()
*/
void QNativeInterface::QAndroidApplication::hideSplashScreen(int duration)
{
- QJniObject::callStaticMethod<void>("org/qtproject/qt/android/QtNative",
- "hideSplashScreen", "(I)V", duration);
+ QtAndroidPrivate::activity().callMethod<void>("hideSplashScreen", duration);
}
/*!
@@ -191,12 +162,12 @@ QFuture<QVariant> QNativeInterface::QAndroidApplication::runOnAndroidMainThread(
const std::function<QVariant()> &runnable,
const QDeadlineTimer timeout)
{
- QSharedPointer<QPromise<QVariant>> promise(new QPromise<QVariant>());
+ auto promise = std::make_shared<QPromise<QVariant>>();
QFuture<QVariant> future = promise->future();
promise->start();
- (void) QtConcurrent::run([=, &future]() {
- if (!timeout.isForever()) {
+ if (!timeout.isForever()) {
+ QThreadPool::globalInstance()->start([=]() mutable {
QEventLoop loop;
QTimer::singleShot(timeout.remainingTime(), &loop, [&]() {
future.cancel();
@@ -212,12 +183,24 @@ QFuture<QVariant> QNativeInterface::QAndroidApplication::runOnAndroidMainThread(
loop.quit();
});
watcher.setFuture(future);
+
+ // we're going to sleep, make sure we don't block
+ // QThreadPool::globalInstance():
+
+ QThreadPool::globalInstance()->releaseThread();
+ const auto sg = qScopeGuard([] {
+ QThreadPool::globalInstance()->reserveThread();
+ });
loop.exec();
- }
- });
+ });
+ }
QMutexLocker locker(&g_pendingRunnablesMutex);
- g_pendingRunnables->push_back(std::pair(runnable, promise));
+#ifdef __cpp_aggregate_paren_init
+ g_pendingRunnables->emplace_back(runnable, std::move(promise));
+#else
+ g_pendingRunnables->push_back({runnable, std::move(promise)});
+#endif
locker.unlock();
QJniObject::callStaticMethod<void>(qtNativeClassName,
@@ -235,24 +218,23 @@ static void runPendingCppRunnables(JNIEnv */*env*/, jobject /*obj*/)
if (g_pendingRunnables->empty())
break;
- std::pair pair = std::move(g_pendingRunnables->front());
+ PendingRunnable r = std::move(g_pendingRunnables->front());
g_pendingRunnables->pop_front();
locker.unlock();
// run the runnable outside the sync block!
- auto promise = pair.second;
- if (!promise->isCanceled())
- promise->addResult(pair.first());
- promise->finish();
+ if (!r.promise->isCanceled())
+ r.promise->addResult(r.function());
+ r.promise->finish();
}
}
#endif
-bool QtAndroidPrivate::registerNativeInterfaceNatives()
+bool QtAndroidPrivate::registerNativeInterfaceNatives(QJniEnvironment &env)
{
#if QT_CONFIG(future) && !defined(QT_NO_QOBJECT)
- JNINativeMethod methods = {"runPendingCppRunnables", "()V", (void *)runPendingCppRunnables};
- return QJniEnvironment().registerNativeMethods(qtNativeClassName, &methods, 1);
+ const JNINativeMethod methods = {"runPendingCppRunnables", "()V", (void *)runPendingCppRunnables};
+ return env.registerNativeMethods(qtNativeClassName, &methods, 1);
#else
return true;
#endif