changes to improve longevity of background service/Tor process

This commit is contained in:
Nathan Freitas 2014-11-24 13:11:38 -05:00
parent 611558218d
commit 87d5ac9c44
2 changed files with 35 additions and 30 deletions

View File

@ -1,12 +1,12 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.torproject.android" package="org.torproject.android"
android:versionName="14.1.1" android:versionName="14.1.3-PIE"
android:versionCode="128" android:versionCode="130"
android:installLocation="auto" 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> <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" /> <uses-permission android:name="android.permission.INTERNET" />
@ -23,6 +23,7 @@
android:allowBackup="false" android:allowBackup="false"
android:allowClearUserData="true" android:allowClearUserData="true"
android:persistent="true" android:persistent="true"
android:stopWithTask="false"
> >
<activity android:name=".Orbot" android:configChanges="orientation|screenSize" <activity android:name=".Orbot" android:configChanges="orientation|screenSize"
@ -85,6 +86,7 @@
<service android:enabled="true" <service android:enabled="true"
android:name=".service.TorService" android:name=".service.TorService"
android:permission="org.torproject.android.MANAGE_TOR" android:permission="org.torproject.android.MANAGE_TOR"
android:stopWithTask="false"
> >
<intent-filter> <intent-filter>
<action android:name="org.torproject.android.service.ITorService" /> <action android:name="org.torproject.android.service.ITorService" />

View File

@ -58,6 +58,7 @@ import org.torproject.android.vpn.OrbotVpnService;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.app.Application; import android.app.Application;
import android.app.IntentService;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
@ -142,7 +143,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
private boolean mTransProxyNetworkRefresh = false; private boolean mTransProxyNetworkRefresh = false;
private ExecutorService mExecutor = Executors.newCachedThreadPool(); private ExecutorService mExecutor = Executors.newCachedThreadPool();
public void debug(String msg) public void debug(String msg)
{ {
if (ENABLE_DEBUG_LOG) if (ENABLE_DEBUG_LOG)
@ -330,8 +331,10 @@ public class TorService extends Service implements TorServiceConstants, TorConst
{ {
startForeground(NOTIFY_ID, mNotification); startForeground(NOTIFY_ID, mNotification);
} }
else
mNotificationManager.notify(NOTIFY_ID, mNotification); {
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) { public int onStartCommand(Intent intent, int flags, int startId) {
try new Thread (new TorStarter(intent)).start();
{
return START_REDELIVER_INTENT;
new Thread (new TorStarter(intent)).start();
}
catch (Exception e)
{
logException ("Error starting service",e);
return Service.START_NOT_STICKY;
}
return Service.START_REDELIVER_INTENT;
} }
@ -387,6 +380,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
else if (action.equals(CMD_INIT)) else if (action.equals(CMD_INIT))
{ {
initialize();
sendCallbackStatus(mCurrentStatus); sendCallbackStatus(mCurrentStatus);
} }
else if (action.equals(CMD_NEWNYM)) 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"); 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 () public void onDestroy ()
{ {
super.onDestroy(); super.onDestroy();
@ -430,6 +433,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
unregisterReceiver(mNetworkStateReceiver); unregisterReceiver(mNetworkStateReceiver);
clearNotifications ();
} }
private void stopTor () private void stopTor ()
@ -930,22 +935,19 @@ public class TorService extends Service implements TorServiceConstants, TorConst
return true; return true;
} }
Shell mShellTor;
private boolean runTorShellCmd() throws Exception private boolean runTorShellCmd() throws Exception
{ {
Shell shellTor;
String torrcPath = new File(appBinHome, TORRC_ASSET_KEY).getCanonicalPath(); String torrcPath = new File(appBinHome, TORRC_ASSET_KEY).getCanonicalPath();
updateTorConfigFile(); updateTorConfigFile();
sendCallbackLogMessage(getString(R.string.status_starting_up)); sendCallbackLogMessage(getString(R.string.status_starting_up));
if (mShellTor != null)
mShellTor.close();
//start Tor in the background //start Tor in the background
mShellTor = Shell.startShell(); shellTor = Shell.startShell();
String torCmdString = fileTor.getCanonicalPath() String torCmdString = fileTor.getCanonicalPath()
+ " DataDirectory " + appCacheHome.getCanonicalPath() + " DataDirectory " + appCacheHome.getCanonicalPath()
@ -955,7 +957,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
debug(torCmdString); debug(torCmdString);
SimpleCommand shellTorCommand = new SimpleCommand(torCmdString + " --verify-config"); SimpleCommand shellTorCommand = new SimpleCommand(torCmdString + " --verify-config");
mShellTor.add(shellTorCommand).waitForFinish(); shellTor.add(shellTorCommand).waitForFinish();
int exitCode = shellTorCommand.getExitCode(); int exitCode = shellTorCommand.getExitCode();
String output = shellTorCommand.getOutput(); String output = shellTorCommand.getOutput();
@ -968,7 +970,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
shellTorCommand = new SimpleCommand(torCmdString); shellTorCommand = new SimpleCommand(torCmdString);
mShellTor.add(shellTorCommand).waitForFinish(); shellTor.add(shellTorCommand).waitForFinish();
exitCode = shellTorCommand.getExitCode(); exitCode = shellTorCommand.getExitCode();
output = shellTorCommand.getOutput(); output = shellTorCommand.getOutput();
@ -1001,6 +1003,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
shellTor.close();
return true; return true;
} }
@ -2474,6 +2478,5 @@ public class TorService extends Service implements TorServiceConstants, TorConst
// TODO Auto-generated method stub // TODO Auto-generated method stub
return null; return null;
} }
} }