Browse Source

fixes grac stop

pull/1095/head
hypnosis-i2p 7 years ago
parent
commit
56f6e57118
  1. 60
      android/src/org/purplei2p/i2pd/I2PDActivity.java

60
android/src/org/purplei2p/i2pd/I2PDActivity.java

@ -20,7 +20,7 @@ import android.widget.Toast;
public class I2PDActivity extends Activity { public class I2PDActivity extends Activity {
private static final String TAG = "i2pdActvt"; private static final String TAG = "i2pdActvt";
public static final int GRACEFUL_DELAY_MILLIS = 10 * 60 * 1000; public static final int GRACEFUL_DELAY_MILLIS = 120 * 1000;
private TextView textView; private TextView textView;
@ -55,10 +55,10 @@ public class I2PDActivity extends Activity {
}); });
} }
}; };
private volatile long graceStartedMillis; private static volatile long graceStartedMillis;
private final Object graceStartedMillis_LOCK=new Object(); private static final Object graceStartedMillis_LOCK=new Object();
private String formatGraceTimeRemaining() { private static String formatGraceTimeRemaining() {
long remainingSeconds; long remainingSeconds;
synchronized (graceStartedMillis_LOCK){ synchronized (graceStartedMillis_LOCK){
remainingSeconds=Math.round(Math.max(0,graceStartedMillis+GRACEFUL_DELAY_MILLIS-System.currentTimeMillis())/1000.0D); remainingSeconds=Math.round(Math.max(0,graceStartedMillis+GRACEFUL_DELAY_MILLIS-System.currentTimeMillis())/1000.0D);
@ -79,6 +79,15 @@ public class I2PDActivity extends Activity {
//set the app be foreground //set the app be foreground
doBindService(); doBindService();
final Timer gracefulQuitTimer = getGracefulQuitTimer();
if(gracefulQuitTimer!=null){
long gracefulStopAtMillis;
synchronized (graceStartedMillis_LOCK) {
gracefulStopAtMillis = graceStartedMillis + GRACEFUL_DELAY_MILLIS;
}
rescheduleGraceStop(gracefulQuitTimer, gracefulStopAtMillis);
}
} }
@Override @Override
@ -86,7 +95,7 @@ public class I2PDActivity extends Activity {
super.onDestroy(); super.onDestroy();
textView = null; textView = null;
daemon.removeStateChangeListener(daemonStateUpdatedListener); daemon.removeStateChangeListener(daemonStateUpdatedListener);
cancelGracefulStop(); //cancelGracefulStop();
try{ try{
doUnbindService(); doUnbindService();
}catch(Throwable tr){ }catch(Throwable tr){
@ -94,7 +103,7 @@ public class I2PDActivity extends Activity {
} }
} }
private void cancelGracefulStop() { private static void cancelGracefulStop() {
Timer gracefulQuitTimer = getGracefulQuitTimer(); Timer gracefulQuitTimer = getGracefulQuitTimer();
if(gracefulQuitTimer!=null) { if(gracefulQuitTimer!=null) {
gracefulQuitTimer.cancel(); gracefulQuitTimer.cancel();
@ -205,7 +214,7 @@ public class I2PDActivity extends Activity {
},"stop").start(); },"stop").start();
} }
private volatile Timer gracefulQuitTimer; private static volatile Timer gracefulQuitTimer;
private void i2pdGracefulStop() { private void i2pdGracefulStop() {
if(daemon.getState()==DaemonSingleton.State.stopped){ if(daemon.getState()==DaemonSingleton.State.stopped){
@ -228,11 +237,27 @@ public class I2PDActivity extends Activity {
Log.d(TAG, "grac stopping"); Log.d(TAG, "grac stopping");
if(daemon.isStartedOkay()) { if(daemon.isStartedOkay()) {
daemon.stopAcceptingTunnels(); daemon.stopAcceptingTunnels();
final Timer gracefulQuitTimer = new Timer(true); long gracefulStopAtMillis;
setGracefulQuitTimer(gracefulQuitTimer);
synchronized (graceStartedMillis_LOCK) { synchronized (graceStartedMillis_LOCK) {
graceStartedMillis = System.currentTimeMillis(); graceStartedMillis = System.currentTimeMillis();
gracefulStopAtMillis = graceStartedMillis + GRACEFUL_DELAY_MILLIS;
}
rescheduleGraceStop(null,gracefulStopAtMillis);
}else{
i2pdStop();
}
} catch(Throwable tr) {
Log.e(TAG,"",tr);
}
} }
},"gracInit").start();
}
private void rescheduleGraceStop(Timer gracefulQuitTimerOld, long gracefulStopAtMillis) {
if(gracefulQuitTimerOld!=null)gracefulQuitTimerOld.cancel();
final Timer gracefulQuitTimer = new Timer(true);
setGracefulQuitTimer(gracefulQuitTimer);
gracefulQuitTimer.schedule(new TimerTask(){ gracefulQuitTimer.schedule(new TimerTask(){
@Override @Override
@ -240,7 +265,7 @@ public class I2PDActivity extends Activity {
i2pdStop(); i2pdStop();
} }
}, GRACEFUL_DELAY_MILLIS); }, Math.max(0,gracefulStopAtMillis-System.currentTimeMillis()));
final TimerTask tickerTask = new TimerTask() { final TimerTask tickerTask = new TimerTask() {
@Override @Override
public void run() { public void run() {
@ -248,22 +273,13 @@ public class I2PDActivity extends Activity {
} }
}; };
gracefulQuitTimer.scheduleAtFixedRate(tickerTask,0/*start delay*/,1000/*millis period*/); gracefulQuitTimer.scheduleAtFixedRate(tickerTask,0/*start delay*/,1000/*millis period*/);
}else{
i2pdStop();
}
} catch(Throwable tr) {
Log.e(TAG,"",tr);
}
}
},"gracInit").start();
} }
private Timer getGracefulQuitTimer() { private static Timer getGracefulQuitTimer() {
return gracefulQuitTimer; return gracefulQuitTimer;
} }
private void setGracefulQuitTimer(Timer gracefulQuitTimer) { private static void setGracefulQuitTimer(Timer gracefulQuitTimer) {
this.gracefulQuitTimer = gracefulQuitTimer; I2PDActivity.gracefulQuitTimer = gracefulQuitTimer;
} }
} }

Loading…
Cancel
Save