NetworkInformation/Android: Modernize with new JNI functionality

Task-number: QTBUG-104188
Change-Id: Ib8fcf9b914de5deeb0f811be59a0d1b06e413f29
Reviewed-by: Ievgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io>
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
This commit is contained in:
Mårten Nordheim 2022-08-08 15:15:54 +02:00
parent 1a825d8849
commit 431ddc58e7
2 changed files with 27 additions and 32 deletions

View File

@ -15,7 +15,7 @@ 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 networkConnectivityChanged(AndroidConnectivity connectivity);
private static native void genericInfoChanged(boolean captivePortal, boolean metered);
private static native void transportMediumChanged(Transport transportMedium);
@ -96,7 +96,7 @@ public class QtAndroidNetworkInformation {
private void setState(AndroidConnectivity s) {
if (previousState != s) {
previousState = s;
connectivityChanged(s);
networkConnectivityChanged(s);
}
}

View File

@ -20,7 +20,10 @@ Q_GLOBAL_STATIC(AndroidConnectivityManagerInstance, androidConnManagerInstance)
static const char networkInformationClass[] =
"org/qtproject/qt/android/networkinformation/QtAndroidNetworkInformation";
static void networkConnectivityChanged(JNIEnv *env, jobject obj, jobject enumValue)
Q_DECLARE_JNI_TYPE(AndroidConnectivity,
"Lorg/qtproject/qt/android/networkinformation/QtAndroidNetworkInformation$AndroidConnectivity;");
static void networkConnectivityChanged(JNIEnv *env, jobject obj,
QtJniTypes::AndroidConnectivity enumValue)
{
Q_UNUSED(env);
Q_UNUSED(obj);
@ -28,6 +31,7 @@ static void networkConnectivityChanged(JNIEnv *env, jobject obj, jobject enumVal
const auto connectivity = static_cast<AndroidConnectivityManager::AndroidConnectivity>(value);
Q_EMIT androidConnManagerInstance->connManager->connectivityChanged(connectivity);
}
Q_DECLARE_JNI_NATIVE_METHOD(networkConnectivityChanged)
static void genericInfoChanged(JNIEnv *env, jobject obj, jboolean captivePortal, jboolean metered)
{
@ -36,8 +40,11 @@ static void genericInfoChanged(JNIEnv *env, jobject obj, jboolean captivePortal,
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)
Q_DECLARE_JNI_TYPE(Transport,
"Lorg/qtproject/qt/android/networkinformation/QtAndroidNetworkInformation$Transport;");
static void transportMediumChanged(JNIEnv *env, jobject obj, QtJniTypes::Transport enumValue)
{
Q_UNUSED(env);
Q_UNUSED(obj);
@ -45,21 +52,22 @@ static void transportMediumChangedCallback(JNIEnv *env, jobject obj, jobject enu
const auto transport = static_cast<AndroidConnectivityManager::AndroidTransport>(value);
emit androidConnManagerInstance->connManager->transportMediumChanged(transport);
}
Q_DECLARE_JNI_NATIVE_METHOD(transportMediumChanged)
Q_DECLARE_JNI_TYPE(ConnectivityManager, "Landroid/net/ConnectivityManager;")
AndroidConnectivityManager::AndroidConnectivityManager()
{
if (!registerNatives())
return;
m_connectivityManager = QJniObject::callStaticObjectMethod(
networkInformationClass, "getConnectivityManager",
"(Landroid/content/Context;)Landroid/net/ConnectivityManager;",
QAndroidApplication::context());
m_connectivityManager = QJniObject::callStaticObjectMethod<QtJniTypes::ConnectivityManager>(
networkInformationClass, "getConnectivityManager", QAndroidApplication::context());
if (!m_connectivityManager.isValid())
return;
QJniObject::callStaticMethod<void>(networkInformationClass, "registerReceiver",
"(Landroid/content/Context;)V", QAndroidApplication::context());
QAndroidApplication::context());
}
AndroidConnectivityManager *AndroidConnectivityManager::getInstance()
@ -74,33 +82,20 @@ AndroidConnectivityManager *AndroidConnectivityManager::getInstance()
AndroidConnectivityManager::~AndroidConnectivityManager()
{
QJniObject::callStaticMethod<void>(networkInformationClass, "unregisterReceiver",
"(Landroid/content/Context;)V", QAndroidApplication::context());
QAndroidApplication::context());
}
bool AndroidConnectivityManager::registerNatives()
{
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) },
{ "genericInfoChanged", "(ZZ)V",
reinterpret_cast<void *>(genericInfoChanged) },
{ "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 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