diff options
Diffstat (limited to 'src/plugins/networkinformation/android')
5 files changed, 536 insertions, 0 deletions
diff --git a/src/plugins/networkinformation/android/CMakeLists.txt b/src/plugins/networkinformation/android/CMakeLists.txt new file mode 100644 index 0000000000..f1d260547a --- /dev/null +++ b/src/plugins/networkinformation/android/CMakeLists.txt @@ -0,0 +1,43 @@ + +set(java_sources + jar/src/org/qtproject/qt/android/networkinformation/QtAndroidNetworkInformation.java +) + +qt_internal_add_jar(Qt${QtBase_VERSION_MAJOR}AndroidNetworkInformationBackend + INCLUDE_JARS ${QT_ANDROID_JAR} + SOURCES ${java_sources} + OUTPUT_DIR "${QT_BUILD_DIR}/jar" +) + +install_jar(Qt${QtBase_VERSION_MAJOR}AndroidNetworkInformationBackend + DESTINATION jar + COMPONENT Devel +) + +qt_internal_add_plugin(QAndroidNetworkInformationPlugin + OUTPUT_NAME androidnetworkinformation + CLASS_NAME QAndroidNetworkInformationBackendFactory + TYPE networkinformation + DEFAULT_IF ANDROID + SOURCES + qandroidnetworkinformationbackend.cpp + wrapper/androidconnectivitymanager.cpp wrapper/androidconnectivitymanager.h + LIBRARIES + Qt::NetworkPrivate + DEFINES + QT_USE_QSTRINGBUILDER +) + +set_property( + TARGET + QAndroidNetworkInformationPlugin + APPEND PROPERTY QT_ANDROID_BUNDLED_JAR_DEPENDENCIES + jar/Qt${QtBase_VERSION_MAJOR}AndroidNetworkInformationBackend.jar +) + +set_property( + TARGET + QAndroidNetworkInformationPlugin + APPEND PROPERTY QT_ANDROID_PERMISSIONS + android.permission.ACCESS_NETWORK_STATE +) diff --git a/src/plugins/networkinformation/android/jar/src/org/qtproject/qt/android/networkinformation/QtAndroidNetworkInformation.java b/src/plugins/networkinformation/android/jar/src/org/qtproject/qt/android/networkinformation/QtAndroidNetworkInformation.java new file mode 100644 index 0000000000..0e89d23ab4 --- /dev/null +++ b/src/plugins/networkinformation/android/jar/src/org/qtproject/qt/android/networkinformation/QtAndroidNetworkInformation.java @@ -0,0 +1,136 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins 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$ +** +****************************************************************************/ + +package org.qtproject.qt.android.networkinformation; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.net.ConnectivityManager; +import android.net.ConnectivityManager.NetworkCallback; +import android.net.NetworkRequest; +import android.net.NetworkCapabilities; +import android.net.Network; + +public class QtAndroidNetworkInformation { + private static final String LOG_TAG = "QtAndroidNetworkInformation"; + + private static native void connectivityChanged(); + private static native void behindCaptivePortalChanged(boolean state); + + private static QtNetworkInformationCallback m_callback = null; + private static final Object m_lock = new Object(); + + enum AndroidConnectivity { + Connected, Unknown, Disconnected + } + + private static class QtNetworkInformationCallback extends NetworkCallback { + public AndroidConnectivity previousState = null; + + QtNetworkInformationCallback() { + } + + @Override + public void onCapabilitiesChanged(Network network, NetworkCapabilities capabilities) { + AndroidConnectivity s; + if (!capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) + s = AndroidConnectivity.Disconnected; + else if (capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) + s = AndroidConnectivity.Connected; + else + s = AndroidConnectivity.Unknown; // = we _may_ have Internet access + setState(s); + + final boolean captive + = capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL); + behindCaptivePortalChanged(captive); + } + + private void setState(AndroidConnectivity s) { + if (previousState != s) { + previousState = s; + connectivityChanged(); + } + } + + @Override + public void onLost(Network network) { + setState(AndroidConnectivity.Disconnected); + } + } + + private QtAndroidNetworkInformation() { + } + + public static AndroidConnectivity state() { + if (m_callback != null && m_callback.previousState != null) + return m_callback.previousState; + return AndroidConnectivity.Unknown; + } + + public static void registerReceiver(final Context context) { + synchronized (m_lock) { + if (m_callback == null) { + ConnectivityManager manager = getConnectivityManager(context); + m_callback = new QtNetworkInformationCallback(); + NetworkRequest.Builder builder = new NetworkRequest.Builder(); + builder = builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) + builder = builder.addCapability(NetworkCapabilities.NET_CAPABILITY_FOREGROUND); + NetworkRequest request = builder.build(); + manager.registerNetworkCallback(request, m_callback); + } + } + } + + public static void unregisterReceiver(final Context context) { + synchronized (m_lock) { + if (m_callback != null) { + getConnectivityManager(context).unregisterNetworkCallback(m_callback); + m_callback = null; + } + } + } + + public static ConnectivityManager getConnectivityManager(final Context context) { + return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + } +} diff --git a/src/plugins/networkinformation/android/qandroidnetworkinformationbackend.cpp b/src/plugins/networkinformation/android/qandroidnetworkinformationbackend.cpp new file mode 100644 index 0000000000..9eef471989 --- /dev/null +++ b/src/plugins/networkinformation/android/qandroidnetworkinformationbackend.cpp @@ -0,0 +1,136 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtNetwork 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$ +** +****************************************************************************/ + +#include <QtNetwork/private/qnetworkinformation_p.h> + +#include "wrapper/androidconnectivitymanager.h" + +#include <QtCore/qglobal.h> + +QT_BEGIN_NAMESPACE +Q_DECLARE_LOGGING_CATEGORY(lcNetInfoAndroid) +Q_LOGGING_CATEGORY(lcNetInfoAndroid, "qt.network.info.android"); + +static const QString backendName = QStringLiteral("android"); + +class QAndroidNetworkInformationBackend : public QNetworkInformationBackend +{ + Q_OBJECT +public: + QAndroidNetworkInformationBackend(); + ~QAndroidNetworkInformationBackend() { m_valid = false; } + + QString name() const override { return backendName; } + QNetworkInformation::Features featuresSupported() const override + { + return featuresSupportedStatic(); + } + + static QNetworkInformation::Features featuresSupportedStatic() + { + using Feature = QNetworkInformation::Feature; + return QNetworkInformation::Features(Feature::Reachability | Feature::CaptivePortal); + } + + bool isValid() { return m_valid; } + +private: + Q_DISABLE_COPY_MOVE(QAndroidNetworkInformationBackend); + bool m_valid = false; +}; + +class QAndroidNetworkInformationBackendFactory : public QNetworkInformationBackendFactory +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID QNetworkInformationBackendFactory_iid) + Q_INTERFACES(QNetworkInformationBackendFactory) +public: + QAndroidNetworkInformationBackendFactory() = default; + ~QAndroidNetworkInformationBackendFactory() = default; + QString name() const override { return backendName; } + QNetworkInformation::Features featuresSupported() const override + { + return QAndroidNetworkInformationBackend::featuresSupportedStatic(); + } + + QNetworkInformationBackend * + create(QNetworkInformation::Features requiredFeatures) const override + { + if ((requiredFeatures & featuresSupported()) != requiredFeatures) + return nullptr; + auto backend = new QAndroidNetworkInformationBackend(); + if (!backend->isValid()) + delete std::exchange(backend, nullptr); + return backend; + } + +private: + Q_DISABLE_COPY_MOVE(QAndroidNetworkInformationBackendFactory); +}; + +QAndroidNetworkInformationBackend::QAndroidNetworkInformationBackend() +{ + auto conman = AndroidConnectivityManager::getInstance(); + if (!conman) + return; + m_valid = true; + setReachability(QNetworkInformation::Reachability::Unknown); + connect(conman, &AndroidConnectivityManager::connectivityChanged, this, [this, conman]() { + static const auto mapState = [](AndroidConnectivityManager::AndroidConnectivity state) { + switch (state) { + case AndroidConnectivityManager::AndroidConnectivity::Connected: + return QNetworkInformation::Reachability::Online; + case AndroidConnectivityManager::AndroidConnectivity::Disconnected: + return QNetworkInformation::Reachability::Disconnected; + case AndroidConnectivityManager::AndroidConnectivity::Unknown: + default: + return QNetworkInformation::Reachability::Unknown; + } + }; + + setReachability(mapState(conman->networkConnectivity())); + }); + + connect(conman, &AndroidConnectivityManager::captivePortalChanged, this, + &QAndroidNetworkInformationBackend::setBehindCaptivePortal); +} + +QT_END_NAMESPACE + +#include "qandroidnetworkinformationbackend.moc" diff --git a/src/plugins/networkinformation/android/wrapper/androidconnectivitymanager.cpp b/src/plugins/networkinformation/android/wrapper/androidconnectivitymanager.cpp new file mode 100644 index 0000000000..e88fe7d955 --- /dev/null +++ b/src/plugins/networkinformation/android/wrapper/androidconnectivitymanager.cpp @@ -0,0 +1,146 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins 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$ +** +****************************************************************************/ + +#include "androidconnectivitymanager.h" + +#include <QtCore/qcoreapplication.h> +#include <QtCore/qjnienvironment.h> + +QT_BEGIN_NAMESPACE + +using namespace QNativeInterface; + +struct AndroidConnectivityManagerInstance +{ + AndroidConnectivityManagerInstance() : connManager(new AndroidConnectivityManager) { } + std::unique_ptr<AndroidConnectivityManager> connManager = nullptr; +}; +Q_GLOBAL_STATIC(AndroidConnectivityManagerInstance, androidConnManagerInstance) + +static const char networkInformationClass[] = + "org/qtproject/qt/android/networkinformation/QtAndroidNetworkInformation"; + +static void networkConnectivityChanged(JNIEnv *env, jobject obj) +{ + Q_UNUSED(env); + Q_UNUSED(obj); + Q_EMIT androidConnManagerInstance->connManager->connectivityChanged(); +} + +static void behindCaptivePortalChanged(JNIEnv *env, jobject obj, jboolean state) +{ + Q_UNUSED(env); + Q_UNUSED(obj); + Q_EMIT androidConnManagerInstance->connManager->captivePortalChanged(state); +} + +AndroidConnectivityManager::AndroidConnectivityManager() +{ + if (!registerNatives()) + return; + + m_connectivityManager = QJniObject::callStaticObjectMethod( + networkInformationClass, "getConnectivityManager", + "(Landroid/content/Context;)Landroid/net/ConnectivityManager;", + QAndroidApplication::context()); + if (!m_connectivityManager.isValid()) + return; + + QJniObject::callStaticMethod<void>(networkInformationClass, "registerReceiver", + "(Landroid/content/Context;)V", QAndroidApplication::context()); +} + +AndroidConnectivityManager *AndroidConnectivityManager::getInstance() +{ + if (!androidConnManagerInstance()) + return nullptr; + return androidConnManagerInstance->connManager->isValid() + ? androidConnManagerInstance->connManager.get() + : nullptr; +} + +AndroidConnectivityManager::~AndroidConnectivityManager() +{ + QJniObject::callStaticMethod<void>(networkInformationClass, "unregisterReceiver", + "(Landroid/content/Context;)V", QAndroidApplication::context()); +} + +AndroidConnectivityManager::AndroidConnectivity AndroidConnectivityManager::networkConnectivity() +{ + QJniEnvironment env; + QJniObject networkReceiver(networkInformationClass); + jclass clazz = env->GetObjectClass(networkReceiver.object()); + static const QByteArray functionSignature = + QByteArray(QByteArray("()L") + networkInformationClass + "$AndroidConnectivity;"); + QJniObject enumObject = + QJniObject::callStaticObjectMethod(clazz, "state", functionSignature.data()); + if (!enumObject.isValid()) + return AndroidConnectivityManager::AndroidConnectivity::Unknown; + + QJniObject enumName = enumObject.callObjectMethod<jstring>("name"); + if (!enumName.isValid()) + return AndroidConnectivityManager::AndroidConnectivity::Unknown; + + QString name = enumName.toString(); + if (name == u"Connected") + return AndroidConnectivity::Connected; + if (name == u"Disconnected") + return AndroidConnectivity::Disconnected; + return AndroidConnectivity::Unknown; +} + +bool AndroidConnectivityManager::registerNatives() +{ + QJniEnvironment env; + QJniObject networkReceiver(networkInformationClass); + if (!networkReceiver.isValid()) + return false; + + jclass clazz = env->GetObjectClass(networkReceiver.object()); + static JNINativeMethod methods[] = { + { "connectivityChanged", "()V", reinterpret_cast<void *>(networkConnectivityChanged) }, + { "behindCaptivePortalChanged", "(Z)V", + reinterpret_cast<void *>(behindCaptivePortalChanged) } + }; + const bool ret = (env->RegisterNatives(clazz, methods, std::size(methods)) == JNI_OK); + env->DeleteLocalRef(clazz); + return ret; +} + +QT_END_NAMESPACE diff --git a/src/plugins/networkinformation/android/wrapper/androidconnectivitymanager.h b/src/plugins/networkinformation/android/wrapper/androidconnectivitymanager.h new file mode 100644 index 0000000000..14f5aa9b57 --- /dev/null +++ b/src/plugins/networkinformation/android/wrapper/androidconnectivitymanager.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins 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$ +** +****************************************************************************/ + +#ifndef ANDROIDCONNECTIVITYMANAGER_H +#define ANDROIDCONNECTIVITYMANAGER_H + +#include <QObject> +#include <QtCore/qjniobject.h> + +QT_BEGIN_NAMESPACE + +class AndroidConnectivityManager : public QObject +{ + Q_OBJECT +public: + enum class AndroidConnectivity { Connected, Unknown, Disconnected }; + Q_ENUM(AndroidConnectivity); + static AndroidConnectivityManager *getInstance(); + ~AndroidConnectivityManager(); + + AndroidConnectivity networkConnectivity(); + inline bool isValid() const { return m_connectivityManager.isValid(); } + +Q_SIGNALS: + void connectivityChanged(); + void captivePortalChanged(bool state); + +private: + friend struct AndroidConnectivityManagerInstance; + AndroidConnectivityManager(); + bool registerNatives(); + QJniObject m_connectivityManager; + + Q_DISABLE_COPY_MOVE(AndroidConnectivityManager); +}; + +QT_END_NAMESPACE + +#endif // ANDROIDCONNECTIVITYMANAGER_H |