[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [orbot/master] enable limit snowflake proxy mode to plugged in and unmetered connections



commit a398b44641b819468256e721351dc45794a77b74
Author: n8fr8 <nathan@xxxxxxxxxxxxxxxxxxxx>
Date:   Mon Jun 28 22:58:11 2021 -0400

    enable limit snowflake proxy mode to plugged in and unmetered connections
---
 app/src/main/AndroidManifest.xml                   |  2 ++
 app/src/main/res/values/strings.xml                |  5 ++++
 app/src/main/res/xml/preferences.xml               | 10 +++++---
 .../torproject/android/service/OrbotService.java   | 27 +++++++++++++++++++++-
 .../org/torproject/android/service/util/Prefs.java |  6 +++++
 5 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 3bbaf610..2a98ad6f 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -16,6 +16,8 @@
     <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
+    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
+
     <uses-permission
         android:name="android.permission.WRITE_EXTERNAL_STORAGE"
         android:maxSdkVersion="18" />
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 70efb465..94cfc800 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -265,4 +265,9 @@
     <string name="bridge_snowflake">Connect through other Tor peers (experimental)</string>
     <string name="be_a_snowflake_title">Run Snowflake Proxy</string>
     <string name="be_a_snowflake_desc">Allow other Tor users to connect to Tor through your device</string>
+
+    <string name="be_a_snowflake_title_limit">Snowflake Proxy Limits</string>
+    <string name="be_a_snowflake_desc_limit">Only when device is plugged in and on wifi</string>
+
+
 </resources>
diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml
index f273f318..ce5b1bbc 100644
--- a/app/src/main/res/xml/preferences.xml
+++ b/app/src/main/res/xml/preferences.xml
@@ -295,9 +295,13 @@
         <CheckBoxPreference
             android:defaultValue="false"
             android:key="pref_be_a_snowflake"
-            android:summary="@string/be_a_snowflake_title"
-            android:title="@string/be_a_snowflake_desc" />
-
+            android:title="@string/be_a_snowflake_title"
+            android:summary="@string/be_a_snowflake_desc" />
+        <CheckBoxPreference
+            android:defaultValue="false"
+            android:key="pref_be_a_snowflake_limit"
+            android:title="@string/be_a_snowflake_title_limit"
+            android:summary="@string/be_a_snowflake_desc_limit" />
     </PreferenceCategory>
 
 </PreferenceScreen>
diff --git a/orbotservice/src/main/java/org/torproject/android/service/OrbotService.java b/orbotservice/src/main/java/org/torproject/android/service/OrbotService.java
index 483e7120..9ebd506f 100644
--- a/orbotservice/src/main/java/org/torproject/android/service/OrbotService.java
+++ b/orbotservice/src/main/java/org/torproject/android/service/OrbotService.java
@@ -26,8 +26,10 @@ import android.content.ServiceConnection;
 import android.content.SharedPreferences;
 import android.content.pm.PackageManager;
 import android.database.Cursor;
+import android.net.ConnectivityManager;
 import android.net.Uri;
 import android.net.VpnService;
+import android.os.BatteryManager;
 import android.os.Build;
 import android.os.Handler;
 import android.os.HandlerThread;
@@ -1554,7 +1556,16 @@ public class OrbotService extends VpnService implements TorServiceConstants, Orb
                             startSnowflakeClient();
                     }
                     else if (Prefs.beSnowflakeProxy())
-                        enableSnowflakeProxy();
+                    {
+
+                        if (Prefs.limitSnowflakeProxying()
+                        && isChargingAndWifi(OrbotService.this))
+                        {
+                            enableSnowflakeProxy();
+                        }
+                        else
+                            enableSnowflakeProxy();
+                    }
 
                     startTor();
                     replyWithStatus(mIntent);
@@ -1608,6 +1619,20 @@ public class OrbotService extends VpnService implements TorServiceConstants, Orb
         }
     }
 
+    public static boolean isChargingAndWifi(Context context) {
+        Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
+        int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
+        boolean isCharging = plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
+
+        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
+
+        boolean isUnmetered = cm.getActiveNetworkInfo() != null
+                && cm.getActiveNetworkInfo().isConnected()
+                && (!cm.isActiveNetworkMetered());
+
+        return isCharging && isUnmetered;
+    }
+
     private class ActionBroadcastReceiver extends BroadcastReceiver {
         public void onReceive(Context context, Intent intent) {
             switch (intent.getAction()) {
diff --git a/orbotservice/src/main/java/org/torproject/android/service/util/Prefs.java b/orbotservice/src/main/java/org/torproject/android/service/util/Prefs.java
index 5c36793d..5fa167c2 100644
--- a/orbotservice/src/main/java/org/torproject/android/service/util/Prefs.java
+++ b/orbotservice/src/main/java/org/torproject/android/service/util/Prefs.java
@@ -21,6 +21,8 @@ public class Prefs {
     private final static String PREF_USE_VPN = "pref_vpn";
     private final static String PREF_EXIT_NODES = "pref_exit_nodes";
     private final static String PREF_BE_A_SNOWFLAKE = "pref_be_a_snowflake";
+    private final static String PREF_BE_A_SNOWFLAKE_LIMIT = "pref_be_a_snowflake_limit";
+
     private final static String PREF_HOST_ONION_SERVICES = "pref_host_onionservices";
 
 
@@ -80,6 +82,10 @@ public class Prefs {
         putBoolean(PREF_BE_A_SNOWFLAKE,beSnowflakeProxy);
     }
 
+    public static boolean limitSnowflakeProxying () {
+        return prefs.getBoolean(PREF_BE_A_SNOWFLAKE_LIMIT,false);
+    }
+
     public static void setDefaultLocale(String value) {
         putString(PREF_DEFAULT_LOCALE, value);
     }



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits