[tor-commits] [orbot/master] changes to improve longevity of background service/Tor process

n8fr8 at torproject.org n8fr8 at torproject.org
Mon Nov 24 18:24:23 UTC 2014


commit 87d5ac9c44855dd6fe6f372f21b8e5ffa0600cf1
Author: Nathan Freitas <nathan at freitas.net>
Date:   Mon Nov 24 13:11:38 2014 -0500

    changes to improve longevity of background service/Tor process
---
 AndroidManifest.xml                                |    8 +--
 src/org/torproject/android/service/TorService.java |   57 ++++++++++----------
 2 files changed, 35 insertions(+), 30 deletions(-)

diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 4d909b1..de175f9 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -1,12 +1,12 @@
 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
       package="org.torproject.android" 
-      android:versionName="14.1.1" 
-      android:versionCode="128"
+      android:versionName="14.1.3-PIE" 
+      android:versionCode="130"
         android:installLocation="auto"      
       >
  	    
-    <uses-sdk android:minSdkVersion="9" android:maxSdkVersion="20" android:targetSdkVersion="19"/>
+    <uses-sdk android:minSdkVersion="21" android:maxSdkVersion="21" android:targetSdkVersion="21"/>
  <permission android:name="org.torproject.android.MANAGE_TOR" android:label="@string/permission_manage_tor_label" android:description="@string/permission_manage_tor_description" android:protectionLevel="signature"></permission>
     
  <uses-permission android:name="android.permission.INTERNET" />
@@ -23,6 +23,7 @@
       android:allowBackup="false"
       android:allowClearUserData="true"
       android:persistent="true"
+      android:stopWithTask="false"
      >
       
         <activity android:name=".Orbot" android:configChanges="orientation|screenSize"
@@ -85,6 +86,7 @@
     	<service android:enabled="true"
     		android:name=".service.TorService" 
 		android:permission="org.torproject.android.MANAGE_TOR"
+		android:stopWithTask="false"
     		>
 	            <intent-filter>
 	                <action android:name="org.torproject.android.service.ITorService" />
diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java
index adb6fcf..6323d83 100644
--- a/src/org/torproject/android/service/TorService.java
+++ b/src/org/torproject/android/service/TorService.java
@@ -58,6 +58,7 @@ import org.torproject.android.vpn.OrbotVpnService;
 import android.annotation.SuppressLint;
 import android.annotation.TargetApi;
 import android.app.Application;
+import android.app.IntentService;
 import android.app.Notification;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
@@ -142,7 +143,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
     private boolean mTransProxyNetworkRefresh = false;
     
     private ExecutorService mExecutor = Executors.newCachedThreadPool();
-    
+
     public void debug(String msg)
     {
     	if (ENABLE_DEBUG_LOG)  
@@ -330,8 +331,10 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 		{
 			startForeground(NOTIFY_ID, mNotification);		
 		}
-				
-		mNotificationManager.notify(NOTIFY_ID, mNotification);
+		else
+		{
+			mNotificationManager.notify(NOTIFY_ID, mNotification);
+		}
  	}
     
 
@@ -340,19 +343,9 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 	 */
 	public int onStartCommand(Intent intent, int flags, int startId) {
 
-		try
-		{
-				
-			new Thread (new TorStarter(intent)).start();
-			
-		}
-		catch (Exception e)
-		{
-			logException ("Error starting service",e);
-			return Service.START_NOT_STICKY;
-		}
-
-	    return Service.START_REDELIVER_INTENT;
+		new Thread (new TorStarter(intent)).start();
+		
+	    return START_REDELIVER_INTENT;
 
 	}
 	
@@ -387,6 +380,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 						}
 						else if (action.equals(CMD_INIT))
 						{
+							initialize();
 							sendCallbackStatus(mCurrentStatus);
 						}
 						else if (action.equals(CMD_NEWNYM))
@@ -421,7 +415,16 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 		logNotice("Orbot was swiped away... background service will keep running");    	
 	}
 
+    @Override
+	public boolean stopService(Intent name) {
+		
+    	logNotice("TorService is being stopped: " + name);
+
+		return super.stopService(name);
+		
+	}
 
+	@Override
     public void onDestroy ()
     {
     	super.onDestroy();
@@ -430,6 +433,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
     		
         unregisterReceiver(mNetworkStateReceiver);        
         
+        clearNotifications ();
+        
     }
     
     private void stopTor ()
@@ -930,22 +935,19 @@ public class TorService extends Service implements TorServiceConstants, TorConst
      	return true;
  	}
     
-    Shell mShellTor;
-    
     private boolean runTorShellCmd() throws Exception
     {
-    	
+
+        Shell shellTor;
+        
 		String torrcPath = new File(appBinHome, TORRC_ASSET_KEY).getCanonicalPath();
 
     	updateTorConfigFile();
     	
     	sendCallbackLogMessage(getString(R.string.status_starting_up));
 
-		if (mShellTor != null)
-			mShellTor.close();
-		
 		//start Tor in the background
-		mShellTor = Shell.startShell();
+    	shellTor = Shell.startShell();
 		
 		String torCmdString = fileTor.getCanonicalPath() 
 				+ " DataDirectory " + appCacheHome.getCanonicalPath() 
@@ -955,7 +957,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 		debug(torCmdString);
 		
 		SimpleCommand shellTorCommand = new SimpleCommand(torCmdString + " --verify-config");
-		mShellTor.add(shellTorCommand).waitForFinish();
+		shellTor.add(shellTorCommand).waitForFinish();
 		
 		int exitCode = shellTorCommand.getExitCode();
 		String output = shellTorCommand.getOutput();
@@ -968,7 +970,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 		}
 
 		shellTorCommand = new SimpleCommand(torCmdString);
-		mShellTor.add(shellTorCommand).waitForFinish();
+		shellTor.add(shellTorCommand).waitForFinish();
 		
 		exitCode = shellTorCommand.getExitCode();
 		output = shellTorCommand.getOutput();
@@ -1001,6 +1003,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 
 	    }
 		
+		shellTor.close();
+		
 		return true;
     }
     
@@ -2474,6 +2478,5 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 		// TODO Auto-generated method stub
 		return null;
 	}
-   
-   
+
 }





More information about the tor-commits mailing list