PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/plugins/platforms/android/qandroidplatformservices.cpp

https://gitlab.com/f3822/qtbase
C++ | 125 lines | 65 code | 17 blank | 43 comment | 8 complexity | 04936ad900bfd915d83a6679ca15fc0f MD5 | raw file
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 BogDan Vatra <bogdan@kde.org>
  4. ** Copyright (C) 2021 The Qt Company Ltd.
  5. ** Contact: https://www.qt.io/licensing/
  6. **
  7. ** This file is part of the plugins of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** Commercial License Usage
  11. ** Licensees holding valid commercial Qt licenses may use this file in
  12. ** accordance with the commercial license agreement provided with the
  13. ** Software or, alternatively, in accordance with the terms contained in
  14. ** a written agreement between you and The Qt Company. For licensing terms
  15. ** and conditions see https://www.qt.io/terms-conditions. For further
  16. ** information use the contact form at https://www.qt.io/contact-us.
  17. **
  18. ** GNU Lesser General Public License Usage
  19. ** Alternatively, this file may be used under the terms of the GNU Lesser
  20. ** General Public License version 3 as published by the Free Software
  21. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  22. ** packaging of this file. Please review the following information to
  23. ** ensure the GNU Lesser General Public License version 3 requirements
  24. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  25. **
  26. ** GNU General Public License Usage
  27. ** Alternatively, this file may be used under the terms of the GNU
  28. ** General Public License version 2.0 or (at your option) the GNU General
  29. ** Public license version 3 or any later version approved by the KDE Free
  30. ** Qt Foundation. The licenses are as published by the Free Software
  31. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  32. ** included in the packaging of this file. Please review the following
  33. ** information to ensure the GNU General Public License requirements will
  34. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  35. ** https://www.gnu.org/licenses/gpl-3.0.html.
  36. **
  37. ** $QT_END_LICENSE$
  38. **
  39. ****************************************************************************/
  40. #include "qandroidplatformservices.h"
  41. #include <QDebug>
  42. #include <QDesktopServices>
  43. #include <QFile>
  44. #include <QMimeDatabase>
  45. #include <QtCore/QJniObject>
  46. #include <QtCore/qcoreapplication.h>
  47. #include <QtCore/qscopedvaluerollback.h>
  48. QT_BEGIN_NAMESPACE
  49. QAndroidPlatformServices::QAndroidPlatformServices()
  50. {
  51. m_actionView = QJniObject::getStaticObjectField("android/content/Intent", "ACTION_VIEW",
  52. "Ljava/lang/String;")
  53. .toString();
  54. QtAndroidPrivate::registerNewIntentListener(this);
  55. QMetaObject::invokeMethod(
  56. this,
  57. [this] {
  58. QJniObject context = QJniObject(QtAndroidPrivate::context());
  59. QJniObject intent =
  60. context.callObjectMethod("getIntent", "()Landroid/content/Intent;");
  61. handleNewIntent(nullptr, intent.object());
  62. },
  63. Qt::QueuedConnection);
  64. }
  65. bool QAndroidPlatformServices::openUrl(const QUrl &theUrl)
  66. {
  67. QString mime;
  68. QUrl url(theUrl);
  69. // avoid recursing back into self
  70. if (url == m_handlingUrl)
  71. return false;
  72. // if the file is local, we need to pass the MIME type, otherwise Android
  73. // does not start an Intent to view this file
  74. QLatin1String fileScheme("file");
  75. if ((url.scheme().isEmpty() || url.scheme() == fileScheme) && QFile::exists(url.path())) {
  76. // a real URL including the scheme is needed, else the Intent can not be started
  77. url.setScheme(fileScheme);
  78. QMimeDatabase mimeDb;
  79. mime = mimeDb.mimeTypeForUrl(url).name();
  80. }
  81. using namespace QNativeInterface;
  82. QJniObject urlString = QJniObject::fromString(url.toString());
  83. QJniObject mimeString = QJniObject::fromString(mime);
  84. return QJniObject::callStaticMethod<jboolean>(
  85. QtAndroid::applicationClass(), "openURL",
  86. "(Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;)Z",
  87. QAndroidApplication::context(), urlString.object(), mimeString.object());
  88. }
  89. bool QAndroidPlatformServices::openDocument(const QUrl &url)
  90. {
  91. return openUrl(url);
  92. }
  93. QByteArray QAndroidPlatformServices::desktopEnvironment() const
  94. {
  95. return QByteArray("Android");
  96. }
  97. bool QAndroidPlatformServices::handleNewIntent(JNIEnv *env, jobject intent)
  98. {
  99. Q_UNUSED(env);
  100. const QJniObject jniIntent(intent);
  101. const QString action = jniIntent.callObjectMethod<jstring>("getAction").toString();
  102. if (action != m_actionView)
  103. return false;
  104. const QString url = jniIntent.callObjectMethod<jstring>("getDataString").toString();
  105. QScopedValueRollback<QUrl> rollback(m_handlingUrl, url);
  106. return QDesktopServices::openUrl(url);
  107. }
  108. QT_END_NAMESPACE