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:
parent
1a825d8849
commit
431ddc58e7
@ -15,7 +15,7 @@ import android.os.Build;
|
|||||||
public class QtAndroidNetworkInformation {
|
public class QtAndroidNetworkInformation {
|
||||||
private static final String LOG_TAG = "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 genericInfoChanged(boolean captivePortal, boolean metered);
|
||||||
private static native void transportMediumChanged(Transport transportMedium);
|
private static native void transportMediumChanged(Transport transportMedium);
|
||||||
|
|
||||||
@ -96,7 +96,7 @@ public class QtAndroidNetworkInformation {
|
|||||||
private void setState(AndroidConnectivity s) {
|
private void setState(AndroidConnectivity s) {
|
||||||
if (previousState != s) {
|
if (previousState != s) {
|
||||||
previousState = s;
|
previousState = s;
|
||||||
connectivityChanged(s);
|
networkConnectivityChanged(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,7 +20,10 @@ Q_GLOBAL_STATIC(AndroidConnectivityManagerInstance, androidConnManagerInstance)
|
|||||||
static const char networkInformationClass[] =
|
static const char networkInformationClass[] =
|
||||||
"org/qtproject/qt/android/networkinformation/QtAndroidNetworkInformation";
|
"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(env);
|
||||||
Q_UNUSED(obj);
|
Q_UNUSED(obj);
|
||||||
@ -28,6 +31,7 @@ static void networkConnectivityChanged(JNIEnv *env, jobject obj, jobject enumVal
|
|||||||
const auto connectivity = static_cast<AndroidConnectivityManager::AndroidConnectivity>(value);
|
const auto connectivity = static_cast<AndroidConnectivityManager::AndroidConnectivity>(value);
|
||||||
Q_EMIT androidConnManagerInstance->connManager->connectivityChanged(connectivity);
|
Q_EMIT androidConnManagerInstance->connManager->connectivityChanged(connectivity);
|
||||||
}
|
}
|
||||||
|
Q_DECLARE_JNI_NATIVE_METHOD(networkConnectivityChanged)
|
||||||
|
|
||||||
static void genericInfoChanged(JNIEnv *env, jobject obj, jboolean captivePortal, jboolean metered)
|
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->captivePortalChanged(captivePortal);
|
||||||
Q_EMIT androidConnManagerInstance->connManager->meteredChanged(metered);
|
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(env);
|
||||||
Q_UNUSED(obj);
|
Q_UNUSED(obj);
|
||||||
@ -45,21 +52,22 @@ static void transportMediumChangedCallback(JNIEnv *env, jobject obj, jobject enu
|
|||||||
const auto transport = static_cast<AndroidConnectivityManager::AndroidTransport>(value);
|
const auto transport = static_cast<AndroidConnectivityManager::AndroidTransport>(value);
|
||||||
emit androidConnManagerInstance->connManager->transportMediumChanged(transport);
|
emit androidConnManagerInstance->connManager->transportMediumChanged(transport);
|
||||||
}
|
}
|
||||||
|
Q_DECLARE_JNI_NATIVE_METHOD(transportMediumChanged)
|
||||||
|
|
||||||
|
Q_DECLARE_JNI_TYPE(ConnectivityManager, "Landroid/net/ConnectivityManager;")
|
||||||
|
|
||||||
AndroidConnectivityManager::AndroidConnectivityManager()
|
AndroidConnectivityManager::AndroidConnectivityManager()
|
||||||
{
|
{
|
||||||
if (!registerNatives())
|
if (!registerNatives())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
m_connectivityManager = QJniObject::callStaticObjectMethod(
|
m_connectivityManager = QJniObject::callStaticObjectMethod<QtJniTypes::ConnectivityManager>(
|
||||||
networkInformationClass, "getConnectivityManager",
|
networkInformationClass, "getConnectivityManager", QAndroidApplication::context());
|
||||||
"(Landroid/content/Context;)Landroid/net/ConnectivityManager;",
|
|
||||||
QAndroidApplication::context());
|
|
||||||
if (!m_connectivityManager.isValid())
|
if (!m_connectivityManager.isValid())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
QJniObject::callStaticMethod<void>(networkInformationClass, "registerReceiver",
|
QJniObject::callStaticMethod<void>(networkInformationClass, "registerReceiver",
|
||||||
"(Landroid/content/Context;)V", QAndroidApplication::context());
|
QAndroidApplication::context());
|
||||||
}
|
}
|
||||||
|
|
||||||
AndroidConnectivityManager *AndroidConnectivityManager::getInstance()
|
AndroidConnectivityManager *AndroidConnectivityManager::getInstance()
|
||||||
@ -74,33 +82,20 @@ AndroidConnectivityManager *AndroidConnectivityManager::getInstance()
|
|||||||
AndroidConnectivityManager::~AndroidConnectivityManager()
|
AndroidConnectivityManager::~AndroidConnectivityManager()
|
||||||
{
|
{
|
||||||
QJniObject::callStaticMethod<void>(networkInformationClass, "unregisterReceiver",
|
QJniObject::callStaticMethod<void>(networkInformationClass, "unregisterReceiver",
|
||||||
"(Landroid/content/Context;)V", QAndroidApplication::context());
|
QAndroidApplication::context());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AndroidConnectivityManager::registerNatives()
|
bool AndroidConnectivityManager::registerNatives()
|
||||||
{
|
{
|
||||||
QJniEnvironment env;
|
static bool registered = []() {
|
||||||
QJniObject networkReceiver(networkInformationClass);
|
QJniEnvironment env;
|
||||||
if (!networkReceiver.isValid())
|
return env.registerNativeMethods(networkInformationClass, {
|
||||||
return false;
|
Q_JNI_NATIVE_METHOD(networkConnectivityChanged),
|
||||||
|
Q_JNI_NATIVE_METHOD(genericInfoChanged),
|
||||||
const QByteArray connectivityEnumSig =
|
Q_JNI_NATIVE_METHOD(transportMediumChanged),
|
||||||
QByteArray("(L") + networkInformationClass + "$AndroidConnectivity;)V";
|
});
|
||||||
const QByteArray transportEnumSig =
|
}();
|
||||||
QByteArray("(L") + networkInformationClass + "$Transport;)V";
|
return registered;
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
|
Loading…
Reference in New Issue
Block a user