From 30c03ac66cf5234a584fdaa26e80f413d8f928d3 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Thu, 18 Jul 2019 20:42:50 -0700 Subject: [PATCH] Fix proxying through Orbot on API 19. https://redmine.stoutner.com/issues/474 --- .../helpers/OrbotProxyHelper.java | 33 ++++++++++--------- app/src/main/res/values-de/strings.xml | 2 +- app/src/main/res/values-es/strings.xml | 2 +- app/src/main/res/values-it/strings.xml | 2 +- app/src/main/res/values-ru/strings.xml | 2 +- app/src/main/res/values-tr/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 7 files changed, 24 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/com/stoutner/privacybrowser/helpers/OrbotProxyHelper.java b/app/src/main/java/com/stoutner/privacybrowser/helpers/OrbotProxyHelper.java index 201cf609..2a55d619 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/helpers/OrbotProxyHelper.java +++ b/app/src/main/java/com/stoutner/privacybrowser/helpers/OrbotProxyHelper.java @@ -27,6 +27,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.net.Proxy; +import android.os.Build; import android.os.Parcelable; import android.preference.PreferenceManager; import android.util.ArrayMap; @@ -72,18 +73,18 @@ public class OrbotProxyHelper { @SuppressLint("PrivateApi") Class loadedApkClass = Class.forName("android.app.LoadedApk"); // Get the declared fields. Suppress the lint warning that `mLoadedApk` cannot be resolved. - @SuppressWarnings("JavaReflectionMemberAccess") Field mLoadedApkField = applicationClass.getDeclaredField("mLoadedApk"); - Field mReceiversField = loadedApkClass.getDeclaredField("mReceivers"); + @SuppressWarnings("JavaReflectionMemberAccess") Field methodLoadedApkField = applicationClass.getDeclaredField("mLoadedApk"); + Field methodReceiversField = loadedApkClass.getDeclaredField("mReceivers"); // Allow the values to be changed. - mLoadedApkField.setAccessible(true); - mReceiversField.setAccessible(true); + methodLoadedApkField.setAccessible(true); + methodReceiversField.setAccessible(true); // Get the APK object. - Object mLoadedApkObject = mLoadedApkField.get(privacyBrowserContext); + Object methodLoadedApkObject = methodLoadedApkField.get(privacyBrowserContext); // Get an array map of the receivers. - ArrayMap receivers = (ArrayMap) mReceiversField.get(mLoadedApkObject); + ArrayMap receivers = (ArrayMap) methodReceiversField.get(methodLoadedApkObject); // Set the proxy. for (Object receiverMap : receivers.values()) { @@ -100,18 +101,20 @@ public class OrbotProxyHelper { // Create a proxy change intent. Intent proxyChangeIntent = new Intent(Proxy.PROXY_CHANGE_ACTION); - // Get a proxy info class. - // `Class`, which is an `unbounded wildcard parameterized type`, must be used instead of `Class`, which is a `raw type`. Otherwise, `proxyInfoClass.getMethod()` is unhappy. - Class proxyInfoClass = Class.forName("android.net.ProxyInfo"); + if (Build.VERSION.SDK_INT >= 21) { + // Get a proxy info class. + // `Class`, which is an `unbounded wildcard parameterized type`, must be used instead of `Class`, which is a `raw type`. Otherwise, `proxyInfoClass.getMethod()` is unhappy. + Class proxyInfoClass = Class.forName("android.net.ProxyInfo"); - // Get the build direct proxy method from the proxy info class. - Method buildDirectProxyMethod = proxyInfoClass.getMethod("buildDirectProxy", String.class, Integer.TYPE); + // Get the build direct proxy method from the proxy info class. + Method buildDirectProxyMethod = proxyInfoClass.getMethod("buildDirectProxy", String.class, Integer.TYPE); - // Populate a proxy info object with the new proxy information. - Object proxyInfoObject = buildDirectProxyMethod.invoke(proxyInfoClass, proxyHost, Integer.valueOf(proxyPort)); + // Populate a proxy info object with the new proxy information. + Object proxyInfoObject = buildDirectProxyMethod.invoke(proxyInfoClass, proxyHost, Integer.valueOf(proxyPort)); - // Add the proxy info object into the proxy change intent. - proxyChangeIntent.putExtra("proxy", (Parcelable) proxyInfoObject); + // Add the proxy info object into the proxy change intent. + proxyChangeIntent.putExtra("proxy", (Parcelable) proxyInfoObject); + } // Pass the proxy change intent to the `onReceive` method of the receiver class. onReceiveMethod.invoke(receiver, privacyBrowserContext, proxyChangeIntent); diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index ebec1e0d..6e37b71d 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -399,7 +399,7 @@ Orbot-Proxy wird nicht funktionieren, solange Orbot nicht installiert ist. - Warte, bis sich Orbot verbindet… + Warte, bis sich Orbot verbindet... Über Privacy Browser diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 28892e85..3cef23e0 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -399,7 +399,7 @@ Enviar a través de Orbot no funcionará a menos que se instale Orbot. - Esperando a Orbot para conectar… + Esperando a Orbot para conectar... Acerca de Navegador Privado diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index f36315db..df1f3a24 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -398,7 +398,7 @@ Il Proxy con Orbot funziona solo se è installato Orbot. - In attesa della connessione di Orbot… + In attesa della connessione di Orbot... Informazioni su Privacy Browser diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 4f42f347..ac67f1f3 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -393,7 +393,7 @@ Проксирование Orbot работает только с установленным Orbot. - Ожидание Orbot для подключения… + Ожидание Orbot для подключения... О Privacy Browser diff --git a/app/src/main/res/values-tr/strings.xml b/app/src/main/res/values-tr/strings.xml index 4b397c5d..724470a6 100644 --- a/app/src/main/res/values-tr/strings.xml +++ b/app/src/main/res/values-tr/strings.xml @@ -393,7 +393,7 @@ Orbot yüklenmeden Orbot vekil sunucusu çalışmayacaktır. - Orbot\'un bağlanması bekleniyor… + Orbot\'un bağlanması bekleniyor... Privacy Browser Hakkında diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 45eb5ca5..901ab161 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -400,7 +400,7 @@ Orbot proxy will not work unless Orbot is installed. - Waiting for Orbot to connect… + Waiting for Orbot to connect... About Privacy Browser -- 2.45.2