use context.stopService() to shutdown TorService instead of custom message
Since running stopService() automatically triggers Service.onDestroy(), there is a nice way to hook in and run the shutdown procedure. This provides an obvious point of entry as well as simplifying the shutdown procedure.
This commit is contained in:
parent
bfb0a80a18
commit
6ac9a2cee6
|
@ -11,7 +11,7 @@
|
||||||
<string name="status_starting_up">Orbot is starting…</string>
|
<string name="status_starting_up">Orbot is starting…</string>
|
||||||
<string name="status_activated">Connected to the Tor network</string>
|
<string name="status_activated">Connected to the Tor network</string>
|
||||||
<string name="status_disabled">Orbot is deactivated</string>
|
<string name="status_disabled">Orbot is deactivated</string>
|
||||||
<string name="status_shutting_down">Orbot is shutting down</string>
|
<string name="status_shutting_down">TorService is shutting down</string>
|
||||||
<string name="tor_process_starting">Starting Tor client…</string>
|
<string name="tor_process_starting">Starting Tor client…</string>
|
||||||
<string name="tor_process_complete">complete.</string>
|
<string name="tor_process_complete">complete.</string>
|
||||||
<string name="tor_process_waiting">waiting.</string>
|
<string name="tor_process_waiting">waiting.</string>
|
||||||
|
|
|
@ -121,22 +121,16 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
|
||||||
torService.setAction(action);
|
torService.setAction(action);
|
||||||
startService(torService);
|
startService(torService);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void stopService ()
|
private void stopTor() {
|
||||||
{
|
Intent torService = new Intent(this, TorService.class);
|
||||||
|
stopService(torService);
|
||||||
Intent torService = new Intent(this, TorService.class);
|
}
|
||||||
stopService(torService);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Our handler for received Intents. This will be called whenever an Intent
|
// Our handler for received Intents. This will be called whenever an Intent
|
||||||
// with an action named "custom-event-name" is broadcasted.
|
// with an action named "custom-event-name" is broadcasted.
|
||||||
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
|
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
// Get extra data included in the Intent
|
// Get extra data included in the Intent
|
||||||
|
@ -425,34 +419,21 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is our attempt to REALLY exit Orbot, and stop the background service
|
* This is our attempt to REALLY exit Orbot, and stop the background service
|
||||||
* However, Android doesn't like people "quitting" apps, and/or our code may not
|
* However, Android doesn't like people "quitting" apps, and/or our code may
|
||||||
* be quite right b/c no matter what we do, it seems like the TorService still exists
|
* not be quite right b/c no matter what we do, it seems like the TorService
|
||||||
**/
|
* still exists
|
||||||
private void doExit ()
|
**/
|
||||||
{
|
private void doExit() {
|
||||||
try {
|
stopTor();
|
||||||
|
|
||||||
//one of the confusing things about all of this code is the multiple
|
// Kill all the wizard activities
|
||||||
//places where things like "stopTor" are called, both in the Activity and the Service
|
setResult(RESULT_CLOSE_ALL);
|
||||||
//not something to tackle in your first iteration, but i thin we can talk about fixing
|
finish();
|
||||||
//terminology but also making sure there are clear distinctions in control
|
}
|
||||||
stopTor();
|
|
||||||
stopService ();
|
|
||||||
|
|
||||||
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
Log.w(TAG, e);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Kill all the wizard activities
|
|
||||||
setResult(RESULT_CLOSE_ALL);
|
|
||||||
finish();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -1223,49 +1204,20 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
|
||||||
msg.getData().putString(HANDLER_TOR_MSG, getString(R.string.status_starting_up));
|
msg.getData().putString(HANDLER_TOR_MSG, getString(R.string.status_starting_up));
|
||||||
mStatusUpdateHandler.sendMessage(msg);
|
mStatusUpdateHandler.sendMessage(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
//now we stop Tor! amazing!
|
|
||||||
private void stopTor () throws RemoteException
|
|
||||||
{
|
|
||||||
sendIntentToService (TorServiceConstants.CMD_STOP);
|
|
||||||
torStatus = TorServiceConstants.STATUS_OFF;
|
|
||||||
Message msg = mStatusUpdateHandler.obtainMessage(TorServiceConstants.DISABLE_TOR_MSG);
|
|
||||||
mStatusUpdateHandler.sendMessage(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
* @see android.view.View.OnClickListener#onClick(android.view.View)
|
|
||||||
*/
|
|
||||||
public boolean onLongClick(View view) {
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
|
|
||||||
if (torStatus == TorServiceConstants.STATUS_OFF)
|
|
||||||
{
|
|
||||||
|
|
||||||
startTor();
|
public boolean onLongClick(View view) {
|
||||||
}
|
try {
|
||||||
else
|
if (torStatus == TorServiceConstants.STATUS_OFF) {
|
||||||
{
|
startTor();
|
||||||
|
} else {
|
||||||
stopTor();
|
stopTor();
|
||||||
stopService ();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
return true;
|
||||||
{
|
} catch (RemoteException e) {
|
||||||
Log.d(TAG,"error onclick",e);
|
Log.d(TAG, "error onclick", e);
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// this is what takes messages or values from the callback threads or other non-mainUI threads
|
// this is what takes messages or values from the callback threads or other non-mainUI threads
|
||||||
//and passes them back into the main UI thread for display to the user
|
//and passes them back into the main UI thread for display to the user
|
||||||
|
|
|
@ -343,8 +343,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
|
||||||
if (action != null) {
|
if (action != null) {
|
||||||
if (action.equals(CMD_START)) {
|
if (action.equals(CMD_START)) {
|
||||||
startTor();
|
startTor();
|
||||||
} else if (action.equals(CMD_STOP)) {
|
// stopTor() is called when the Service is destroyed
|
||||||
stopTor();
|
|
||||||
} else if (action.equals(CMD_NEWNYM)) {
|
} else if (action.equals(CMD_NEWNYM)) {
|
||||||
newIdentity();
|
newIdentity();
|
||||||
} else if (action.equals(CMD_FLUSH)) {
|
} else if (action.equals(CMD_FLUSH)) {
|
||||||
|
@ -371,20 +370,9 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean stopService(Intent name) {
|
public void onDestroy() {
|
||||||
logNotice("TorService is being stopped: " + name);
|
stopTor();
|
||||||
return super.stopService(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDestroy ()
|
|
||||||
{
|
|
||||||
Log.i("TorService", "onDestroy");
|
|
||||||
String msg = ("TorService is being DESTROYED... shutting down!");
|
|
||||||
Log.d(TAG, msg);
|
|
||||||
sendCallbackLogMessage(msg);
|
|
||||||
unregisterReceiver(mNetworkStateReceiver);
|
unregisterReceiver(mNetworkStateReceiver);
|
||||||
clearNotifications ();
|
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -392,9 +380,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
|
||||||
Log.i("TorService", "stopTor");
|
Log.i("TorService", "stopTor");
|
||||||
try {
|
try {
|
||||||
sendCallbackStatus(STATUS_STOPPING);
|
sendCallbackStatus(STATUS_STOPPING);
|
||||||
|
|
||||||
sendCallbackLogMessage(getString(R.string.status_shutting_down));
|
sendCallbackLogMessage(getString(R.string.status_shutting_down));
|
||||||
Log.d(TAG,"Tor is stopping NOW");
|
|
||||||
|
|
||||||
killAllDaemons();
|
killAllDaemons();
|
||||||
|
|
||||||
|
@ -408,13 +394,10 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
|
||||||
shellRoot.close();
|
shellRoot.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
clearNotifications();
|
|
||||||
|
|
||||||
sendCallbackLogMessage(getString(R.string.status_disabled));
|
sendCallbackLogMessage(getString(R.string.status_disabled));
|
||||||
}
|
}
|
||||||
catch (CannotKillException e)
|
catch (CannotKillException e)
|
||||||
{
|
{
|
||||||
Log.d(TAG, "An error occured stopping Tor", e);
|
|
||||||
logNotice("An error occured stopping Tor: " + e.getMessage());
|
logNotice("An error occured stopping Tor: " + e.getMessage());
|
||||||
sendCallbackLogMessage(getString(R.string.unable_to_reset_tor));
|
sendCallbackLogMessage(getString(R.string.unable_to_reset_tor));
|
||||||
showToolbarNotification(getString(R.string.unable_to_reset_tor),
|
showToolbarNotification(getString(R.string.unable_to_reset_tor),
|
||||||
|
@ -422,11 +405,10 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Log.d(TAG, "An error occured stopping Tor",e);
|
|
||||||
logNotice("An error occured stopping Tor: " + e.getMessage());
|
logNotice("An error occured stopping Tor: " + e.getMessage());
|
||||||
sendCallbackLogMessage(getString(R.string.something_bad_happened));
|
sendCallbackLogMessage(getString(R.string.something_bad_happened));
|
||||||
}
|
}
|
||||||
|
clearNotifications();
|
||||||
sendCallbackStatus(STATUS_OFF);
|
sendCallbackStatus(STATUS_OFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -84,7 +84,6 @@ public interface TorServiceConstants {
|
||||||
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_START = "start";
|
||||||
public static final String CMD_STOP = "stop";
|
|
||||||
public static final String CMD_FLUSH = "flush";
|
public static final String CMD_FLUSH = "flush";
|
||||||
public static final String CMD_NEWNYM = "newnym";
|
public static final String CMD_NEWNYM = "newnym";
|
||||||
public static final String CMD_VPN = "vpn";
|
public static final String CMD_VPN = "vpn";
|
||||||
|
|
Loading…
Reference in New Issue