improves in start/stop logic for background service

This commit is contained in:
Nathan Freitas 2014-11-17 23:28:33 -05:00
parent e3b9dae1c5
commit cf21809052
4 changed files with 91 additions and 71 deletions

View File

@ -106,7 +106,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("log")); new IntentFilter("log"));
startService("init"); startService(TorServiceConstants.CMD_INIT);
} }
// Our handler for received Intents. This will be called whenever an Intent // Our handler for received Intents. This will be called whenever an Intent
@ -154,6 +154,13 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
torService.setAction(action); torService.setAction(action);
startService(torService); startService(torService);
}
private void stopService ()
{
Intent torService = new Intent(this, TorService.class);
stopService(torService);
} }
@ -412,7 +419,9 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
{ {
if (mItemOnOff != null) if (mItemOnOff != null)
mItemOnOff.setTitle(R.string.menu_start); mItemOnOff.setTitle(R.string.menu_start);
stopTor(); stopTor();
stopService ();
} }
@ -472,7 +481,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
//not something to tackle in your first iteration, but i thin we can talk about fixing //not something to tackle in your first iteration, but i thin we can talk about fixing
//terminology but also making sure there are clear distinctions in control //terminology but also making sure there are clear distinctions in control
stopTor(); stopTor();
stopService ();
//onDestroy(); //onDestroy();
@ -855,7 +864,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
} }
else if (request == REQUEST_VPN && response == RESULT_OK) else if (request == REQUEST_VPN && response == RESULT_OK)
{ {
startService("vpn"); startService(TorServiceConstants.CMD_VPN);
} }
} }
@ -871,21 +880,21 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
} }
else else
{ {
startService("vpn"); startService(TorServiceConstants.CMD_VPN);
} }
} }
private boolean flushTransProxy () private boolean flushTransProxy ()
{ {
startService("flush"); startService(TorServiceConstants.CMD_FLUSH);
return true; return true;
} }
private boolean updateSettings () private boolean updateSettings ()
{ {
//todo send service command //todo send service command
startService("update"); startService(TorServiceConstants.CMD_UPDATE);
return true; return true;
} }
@ -1022,7 +1031,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
{ {
startService ("start"); startService (TorServiceConstants.CMD_START);
torStatus = TorServiceConstants.STATUS_CONNECTING; torStatus = TorServiceConstants.STATUS_CONNECTING;
mTxtOrbotLog.setText(""); mTxtOrbotLog.setText("");
@ -1050,7 +1059,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
private void stopTor () throws RemoteException private void stopTor () throws RemoteException
{ {
startService ("stop"); startService (TorServiceConstants.CMD_STOP);
torStatus = TorServiceConstants.STATUS_OFF; torStatus = TorServiceConstants.STATUS_OFF;
// mService.setProfile(TorServiceConstants.STATUS_OFF); // mService.setProfile(TorServiceConstants.STATUS_OFF);
@ -1058,6 +1067,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
mHandler.sendMessage(msg); mHandler.sendMessage(msg);
} }
/* /*
@ -1080,6 +1090,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
{ {
stopTor(); stopTor();
stopService ();
} }
@ -1303,7 +1314,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
public void spinOrbot (float direction) public void spinOrbot (float direction)
{ {
startService ("newnym"); startService (TorServiceConstants.CMD_NEWNYM);
//mService.newIdentity(); //request a new identity //mService.newIdentity(); //request a new identity
//TODO trigger newnym //TODO trigger newnym

View File

@ -377,31 +377,31 @@ public class TorService extends Service implements TorServiceConstants, TorConst
if (action!=null) if (action!=null)
{ {
if(action.equals(Intent.ACTION_BOOT_COMPLETED)||action.equals("start")) if(action.equals(Intent.ACTION_BOOT_COMPLETED)||action.equals(CMD_START))
{ {
setTorProfile(STATUS_ON); setTorProfile(STATUS_ON);
} }
else if (action.equals("stop")) else if (action.equals(CMD_STOP))
{ {
setTorProfile(STATUS_OFF); setTorProfile(STATUS_OFF);
} }
else if (action.equals("init")) else if (action.equals(CMD_INIT))
{ {
sendCallbackStatus(mCurrentStatus); sendCallbackStatus(mCurrentStatus);
} }
else if (action.equals("newnym")) else if (action.equals(CMD_NEWNYM))
{ {
newIdentity(); newIdentity();
} }
else if (action.equals("flush")) else if (action.equals(CMD_FLUSH))
{ {
flushTransparentProxyRules(); flushTransparentProxyRules();
} }
else if (action.equals("update")) else if (action.equals(CMD_UPDATE))
{ {
processSettings(); processSettings();
} }
else if (action.equals("vpn")) else if (action.equals(CMD_VPN))
{ {
startVpnService(); startVpnService();
} }
@ -774,7 +774,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
public void startTor () throws Exception private void startTor () throws Exception
{ {
mCurrentStatus = STATUS_CONNECTING; mCurrentStatus = STATUS_CONNECTING;
@ -1074,6 +1074,9 @@ public class TorService extends Service implements TorServiceConstants, TorConst
int attempt = 0; int attempt = 0;
logNotice( "Waiting for control port...");
while (conn == null && attempt++ < maxTries) while (conn == null && attempt++ < maxTries)
{ {
try try
@ -1098,13 +1101,13 @@ public class TorService extends Service implements TorServiceConstants, TorConst
catch (Exception ce) catch (Exception ce)
{ {
conn = null; conn = null;
logException( "Error connecting to Tor local control port: " + ce.getMessage(),ce); // logException( "Error connecting to Tor local control port: " + ce.getMessage(),ce);
} }
try { try {
logNotice("waiting..."); // logNotice("waiting...");
Thread.sleep(1000); } Thread.sleep(1000); }
catch (Exception e){} catch (Exception e){}
@ -1264,7 +1267,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
{ {
if (fileControlPort.exists()) if (fileControlPort.exists())
{ {
logNotice("Reading control port config file: " + fileControlPort.getCanonicalPath()); debug("Reading control port config file: " + fileControlPort.getCanonicalPath());
BufferedReader bufferedReader = new BufferedReader(new FileReader(fileControlPort)); BufferedReader bufferedReader = new BufferedReader(new FileReader(fileControlPort));
String line = bufferedReader.readLine(); String line = bufferedReader.readLine();
@ -1284,7 +1287,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
else else
{ {
logNotice("Control Port config file does not yet exist (waiting for tor): " + fileControlPort.getCanonicalPath()); debug("Control Port config file does not yet exist (waiting for tor): " + fileControlPort.getCanonicalPath());
} }
@ -1292,11 +1295,11 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
catch (FileNotFoundException e) catch (FileNotFoundException e)
{ {
logNotice("unable to get control port; file not found"); debug("unable to get control port; file not found");
} }
catch (Exception e) catch (Exception e)
{ {
logNotice("unable to read control port config file"); debug("unable to read control port config file");
} }
return result; return result;
@ -1376,17 +1379,28 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
public void setTorProfile(int profile) { public void setTorProfile(int newState) {
if (profile == STATUS_ON && mCurrentStatus != STATUS_ON) if (newState == STATUS_ON)
{ {
if (mCurrentStatus == STATUS_OFF)
{
sendCallbackLogMessage (getString(R.string.status_starting_up)); sendCallbackLogMessage (getString(R.string.status_starting_up));
try try
{ {
startTor();
boolean found = findExistingProc ();
if (!found)
{
killProcess(fileTor);
killProcess(filePolipo);
startTor();
}
} }
catch (Exception e) catch (Exception e)
{ {
@ -1398,8 +1412,12 @@ public class TorService extends Service implements TorServiceConstants, TorConst
showToolbarNotification(getString(R.string.unable_to_start_tor) + ": " + e.getMessage(), ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr); showToolbarNotification(getString(R.string.unable_to_start_tor) + ": " + e.getMessage(), ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr);
stopTor(); stopTor();
} }
} }
else if (profile == STATUS_OFF && mCurrentStatus != STATUS_OFF) }
else if (newState == STATUS_OFF)
{
if (mCurrentStatus == STATUS_ON)
{ {
sendCallbackLogMessage (getString(R.string.status_shutting_down)); sendCallbackLogMessage (getString(R.string.status_shutting_down));
@ -1407,6 +1425,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
mCurrentStatus = STATUS_OFF; mCurrentStatus = STATUS_OFF;
sendCallbackStatus(mCurrentStatus); sendCallbackStatus(mCurrentStatus);
}
} }
} }
@ -1734,27 +1753,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
public boolean checkAndInitImpl ()
{
if (fileTor != null)
{
try {
if (TorServiceUtils.findProcessId(fileTor.getCanonicalPath()) != -1)
{
initialize();
return true;
}
} catch (IOException e) {
logException("error init Tor", e);
}
}
return false;
}
public void processSettings () public void processSettings ()
{ {

View File

@ -76,6 +76,17 @@ public interface TorServiceConstants {
public static final int DISABLE_TOR_MSG = 3; public static final int DISABLE_TOR_MSG = 3;
public static final int LOG_MSG = 4; public static final int LOG_MSG = 4;
public static final String CMD_START = "start";
public static final String CMD_STOP = "stop";
public static final String CMD_FLUSH = "flush";
public static final String CMD_NEWNYM = "newnym";
public static final String CMD_INIT = "init";
public static final String CMD_VPN = "vpn";
public static final String CMD_UPDATE = "update";
public static final String BINARY_TOR_VERSION = "0.2.5.10-openssl1.0.1i-nonPIE-polipofix"; public static final String BINARY_TOR_VERSION = "0.2.5.10-openssl1.0.1i-nonPIE-polipofix";
public static final String PREF_BINARY_TOR_VERSION_INSTALLED = "BINARY_TOR_VERSION_INSTALLED"; public static final String PREF_BINARY_TOR_VERSION_INSTALLED = "BINARY_TOR_VERSION_INSTALLED";

View File

@ -108,7 +108,7 @@ public class TorServiceUtils implements TorServiceConstants {
} }
} }
procPs.destroy(); try { procPs.destroy(); } catch (Exception e) {} // try to destroy just to make sure we clean it up
return procId; return procId;