From 3d9527a10208111f0f176ff54949f981aca413ac Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Tue, 11 Jun 2024 13:57:37 +0200 Subject: [PATCH] JNI: port QNetworkProxyFactory to QtJniTypes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Simplify the relevant QJniObject calls to not use callObjectMethod, but rely on the return type that we have to pass in anyway. Replace manual loop unwrapping with QtJniArray, and declare the QtNetwork Java class so that we can use it directly. This removes the last usage of Q_DECLARE_JNI_TYPE in qtbase. Change-Id: If872684c8b52bc7789e248cfc3046817c411903c Reviewed-by: MÃ¥rten Nordheim (cherry picked from commit 909493bae27eeb55c985aa9522a57bc4e60d0893) Reviewed-by: Qt Cherry-pick Bot --- src/network/kernel/qnetworkproxy_android.cpp | 34 +++++++------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/src/network/kernel/qnetworkproxy_android.cpp b/src/network/kernel/qnetworkproxy_android.cpp index 3d37266b701..bc1894db5ab 100644 --- a/src/network/kernel/qnetworkproxy_android.cpp +++ b/src/network/kernel/qnetworkproxy_android.cpp @@ -19,26 +19,22 @@ public: }; using namespace QNativeInterface; +using namespace QtJniTypes; Q_GLOBAL_STATIC(ProxyInfoObject, proxyInfoInstance) -static const char networkClass[] = "org/qtproject/qt/android/network/QtNetwork"; - +Q_DECLARE_JNI_CLASS(QtNetwork, "org/qtproject/qt/android/network/QtNetwork") Q_DECLARE_JNI_CLASS(ProxyInfo, "android/net/ProxyInfo") -Q_DECLARE_JNI_TYPE(JStringArray, "[Ljava/lang/String;") +Q_DECLARE_JNI_CLASS(String, "java/lang/String") ProxyInfoObject::ProxyInfoObject() { - QJniObject::callStaticMethod(networkClass, - "registerReceiver", - QAndroidApplication::context()); + QtNetwork::callStaticMethod("registerReceiver", QAndroidApplication::context()); } ProxyInfoObject::~ProxyInfoObject() { - QJniObject::callStaticMethod(networkClass, - "unregisterReceiver", - QAndroidApplication::context()); + QtNetwork::callStaticMethod("unregisterReceiver", QAndroidApplication::context()); } QList QNetworkProxyFactory::systemProxyForQuery(const QNetworkProxyQuery &query) @@ -47,20 +43,14 @@ QList QNetworkProxyFactory::systemProxyForQuery(const QNetworkPro if (!proxyInfoInstance) return proxyList; - QJniObject proxyInfo = QJniObject::callStaticObjectMethod( - networkClass, "getProxyInfo", QAndroidApplication::context()); + QJniObject proxyInfo = QtNetwork::callStaticMethod("getProxyInfo", + QAndroidApplication::context()); if (proxyInfo.isValid()) { - QJniObject exclusionList = - proxyInfo.callObjectMethod("getExclusionList"); + const QJniArray exclusionList = proxyInfo.callMethod("getExclusionList"); bool exclude = false; if (exclusionList.isValid()) { - jobjectArray listObject = exclusionList.object(); - QJniEnvironment env; - QJniObject entry; - const int size = env->GetArrayLength(listObject); - QUrl host = QUrl(query.url().host()); - for (int i = 0; i < size; ++i) { - entry = env->GetObjectArrayElement(listObject, i); + const QUrl host = QUrl(query.url().host()); + for (const auto &entry : exclusionList) { if (host.matches(QUrl(entry.toString()), QUrl::RemoveScheme)) { exclude = true; break; @@ -68,9 +58,9 @@ QList QNetworkProxyFactory::systemProxyForQuery(const QNetworkPro } } if (!exclude) { - QJniObject hostName = proxyInfo.callObjectMethod("getHost"); + const QString hostName = proxyInfo.callMethod("getHost"); const int port = proxyInfo.callMethod("getPort"); - QNetworkProxy proxy(QNetworkProxy::HttpProxy, hostName.toString(), port); + QNetworkProxy proxy(QNetworkProxy::HttpProxy, hostName, port); proxyList << proxy; } }