[tor-commits] [orbot/master] attempted to address leaking bound service

n8fr8 at torproject.org n8fr8 at torproject.org
Fri Jun 29 02:54:48 UTC 2012


commit 3de5d664402d37d1bf6372184e2a1f222a3cfcd6
Author: n8fr8 <nathan at freitas.net>
Date:   Thu Jun 28 17:01:35 2012 -0400

    attempted to address leaking bound service
---
 src/org/torproject/android/Orbot.java              |   43 ++++++--------------
 src/org/torproject/android/service/TorService.java |   42 ++++++++++++-------
 2 files changed, 40 insertions(+), 45 deletions(-)

diff --git a/src/org/torproject/android/Orbot.java b/src/org/torproject/android/Orbot.java
index 654aea6..e10f9af 100644
--- a/src/org/torproject/android/Orbot.java
+++ b/src/org/torproject/android/Orbot.java
@@ -82,9 +82,6 @@ public class Orbot extends Activity implements TorConstants, OnLongClickListener
            	
         	setLocale();
         
-        	bindService();
-           	startService(new Intent(INTENT_TOR_SERVICE));
-
             prefs = PreferenceManager.getDefaultSharedPreferences(this);
             
             setContentView(R.layout.layout_main);
@@ -94,8 +91,6 @@ public class Orbot extends Activity implements TorConstants, OnLongClickListener
                 
             imgStatus = (ImageView)findViewById(R.id.imgStatus);
             imgStatus.setOnLongClickListener(this);
-            
-            
 
     }
     
@@ -244,26 +239,13 @@ public class Orbot extends Activity implements TorConstants, OnLongClickListener
 	protected void onPause() {
 		super.onPause();
 		
+		unbindService();
+		
 		hideProgressDialog();
 
 		if (aDialog != null)
 			aDialog.dismiss();
 	}
-
-	public void onSaveInstanceState(Bundle savedInstanceState) {
-		  // Save UI state changes to the savedInstanceState.
-		  // This bundle will be passed to onCreate if the process is
-		  // killed and restarted.
-		  // etc.
-		  super.onSaveInstanceState(savedInstanceState);
-		}
-	
-	public void onRestoreInstanceState(Bundle savedInstanceState) {
-	  super.onRestoreInstanceState(savedInstanceState);
-	  // Restore UI state from the savedInstanceState.
-	  // This bundle has also been passed to onCreate.
-	 
-	}
 	
 	private void doTorCheck ()
 	{
@@ -327,6 +309,11 @@ public class Orbot extends Activity implements TorConstants, OnLongClickListener
 		
 		setLocale();
 		
+    	bindService();
+        // 	startService(new Intent(INTENT_TOR_SERVICE));
+
+
+		
 		if (getIntent() == null)
 			return;
 		
@@ -425,7 +412,7 @@ public class Orbot extends Activity implements TorConstants, OnLongClickListener
 	protected void onStop() {
 		super.onStop();
 		
-		//unbindService();
+		unbindService();
 	}
 
 
@@ -607,9 +594,6 @@ public class Orbot extends Activity implements TorConstants, OnLongClickListener
   // to start Tor
     private void startTor () throws RemoteException
     {
-            // here we bind AGAIN - at some point i think we had to bind multiple times just in case
-            // but i would love to clarify, clean this up
-            bindService();
             
             // this is a bit of a strange/old/borrowed code/design i used to change the service state
             // not sure it really makes sense when what we want to say is just "startTor"
@@ -877,12 +861,11 @@ public class Orbot extends Activity implements TorConstants, OnLongClickListener
     private void hideProgressDialog ()
     {
 
-                if (progressDialog != null && progressDialog.isShowing())
-                {
-                        progressDialog.dismiss();
-                        progressDialog = null;
-                }
-                
+        if (progressDialog != null && progressDialog.isShowing())
+        {
+                progressDialog.dismiss();
+                progressDialog = null;
+        }
                 
     }
     
diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java
index b07026a..85095ee 100644
--- a/src/org/torproject/android/service/TorService.java
+++ b/src/org/torproject/android/service/TorService.java
@@ -77,11 +77,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
     private File filePrivoxy;
     private File fileObfsProxy;
     
-    /** Called when the activity is first created. */
-    public void onCreate() {
-    	super.onCreate();
-      
-    }
     
     public static void logMessage(String msg)
     {
@@ -168,6 +163,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 	{
 		NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
 		mNotificationManager.cancelAll();
+		
+		
 	}
    
 	private void showToolbarNotification (String notifyMsg, int notifyId, int icon, int flags)
@@ -322,7 +319,10 @@ public class TorService extends Service implements TorServiceConstants, TorConst
     	try
     	{	
     		killTorProcess ();
-				
+    		
+    		//stop the foreground priority and make sure to remove the persistant notification
+    		stopForeground(true);
+    			
     		currentStatus = STATUS_OFF;
     
     		clearNotifications();
@@ -793,7 +793,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 			
 			initControlConnection ();
 
-	        applyPreferences();
+			updateTorConfiguration();
 	    }
     }
     
@@ -1033,6 +1033,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 			
 			startForeground(NOTIFY_ID,notice);
 		
+			
 		}
 		
 
@@ -1156,6 +1157,15 @@ public class TorService extends Service implements TorServiceConstants, TorConst
     	_torInstance = this;
     	initTorPaths();
     	
+    	
+    	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
+    	
+    	ENABLE_DEBUG_LOG = prefs.getBoolean("pref_enable_logging",false);
+    	Log.i(TAG,"debug logging:" + ENABLE_DEBUG_LOG);
+    	
+    	prefPersistNotifications = prefs.getBoolean(TorConstants.PREF_PERSIST_NOTIFICATIONS, true);
+    	
+    	
     	new Thread ()
     	{
     		
@@ -1208,8 +1218,14 @@ public class TorService extends Service implements TorServiceConstants, TorConst
         	
         	
         	try {
-				applyPreferences();
-				
+        		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(TorService.this);
+            	
+            	ENABLE_DEBUG_LOG = prefs.getBoolean("pref_enable_logging",false);
+            	Log.i(TAG,"debug logging:" + ENABLE_DEBUG_LOG);
+            	
+            	prefPersistNotifications = prefs.getBoolean(TorConstants.PREF_PERSIST_NOTIFICATIONS, true);
+            	
+        		updateTorConfiguration();
 
 		        if (currentStatus == STATUS_ON)
 		        {
@@ -1411,14 +1427,10 @@ public class TorService extends Service implements TorServiceConstants, TorConst
     	
     }
     
-    private boolean applyPreferences () throws RemoteException
+    private boolean updateTorConfiguration () throws RemoteException
     {
-    	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
-    	
-    	ENABLE_DEBUG_LOG = prefs.getBoolean("pref_enable_logging",false);
-    	Log.i(TAG,"debug logging:" + ENABLE_DEBUG_LOG);
     	
-    	prefPersistNotifications = prefs.getBoolean(TorConstants.PREF_PERSIST_NOTIFICATIONS, true);
+    	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
     	
 		boolean useBridges = prefs.getBoolean(TorConstants.PREF_BRIDGES_ENABLED, false); 
 		





More information about the tor-commits mailing list