diff options
Diffstat (limited to 'src/plugins/networkinformation/android')
6 files changed, 68 insertions, 211 deletions
diff --git a/src/plugins/networkinformation/android/CMakeLists.txt b/src/plugins/networkinformation/android/CMakeLists.txt index 0883ec74e2..07d9201bbb 100644 --- a/src/plugins/networkinformation/android/CMakeLists.txt +++ b/src/plugins/networkinformation/android/CMakeLists.txt @@ -1,3 +1,6 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + set(java_sources jar/src/org/qtproject/qt/android/networkinformation/QtAndroidNetworkInformation.java @@ -9,8 +12,10 @@ qt_internal_add_jar(Qt${QtBase_VERSION_MAJOR}AndroidNetworkInformationBackend OUTPUT_DIR "${QT_BUILD_DIR}/jar" ) +qt_path_join(destination ${INSTALL_DATADIR} "jar") + install_jar(Qt${QtBase_VERSION_MAJOR}AndroidNetworkInformationBackend - DESTINATION jar + DESTINATION ${destination} COMPONENT Devel ) @@ -24,8 +29,6 @@ qt_internal_add_plugin(QAndroidNetworkInformationPlugin wrapper/androidconnectivitymanager.cpp wrapper/androidconnectivitymanager.h LIBRARIES Qt::NetworkPrivate - DEFINES - QT_USE_QSTRINGBUILDER ) set_property( diff --git a/src/plugins/networkinformation/android/jar/build.gradle b/src/plugins/networkinformation/android/jar/build.gradle index c947852f79..68a9381ad2 100644 --- a/src/plugins/networkinformation/android/jar/build.gradle +++ b/src/plugins/networkinformation/android/jar/build.gradle @@ -7,7 +7,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:7.0.2' + classpath 'com.android.tools.build:gradle:8.0.2' } } @@ -23,12 +23,10 @@ repositories { } android { - compileSdkVersion 31 - buildToolsVersion "31.0.3" + compileSdk 34 defaultConfig { minSdkVersion 23 - targetSdkVersion 31 } sourceSets { 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 index 51a3b43fd0..6a56c506b0 100644 --- 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 @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** 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$ -** -****************************************************************************/ +// 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 package org.qtproject.qt.android.networkinformation; @@ -51,9 +15,9 @@ import android.os.Build; public class QtAndroidNetworkInformation { private static final String LOG_TAG = "QtAndroidNetworkInformation"; - private static native void connectivityChanged(AndroidConnectivity connectivity); - private static native void behindCaptivePortalChanged(boolean state); - private static native void transportMediumChanged(Transport transportMedium); + private static native void networkConnectivityChanged(int connectivity); + private static native void genericInfoChanged(boolean captivePortal, boolean metered); + private static native void transportMediumChanged(int transportMedium); private static QtNetworkInformationCallback m_callback = null; private static final Object m_lock = new Object(); @@ -101,7 +65,9 @@ public class QtAndroidNetworkInformation { final boolean captive = capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL); - behindCaptivePortalChanged(captive); + final boolean metered + = !capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED); + genericInfoChanged(captive, metered); } private Transport getTransport(NetworkCapabilities capabilities) @@ -130,14 +96,14 @@ public class QtAndroidNetworkInformation { private void setState(AndroidConnectivity s) { if (previousState != s) { previousState = s; - connectivityChanged(s); + networkConnectivityChanged(s.ordinal()); } } private void setTransportMedium(Transport t) { if (previousTransport != t) { previousTransport = t; - transportMediumChanged(t); + transportMediumChanged(t.ordinal()); } } diff --git a/src/plugins/networkinformation/android/qandroidnetworkinformationbackend.cpp b/src/plugins/networkinformation/android/qandroidnetworkinformationbackend.cpp index 14e42a9de8..44e4d447d2 100644 --- a/src/plugins/networkinformation/android/qandroidnetworkinformationbackend.cpp +++ b/src/plugins/networkinformation/android/qandroidnetworkinformationbackend.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** 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$ -** -****************************************************************************/ +// 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 <QtNetwork/private/qnetworkinformation_p.h> @@ -47,7 +11,10 @@ QT_BEGIN_NAMESPACE Q_DECLARE_LOGGING_CATEGORY(lcNetInfoAndroid) Q_LOGGING_CATEGORY(lcNetInfoAndroid, "qt.network.info.android"); -static const QString backendName = QStringLiteral("android"); +static QString backendName() { + return QString::fromUtf16(QNetworkInformationBackend::PluginNames + [QNetworkInformationBackend::PluginNamesAndroidIndex]); +} class QAndroidNetworkInformationBackend : public QNetworkInformationBackend { @@ -56,7 +23,7 @@ public: QAndroidNetworkInformationBackend(); ~QAndroidNetworkInformationBackend() { m_valid = false; } - QString name() const override { return backendName; } + QString name() const override { return backendName(); } QNetworkInformation::Features featuresSupported() const override { return featuresSupportedStatic(); @@ -88,7 +55,7 @@ class QAndroidNetworkInformationBackendFactory : public QNetworkInformationBacke public: QAndroidNetworkInformationBackendFactory() = default; ~QAndroidNetworkInformationBackendFactory() = default; - QString name() const override { return backendName; } + QString name() const override { return backendName(); } QNetworkInformation::Features featuresSupported() const override { return QAndroidNetworkInformationBackend::featuresSupportedStatic(); @@ -124,6 +91,9 @@ QAndroidNetworkInformationBackend::QAndroidNetworkInformationBackend() connect(conman, &AndroidConnectivityManager::transportMediumChanged, this, &QAndroidNetworkInformationBackend::updateTransportMedium); + + connect(conman, &AndroidConnectivityManager::meteredChanged, this, + &QAndroidNetworkInformationBackend::setMetered); } void QAndroidNetworkInformationBackend::updateConnectivity( diff --git a/src/plugins/networkinformation/android/wrapper/androidconnectivitymanager.cpp b/src/plugins/networkinformation/android/wrapper/androidconnectivitymanager.cpp index e95a7db610..3c9f952968 100644 --- a/src/plugins/networkinformation/android/wrapper/androidconnectivitymanager.cpp +++ b/src/plugins/networkinformation/android/wrapper/androidconnectivitymanager.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** 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$ -** -****************************************************************************/ +// 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 "androidconnectivitymanager.h" @@ -56,45 +20,43 @@ Q_GLOBAL_STATIC(AndroidConnectivityManagerInstance, androidConnManagerInstance) static const char networkInformationClass[] = "org/qtproject/qt/android/networkinformation/QtAndroidNetworkInformation"; -static void networkConnectivityChanged(JNIEnv *env, jobject obj, jobject enumValue) +static void networkConnectivityChanged(JNIEnv *env, jobject obj, jint enumValue) { Q_UNUSED(env); Q_UNUSED(obj); - const jint value = QJniObject(enumValue).callMethod<jint>("ordinal"); - const auto connectivity = static_cast<AndroidConnectivityManager::AndroidConnectivity>(value); + const auto connectivity = + static_cast<AndroidConnectivityManager::AndroidConnectivity>(enumValue); Q_EMIT androidConnManagerInstance->connManager->connectivityChanged(connectivity); } +Q_DECLARE_JNI_NATIVE_METHOD(networkConnectivityChanged) -static void behindCaptivePortalChanged(JNIEnv *env, jobject obj, jboolean state) +static void genericInfoChanged(JNIEnv *env, jobject obj, jboolean captivePortal, jboolean metered) { Q_UNUSED(env); Q_UNUSED(obj); - Q_EMIT androidConnManagerInstance->connManager->captivePortalChanged(state); + Q_EMIT androidConnManagerInstance->connManager->captivePortalChanged(captivePortal); + Q_EMIT androidConnManagerInstance->connManager->meteredChanged(metered); } +Q_DECLARE_JNI_NATIVE_METHOD(genericInfoChanged) -static void transportMediumChangedCallback(JNIEnv *env, jobject obj, jobject enumValue) +static void transportMediumChanged(JNIEnv *env, jobject obj, jint enumValue) { Q_UNUSED(env); Q_UNUSED(obj); - const jint value = QJniObject(enumValue).callMethod<jint>("ordinal"); - const auto transport = static_cast<AndroidConnectivityManager::AndroidTransport>(value); + const auto transport = static_cast<AndroidConnectivityManager::AndroidTransport>(enumValue); emit androidConnManagerInstance->connManager->transportMediumChanged(transport); } +Q_DECLARE_JNI_NATIVE_METHOD(transportMediumChanged) + +Q_DECLARE_JNI_CLASS(ConnectivityManager, "android/net/ConnectivityManager") 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()); + QAndroidApplication::context()); } AndroidConnectivityManager *AndroidConnectivityManager::getInstance() @@ -106,36 +68,30 @@ AndroidConnectivityManager *AndroidConnectivityManager::getInstance() : nullptr; } +bool AndroidConnectivityManager::isValid() const +{ + return registerNatives(); +} + AndroidConnectivityManager::~AndroidConnectivityManager() { QJniObject::callStaticMethod<void>(networkInformationClass, "unregisterReceiver", - "(Landroid/content/Context;)V", QAndroidApplication::context()); + QAndroidApplication::context()); } -bool AndroidConnectivityManager::registerNatives() +bool AndroidConnectivityManager::registerNatives() const { - QJniEnvironment env; - QJniObject networkReceiver(networkInformationClass); - if (!networkReceiver.isValid()) - return false; - - const QByteArray connectivityEnumSig = - QByteArray("(L") + networkInformationClass + "$AndroidConnectivity;)V"; - const QByteArray transportEnumSig = - QByteArray("(L") + networkInformationClass + "$Transport;)V"; - - jclass clazz = env->GetObjectClass(networkReceiver.object()); - static JNINativeMethod methods[] = { - { "connectivityChanged", connectivityEnumSig.data(), - reinterpret_cast<void *>(networkConnectivityChanged) }, - { "behindCaptivePortalChanged", "(Z)V", - reinterpret_cast<void *>(behindCaptivePortalChanged) }, - { "transportMediumChanged", transportEnumSig.data(), - reinterpret_cast<void *>(transportMediumChangedCallback) }, - }; - const bool ret = (env->RegisterNatives(clazz, methods, std::size(methods)) == JNI_OK); - env->DeleteLocalRef(clazz); - return ret; + static const bool registered = []() { + QJniEnvironment env; + return env.registerNativeMethods(networkInformationClass, { + Q_JNI_NATIVE_METHOD(networkConnectivityChanged), + Q_JNI_NATIVE_METHOD(genericInfoChanged), + Q_JNI_NATIVE_METHOD(transportMediumChanged), + }); + }(); + return registered; } QT_END_NAMESPACE + +#include "moc_androidconnectivitymanager.cpp" diff --git a/src/plugins/networkinformation/android/wrapper/androidconnectivitymanager.h b/src/plugins/networkinformation/android/wrapper/androidconnectivitymanager.h index b335a4a3e4..d15faf0e8e 100644 --- a/src/plugins/networkinformation/android/wrapper/androidconnectivitymanager.h +++ b/src/plugins/networkinformation/android/wrapper/androidconnectivitymanager.h @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** 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$ -** -****************************************************************************/ +// 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 #ifndef ANDROIDCONNECTIVITYMANAGER_H #define ANDROIDCONNECTIVITYMANAGER_H @@ -69,18 +33,18 @@ public: static AndroidConnectivityManager *getInstance(); ~AndroidConnectivityManager(); - inline bool isValid() const { return m_connectivityManager.isValid(); } + inline bool isValid() const; Q_SIGNALS: void connectivityChanged(AndroidConnectivity connectivity); void captivePortalChanged(bool state); void transportMediumChanged(AndroidTransport transport); + void meteredChanged(bool state); private: friend struct AndroidConnectivityManagerInstance; AndroidConnectivityManager(); - bool registerNatives(); - QJniObject m_connectivityManager; + bool registerNatives() const; Q_DISABLE_COPY_MOVE(AndroidConnectivityManager); }; |