From 72cfcf13c51e2b00171e8393a14fb775de718134 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Mon, 8 Aug 2022 15:57:24 +0200 Subject: [PATCH] NetworkInformation/Android: Delete member only used for validity check And instead base the isValid function on the success of registering callbacks. The registerReceiver call has no way to report failures so I assume it always succeeds. Task-number: QTBUG-104188 Change-Id: I2b002f791d03f2ebc72fe5cfa215f8f8a1dabdf8 Reviewed-by: Ievgenii Meshcheriakov Reviewed-by: Ivan Solovev --- .../android/wrapper/androidconnectivitymanager.cpp | 14 +++++++------- .../android/wrapper/androidconnectivitymanager.h | 5 ++--- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/plugins/networkinformation/android/wrapper/androidconnectivitymanager.cpp b/src/plugins/networkinformation/android/wrapper/androidconnectivitymanager.cpp index dffe0fdc5f..203772971b 100644 --- a/src/plugins/networkinformation/android/wrapper/androidconnectivitymanager.cpp +++ b/src/plugins/networkinformation/android/wrapper/androidconnectivitymanager.cpp @@ -55,11 +55,6 @@ AndroidConnectivityManager::AndroidConnectivityManager() if (!registerNatives()) return; - m_connectivityManager = QJniObject::callStaticObjectMethod( - networkInformationClass, "getConnectivityManager", QAndroidApplication::context()); - if (!m_connectivityManager.isValid()) - return; - QJniObject::callStaticMethod(networkInformationClass, "registerReceiver", QAndroidApplication::context()); } @@ -73,15 +68,20 @@ AndroidConnectivityManager *AndroidConnectivityManager::getInstance() : nullptr; } +bool AndroidConnectivityManager::isValid() const +{ + return registerNatives(); +} + AndroidConnectivityManager::~AndroidConnectivityManager() { QJniObject::callStaticMethod(networkInformationClass, "unregisterReceiver", QAndroidApplication::context()); } -bool AndroidConnectivityManager::registerNatives() +bool AndroidConnectivityManager::registerNatives() const { - static bool registered = []() { + static const bool registered = []() { QJniEnvironment env; return env.registerNativeMethods(networkInformationClass, { Q_JNI_NATIVE_METHOD(networkConnectivityChanged), diff --git a/src/plugins/networkinformation/android/wrapper/androidconnectivitymanager.h b/src/plugins/networkinformation/android/wrapper/androidconnectivitymanager.h index 9b51bbfc19..d15faf0e8e 100644 --- a/src/plugins/networkinformation/android/wrapper/androidconnectivitymanager.h +++ b/src/plugins/networkinformation/android/wrapper/androidconnectivitymanager.h @@ -33,7 +33,7 @@ public: static AndroidConnectivityManager *getInstance(); ~AndroidConnectivityManager(); - inline bool isValid() const { return m_connectivityManager.isValid(); } + inline bool isValid() const; Q_SIGNALS: void connectivityChanged(AndroidConnectivity connectivity); @@ -44,8 +44,7 @@ Q_SIGNALS: private: friend struct AndroidConnectivityManagerInstance; AndroidConnectivityManager(); - bool registerNatives(); - QJniObject m_connectivityManager; + bool registerNatives() const; Q_DISABLE_COPY_MOVE(AndroidConnectivityManager); };