Fix state recognition on restart.

This commit is contained in:
sponge
2012-09-11 12:44:00 +00:00
parent 742df967e2
commit 53caad9f2a
2 changed files with 16 additions and 6 deletions

View File

@ -24,7 +24,7 @@ public class MainActivity extends I2PActivityBase {
private Runnable _updater; private Runnable _updater;
private Runnable _oneShotUpdate; private Runnable _oneShotUpdate;
private String _savedStatus; private String _savedStatus;
private boolean _keep; private boolean _keep = true;
protected static final String PROP_NEW_INSTALL = "i2p.newInstall"; protected static final String PROP_NEW_INSTALL = "i2p.newInstall";
@ -272,13 +272,16 @@ public class MainActivity extends I2PActivityBase {
@Override @Override
public void onBackPressed() { public void onBackPressed() {
RouterContext ctx = getRouterContext(); RouterContext ctx = getRouterContext();
// RouterService svc = _routerService;
_keep = Util.isConnected(this) && ctx != null; _keep = Util.isConnected(this) && ctx != null;
Util.e("*********************************************************");
Util.e("Back pressed, Keep? " + _keep);
Util.e("*********************************************************");
super.onBackPressed(); super.onBackPressed();
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
// RouterContext ctx = getRouterContext();
super.onDestroy(); super.onDestroy();
if (!_keep) { if (!_keep) {
Thread t = new Thread(new KillMe()); Thread t = new Thread(new KillMe());
@ -288,6 +291,9 @@ public class MainActivity extends I2PActivityBase {
private class KillMe implements Runnable { private class KillMe implements Runnable {
public void run() { public void run() {
Util.e("*********************************************************");
Util.e("KillMe started!");
Util.e("*********************************************************");
try { try {
Thread.sleep(500); // is 500ms long enough? Thread.sleep(500); // is 500ms long enough?
} catch (InterruptedException ex) { } catch (InterruptedException ex) {

View File

@ -28,8 +28,8 @@ import net.i2p.util.OrderedProperties;
*/ */
public class RouterService extends Service { public class RouterService extends Service {
// These states persist even if we died... Yuck, it causes issues.
public enum State { public enum State {
INIT, WAITING, STARTING, RUNNING, INIT, WAITING, STARTING, RUNNING,
// unplanned (router stopped itself), next: killSelf() // unplanned (router stopped itself), next: killSelf()
STOPPING, STOPPED, STOPPING, STOPPED,
@ -80,10 +80,13 @@ public class RouterService extends Service {
Intent intent = new Intent(this, RouterService.class); Intent intent = new Intent(this, RouterService.class);
intent.putExtra(EXTRA_RESTART, true); intent.putExtra(EXTRA_RESTART, true);
onStartCommand(intent, 12345, 67890); onStartCommand(intent, 12345, 67890);
} else if(lastState == State.MANUAL_QUITTING || lastState == State.MANUAL_QUITTED) { } else if(lastState == State.MANUAL_QUITTING) {
synchronized(_stateLock) {
setState(State.MANUAL_QUITTED);
stopSelf(); // Die. stopSelf(); // Die.
} }
} }
}
/** /**
* NOT called by system if it restarts us after a crash * NOT called by system if it restarts us after a crash
@ -657,14 +660,15 @@ public class RouterService extends Service {
|| _state == State.STOPPING) { || _state == State.STOPPING) {
Util.i(this + " died of unknown causes"); Util.i(this + " died of unknown causes");
setState(State.STOPPED); setState(State.STOPPED);
stopForeground(true);
stopSelf(); stopSelf();
} else if(_state == State.MANUAL_QUITTING) { } else if(_state == State.MANUAL_QUITTING) {
setState(State.MANUAL_QUITTED); setState(State.MANUAL_QUITTED);
stopForeground(true);
stopSelf(); stopSelf();
} }
} }
} finally { } finally {
stopForeground(true);
_statusBar.remove(); _statusBar.remove();
} }
} }