[tor-commits] [orbot/master] make prefs called directly when needed instead of requiring refresh

n8fr8 at torproject.org n8fr8 at torproject.org
Thu Jun 25 14:59:58 UTC 2015


commit a3d37e8b2a3d7da43188ca54bd471d209f38bc02
Author: Hans-Christoph Steiner <hans at eds.org>
Date:   Mon Jun 8 22:57:42 2015 -0400

    make prefs called directly when needed instead of requiring refresh
    
    The code was using global variables that were refreshed from the prefs on
    certain occasions.  That means that the global vars could easily get out of
    sync with the actual values.  Instead, just read the prefs directly when
    the values are needed, and they will always be up-to-date.
---
 src/org/torproject/android/OrbotApp.java           |   16 +--
 src/org/torproject/android/OrbotConstants.java     |   13 --
 src/org/torproject/android/OrbotMainActivity.java  |   62 +++------
 src/org/torproject/android/Prefs.java              |  116 ++++++++++++++++
 .../torproject/android/service/OnBootReceiver.java |   15 +--
 src/org/torproject/android/service/TorService.java |  141 ++++++--------------
 .../android/service/TorServiceConstants.java       |    2 +-
 .../torproject/android/service/TorTransProxy.java  |   16 +--
 8 files changed, 191 insertions(+), 190 deletions(-)

diff --git a/src/org/torproject/android/OrbotApp.java b/src/org/torproject/android/OrbotApp.java
index 070d7d7..c6f4797 100644
--- a/src/org/torproject/android/OrbotApp.java
+++ b/src/org/torproject/android/OrbotApp.java
@@ -5,8 +5,6 @@ import android.annotation.TargetApi;
 import android.app.Activity;
 import android.app.Application;
 import android.content.Intent;
-import android.content.SharedPreferences;
-import android.content.SharedPreferences.Editor;
 import android.content.res.Configuration;
 import android.os.Build;
 import android.text.TextUtils;
@@ -14,29 +12,26 @@ import android.util.Log;
 
 import info.guardianproject.util.Languages;
 
-import org.torproject.android.service.TorServiceUtils;
-
 import java.util.Locale;
 
 public class OrbotApp extends Application implements OrbotConstants
 {
 
     private Locale locale;
-    private SharedPreferences prefs;
 
     @Override
     public void onCreate() {
         super.onCreate();
+        Prefs.setContext(this);
 
-        prefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
-        setNewLocale(prefs.getString(PREF_DEFAULT_LOCALE, Locale.getDefault().getLanguage()));
+        setNewLocale(Prefs.getDefaultLocale());
     }
 
     @Override
     public void onConfigurationChanged(Configuration newConfig) {
         super.onConfigurationChanged(newConfig);
         Log.i(TAG, "onConfigurationChanged " + newConfig.locale.getLanguage());
-        setNewLocale(prefs.getString(PREF_DEFAULT_LOCALE, Locale.getDefault().getLanguage()));
+        setNewLocale(Prefs.getDefaultLocale());
     }
 
     @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@@ -65,10 +60,7 @@ public class OrbotApp extends Application implements OrbotConstants
          * wrong. If setting the locale causes an Exception, it should be set in
          * the preferences, otherwise ChatSecure will be stuck in a crash loop.
          */
-        SharedPreferences prefs = TorServiceUtils.getSharedPrefs(this);
-        Editor prefEdit = prefs.edit();
-        prefEdit.putString(PREF_DEFAULT_LOCALE, language);
-        prefEdit.apply();
+        Prefs.setDefaultLocale(language);
         Log.i(TAG, "setNewLocale complete: locale: " + locale.getLanguage()
                 + " Locale.getDefault: " + Locale.getDefault().getLanguage());
     }
diff --git a/src/org/torproject/android/OrbotConstants.java b/src/org/torproject/android/OrbotConstants.java
index f1521e8..373ff3f 100644
--- a/src/org/torproject/android/OrbotConstants.java
+++ b/src/org/torproject/android/OrbotConstants.java
@@ -6,7 +6,6 @@ package org.torproject.android;
 public interface OrbotConstants {
 
 	public final static String TAG = "Orbot";
-	
 
 	public final static String PREFS_KEY = "OrbotPrefs";
 	public final static String PREFS_KEY_TORIFIED = "PrefTord";
@@ -22,27 +21,15 @@ public interface OrbotConstants {
     	
     public final static String HANDLER_TOR_MSG = "torServiceMsg";
 	
-	public final static String PREF_BRIDGES_ENABLED = "pref_bridges_enabled";
 	public final static String PREF_BRIDGES_UPDATED = "pref_bridges_enabled";
-	public final static String PREF_BRIDGES_LIST = "pref_bridges_list";
 	//public final static String PREF_BRIDGES_OBFUSCATED = "pref_bridges_obfuscated";
     public final static String PREF_OR = "pref_or";
     public final static String PREF_OR_PORT = "pref_or_port";
     public final static String PREF_OR_NICKNAME = "pref_or_nickname";
     public final static String PREF_REACHABLE_ADDRESSES = "pref_reachable_addresses";
     public final static String PREF_REACHABLE_ADDRESSES_PORTS = "pref_reachable_addresses_ports";
-	public final static String PREF_TRANSPARENT = "pref_transparent";
-	public final static String PREF_TRANSPARENT_ALL = "pref_transparent_all";
-	
-	public final static String PREF_HAS_ROOT = "has_root";
 	public final static  int RESULT_CLOSE_ALL = 0;
 	
-	public final static String PREF_USE_SYSTEM_IPTABLES = "pref_use_sys_iptables";
-	
-	public final static String PREF_PERSIST_NOTIFICATIONS = "pref_persistent_notifications";
-	
-	public final static String PREF_DEFAULT_LOCALE = "pref_default_locale";
-	
 	public final static String PREF_DISABLE_NETWORK = "pref_disable_network";
 	
 	public final static String PREF_TOR_SHARED_PREFS = "org.torproject.android_preferences";
diff --git a/src/org/torproject/android/OrbotMainActivity.java b/src/org/torproject/android/OrbotMainActivity.java
index 2f1e67a..b68c9aa 100644
--- a/src/org/torproject/android/OrbotMainActivity.java
+++ b/src/org/torproject/android/OrbotMainActivity.java
@@ -92,14 +92,13 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
 
     private boolean autoStartFromIntent = false;
     
-    private final static long INIT_DELAY = 100;
     private final static int REQUEST_VPN = 8888;
     private final static int REQUEST_SETTINGS = 0x9874;
     
     /** Called when the activity is first created. */
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        
+
         mPrefs = TorServiceUtils.getSharedPrefs(getApplicationContext());        
         mPrefs.registerOnSharedPreferenceChangeListener(this);
         
@@ -238,7 +237,7 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
 		
 		mBtnVPN = (ToggleButton)findViewById(R.id.btnVPN);
 		
-		boolean useVPN = mPrefs.getBoolean("pref_vpn", false);
+		boolean useVPN = Prefs.useVpn();
 		mBtnVPN.setChecked(useVPN);
 		
 		if (useVPN)
@@ -262,12 +261,8 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
 		});
 		
 		
-		mBtnBridges =  (ToggleButton)findViewById(R.id.btnBridges);
-		boolean useBridges = mPrefs.getBoolean("pref_bridges_enabled", false);
-		mBtnBridges.setChecked(useBridges);
-		
-		
-		
+		mBtnBridges = (ToggleButton)findViewById(R.id.btnBridges);
+		mBtnBridges.setChecked(Prefs.bridgesEnabled());
 		mBtnBridges.setOnClickListener(new View.OnClickListener ()
 		{
 
@@ -407,7 +402,7 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
                 else if (item.getItemId() == R.id.menu_share_bridge)
                 {
                 	
-            		String bridges = mPrefs.getString(OrbotConstants.PREF_BRIDGES_LIST, null);
+            		String bridges = Prefs.getBridgesList();
                 	
             		if (bridges != null && bridges.length() > 0)
             		{
@@ -655,12 +650,8 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
 
 		showAlert(getString(R.string.bridges_updated),getString(R.string.restart_orbot_to_use_this_bridge_) + newBridgeValue,false);	
 		
-		Editor pEdit = mPrefs.edit();
-		
-		pEdit.putString(OrbotConstants.PREF_BRIDGES_LIST,newBridgeValue); //set the string to a preference
-		pEdit.putBoolean(OrbotConstants.PREF_BRIDGES_ENABLED,true);
-	
-		pEdit.commit();
+		Prefs.setBridgesList(newBridgeValue); //set the string to a preference
+		Prefs.putBridgesEnabled(true);
 		
 		setResult(RESULT_OK);
 		
@@ -675,7 +666,6 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
 	private void openBrowser(final String browserLaunchUrl,boolean forceExternal)
 	{
 		boolean isOrwebInstalled = appInstalledOrNot("info.guardianproject.browser");
-		boolean isTransProxy =  mPrefs.getBoolean("pref_transparent", false);
 		
 		if (mBtnVPN.isChecked()||forceExternal)
 		{
@@ -684,7 +674,7 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
 			intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
 			startActivity(intent);
 		}
-		else if (isTransProxy)
+		else if (Prefs.useTransparentProxying())
 		{
 			Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(browserLaunchUrl));
 			intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
@@ -793,7 +783,7 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
             }
             else if (torStatus == TorServiceConstants.STATUS_ON)
             {
-                updateSettings();
+                updateTransProxy();
                 Toast.makeText(this, R.string.you_may_need_to_stop_and_start_orbot_for_settings_change_to_be_enabled_, Toast.LENGTH_SHORT).show();
 
             }
@@ -867,17 +857,17 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
             		   
             		   break;
             	   case 3: //azure
-            		   mPrefs.edit().putString(OrbotConstants.PREF_BRIDGES_LIST,"2").commit();
+            		   Prefs.setBridgesList("2");
             		   enableBridges(true);
             		   
             		   break;
             	   case 4: //amazon
-            		   mPrefs.edit().putString(OrbotConstants.PREF_BRIDGES_LIST,"1").commit();
+                       Prefs.setBridgesList("1");
             		   enableBridges(true);
             		   
             		   break;
             	   case 5: //google
-            		   mPrefs.edit().putString(OrbotConstants.PREF_BRIDGES_LIST,"0").commit();
+                       Prefs.setBridgesList("0");
             		   enableBridges(true);
             		   
             		   break;
@@ -974,24 +964,17 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
     
     private void enableBridges (boolean enable)
     {
+		Prefs.putBridgesEnabled(enable);
 
-		Editor edit = mPrefs.edit();
-		edit.putBoolean(OrbotConstants.PREF_BRIDGES_ENABLED, enable);
-		edit.commit();
-		
-		updateSettings();
-		
 		if (torStatus == TorServiceConstants.STATUS_ON)
 		{
-			String bridgeList = mPrefs.getString(OrbotConstants.PREF_BRIDGES_LIST,null);
+			String bridgeList = Prefs.getBridgesList();
 			if (bridgeList != null && bridgeList.length() > 0)
 			{
 				restartTor ();
 			}
 			
 		}
-				
-		
     }
     
     private void restartTor ()
@@ -1038,11 +1021,8 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
 
 			@Override
 			public void onClick(DialogInterface dialog, int which) {
-				
-				mPrefs.edit().putBoolean("pref_vpn", true).commit();
-		        
+		        Prefs.putUseVpn(true);
 				startVpnService();
-				
 			}
 
         	 
@@ -1090,10 +1070,9 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
         return true;
     }
     
-    private boolean updateSettings ()
+    private boolean updateTransProxy ()
     {
-        //todo send service command
-        startService(TorServiceConstants.CMD_UPDATE);
+        startService(TorServiceConstants.CMD_UPDATE_TRANS_PROXY);
         return true;
     }
 
@@ -1103,11 +1082,8 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
 
         if (mPrefs != null)
         {
-	        boolean useVPN = mPrefs.getBoolean("pref_vpn", false);
-			mBtnVPN.setChecked(useVPN);
-			
-			boolean useBridges = mPrefs.getBoolean("pref_bridges_enabled", false);
-			mBtnBridges.setChecked(useBridges);
+			mBtnVPN.setChecked(Prefs.useVpn());			
+			mBtnBridges.setChecked(Prefs.bridgesEnabled());
         }
 
         mHandler.postDelayed(new Runnable ()
diff --git a/src/org/torproject/android/Prefs.java b/src/org/torproject/android/Prefs.java
new file mode 100644
index 0000000..b1b1793
--- /dev/null
+++ b/src/org/torproject/android/Prefs.java
@@ -0,0 +1,116 @@
+
+package org.torproject.android;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+import org.torproject.android.service.TorServiceUtils;
+
+import java.util.Locale;
+
+public class Prefs {
+    private final static String PREF_BRIDGES_ENABLED = "pref_bridges_enabled";
+    private final static String PREF_BRIDGES_LIST = "pref_bridges_list";
+    private final static String PREF_DEFAULT_LOCALE = "pref_default_locale";
+    private final static String PREF_ENABLE_LOGGING = "pref_enable_logging";
+    private final static String PREF_EXPANDED_NOTIFICATIONS = "pref_expanded_notifications";
+    private final static String PREF_HAS_ROOT = "has_root";
+    private final static String PREF_PERSIST_NOTIFICATIONS = "pref_persistent_notifications";
+    private final static String PREF_START_ON_BOOT = "pref_start_boot";
+    private final static String PREF_TRANSPARENT = "pref_transparent";
+    private final static String PREF_TRANSPARENT_ALL = "pref_transparent_all";
+    private final static String PREF_TRANSPARENT_TETHERING = "pref_transparent_tethering";
+    private final static String PREF_TRANSPROXY_REFRESH = "pref_transproxy_refresh";
+    private final static String PREF_USE_SYSTEM_IPTABLES = "pref_use_sys_iptables";
+    private final static String PREF_USE_VPN = "pref_vpn";
+
+    private static SharedPreferences prefs;
+
+    public static void setContext(Context context) {
+        if (prefs == null)
+            prefs = TorServiceUtils.getSharedPrefs(context);
+    }
+
+    private static void putBoolean(String key, boolean value) {
+        prefs.edit().putBoolean(key, value).apply();
+    }
+
+    private static void putString(String key, String value) {
+        prefs.edit().putString(key, value).apply();
+    }
+
+    public static boolean bridgesEnabled() {
+        return prefs.getBoolean(PREF_BRIDGES_ENABLED, false);
+    }
+
+    public static void putBridgesEnabled(boolean value) {
+        putBoolean(PREF_BRIDGES_ENABLED, value);
+    }
+
+    public static String getBridgesList() {
+        return prefs.getString(PREF_BRIDGES_LIST, "");
+    }
+
+    public static void setBridgesList(String value) {
+        putString(PREF_BRIDGES_LIST, value);
+    }
+
+    public static String getDefaultLocale() {
+        return prefs.getString(PREF_DEFAULT_LOCALE, Locale.getDefault().getLanguage());
+    }
+
+    public static void setDefaultLocale(String value) {
+        putString(PREF_DEFAULT_LOCALE, value);
+    }
+
+    public static boolean useSystemIpTables() {
+        return prefs.getBoolean(PREF_USE_SYSTEM_IPTABLES, false);
+    }
+
+    public static boolean useRoot() {
+        return prefs.getBoolean(PREF_HAS_ROOT, false);
+    }
+
+    public static boolean useTransparentProxying() {
+        return prefs.getBoolean(PREF_TRANSPARENT, false);
+    }
+
+    public static boolean transparentProxyAll() {
+        return prefs.getBoolean(PREF_TRANSPARENT_ALL, false);
+    }
+
+    public static boolean transparentTethering() {
+        return prefs.getBoolean(PREF_TRANSPARENT_TETHERING, false);
+    }
+
+    public static boolean transProxyNetworkRefresh() {
+        return prefs.getBoolean(PREF_TRANSPROXY_REFRESH, false);
+    }
+
+    public static boolean expandedNotifications() {
+        return prefs.getBoolean(PREF_EXPANDED_NOTIFICATIONS, false);
+    }
+
+    public static boolean useDebugLogging() {
+        return prefs.getBoolean(PREF_ENABLE_LOGGING, false);
+    }
+
+    public static boolean persistNotifications() {
+        return prefs.getBoolean(PREF_PERSIST_NOTIFICATIONS, true);
+    }
+
+    public static boolean useVpn() {
+        return prefs.getBoolean(PREF_USE_VPN, false);
+    }
+
+    public static void putUseVpn(boolean value) {
+        putBoolean(PREF_USE_VPN, value);
+    }
+
+    public static boolean startOnBoot() {
+        return prefs.getBoolean(PREF_START_ON_BOOT, true);
+    }
+
+    public static void putStartOnBoot(boolean value) {
+        putBoolean(PREF_START_ON_BOOT, value);
+    }
+}
diff --git a/src/org/torproject/android/service/OnBootReceiver.java b/src/org/torproject/android/service/OnBootReceiver.java
index bc667b2..49c4ab8 100644
--- a/src/org/torproject/android/service/OnBootReceiver.java
+++ b/src/org/torproject/android/service/OnBootReceiver.java
@@ -5,27 +5,22 @@ import android.annotation.SuppressLint;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
-import android.content.SharedPreferences;
 import android.net.VpnService;
 
+import org.torproject.android.Prefs;
+
 public class OnBootReceiver extends BroadcastReceiver {
 	
 	@Override
 	public void onReceive(Context context, Intent intent) {
-		
-		SharedPreferences prefs = TorServiceUtils.getSharedPrefs(context.getApplicationContext());
-		
-		boolean startOnBoot = prefs.getBoolean("pref_start_boot",true);
-		boolean useVPN = prefs.getBoolean("pref_vpn",true);
-		
-		if (startOnBoot)
+	    Prefs.setContext(context);
+		if (Prefs.startOnBoot())
 		{
 			startService(TorServiceConstants.CMD_START,context);		
 			
-			if (useVPN)
+			if (Prefs.useVpn())
 				startVpnService(context);
 		}
-
 	}
 	
 	 @SuppressLint("NewApi")
diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java
index 38b4bd3..9baf723 100644
--- a/src/org/torproject/android/service/TorService.java
+++ b/src/org/torproject/android/service/TorService.java
@@ -8,6 +8,7 @@
 package org.torproject.android.service;
 
 
+import org.torproject.android.Prefs;
 import java.io.BufferedReader;
 import java.io.ByteArrayOutputStream;
 import java.io.DataInputStream;
@@ -82,8 +83,6 @@ import android.widget.RemoteViews;
 public class TorService extends Service implements TorServiceConstants, OrbotConstants, EventHandler
 {
     
-    public static boolean ENABLE_DEBUG_LOG = true;
-    
     private String mCurrentStatus = STATUS_OFF;
     
     private final static int CONTROL_SOCKET_TIMEOUT = 0;
@@ -101,8 +100,6 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
     private static final int ERROR_NOTIFY_ID = 3;
     private static final int HS_NOTIFY_ID = 4;
     
-    private boolean prefPersistNotifications = true;
-    
     private static final int MAX_START_TRIES = 3;
 
     private ArrayList<String> configBuffer = null;
@@ -134,16 +131,8 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
     private NotificationManager mNotificationManager = null;
     private Builder mNotifyBuilder;
     private Notification mNotification;
-    private boolean mShowExpandedNotifications = false;
     private boolean mNotificationShowing = false;
-    
-    private boolean mHasRoot = false;
-    private boolean mEnableTransparentProxy = false;
-    private boolean mTransProxyAll = false;
-    private boolean mTransProxyTethering = false;
-    private boolean mTransProxyNetworkRefresh = false;
-    
-    private boolean mUseVPN = false;
+
 	boolean mIsLollipop = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
 
     private ExecutorService mExecutor = Executors.newFixedThreadPool(1);
@@ -152,7 +141,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
 
     public void debug(String msg)
     {
-        if (ENABLE_DEBUG_LOG)  
+        if (Prefs.useDebugLogging())
         {
             Log.d(TAG,msg);
             sendCallbackLogMessage(msg);    
@@ -162,7 +151,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
     
     public void logException(String msg, Exception e)
     {
-        if (ENABLE_DEBUG_LOG)
+        if (Prefs.useDebugLogging())
         {
             Log.e(TAG,msg,e);
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -268,11 +257,11 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
             mNotifyBuilder.setTicker(null);
         }
         
-        mNotifyBuilder.setOngoing(prefPersistNotifications);
+        mNotifyBuilder.setOngoing(Prefs.persistNotifications());
         
         mNotification = mNotifyBuilder.build();
         
-        if (Build.VERSION.SDK_INT >= 16 && mShowExpandedNotifications) {
+        if (Build.VERSION.SDK_INT >= 16 && Prefs.expandedNotifications()) {
             // Create remote view that needs to be set as bigContentView for the notification.
              RemoteViews expandedView = new RemoteViews(this.getPackageName(), 
                      R.layout.layout_notification_expanded);
@@ -317,7 +306,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
             mNotification.bigContentView = expandedView;
         }
         
-        if (prefPersistNotifications && (!mNotificationShowing))
+        if (Prefs.persistNotifications() && (!mNotificationShowing))
         {
             startForeground(NOTIFY_ID, mNotification);
             logNotice("Set background service to FOREGROUND");
@@ -364,8 +353,8 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
                     newIdentity();
                 } else if (action.equals(CMD_FLUSH)) {
                     flushTransparentProxyRules();
-                } else if (action.equals(CMD_UPDATE)) {
-                    processSettings();
+                } else if (action.equals(CMD_UPDATE_TRANS_PROXY)) {
+                    processTransparentProxying();
                 } else if (action.equals(CMD_VPN)) {
                     enableVpnProxy();
                 } else if (action.equals(CMD_VPN_CLEAR)) {
@@ -394,6 +383,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
     @Override
     public void onDestroy ()
     {
+        Log.i("TorService", "onDestroy");
         String msg = ("TorService is being DESTROYED... shutting down!");
         Log.d(TAG, msg);
         sendCallbackLogMessage(msg);
@@ -417,7 +407,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
             mCurrentStatus = STATUS_OFF;
             sendCallbackStatus(mCurrentStatus);
 
-            if (mHasRoot && mEnableTransparentProxy)
+            if (Prefs.useRoot() && Prefs.useTransparentProxying())
             {
             	Shell shellRoot = Shell.startRootShell();
                 disableTransparentProxy(shellRoot);
@@ -564,7 +554,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
              * uninstall/reinstall with different UID.
              */
             Shell shell;
-            if (mHasRoot && killAttempts > 2) {
+            if (Prefs.useRoot() && killAttempts > 2) {
                 shell = Shell.startRootShell();
                 Log.i(TAG, "using a root shell");
             } else {
@@ -589,7 +579,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
     {
         if (msg != null && msg.trim().length() > 0)
         {
-            if (ENABLE_DEBUG_LOG)            
+            if (Prefs.useDebugLogging())
                 Log.d(TAG, msg);
         
             sendCallbackLogMessage(msg);
@@ -599,6 +589,8 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
     @Override
     public void onCreate() {
         super.onCreate();
+        Log.i("TorService", "onCreate");
+
         try
         {
             mNumberFormat = NumberFormat.getInstance(Locale.getDefault()); //localized numbers!
@@ -614,7 +606,6 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
             }
             
             initBinariesAndDirectories();
-            updateSettings();
         
             new Thread(new Runnable ()
             {
@@ -703,7 +694,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
         fileControlPort = new File(appBinHome,"control.txt");
         extraLines.append(TORRC_CONTROLPORT_FILE_KEY).append(' ').append(fileControlPort.getCanonicalPath()).append('\n');
 
-         if (mTransProxyTethering)
+         if (Prefs.transparentTethering())
          {
              extraLines.append("TransListenAddress 0.0.0.0").append('\n');
              extraLines.append("DNSListenAddress 0.0.0.0").append('\n');
@@ -768,29 +759,6 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
         return fileBin.canExecute();
     }
     
-    
-    private void updateSettings () throws TimeoutException, IOException
-    {
-        
-        SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
-
-        mHasRoot = prefs.getBoolean(PREF_HAS_ROOT,false);
-         
-        mEnableTransparentProxy = prefs.getBoolean("pref_transparent", false);
-         mTransProxyAll = prefs.getBoolean("pref_transparent_all", false);
-         mTransProxyTethering = prefs.getBoolean("pref_transparent_tethering", false);
-         mTransProxyNetworkRefresh = prefs.getBoolean("pref_transproxy_refresh", false);
-         
-         mShowExpandedNotifications  = prefs.getBoolean("pref_expanded_notifications", false);
-         
-        ENABLE_DEBUG_LOG = prefs.getBoolean("pref_enable_logging",false);
-        Log.i(TAG,"debug logging:" + ENABLE_DEBUG_LOG);
-
-        prefPersistNotifications = prefs.getBoolean(OrbotConstants.PREF_PERSIST_NOTIFICATIONS, true);
-        
-        mUseVPN = prefs.getBoolean("pref_vpn", false);
-    }
-    
     private void startTor () throws Exception
     {
         
@@ -805,19 +773,14 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
         enableBinExec(fileObfsclient);
         enableBinExec(fileMeekclient);
         enableBinExec(fileXtables);
-        
-        updateSettings ();
 
         logNotice(getString(R.string.status_starting_up));
         sendCallbackLogMessage(getString(R.string.status_starting_up));
         
         ArrayList<String> customEnv = new ArrayList<String>();
-     
-        SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
-        boolean useBridges = prefs.getBoolean(OrbotConstants.PREF_BRIDGES_ENABLED, false);
 
-        if (useBridges)
-        	if (mUseVPN && !mIsLollipop)
+        if (Prefs.bridgesEnabled())
+        	if (Prefs.useVpn() && !mIsLollipop)
         		customEnv.add("TOR_PT_PROXY=socks5://127.0.0.1:" + OrbotVpnService.mSocksProxyPort); 
         
         String baseDirectory = fileTor.getParent();
@@ -830,7 +793,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
             if (mPortHTTP != -1)
                 runPolipoShellCmd(shellUser);
             
-            if (mHasRoot && mEnableTransparentProxy)
+            if (Prefs.useRoot() && Prefs.useTransparentProxying())
             {
                  Shell shellRoot = Shell.startRootShell();
 
@@ -840,7 +803,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
                 shellRoot.close();
             }
             
-            if (mUseVPN) //we need to turn on VPN here so the proxy is running
+            if (Prefs.useVpn()) //we need to turn on VPN here so the proxy is running
             {
             	enableVpnProxy();
             }
@@ -858,7 +821,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
     }
 
     private boolean flushTransparentProxyRules () {
-        if (mHasRoot)
+        if (Prefs.useRoot())
         {
              if (mTransProxy == null)
                  mTransProxy = new TorTransProxy(this, fileXtables);
@@ -909,7 +872,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
         
         int code = 0; // Default state is "okay"
         
-        if(mTransProxyAll)
+        if(Prefs.transparentProxyAll())
         {
 
             code = mTransProxy.setTransparentProxyingAll(this, true, shell);
@@ -926,7 +889,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
         if (code == 0)
         {
 
-            if (mTransProxyTethering)
+            if (Prefs.transparentTethering())
             {
                 showToolbarNotification(getString(R.string.transproxy_enabled_for_tethering_), TRANSPROXY_NOTIFY_ID, R.drawable.ic_stat_tor);
 
@@ -1404,17 +1367,10 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
         }
         
         public void enableVpnProxy () {
-            
         	debug ("enabling VPN Proxy");
-        	
-        	mUseVPN = true;
-            
-            SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
-            Editor ePrefs = prefs.edit();          
-            ePrefs.putBoolean("pref_vpn", true);            
-            ePrefs.commit();
             
-            processSettings();
+            Prefs.putUseVpn(true);
+            processTransparentProxying();
             
             Intent intent = new Intent(TorService.this, OrbotVpnService.class);
             intent.setAction("start");
@@ -1458,16 +1414,8 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
         public void clearVpnProxy ()
         {   
         	debug ("clearing VPN Proxy");
-        	
-            mUseVPN = false;
-
-            
-            SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
-            Editor ePrefs = prefs.edit();  
-            ePrefs.putBoolean("pref_vpn", false);
-            ePrefs.commit();
-
-            processSettings();
+            Prefs.putUseVpn(false);
+            processTransparentProxying();
             
             Intent intent = new Intent(TorService.this, OrbotVpnService.class);
             intent.setAction("stop");
@@ -1556,7 +1504,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
             if (read > 0 || written > 0)
                 iconId = R.drawable.ic_stat_tor_xfer;
             
-            if (mConnectivity && prefPersistNotifications)
+            if (mConnectivity && Prefs.persistNotifications())
                   showToolbarNotification(sb.toString(), NOTIFY_ID, iconId);
 
             mTotalTrafficWritten += written;
@@ -1631,7 +1579,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
                     sb.append (" > ");
             }
             
-            if (ENABLE_DEBUG_LOG)
+            if (Prefs.useDebugLogging())
                 debug(sb.toString());
             else if(status.equals("BUILT"))
             {
@@ -1651,7 +1599,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
 
             }
 
-            if (mShowExpandedNotifications)
+            if (Prefs.expandedNotifications())
             {
                 //get IP from last nodename
                 if(status.equals("BUILT")){
@@ -1707,7 +1655,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
                     	
                         Proxy proxy = null;
                         
-                        if (!mUseVPN) //if not on the VPN then we should proxy
+                        if (!Prefs.useVpn()) //if not on the VPN then we should proxy
                         {
                         	proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8118));
                         	conn = new URL(ONIONOO_BASE_URL + mNode.id).openConnection(proxy);
@@ -1787,21 +1735,16 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
         else
             return node;
     }
-    
 
-        
-        public void processSettings (){
+        public void processTransparentProxying() {
             try{
-                boolean hadEnableTransparentProxy = mEnableTransparentProxy;
-                updateSettings ();
-                if (mHasRoot)
+                if (Prefs.useRoot())
                 {
                      Shell shell = Shell.startRootShell();
-                    if (hadEnableTransparentProxy){
-                        disableTransparentProxy(shell);
-                    }
-                    if (mEnableTransparentProxy){
+                    if (Prefs.useTransparentProxying()){
                         enableTransparentProxy(shell);
+                    } else {
+                        disableTransparentProxy(shell);                        
                     }
                     shell.close();
                 }
@@ -2101,7 +2044,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
 	                    {
 	                        if (mConnectivity)
 	                        {
-	                            if (mHasRoot && mEnableTransparentProxy && mTransProxyNetworkRefresh)
+	                            if (Prefs.useRoot() && Prefs.useTransparentProxying() && Prefs.transProxyNetworkRefresh())
 	                            {
 	                                
 	                                Shell shell = Shell.startRootShell();
@@ -2111,7 +2054,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
 	                                
 	                                shell.close();
 	                            }
-	                            else if (mUseVPN) //we need to turn on VPN here so the proxy is running
+	                            else if (Prefs.useVpn()) //we need to turn on VPN here so the proxy is running
 	                            	refreshVpnProxy();
 	            	            
 	                        }
@@ -2145,7 +2088,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
         enableTransProxyAndDNSPorts(transPort, dnsPort);
         */
         
-        boolean useBridges = prefs.getBoolean(OrbotConstants.PREF_BRIDGES_ENABLED, false);
+        boolean useBridges = Prefs.bridgesEnabled();
 
         boolean becomeRelay = prefs.getBoolean(OrbotConstants.PREF_OR, false);
         boolean ReachableAddresses = prefs.getBoolean(OrbotConstants.PREF_REACHABLE_ADDRESSES,false);
@@ -2161,7 +2104,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
            
             extraLines.append("UseBridges 0").append('\n');
 
-	        if (mUseVPN) //set the proxy here if we aren't using a bridge
+	        if (Prefs.useVpn()) //set the proxy here if we aren't using a bridge
 	        {
 	        	
 	        	if (!mIsLollipop)
@@ -2212,7 +2155,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
 
             extraLines.append("UseBridges 1").append('\n');
             
-            String bridgeList = new String(prefs.getString(OrbotConstants.PREF_BRIDGES_LIST,"").getBytes("ISO-8859-1"));
+            String bridgeList = new String(Prefs.getBridgesList().getBytes("ISO-8859-1"));
             
             if (bridgeList != null && bridgeList.length() > 1) //longer then 1 = some real values here
             {
@@ -2411,7 +2354,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
             
         }
         
-        if (mUseVPN)
+        if (Prefs.useVpn())
         {
         	extraLines.append("DNSListenAddress" + ' ' + "10.0.0.1:" + TorServiceConstants.TOR_DNS_PORT_DEFAULT).append('\n');
         }
diff --git a/src/org/torproject/android/service/TorServiceConstants.java b/src/org/torproject/android/service/TorServiceConstants.java
index 2a23895..f466dae 100644
--- a/src/org/torproject/android/service/TorServiceConstants.java
+++ b/src/org/torproject/android/service/TorServiceConstants.java
@@ -89,7 +89,7 @@ public interface TorServiceConstants {
     public static final String CMD_NEWNYM = "newnym";
     public static final String CMD_VPN = "vpn";
     public static final String CMD_VPN_CLEAR = "vpnclear";
-    public static final String CMD_UPDATE = "update";
+    public static final String CMD_UPDATE_TRANS_PROXY = "update";
      
     
     public static final String BINARY_TOR_VERSION = "0.2.6.7";
diff --git a/src/org/torproject/android/service/TorTransProxy.java b/src/org/torproject/android/service/TorTransProxy.java
index d63abb2..f88e46b 100644
--- a/src/org/torproject/android/service/TorTransProxy.java
+++ b/src/org/torproject/android/service/TorTransProxy.java
@@ -6,6 +6,7 @@ import java.util.ArrayList;
 import org.sufficientlysecure.rootcommands.Shell;
 import org.sufficientlysecure.rootcommands.command.SimpleCommand;
 import org.torproject.android.OrbotConstants;
+import org.torproject.android.Prefs;
 import org.torproject.android.settings.TorifiedApp;
 
 import android.content.Context;
@@ -13,7 +14,6 @@ import android.content.SharedPreferences;
 
 public class TorTransProxy implements TorServiceConstants {
 	
-	private boolean useSystemIpTables = false;
 	private String mSysIptables = null;
 	private TorService mTorService = null;
 	private File mFileXtables = null;
@@ -43,12 +43,8 @@ public class TorTransProxy implements TorServiceConstants {
 	{
 
 		String ipTablesPath = null;
-		
-		SharedPreferences prefs = TorServiceUtils.getSharedPrefs(context);
 
-		useSystemIpTables = prefs.getBoolean(OrbotConstants.PREF_USE_SYSTEM_IPTABLES, false);
-		
-		if (useSystemIpTables)
+		if (Prefs.useSystemIpTables())
 		{
 			ipTablesPath = findSystemIPTables();
 		}
@@ -67,11 +63,7 @@ public class TorTransProxy implements TorServiceConstants {
 
 		String ipTablesPath = null;
 		
-		SharedPreferences prefs = TorServiceUtils.getSharedPrefs(context);
-
-		useSystemIpTables = prefs.getBoolean(OrbotConstants.PREF_USE_SYSTEM_IPTABLES, false);
-		
-		if (useSystemIpTables)
+		if (Prefs.useSystemIpTables())
 		{
 			ipTablesPath = findSystemIP6Tables();
 		}
@@ -674,7 +666,7 @@ public class TorTransProxy implements TorServiceConstants {
 		script = new StringBuilder();
 		
 		
-		if (TorService.ENABLE_DEBUG_LOG)
+		if (Prefs.useDebugLogging())
 		{
 			//XXX: Comment the following rules for non-debug builds
 			script.append(ipTablesPath);			





More information about the tor-commits mailing list