improves in start/stop logic for background service
This commit is contained in:
parent
e3b9dae1c5
commit
cf21809052
|
@ -106,7 +106,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
|
|||
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
|
||||
new IntentFilter("log"));
|
||||
|
||||
startService("init");
|
||||
startService(TorServiceConstants.CMD_INIT);
|
||||
}
|
||||
|
||||
// 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);
|
||||
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)
|
||||
mItemOnOff.setTitle(R.string.menu_start);
|
||||
|
||||
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
|
||||
//terminology but also making sure there are clear distinctions in control
|
||||
stopTor();
|
||||
|
||||
stopService ();
|
||||
//onDestroy();
|
||||
|
||||
|
||||
|
@ -855,7 +864,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
|
|||
}
|
||||
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
|
||||
{
|
||||
startService("vpn");
|
||||
startService(TorServiceConstants.CMD_VPN);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private boolean flushTransProxy ()
|
||||
{
|
||||
startService("flush");
|
||||
startService(TorServiceConstants.CMD_FLUSH);
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean updateSettings ()
|
||||
{
|
||||
//todo send service command
|
||||
startService("update");
|
||||
startService(TorServiceConstants.CMD_UPDATE);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -1022,7 +1031,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
|
|||
{
|
||||
|
||||
|
||||
startService ("start");
|
||||
startService (TorServiceConstants.CMD_START);
|
||||
torStatus = TorServiceConstants.STATUS_CONNECTING;
|
||||
|
||||
mTxtOrbotLog.setText("");
|
||||
|
@ -1050,7 +1059,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
|
|||
private void stopTor () throws RemoteException
|
||||
{
|
||||
|
||||
startService ("stop");
|
||||
startService (TorServiceConstants.CMD_STOP);
|
||||
torStatus = TorServiceConstants.STATUS_OFF;
|
||||
|
||||
// mService.setProfile(TorServiceConstants.STATUS_OFF);
|
||||
|
@ -1058,6 +1067,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
|
|||
mHandler.sendMessage(msg);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -1080,6 +1090,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
|
|||
{
|
||||
|
||||
stopTor();
|
||||
stopService ();
|
||||
|
||||
}
|
||||
|
||||
|
@ -1303,7 +1314,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
|
|||
|
||||
public void spinOrbot (float direction)
|
||||
{
|
||||
startService ("newnym");
|
||||
startService (TorServiceConstants.CMD_NEWNYM);
|
||||
|
||||
//mService.newIdentity(); //request a new identity
|
||||
//TODO trigger newnym
|
||||
|
|
|
@ -377,31 +377,31 @@ public class TorService extends Service implements TorServiceConstants, TorConst
|
|||
|
||||
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);
|
||||
}
|
||||
else if (action.equals("stop"))
|
||||
else if (action.equals(CMD_STOP))
|
||||
{
|
||||
setTorProfile(STATUS_OFF);
|
||||
}
|
||||
else if (action.equals("init"))
|
||||
else if (action.equals(CMD_INIT))
|
||||
{
|
||||
sendCallbackStatus(mCurrentStatus);
|
||||
}
|
||||
else if (action.equals("newnym"))
|
||||
else if (action.equals(CMD_NEWNYM))
|
||||
{
|
||||
newIdentity();
|
||||
}
|
||||
else if (action.equals("flush"))
|
||||
else if (action.equals(CMD_FLUSH))
|
||||
{
|
||||
flushTransparentProxyRules();
|
||||
}
|
||||
else if (action.equals("update"))
|
||||
else if (action.equals(CMD_UPDATE))
|
||||
{
|
||||
processSettings();
|
||||
}
|
||||
else if (action.equals("vpn"))
|
||||
else if (action.equals(CMD_VPN))
|
||||
{
|
||||
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;
|
||||
|
@ -1074,6 +1074,9 @@ public class TorService extends Service implements TorServiceConstants, TorConst
|
|||
|
||||
int attempt = 0;
|
||||
|
||||
|
||||
logNotice( "Waiting for control port...");
|
||||
|
||||
while (conn == null && attempt++ < maxTries)
|
||||
{
|
||||
try
|
||||
|
@ -1098,13 +1101,13 @@ public class TorService extends Service implements TorServiceConstants, TorConst
|
|||
catch (Exception ce)
|
||||
{
|
||||
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 {
|
||||
logNotice("waiting...");
|
||||
// logNotice("waiting...");
|
||||
Thread.sleep(1000); }
|
||||
catch (Exception e){}
|
||||
|
||||
|
@ -1264,7 +1267,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
|
|||
{
|
||||
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));
|
||||
String line = bufferedReader.readLine();
|
||||
|
||||
|
@ -1284,7 +1287,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
|
|||
}
|
||||
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)
|
||||
{
|
||||
logNotice("unable to get control port; file not found");
|
||||
debug("unable to get control port; file not found");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logNotice("unable to read control port config file");
|
||||
debug("unable to read control port config file");
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -1376,37 +1379,53 @@ 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)
|
||||
{
|
||||
|
||||
sendCallbackLogMessage (getString(R.string.status_starting_up));
|
||||
if (mCurrentStatus == STATUS_OFF)
|
||||
{
|
||||
sendCallbackLogMessage (getString(R.string.status_starting_up));
|
||||
|
||||
try
|
||||
{
|
||||
startTor();
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
logException("Unable to start Tor: " + e.toString(),e);
|
||||
mCurrentStatus = STATUS_OFF;
|
||||
sendCallbackStatus(mCurrentStatus);
|
||||
boolean found = findExistingProc ();
|
||||
|
||||
showToolbarNotification(getString(R.string.unable_to_start_tor) + ": " + e.getMessage(), ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr);
|
||||
stopTor();
|
||||
}
|
||||
if (!found)
|
||||
{
|
||||
killProcess(fileTor);
|
||||
killProcess(filePolipo);
|
||||
|
||||
startTor();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
||||
logException("Unable to start Tor: " + e.toString(),e);
|
||||
mCurrentStatus = STATUS_OFF;
|
||||
sendCallbackStatus(mCurrentStatus);
|
||||
|
||||
showToolbarNotification(getString(R.string.unable_to_start_tor) + ": " + e.getMessage(), ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr);
|
||||
stopTor();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
else if (profile == STATUS_OFF && mCurrentStatus != STATUS_OFF)
|
||||
else if (newState == STATUS_OFF)
|
||||
{
|
||||
sendCallbackLogMessage (getString(R.string.status_shutting_down));
|
||||
if (mCurrentStatus == STATUS_ON)
|
||||
{
|
||||
sendCallbackLogMessage (getString(R.string.status_shutting_down));
|
||||
|
||||
stopTor();
|
||||
stopTor();
|
||||
|
||||
mCurrentStatus = STATUS_OFF;
|
||||
sendCallbackStatus(mCurrentStatus);
|
||||
mCurrentStatus = STATUS_OFF;
|
||||
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 ()
|
||||
{
|
||||
|
||||
|
|
|
@ -76,6 +76,17 @@ public interface TorServiceConstants {
|
|||
public static final int DISABLE_TOR_MSG = 3;
|
||||
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 PREF_BINARY_TOR_VERSION_INSTALLED = "BINARY_TOR_VERSION_INSTALLED";
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
Loading…
Reference in New Issue