|
|
|
@ -19,14 +19,13 @@ import android.widget.TextView;
@@ -19,14 +19,13 @@ import android.widget.TextView;
|
|
|
|
|
import android.widget.Toast; |
|
|
|
|
|
|
|
|
|
public class I2PDActivity extends Activity { |
|
|
|
|
private static final String TAG = "i2pdActvt"; |
|
|
|
|
public static final int GRACEFUL_DELAY_MILLIS = 10 * 60 * 1000; |
|
|
|
|
private static final String TAG = "i2pd"; |
|
|
|
|
|
|
|
|
|
private TextView textView; |
|
|
|
|
|
|
|
|
|
private static final DaemonSingleton daemon = DaemonSingleton.getInstance(); |
|
|
|
|
private final DaemonSingleton daemon = DaemonSingleton.getInstance(); |
|
|
|
|
|
|
|
|
|
private final DaemonSingleton.StateUpdateListener daemonStateUpdatedListener = |
|
|
|
|
private DaemonSingleton.StateUpdateListener daemonStateUpdatedListener = |
|
|
|
|
new DaemonSingleton.StateUpdateListener() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@ -43,11 +42,8 @@ public class I2PDActivity extends Activity {
@@ -43,11 +42,8 @@ public class I2PDActivity extends Activity {
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
DaemonSingleton.State state = daemon.getState(); |
|
|
|
|
textView.setText( |
|
|
|
|
String.valueOf(state)+ |
|
|
|
|
(DaemonSingleton.State.startFailed.equals(state)?": "+daemon.getDaemonStartResult():"")+ |
|
|
|
|
(DaemonSingleton.State.gracefulShutdownInProgress.equals(state)?": "+formatGraceTimeRemaining()+" "+getText(R.string.remaining):"") |
|
|
|
|
); |
|
|
|
|
textView.setText(String.valueOf(state)+ |
|
|
|
|
(DaemonSingleton.State.startFailed.equals(state)?": "+daemon.getDaemonStartResult():"")); |
|
|
|
|
} catch (Throwable tr) { |
|
|
|
|
Log.e(TAG,"error ignored",tr); |
|
|
|
|
} |
|
|
|
@ -55,18 +51,6 @@ public class I2PDActivity extends Activity {
@@ -55,18 +51,6 @@ public class I2PDActivity extends Activity {
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
private static volatile long graceStartedMillis; |
|
|
|
|
private static final Object graceStartedMillis_LOCK=new Object(); |
|
|
|
|
|
|
|
|
|
private static String formatGraceTimeRemaining() { |
|
|
|
|
long remainingSeconds; |
|
|
|
|
synchronized (graceStartedMillis_LOCK){ |
|
|
|
|
remainingSeconds=Math.round(Math.max(0,graceStartedMillis+GRACEFUL_DELAY_MILLIS-System.currentTimeMillis())/1000.0D); |
|
|
|
|
} |
|
|
|
|
long remainingMinutes=(long)Math.floor(remainingSeconds/60.0D); |
|
|
|
|
long remSec=remainingSeconds-remainingMinutes*60; |
|
|
|
|
return remainingMinutes+":"+(remSec/10)+remSec%10; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onCreate(Bundle savedInstanceState) { |
|
|
|
@ -74,41 +58,32 @@ public class I2PDActivity extends Activity {
@@ -74,41 +58,32 @@ public class I2PDActivity extends Activity {
|
|
|
|
|
|
|
|
|
|
textView = new TextView(this); |
|
|
|
|
setContentView(textView); |
|
|
|
|
daemon.addStateChangeListener(daemonStateUpdatedListener); |
|
|
|
|
DaemonSingleton.getInstance().addStateChangeListener(daemonStateUpdatedListener); |
|
|
|
|
daemonStateUpdatedListener.daemonStateUpdate(); |
|
|
|
|
|
|
|
|
|
//set the app be foreground
|
|
|
|
|
doBindService(); |
|
|
|
|
|
|
|
|
|
final Timer gracefulQuitTimer = getGracefulQuitTimer(); |
|
|
|
|
if(gracefulQuitTimer!=null){ |
|
|
|
|
long gracefulStopAtMillis; |
|
|
|
|
synchronized (graceStartedMillis_LOCK) { |
|
|
|
|
gracefulStopAtMillis = graceStartedMillis + GRACEFUL_DELAY_MILLIS; |
|
|
|
|
} |
|
|
|
|
rescheduleGraceStop(gracefulQuitTimer, gracefulStopAtMillis); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void onDestroy() { |
|
|
|
|
super.onDestroy(); |
|
|
|
|
textView = null; |
|
|
|
|
daemon.removeStateChangeListener(daemonStateUpdatedListener); |
|
|
|
|
//cancelGracefulStop();
|
|
|
|
|
try{ |
|
|
|
|
doUnbindService(); |
|
|
|
|
}catch(Throwable tr){ |
|
|
|
|
Log.e(TAG, "", tr); |
|
|
|
|
} |
|
|
|
|
localDestroy(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static void cancelGracefulStop() { |
|
|
|
|
private void localDestroy() { |
|
|
|
|
textView = null; |
|
|
|
|
DaemonSingleton.getInstance().removeStateChangeListener(daemonStateUpdatedListener); |
|
|
|
|
Timer gracefulQuitTimer = getGracefulQuitTimer(); |
|
|
|
|
if(gracefulQuitTimer!=null) { |
|
|
|
|
gracefulQuitTimer.cancel(); |
|
|
|
|
setGracefulQuitTimer(null); |
|
|
|
|
} |
|
|
|
|
// try{
|
|
|
|
|
// doUnbindService();
|
|
|
|
|
// }catch(Throwable tr){
|
|
|
|
|
// Log.e(TAG, "", tr);
|
|
|
|
|
// }
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private CharSequence throwableToString(Throwable tr) { |
|
|
|
@ -147,29 +122,26 @@ public class I2PDActivity extends Activity {
@@ -147,29 +122,26 @@ public class I2PDActivity extends Activity {
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static volatile boolean mIsBound; |
|
|
|
|
private boolean mIsBound; |
|
|
|
|
|
|
|
|
|
private void doBindService() { |
|
|
|
|
synchronized (I2PDActivity.class) { |
|
|
|
|
if (mIsBound) return; |
|
|
|
|
private synchronized void doBindService() { |
|
|
|
|
if(mIsBound)return; |
|
|
|
|
// Establish a connection with the service. We use an explicit
|
|
|
|
|
// class name because we want a specific service implementation that
|
|
|
|
|
// we know will be running in our own process (and thus won't be
|
|
|
|
|
// supporting component replacement by other applications).
|
|
|
|
|
bindService(new Intent(this, ForegroundService.class), mConnection, Context.BIND_AUTO_CREATE); |
|
|
|
|
bindService(new Intent(this, |
|
|
|
|
ForegroundService.class), mConnection, Context.BIND_AUTO_CREATE); |
|
|
|
|
mIsBound = true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void doUnbindService() { |
|
|
|
|
synchronized (I2PDActivity.class) { |
|
|
|
|
if (mIsBound) { |
|
|
|
|
// Detach our existing connection.
|
|
|
|
|
unbindService(mConnection); |
|
|
|
|
mIsBound = false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) { |
|
|
|
@ -198,12 +170,6 @@ public class I2PDActivity extends Activity {
@@ -198,12 +170,6 @@ public class I2PDActivity extends Activity {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void i2pdStop() { |
|
|
|
|
cancelGracefulStop(); |
|
|
|
|
new Thread(new Runnable(){ |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void run() { |
|
|
|
|
Log.d(TAG, "stopping"); |
|
|
|
|
try{ |
|
|
|
|
daemon.stopDaemon(); |
|
|
|
|
}catch (Throwable tr) { |
|
|
|
@ -211,12 +177,9 @@ public class I2PDActivity extends Activity {
@@ -211,12 +177,9 @@ public class I2PDActivity extends Activity {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
},"stop").start(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static volatile Timer gracefulQuitTimer; |
|
|
|
|
|
|
|
|
|
private void i2pdGracefulStop() { |
|
|
|
|
private Timer gracefulQuitTimer; |
|
|
|
|
private final Object gracefulQuitTimerLock = new Object(); |
|
|
|
|
private synchronized void i2pdGracefulStop() { |
|
|
|
|
if(daemon.getState()==DaemonSingleton.State.stopped){ |
|
|
|
|
Toast.makeText(this, R.string.already_stopped, |
|
|
|
|
Toast.LENGTH_SHORT).show(); |
|
|
|
@ -237,12 +200,16 @@ public class I2PDActivity extends Activity {
@@ -237,12 +200,16 @@ public class I2PDActivity extends Activity {
|
|
|
|
|
Log.d(TAG, "grac stopping"); |
|
|
|
|
if(daemon.isStartedOkay()) { |
|
|
|
|
daemon.stopAcceptingTunnels(); |
|
|
|
|
long gracefulStopAtMillis; |
|
|
|
|
synchronized (graceStartedMillis_LOCK) { |
|
|
|
|
graceStartedMillis = System.currentTimeMillis(); |
|
|
|
|
gracefulStopAtMillis = graceStartedMillis + GRACEFUL_DELAY_MILLIS; |
|
|
|
|
Timer gracefulQuitTimer = new Timer(true); |
|
|
|
|
setGracefulQuitTimer(gracefulQuitTimer); |
|
|
|
|
gracefulQuitTimer.schedule(new TimerTask(){ |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void run() { |
|
|
|
|
i2pdStop(); |
|
|
|
|
} |
|
|
|
|
rescheduleGraceStop(null,gracefulStopAtMillis); |
|
|
|
|
|
|
|
|
|
}, 10*60*1000/*milliseconds*/); |
|
|
|
|
}else{ |
|
|
|
|
i2pdStop(); |
|
|
|
|
} |
|
|
|
@ -251,35 +218,18 @@ public class I2PDActivity extends Activity {
@@ -251,35 +218,18 @@ public class I2PDActivity extends Activity {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
},"gracInit").start(); |
|
|
|
|
},"gracQuitInit").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(){ |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void run() { |
|
|
|
|
i2pdStop(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}, Math.max(0,gracefulStopAtMillis-System.currentTimeMillis())); |
|
|
|
|
final TimerTask tickerTask = new TimerTask() { |
|
|
|
|
@Override |
|
|
|
|
public void run() { |
|
|
|
|
daemonStateUpdatedListener.daemonStateUpdate(); |
|
|
|
|
private Timer getGracefulQuitTimer() { |
|
|
|
|
synchronized (gracefulQuitTimerLock) { |
|
|
|
|
return gracefulQuitTimer; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
gracefulQuitTimer.scheduleAtFixedRate(tickerTask,0/*start delay*/,1000/*millis period*/); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static Timer getGracefulQuitTimer() { |
|
|
|
|
return gracefulQuitTimer; |
|
|
|
|
private void setGracefulQuitTimer(Timer gracefulQuitTimer) { |
|
|
|
|
synchronized (gracefulQuitTimerLock) { |
|
|
|
|
this.gracefulQuitTimer = gracefulQuitTimer; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static void setGracefulQuitTimer(Timer gracefulQuitTimer) { |
|
|
|
|
I2PDActivity.gracefulQuitTimer = gracefulQuitTimer; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|