[tor-commits] [orbot/master] fixes for sharing preferences across processes

n8fr8 at torproject.org n8fr8 at torproject.org
Fri Aug 23 20:59:49 UTC 2013


commit e632b3b04fce7ee8ab49a719f627ae04ccd1229e
Author: Nathan Freitas <nathan at freitas.net>
Date:   Fri Aug 23 16:44:15 2013 -0400

    fixes for sharing preferences across processes
---
 src/org/torproject/android/Orbot.java              |   18 ++-
 src/org/torproject/android/TorConstants.java       |    2 +-
 src/org/torproject/android/service/TorService.java |   95 +++++++------
 .../android/service/TorServiceUtils.java           |   53 +++++++-
 .../torproject/android/service/TorTransProxy.java  |  143 ++++++--------------
 .../torproject/android/settings/AppManager.java    |   67 +++------
 6 files changed, 175 insertions(+), 203 deletions(-)

diff --git a/src/org/torproject/android/Orbot.java b/src/org/torproject/android/Orbot.java
index 1ac1656..670de66 100644
--- a/src/org/torproject/android/Orbot.java
+++ b/src/org/torproject/android/Orbot.java
@@ -7,6 +7,7 @@ import java.util.Locale;
 
 import org.torproject.android.service.ITorService;
 import org.torproject.android.service.ITorServiceCallback;
+import org.torproject.android.service.TorService;
 import org.torproject.android.service.TorServiceConstants;
 import org.torproject.android.settings.ProcessSettingsAsyncTask;
 import org.torproject.android.settings.SettingsPreferences;
@@ -81,12 +82,6 @@ public class Orbot extends SherlockActivity implements TorConstants, OnLongClick
 
 	SharedPreferences mPrefs;
 	
-	public static Orbot currentInstance = null;
-	
-    private static void setCurrent(Orbot current){
-    	Orbot.currentInstance = current;
-    }
-    
     /** Called when the activity is first created. */
 	public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -103,11 +98,9 @@ public class Orbot extends SherlockActivity implements TorConstants, OnLongClick
         	}
         */
 
-        mPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
+        mPrefs = getPrefs();
         mPrefs.registerOnSharedPreferenceChangeListener(this);
         
-        Orbot.setCurrent(this);
-
       //if Tor binary is not running, then start the service up
       //might want to look at whether we need to call this every time
       //or whether binding to the service is enough
@@ -164,7 +157,7 @@ public class Orbot extends SherlockActivity implements TorConstants, OnLongClick
 			public boolean onLongClick(View v) {
 				  ClipboardManager cm = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
 	                cm.setText(mTxtOrbotLog.getText());
-	                Toast.makeText(Orbot.this, "LOG COPIED TO CLIPBOARD. PLEASE EMAIL TO help at guardianproject.info TO DEBUG PROBLEM", Toast.LENGTH_SHORT).show();
+	                Toast.makeText(Orbot.this, "LOG COPIED TO CLIPBOARD", Toast.LENGTH_SHORT).show();
 	            return true;
 			}
         });
@@ -1265,4 +1258,9 @@ public class Orbot extends SherlockActivity implements TorConstants, OnLongClick
 
 	    }
 
+	 private SharedPreferences getPrefs ()
+		{
+			return TorService.getSharedPrefs(getApplicationContext());
+			
+		}
 }
diff --git a/src/org/torproject/android/TorConstants.java b/src/org/torproject/android/TorConstants.java
index 9618747..5af1369 100644
--- a/src/org/torproject/android/TorConstants.java
+++ b/src/org/torproject/android/TorConstants.java
@@ -52,5 +52,5 @@ public interface TorConstants {
 	
 	public final static String PREF_DISABLE_NETWORK = "pref_disable_network";
 	
-	public final static String PREF_TOR_SHARED_PREFS = "torprefs";
+	public final static String PREF_TOR_SHARED_PREFS = "org.torproject.android_preferences";
 }
diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java
index 04b6b1e..9599d7f 100644
--- a/src/org/torproject/android/service/TorService.java
+++ b/src/org/torproject/android/service/TorService.java
@@ -44,13 +44,12 @@ import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
-import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
 import android.net.ConnectivityManager;
 import android.net.Uri;
+import android.os.Build;
 import android.os.IBinder;
 import android.os.RemoteCallbackList;
 import android.os.RemoteException;
-import android.preference.PreferenceManager;
 import android.support.v4.app.NotificationCompat;
 import android.util.Log;
 
@@ -249,23 +248,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 		super.onStart(intent, startId);
 
 		_torInstance = this;
-		
-		/*
-		prefs = getSharedPreferences(TorConstants.PREF_TOR_SHARED_PREFS,Context.MODE_MULTI_PROCESS);
-		*/
-		
-		/*
-		mPrefs.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener()
-		{
 
-			@Override
-			public void onSharedPreferenceChanged(
-					SharedPreferences sharedPreferences, String key) {
-				updateSettings();
-				
-			}
-			
-		});*/
+		android.os.Debug.waitForDebugger();
 		
 		initTorPaths();
 
@@ -297,7 +281,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 		if (intent != null && intent.getAction()!=null && intent.getAction().equals("onboot"))
 		{
 			
-			boolean startOnBoot = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("pref_start_boot",false);
+			boolean startOnBoot = getSharedPrefs(getApplicationContext()).getBoolean("pref_start_boot",false);
 			
 			if (startOnBoot)
 			{
@@ -305,6 +289,16 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 			}
 		}
 	}
+	
+	public static SharedPreferences getSharedPrefs (Context context)
+	{
+		if (Build.VERSION.SDK_INT>=11)
+			return context.getSharedPreferences(TorConstants.PREF_TOR_SHARED_PREFS,Context.MODE_MULTI_PROCESS);
+		else
+			return context.getSharedPreferences(TorConstants.PREF_TOR_SHARED_PREFS,Context.MODE_PRIVATE);
+		
+	}
+	
 	 
 	public void run ()
 	{
@@ -366,7 +360,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
     {
     	currentStatus = STATUS_OFF;
     	
- 		boolean hasRoot = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean(PREF_HAS_ROOT,false);
+ 		boolean hasRoot =  getSharedPrefs(getApplicationContext()).getBoolean("has_root", false);
  		
     	try
     	{	
@@ -424,7 +418,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 	private String getHiddenServiceHostname ()
 	{
 
-		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
+		SharedPreferences prefs = getSharedPrefs(getApplicationContext());
 		
         boolean enableHiddenServices = prefs.getBoolean("pref_hs_enable", false);
         
@@ -547,7 +541,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
     public boolean checkTorBinaries (boolean forceInstall) throws Exception
     {
     	
-		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
+		SharedPreferences prefs =getSharedPrefs(getApplicationContext());
 
     	//check and install iptables
     	TorBinaryInstaller.assertIpTablesBinaries(this, true);
@@ -651,7 +645,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
     
     private void updateSettings ()
     {
-		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
+		SharedPreferences prefs = getSharedPrefs(getApplicationContext());
 
     	mHasRoot = prefs.getBoolean(PREF_HAS_ROOT,false);
  		mEnableTransparentProxy = prefs.getBoolean("pref_transparent", false);
@@ -666,6 +660,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
     
     public void initTor () throws Exception
     {
+    	android.os.Debug.waitForDebugger();
     	
     	updateSettings ();
     	
@@ -725,7 +720,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 		{
 			showToolbarNotification(getString(R.string.setting_up_app_based_transparent_proxying_), TRANSPROXY_NOTIFY_ID, R.drawable.ic_stat_tor, -1);
 
-			code = mTransProxy.setTransparentProxyingByApp(this,AppManager.getApps(this));
+			code = mTransProxy.setTransparentProxyingByApp(this,AppManager.getApps(this, getSharedPrefs(getApplicationContext())));
 		}
 			
 	
@@ -767,7 +762,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
  			mTransProxy = new TorTransProxy(this);
  		
  		mTransProxy.clearTransparentProxyingAll(this);
-        mTransProxy.clearTransparentProxyingByApp(this,AppManager.getApps(this));
+       // mTransProxy.clearTransparentProxyingByApp(this,AppManager.getApps(this));
 	    
      	clearNotifications();
      	
@@ -776,7 +771,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
     
     private void runTorShellCmd() throws Exception
     {
-		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
+		SharedPreferences prefs =getSharedPrefs(getApplicationContext());
 
     	StringBuilder log = new StringBuilder();
 		
@@ -1083,6 +1078,12 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 				
 			}				
 
+			if (mNotificationManager == null)
+			{
+				mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+
+			}
+			
 			mNotifyBuilder.setOngoing(persistent);			    
 			mNotifyBuilder.setContentText(message);
 
@@ -1235,28 +1236,32 @@ public class TorService extends Service implements TorServiceConstants, TorConst
    	
 	public void circuitStatus(String status, String circID, String path) {
 		
-		StringBuilder sb = new StringBuilder();
-		sb.append("Circuit (");
-		sb.append((circID));
-		sb.append(") ");
-		sb.append(status);
-		sb.append(": ");
-		
-		StringTokenizer st = new StringTokenizer(path,",");
-		String node = null;
-		
-		while (st.hasMoreTokens())
+		if (status.equals("BUILT") || status.equals("CLOSED"))
 		{
-			node = st.nextToken();
+			StringBuilder sb = new StringBuilder();
+			sb.append("Circuit (");
+			sb.append((circID));
+			sb.append(") ");
+			sb.append(status);
+			sb.append(": ");
 			
-			sb.append(parseNodeName(node));
+			StringTokenizer st = new StringTokenizer(path,",");
+			String node = null;
 			
+			while (st.hasMoreTokens())
+			{
+				node = st.nextToken();
+				
+				sb.append(parseNodeName(node));
+				
+				
+				if (st.hasMoreTokens())
+					sb.append (" > ");
+			}
 			
-			if (st.hasMoreTokens())
-				sb.append (" > ");
+			logNotice(sb.toString());
 		}
-		
-		logNotice(sb.toString());
+			
 	
 	}
 	
@@ -1618,7 +1623,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
     	@Override
     	public void onReceive(Context context, Intent intent) {
 
-    		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
+    		SharedPreferences prefs = getSharedPrefs(getApplicationContext());
 
     		mConnectivity = !intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
 
@@ -1650,7 +1655,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 
     private boolean processSettingsImpl () throws RemoteException
     {
-		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
+		SharedPreferences prefs = getSharedPrefs(getApplicationContext());
 
 		boolean useBridges = prefs.getBoolean(TorConstants.PREF_BRIDGES_ENABLED, false);
 		
diff --git a/src/org/torproject/android/service/TorServiceUtils.java b/src/org/torproject/android/service/TorServiceUtils.java
index e9e929e..fde8c9e 100644
--- a/src/org/torproject/android/service/TorServiceUtils.java
+++ b/src/org/torproject/android/service/TorServiceUtils.java
@@ -132,7 +132,8 @@ public class TorServiceUtils implements TorServiceConstants {
         
         for (int i = 0; i < cmds.length; i++)
         {
-        //	TorService.logMessage("executing shell cmd: " + cmds[i] + "; runAsRoot=" + runAsRoot + ";waitFor=" + waitFor);
+        	if (TorService.ENABLE_DEBUG_LOG)
+        		Log.d(TorService.TAG,"executing shell cmd: " + cmds[i] + "; runAsRoot=" + runAsRoot + ";waitFor=" + waitFor);
     		
         	out.write(cmds[i]);
         	out.write("\n");
@@ -169,4 +170,54 @@ public class TorServiceUtils implements TorServiceConstants {
         return exitCode;
 
 	}
+	
+	public static int doShellCommand(String cmd, StringBuilder log, boolean runAsRoot, boolean waitFor) throws Exception
+	{
+		
+		Process proc = null;
+		int exitCode = -1;
+		
+    	if (runAsRoot)
+    		proc = Runtime.getRuntime().exec("su");
+    	else
+    		proc = Runtime.getRuntime().exec("sh");
+    
+    	OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream());
+        
+        //	TorService.logMessage("executing shell cmd: " + cmds[i] + "; runAsRoot=" + runAsRoot + ";waitFor=" + waitFor);
+    		
+    	out.write(cmd);
+    	out.write("\n");
+    
+        
+        out.flush();
+		out.write("exit\n");
+		out.flush();
+	
+		if (waitFor)
+		{
+			
+			final char buf[] = new char[10];
+			
+			// Consume the "stdout"
+			InputStreamReader reader = new InputStreamReader(proc.getInputStream());
+			int read=0;
+			while ((read=reader.read(buf)) != -1) {
+				if (log != null) log.append(buf, 0, read);
+			}
+			
+			// Consume the "stderr"
+			reader = new InputStreamReader(proc.getErrorStream());
+			read=0;
+			while ((read=reader.read(buf)) != -1) {
+				if (log != null) log.append(buf, 0, read);
+			}
+			
+			exitCode = proc.waitFor();
+		
+		}
+		
+        return exitCode;
+
+	}
 }
diff --git a/src/org/torproject/android/service/TorTransProxy.java b/src/org/torproject/android/service/TorTransProxy.java
index 3798710..5c85140 100644
--- a/src/org/torproject/android/service/TorTransProxy.java
+++ b/src/org/torproject/android/service/TorTransProxy.java
@@ -31,7 +31,8 @@ public class TorTransProxy implements TorServiceConstants {
 
 		String ipTablesPath = null;
 		
-		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
+		SharedPreferences prefs = TorService.getSharedPrefs(context.getApplicationContext());
+
 		useSystemIpTables = prefs.getBoolean(TorConstants.PREF_USE_SYSTEM_IPTABLES, false);
 		
 		if (useSystemIpTables)
@@ -233,7 +234,7 @@ public class TorTransProxy implements TorServiceConstants {
     }	
 	*/
 	
-	
+	/*
 	public int clearTransparentProxyingByApp (Context context, ArrayList<TorifiedApp> apps) throws Exception
 	{
 		boolean runRoot = true;
@@ -270,7 +271,7 @@ public class TorTransProxy implements TorServiceConstants {
 		logMessage(cmdAdd[0] + ";errCode=" + code + ";resp=" + msg);
 		
 		return code;
-	}
+	}*/
 	
 	public int setTransparentProxyingByApp(Context context, ArrayList<TorifiedApp> apps) throws Exception
 	{
@@ -285,21 +286,8 @@ public class TorTransProxy implements TorServiceConstants {
     	StringBuilder res = new StringBuilder();
     	int code = -1;
     	
-    	String chainName = "ORBOT";
-		String jumpChainName = "OUTPUT";
+		String srcChainName = "OUTPUT";
 		
-		script.append(ipTablesPath);
-    	script.append(" --flush ").append(chainName); //delete previous user-defined chain
-    	script.append(" || exit\n");
-    	
-		script.append(ipTablesPath);
-    	script.append(" -D ").append(jumpChainName);
-    	script.append(" -j ").append(chainName);
-    	script.append(" || exit\n");
-    	
-    	script.append(ipTablesPath);
-    	script.append(" -X ").append(chainName); //delete previous user-defined chain
-    	script.append(" || exit\n");
     	
     	//run the delete commands in a separate process as it might error out
     	String[] cmdExecClear = {script.toString()};    	    	
@@ -307,16 +295,7 @@ public class TorTransProxy implements TorServiceConstants {
 		
 		//reset script
 		script = new StringBuilder();    	
-		 
-		script.append(ipTablesPath);
-    	script.append(" -N ").append(chainName); //create user-defined chain
-    	script.append(" || exit\n");
-
-    	script.append(ipTablesPath);
-        script.append(" -A ").append(jumpChainName);
-        script.append(" -j ").append(chainName);
-        script.append(" || exit\n");
-    	    	
+		    	
 		//build up array of shell cmds to execute under one root context
 		for (TorifiedApp tApp:apps)
 		{
@@ -332,7 +311,7 @@ public class TorTransProxy implements TorServiceConstants {
 				// Set up port redirection
 		    	script.append(ipTablesPath);
 		    	script.append(" -t nat");
-		    	script.append(" -A ").append(jumpChainName);				
+		    	script.append(" -A ").append(srcChainName);				
 				script.append(" -p tcp");
 				script.append(" ! -d 127.0.0.1"); //allow access to localhost
 				script.append(" -m owner --uid-owner ");
@@ -345,7 +324,7 @@ public class TorTransProxy implements TorServiceConstants {
 				// Same for DNS
 				script.append(ipTablesPath);
 				script.append(" -t nat");				
-				script.append(" -A ").append(jumpChainName);				
+				script.append(" -A ").append(srcChainName);				
 				script.append(" -p udp -m owner --uid-owner ");
 				script.append(tApp.getUid());
 				script.append(" -m udp --dport "); 
@@ -361,7 +340,7 @@ public class TorTransProxy implements TorServiceConstants {
 					// Allow packets to localhost (contains all the port-redirected ones)
 					script.append(ipTablesPath);
 					script.append(" -t filter");
-			        script.append(" -A ").append(jumpChainName);
+			        script.append(" -A ").append(srcChainName);
 					script.append(" -m owner --uid-owner ");
 					script.append(tApp.getUid());
 					script.append(" -p tcp");
@@ -375,7 +354,7 @@ public class TorTransProxy implements TorServiceConstants {
 				// Allow loopback
 				script.append(ipTablesPath);
 				script.append(" -t filter");
-		        script.append(" -A ").append(jumpChainName);
+		        script.append(" -A ").append(srcChainName);
 				script.append(" -m owner --uid-owner ");
 				script.append(tApp.getUid());
 				script.append(" -p tcp");
@@ -386,7 +365,7 @@ public class TorTransProxy implements TorServiceConstants {
 				// Reject all other outbound TCP packets
 				script.append(ipTablesPath);
 				script.append(" -t filter");
-		        script.append(" -A ").append(jumpChainName);
+		        script.append(" -A ").append(srcChainName);
 				script.append(" -m owner --uid-owner ");
 				script.append(tApp.getUid());
 				script.append(" -p tcp");
@@ -397,7 +376,7 @@ public class TorTransProxy implements TorServiceConstants {
 				// Reject all other outbound UDP packets
 				script.append(ipTablesPath);
 				script.append(" -t filter");
-		        script.append(" -A ").append(jumpChainName);
+		        script.append(" -A ").append(srcChainName);
 				script.append(" -m owner --uid-owner ");
 				script.append(tApp.getUid());
 				script.append(" -p udp");
@@ -475,37 +454,31 @@ public class TorTransProxy implements TorServiceConstants {
 		boolean runRoot = true;
     	boolean waitFor = true;
     	
-		//redirectDNSResolvConf(); //not working yet
-		
 		String ipTablesPath = getIpTablesPath(context);
 		
-    	StringBuilder script = new StringBuilder();
-    	
+    	StringBuilder script = new StringBuilder();    	
     	StringBuilder res = new StringBuilder();
     	int code = -1;
 
-    	String chainName = "ORBOT";
-		String jumpChainName = "OUTPUT";
-
-		script.append(ipTablesPath);
-    	script.append(" --flush ").append(chainName); //delete previous user-defined chain
-    	script.append(" || exit\n");
+    	String chainName = "OUTPUT";
     	
+		script = new StringBuilder();
+		res = new StringBuilder();
 		script.append(ipTablesPath);
-    	script.append(" -D ").append(jumpChainName);
-    	script.append(" -j ").append(chainName);
-    	script.append(" || exit\n");
-    	
-    	script.append(ipTablesPath);
-    	script.append(" -X ").append(chainName); //delete previous user-defined chain
+    	script.append(" -t nat");		
+    	script.append(" -F ").append(chainName); //delete previous user-defined chain
     	script.append(" || exit\n");
+    	code = TorServiceUtils.doShellCommand(script.toString(), res, runRoot, waitFor);		
+		logMessage("Exec resp: cmd> " + script.toString() + "; errCode=" + code + ";resp=" + res.toString());
 		
-		String[] cmdExec = {script.toString()};    	
-    	
-		code = TorServiceUtils.doShellCommand(cmdExec, res, runRoot, waitFor);
-		String msg = res.toString();
-	
-		logMessage("Exec resp: errCode=" + code + ";resp=" + msg);
+		script = new StringBuilder();
+		res = new StringBuilder();
+		script.append(ipTablesPath);
+    	script.append(" -t filter");		
+    	script.append(" -F ").append(chainName); //delete previous user-defined chain
+    	script.append(" || exit\n");
+    	code = TorServiceUtils.doShellCommand(script.toString(), res, runRoot, waitFor);		
+		logMessage("Exec resp: cmd> " + script.toString() + "; errCode=" + code + ";resp=" + res.toString());
 		
     	return code;
 	}
@@ -527,51 +500,21 @@ public class TorTransProxy implements TorServiceConstants {
     	
     	int torUid = context.getApplicationInfo().uid;
 
-    	String chainName = "ORBOT";
-		String jumpChainName = "OUTPUT";
-
-		script.append(ipTablesPath);
-    	script.append(" --flush ").append(chainName); //delete previous user-defined chain
-    	script.append(" || exit\n");
-    	
-		script.append(ipTablesPath);
-    	script.append(" -D ").append(jumpChainName);
-    	script.append(" -j ").append(chainName);
-    	script.append(" || exit\n");
-    	
-    	script.append(ipTablesPath);
-    	script.append(" -X ").append(chainName); //delete previous user-defined chain
-    	script.append(" || exit\n");
-		
-	  	//run the delete commands in a separate process as it might error out
-    	String[] cmdExecClear = {script.toString()};    	    	
-		code = TorServiceUtils.doShellCommand(cmdExecClear, res, runRoot, waitFor);
-		
-		//reset script
-		script = new StringBuilder(); 
-		
-		script.append(ipTablesPath);
-		script.append(" -N ").append(chainName); //create user-defined chain
-		script.append(" || exit\n");
-
-		script.append(ipTablesPath);
-    	script.append(" -A ").append(jumpChainName);
-    	script.append(" -j ").append(chainName);
-    	script.append(" || exit\n");
+    	String srcChainName = "OUTPUT";
     	
 		// Allow everything for Tor
-		script.append(ipTablesPath);
+		script.append(ipTablesPath);			
 		script.append(" -t filter");
-    	script.append(" -A ").append(chainName);		
+		script.append(" -A ").append(srcChainName);
 		script.append(" -m owner --uid-owner ");
 		script.append(torUid);
 		script.append(" -j ACCEPT");
 		script.append(" || exit\n");
 		
     	// Set up port redirection    	
-		script.append(ipTablesPath);
+		script.append(ipTablesPath);		
 		script.append(" -t nat");
-    	script.append(" -A ").append(jumpChainName);		
+		script.append(" -A ").append(srcChainName);
 		script.append(" -p tcp");
 		script.append(" ! -d 127.0.0.1"); //allow access to localhost
 		script.append(" -m owner ! --uid-owner ");
@@ -584,7 +527,7 @@ public class TorTransProxy implements TorServiceConstants {
 		// Same for DNS
 		script.append(ipTablesPath);
 		script.append(" -t nat");
-		script.append(" -A ").append(jumpChainName);		
+		script.append(" -A ").append(srcChainName);
 		script.append(" -p udp -m owner ! --uid-owner ");
 		script.append(torUid);
 		script.append(" -m udp --dport "); 
@@ -598,9 +541,9 @@ public class TorTransProxy implements TorServiceConstants {
 		for (int port : ports)
 		{
 			// Allow packets to localhost (contains all the port-redirected ones)
-			script.append(ipTablesPath);
+			script.append(ipTablesPath);			
 			script.append(" -t filter");
-			script.append(" -A ").append(chainName);			
+			script.append(" -A ").append(srcChainName);
 			script.append(" -m owner ! --uid-owner ");
 			script.append(torUid);
 			script.append(" -p tcp");
@@ -615,7 +558,7 @@ public class TorTransProxy implements TorServiceConstants {
 		// Allow loopback
 		script.append(ipTablesPath);
 		script.append(" -t filter");
-		script.append(" -A ").append(chainName);		
+		script.append(" -A ").append(srcChainName);
 		script.append(" -p tcp");
 		script.append(" -o lo");
 		script.append(" -j ACCEPT");
@@ -625,9 +568,9 @@ public class TorTransProxy implements TorServiceConstants {
 		if (TorService.ENABLE_DEBUG_LOG)
 		{
 			//XXX: Comment the following rules for non-debug builds
-			script.append(ipTablesPath);
+			script.append(ipTablesPath);			
 			script.append(" -t filter");
-			script.append(" -A ").append(chainName);			
+			script.append(" -A ").append(srcChainName);
 			script.append(" -p udp");
 			script.append(" --dport ");
 			script.append(STANDARD_DNS_PORT);
@@ -636,9 +579,9 @@ public class TorTransProxy implements TorServiceConstants {
 			script.append(" --log-uid");
 			script.append(" || exit\n");
 			
-			script.append(ipTablesPath);
+			script.append(ipTablesPath);			
 			script.append(" -t filter");
-			script.append(" -A ").append(chainName);	    	
+			script.append(" -A ").append(srcChainName);
 	    	script.append(" -p tcp");
 			script.append(" -j LOG");
 			script.append(" --log-prefix='ORBOT_TCPLEAK_PROTECTION'");
@@ -650,7 +593,7 @@ public class TorTransProxy implements TorServiceConstants {
 		// Reject all other outbound TCP packets
 		script.append(ipTablesPath);
 		script.append(" -t filter");
-		script.append(" -A ").append(chainName);		
+		script.append(" -A ").append(srcChainName);
 		script.append(" -m owner ! --uid-owner ");
 		script.append(torUid);
 		script.append(" -p tcp");
@@ -661,7 +604,7 @@ public class TorTransProxy implements TorServiceConstants {
 		// Reject all other outbound UDP packets
 		script.append(ipTablesPath);
 		script.append(" -t filter");
-		script.append(" -A ").append(chainName);		
+		script.append(" -A ").append(srcChainName);
 		script.append(" -m owner ! --uid-owner ");
 		script.append(torUid);
 		script.append(" -p udp");
diff --git a/src/org/torproject/android/settings/AppManager.java b/src/org/torproject/android/settings/AppManager.java
index bbf2644..7121e5e 100644
--- a/src/org/torproject/android/settings/AppManager.java
+++ b/src/org/torproject/android/settings/AppManager.java
@@ -6,12 +6,14 @@ package org.torproject.android.settings;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.Iterator;
 import java.util.List;
 import java.util.StringTokenizer;
 
 import org.torproject.android.R;
 import org.torproject.android.TorConstants;
+import org.torproject.android.service.TorService;
 
 import android.app.Activity;
 import android.content.Context;
@@ -21,7 +23,6 @@ import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.os.Bundle;
-import android.preference.PreferenceManager;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.View.OnClickListener;
@@ -36,24 +37,15 @@ import android.widget.ListAdapter;
 import android.widget.ListView;
 import android.widget.TextView;
 
-
 public class AppManager extends Activity implements OnCheckedChangeListener, OnClickListener, TorConstants {
 
-	private static ArrayList<TorifiedApp> apps = null;
-
 	private ListView listApps;
 	
-	private AppManager mAppManager;
-
-
-	private boolean appsLoaded = false;
-	
 	protected void onCreate(Bundle savedInstanceState) {
 		super.onCreate(savedInstanceState);
 	
 		this.setContentView(R.layout.layout_apps);
 		
-		mAppManager = this;
 
 	}
 	
@@ -73,16 +65,17 @@ public class AppManager extends Activity implements OnCheckedChangeListener, OnC
 			}
 		});
 		
-		if (!appsLoaded)
-			loadApps();
+		mPrefs = TorService.getSharedPrefs(getApplicationContext());
+		loadApps(mPrefs);
 	}
 
-
-
-	private void loadApps ()
+	SharedPreferences mPrefs = null;
+	ArrayList<TorifiedApp> mApps = null;
+	
+	private void loadApps (SharedPreferences prefs)
 	{
-		resetApps(this);
-        final ArrayList<TorifiedApp> apps = getApps(this);
+		
+		mApps = getApps(getApplicationContext(), prefs);
         
         /*
         Arrays.sort(apps, new Comparator<TorifiedApp>() {
@@ -91,12 +84,11 @@ public class AppManager extends Activity implements OnCheckedChangeListener, OnC
 				if (o1.isTorified()) return -1;
 				return 1;
 			}
-        });
-        */
+        });*/
         
         final LayoutInflater inflater = getLayoutInflater();
 		
-        final ListAdapter adapter = new ArrayAdapter<TorifiedApp>(this,R.layout.layout_apps_item,R.id.itemtext,apps) {
+        ListAdapter adapter = new ArrayAdapter<TorifiedApp>(this,R.layout.layout_apps_item,R.id.itemtext,mApps) {
         	public View getView(int position, View convertView, ViewGroup parent) {
        			ListEntry entry;
         		if (convertView == null) {
@@ -107,19 +99,19 @@ public class AppManager extends Activity implements OnCheckedChangeListener, OnC
        				entry.box = (CheckBox) convertView.findViewById(R.id.itemcheck);
        				entry.text = (TextView) convertView.findViewById(R.id.itemtext);
        				
-       				entry.text.setOnClickListener(mAppManager);
-       				entry.text.setOnClickListener(mAppManager);
+       				entry.text.setOnClickListener(AppManager.this);
+       				entry.text.setOnClickListener(AppManager.this);
        				
        				convertView.setTag(entry);
        			
-       				entry.box.setOnCheckedChangeListener(mAppManager);
+       				entry.box.setOnCheckedChangeListener(AppManager.this);
         		} else {
         			// Convert an existing view
         			entry = (ListEntry) convertView.getTag();
         		}
         		
         		
-        		final TorifiedApp app = apps.get(position);
+        		final TorifiedApp app = mApps.get(position);
         		
         	
         		entry.icon.setImageDrawable(app.getIcon());
@@ -138,8 +130,6 @@ public class AppManager extends Activity implements OnCheckedChangeListener, OnC
         
         listApps.setAdapter(adapter);
         
-        appsLoaded = true;
-		   
 	}
 	
 	private static class ListEntry {
@@ -157,19 +147,10 @@ public class AppManager extends Activity implements OnCheckedChangeListener, OnC
 		
 	}
 
-	public static ArrayList<TorifiedApp> getApps (Context context)
-	{
-		if (apps == null)
-			resetApps(context);
-		
-		return apps;
-	}
 	
-	public static ArrayList<TorifiedApp> resetApps (Context context)
+	public static ArrayList<TorifiedApp> getApps (Context context, SharedPreferences prefs)
 	{
 
-		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
-
 		String tordAppString = prefs.getString(PREFS_KEY_TORIFIED, "");
 		String[] tordApps;
 		
@@ -190,7 +171,7 @@ public class AppManager extends Activity implements OnCheckedChangeListener, OnC
 		
 		Iterator<ApplicationInfo> itAppInfo = lAppInfo.iterator();
 		
-		apps = new ArrayList<TorifiedApp>();
+		ArrayList<TorifiedApp> apps = new ArrayList<TorifiedApp>();
 		
 		ApplicationInfo aInfo = null;
 		
@@ -269,16 +250,10 @@ public class AppManager extends Activity implements OnCheckedChangeListener, OnC
 
 	public void saveAppSettings (Context context)
 	{
-		if (apps == null)
-			return;
-		
-		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
-
-	//	final SharedPreferences prefs = context.getSharedPreferences(PREFS_KEY, 0);
 
 		StringBuilder tordApps = new StringBuilder();
-		
-		for (TorifiedApp tApp:apps)
+
+		for (TorifiedApp tApp:mApps)
 		{
 			if (tApp.isTorified())
 			{
@@ -287,7 +262,7 @@ public class AppManager extends Activity implements OnCheckedChangeListener, OnC
 			}
 		}
 		
-		Editor edit = prefs.edit();
+		Editor edit = mPrefs.edit();
 		edit.putString(PREFS_KEY_TORIFIED, tordApps.toString());
 		edit.commit();
 		





More information about the tor-commits mailing list