summaryrefslogtreecommitdiffstats
path: root/src/corelib/platform/android/qandroidextras.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/platform/android/qandroidextras.cpp')
-rw-r--r--src/corelib/platform/android/qandroidextras.cpp313
1 files changed, 252 insertions, 61 deletions
diff --git a/src/corelib/platform/android/qandroidextras.cpp b/src/corelib/platform/android/qandroidextras.cpp
index e58e1a1179..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"
@@ -47,6 +11,10 @@
#include <QtCore/qtimer.h>
#include <QtCore/qset.h>
+#if QT_CONFIG(future)
+#include <QtCore/qpromise.h>
+#endif
+
QT_BEGIN_NAMESPACE
class QAndroidParcelPrivate
@@ -149,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
*/
@@ -265,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
*/
@@ -402,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
@@ -412,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
*/
@@ -474,21 +451,19 @@ QJniObject QAndroidServiceConnection::handle() const
*/
+Q_CONSTINIT static QBasicAtomicInteger<uint> nextUniqueActivityRequestCode = Q_BASIC_ATOMIC_INITIALIZER(0);
// Get a unique activity request code.
static int uniqueActivityRequestCode()
{
- static QMutex mutex;
- static int requestCode = 0x1000; // Reserve all request codes under 0x1000 for Qt.
-
- QMutexLocker locker(&mutex);
- if (requestCode == 0xf3ee) // Special case for MINISTRO_INSTALL_REQUEST_CODE
- requestCode++;
+ constexpr uint ReservedForQtOffset = 0x1000; // Reserve all request codes under 0x1000 for Qt
- if (requestCode == INT_MAX)
+ const uint requestCodeBase = nextUniqueActivityRequestCode.fetchAndAddRelaxed(1);
+ if (requestCodeBase == uint(INT_MAX) - ReservedForQtOffset)
qWarning("Unique activity request code has wrapped. Unexpected behavior may occur.");
- return requestCode++;
+ const int requestCode = static_cast<int>(requestCodeBase + ReservedForQtOffset);
+ return requestCode;
}
class QAndroidActivityResultReceiverPrivate: public QtAndroidPrivate::ActivityResultListener
@@ -500,19 +475,22 @@ public:
int globalRequestCode(int localRequestCode) const
{
- if (!localToGlobalRequestCode.contains(localRequestCode)) {
+ const auto oldSize = localToGlobalRequestCode.size();
+ auto &e = localToGlobalRequestCode[localRequestCode];
+ if (localToGlobalRequestCode.size() != oldSize) {
+ // new entry, populate:
int globalRequestCode = uniqueActivityRequestCode();
- localToGlobalRequestCode[localRequestCode] = globalRequestCode;
+ e = globalRequestCode;
globalToLocalRequestCode[globalRequestCode] = localRequestCode;
}
- return localToGlobalRequestCode.value(localRequestCode);
+ return e;
}
bool handleActivityResult(jint requestCode, jint resultCode, jobject data)
{
- if (globalToLocalRequestCode.contains(requestCode)) {
- q->handleActivityResult(globalToLocalRequestCode.value(requestCode),
- resultCode, QJniObject(data));
+ const auto it = std::as_const(globalToLocalRequestCode).find(requestCode);
+ if (it != globalToLocalRequestCode.cend()) {
+ q->handleActivityResult(*it, resultCode, QJniObject(data));
return true;
}
@@ -527,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
*/
/*!
@@ -622,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
*/
@@ -688,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
*/
@@ -819,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
*/
/*!
@@ -1034,4 +1063,166 @@ void QAndroidActivityCallbackResultReceiver::registerCallback(
callbackMap.insert(receiverRequestCode, callbackFunc);
}
+// Permissions API
+
+static const char qtNativeClassName[] = "org/qtproject/qt/android/QtNative";
+
+QtAndroidPrivate::PermissionResult resultFromAndroid(jint value)
+{
+ return value == 0 ? QtAndroidPrivate::Authorized : QtAndroidPrivate::Denied;
+}
+
+using PendingPermissionRequestsHash
+ = QHash<int, QSharedPointer<QPromise<QtAndroidPrivate::PermissionResult>>>;
+Q_GLOBAL_STATIC(PendingPermissionRequestsHash, g_pendingPermissionRequests);
+Q_CONSTINIT static QBasicMutex g_pendingPermissionRequestsMutex;
+
+static int nextRequestCode()
+{
+ Q_CONSTINIT static QBasicAtomicInt counter = Q_BASIC_ATOMIC_INITIALIZER(0);
+ return counter.fetchAndAddRelaxed(1);
+}
+
+/*!
+ \internal
+
+ This function is called when the result of the permission request is available.
+ Once a permission is requested, the result is braodcast by the OS and listened
+ to by QtActivity which passes it to C++ through a native JNI method call.
+ */
+static void sendRequestPermissionsResult(JNIEnv *env, jobject *obj, jint requestCode,
+ jobjectArray permissions, jintArray grantResults)
+{
+ Q_UNUSED(obj);
+
+ QMutexLocker locker(&g_pendingPermissionRequestsMutex);
+ auto it = g_pendingPermissionRequests->constFind(requestCode);
+ if (it == g_pendingPermissionRequests->constEnd()) {
+ qWarning() << "Found no valid pending permission request for request code" << requestCode;
+ return;
+ }
+
+ auto request = *it;
+ g_pendingPermissionRequests->erase(it);
+ locker.unlock();
+
+ const int size = env->GetArrayLength(permissions);
+ std::unique_ptr<jint[]> results(new jint[size]);
+ env->GetIntArrayRegion(grantResults, 0, size, results.get());
+
+ for (int i = 0 ; i < size; ++i) {
+ QtAndroidPrivate::PermissionResult result = resultFromAndroid(results[i]);
+ request->addResult(result, i);
+ }
+
+ QtAndroidPrivate::releaseAndroidDeadlockProtector();
+ request->finish();
+}
+
+QFuture<QtAndroidPrivate::PermissionResult>
+requestPermissionsInternal(const QStringList &permissions)
+{
+ // 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) {
+ 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;
+ jclass clazz = env.findClass("java/lang/String");
+ auto array = env->NewObjectArray(permissions.size(), clazz, nullptr);
+ int index = 0;
+
+ for (auto &perm : permissions)
+ env->SetObjectArrayElement(array, index++, QJniObject::fromString(perm).object());
+
+ QJniObject(QtAndroidPrivate::activity()).callMethod<void>("requestPermissions",
+ "([Ljava/lang/String;I)V",
+ array,
+ requestCode);
+ env->DeleteLocalRef(array);
+ });
+
+ return future;
+}
+
+/*!
+ \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(const QString &permission)
+{
+ return requestPermissions({permission});
+}
+
+QFuture<QtAndroidPrivate::PermissionResult>
+QtAndroidPrivate::requestPermissions(const QStringList &permissions)
+{
+ // avoid the uneccessary call and response to an empty permission string
+ if (permissions.isEmpty())
+ return QtFuture::makeReadyValueFuture(QtAndroidPrivate::Denied);
+ return requestPermissionsInternal(permissions);
+}
+
+/*!
+ \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(const QString &permission)
+{
+ QtAndroidPrivate::PermissionResult result = Denied;
+ if (!permission.isEmpty()) {
+ auto res = QJniObject::callStaticMethod<jint>(qtNativeClassName,
+ "checkSelfPermission",
+ "(Ljava/lang/String;)I",
+ QJniObject::fromString(permission).object());
+ result = resultFromAndroid(res);
+ }
+ return QtFuture::makeReadyValueFuture(result);
+}
+
+bool QtAndroidPrivate::registerPermissionNatives(QJniEnvironment &env)
+{
+ if (QtAndroidPrivate::androidSdkVersion() < 23)
+ return true;
+
+ const JNINativeMethod methods[] = {
+ {"sendRequestPermissionsResult", "(I[Ljava/lang/String;[I)V",
+ reinterpret_cast<void *>(sendRequestPermissionsResult)
+ }};
+
+ return env.registerNativeMethods(qtNativeClassName, methods, 1);
+}
+
QT_END_NAMESPACE
+
+#include "moc_qandroidextras_p.cpp"