diff options
author | Lars Schmertmann <Lars.Schmertmann@governikus.de> | 2020-05-30 19:45:59 +0200 |
---|---|---|
committer | Lars Schmertmann <Lars.Schmertmann@governikus.de> | 2021-09-10 06:41:15 +0200 |
commit | f2b914afc6a5ba4aa2f8dcd13a979f04f07034c2 (patch) | |
tree | 287fe6a90f58c4ff4d83eec2f30943b90c96ee1e /src/plugins/platforms | |
parent | 5083d9dcd635346363a10c886abee3e99b21d9be (diff) |
Android: Enable QDesktopService
QDesktopServices::openUrl(const QUrl &url) is already implemented
on Android. But even if it is possible to set an URL handler, the
mechanism to invoke it is missing. With this commit the URL handler
will work on Android like it is already working on iOS.
Task-number: QTBUG-84382
Pick-to: 6.2
Change-Id: Ic560bd380f1cc59586861aa1a6a3ea064276a39e
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
Diffstat (limited to 'src/plugins/platforms')
-rw-r--r-- | src/plugins/platforms/android/qandroidplatformservices.cpp | 37 | ||||
-rw-r--r-- | src/plugins/platforms/android/qandroidplatformservices.h | 14 |
2 files changed, 49 insertions, 2 deletions
diff --git a/src/plugins/platforms/android/qandroidplatformservices.cpp b/src/plugins/platforms/android/qandroidplatformservices.cpp index 36e1aef609..8c2af5fff4 100644 --- a/src/plugins/platforms/android/qandroidplatformservices.cpp +++ b/src/plugins/platforms/android/qandroidplatformservices.cpp @@ -41,16 +41,32 @@ #include "qandroidplatformservices.h" #include <QDebug> +#include <QDesktopServices> #include <QFile> #include <QMimeDatabase> -#include <QUrl> #include <QtCore/QJniObject> #include <QtCore/qcoreapplication.h> +#include <QtCore/qscopedvaluerollback.h> QT_BEGIN_NAMESPACE QAndroidPlatformServices::QAndroidPlatformServices() { + m_actionView = QJniObject::getStaticObjectField("android/content/Intent", "ACTION_VIEW", + "Ljava/lang/String;") + .toString(); + + QtAndroidPrivate::registerNewIntentListener(this); + + QMetaObject::invokeMethod( + this, + [this] { + QJniObject context = QJniObject(QtAndroidPrivate::context()); + QJniObject intent = + context.callObjectMethod("getIntent", "()Landroid/content/Intent;"); + handleNewIntent(nullptr, intent.object()); + }, + Qt::QueuedConnection); } bool QAndroidPlatformServices::openUrl(const QUrl &theUrl) @@ -58,6 +74,10 @@ bool QAndroidPlatformServices::openUrl(const QUrl &theUrl) QString mime; QUrl url(theUrl); + // avoid recursing back into self + if (url == m_handlingUrl) + return false; + // if the file is local, we need to pass the MIME type, otherwise Android // does not start an Intent to view this file QLatin1String fileScheme("file"); @@ -87,4 +107,19 @@ QByteArray QAndroidPlatformServices::desktopEnvironment() const return QByteArray("Android"); } +bool QAndroidPlatformServices::handleNewIntent(JNIEnv *env, jobject intent) +{ + Q_UNUSED(env); + + const QJniObject jniIntent(intent); + + const QString action = jniIntent.callObjectMethod<jstring>("getAction").toString(); + if (action != m_actionView) + return false; + + const QString url = jniIntent.callObjectMethod<jstring>("getDataString").toString(); + QScopedValueRollback<QUrl> rollback(m_handlingUrl, url); + return QDesktopServices::openUrl(url); +} + QT_END_NAMESPACE diff --git a/src/plugins/platforms/android/qandroidplatformservices.h b/src/plugins/platforms/android/qandroidplatformservices.h index 6f2f0a394f..dff132d56d 100644 --- a/src/plugins/platforms/android/qandroidplatformservices.h +++ b/src/plugins/platforms/android/qandroidplatformservices.h @@ -42,16 +42,28 @@ #include <qpa/qplatformservices.h> #include "androidjnimain.h" +#include <QtCore/private/qjnihelpers_p.h> +#include <QtCore/qobject.h> +#include <QUrl> QT_BEGIN_NAMESPACE -class QAndroidPlatformServices: public QPlatformServices +class QAndroidPlatformServices : public QObject, + public QPlatformServices, + public QtAndroidPrivate::NewIntentListener { public: QAndroidPlatformServices(); + bool openUrl(const QUrl &url) override; bool openDocument(const QUrl &url) override; QByteArray desktopEnvironment() const override; + + bool handleNewIntent(JNIEnv *env, jobject intent) override; + +private: + QUrl m_handlingUrl; + QString m_actionView; }; QT_END_NAMESPACE |