7#if QT_CONFIG(desktopservices)
9#include <QDesktopServices>
11#include <QMimeDatabase>
12#include <QtCore/QJniObject>
13#include <QtCore/qcoreapplication.h>
14#include <QtCore/qscopedvaluerollback.h>
19using namespace QtJniTypes;
20using namespace Qt::StringLiterals;
22#if QT_CONFIG(desktopservices)
23static constexpr auto s_defaultScheme =
"file"_L1;
24static constexpr auto s_defaultProvider =
"qtprovider"_L1;
29#if QT_CONFIG(desktopservices)
30 m_actionView = QJniObject::getStaticObjectField(
"android/content/Intent",
"ACTION_VIEW",
34 QtAndroidPrivate::registerNewIntentListener(
this);
37 if (QNativeInterface::QAndroidApplication::isActivityContext()) {
38 QMetaObject::invokeMethod(
41 QJniObject context = QJniObject(QtAndroidPrivate::context());
43 context.callObjectMethod(
"getIntent",
"()Landroid/content/Intent;");
44 handleNewIntent(
nullptr, intent.object());
46 Qt::QueuedConnection);
53 return QByteArray(
"Android");
61#if QT_CONFIG(desktopservices)
62bool QAndroidPlatformServices::openUrl(
const QUrl &theUrl)
67 if (url == m_handlingUrl)
71 if (url.scheme().isEmpty())
72 url.setScheme(s_defaultScheme);
74 const int sdkVersion = QNativeInterface::QAndroidApplication::sdkVersion();
75 if (url.scheme() != s_defaultScheme || sdkVersion < 24 )
77 return openUrlWithFileProvider(url);
80QString QAndroidPlatformServices::getMimeOfUrl(
const QUrl &url)
const
83 if (url.scheme() == s_defaultScheme)
84 mime = QMimeDatabase().mimeTypeForUrl(url).name();
88bool QAndroidPlatformServices::openURL(
const QUrl &url)
const
90 return openURL(url.toString());
93bool QAndroidPlatformServices::openURL(
const QString &url)
const
95 return QJniObject::callStaticMethod<jboolean>(
96 QtAndroid::applicationClass(),
"openURL",
97 QNativeInterface::QAndroidApplication::context(),
102bool QAndroidPlatformServices::openUrlWithFileProvider(
const QUrl &url)
104 const QJniObject context = QNativeInterface::QAndroidApplication::context();
105 auto authorities = getFileProviderAuthorities(context);
106 if (authorities.isEmpty())
108 return openUrlWithAuthority(url, getAdequateFileproviderAuthority(authorities));
112QString QAndroidPlatformServices::getAdequateFileproviderAuthority(
const QStringList &authorities)
const
114 if (authorities.size() == 1)
115 return authorities[0];
117 QString nonQtAuthority;
118 for (
const auto &authority : authorities) {
119 if (!authority.endsWith(s_defaultProvider, Qt::CaseSensitive)) {
120 nonQtAuthority = authority;
124 return nonQtAuthority;
127bool QAndroidPlatformServices::openUrlWithAuthority(
const QUrl &url,
const QString &authority)
129 const auto urlPath = QJniObject::fromString(url.path());
130 const auto urlFile = QJniObject(Traits<File>::className(),
131 urlPath.object<jstring>());
132 const auto fileProviderUri = QJniObject::callStaticMethod<Uri>(
133 Traits<FileProvider>::className(),
"getUriForFile",
134 QNativeInterface::QAndroidApplication::context(), authority,
135 urlFile.object<File>());
136 if (fileProviderUri.isValid())
137 return openURL(fileProviderUri.toString());
141QStringList QAndroidPlatformServices::getFileProviderAuthorities(
const QJniObject &context)
const
143 QStringList authorityList;
145 const auto packageManager = context.callMethod<PackageManager>(
"getPackageManager");
146 const auto packageName = context.callMethod<QString>(
"getPackageName");
147 const auto packageInfo = packageManager.callMethod<PackageInfo>(
"getPackageInfo",
150 const auto providersArray = packageInfo.getField<ProviderInfo[]>(
"providers");
152 if (providersArray.isValid()) {
153 const auto className = Traits<FileProvider>::className();
154 for (
const auto &fileProvider : providersArray) {
155 auto providerName = fileProvider.getField<QString>(
"name");
156 if (providerName.replace(
".",
"/").contains(className.data())) {
157 const auto authority = fileProvider.getField<QString>(
"authority");
158 if (!authority.isEmpty())
159 authorityList << authority;
163 if (authorityList.isEmpty())
164 qWarning() <<
"No file provider found in the AndroidManifest.xml.";
166 return authorityList;
169bool QAndroidPlatformServices::openDocument(
const QUrl &url)
174bool QAndroidPlatformServices::handleNewIntent(JNIEnv *env, jobject intent)
178 const QJniObject jniIntent(intent);
180 const QString action = jniIntent.callObjectMethod<jstring>(
"getAction").toString();
181 if (action != m_actionView)
184 const QString url = jniIntent.callObjectMethod<jstring>(
"getDataString").toString();
185 QScopedValueRollback<QUrl> rollback(m_handlingUrl, url);
186 return QDesktopServices::openUrl(url);
Q_DECLARE_JNI_CLASS(MotionEvent, "android/view/MotionEvent")