|
|
|
@ -19,13 +19,14 @@ import android.widget.TextView;
@@ -19,13 +19,14 @@ import android.widget.TextView;
|
|
|
|
|
import android.widget.Toast; |
|
|
|
|
|
|
|
|
|
public class I2PDActivity extends Activity { |
|
|
|
|
private static final String TAG = "i2pd"; |
|
|
|
|
private static final String TAG = "i2pdActvt"; |
|
|
|
|
public static final int GRACEFUL_DELAY_MILLIS = 10 * 60 * 1000; |
|
|
|
|
|
|
|
|
|
private TextView textView; |
|
|
|
|
|
|
|
|
|
private final DaemonSingleton daemon = DaemonSingleton.getInstance(); |
|
|
|
|
private static final DaemonSingleton daemon = DaemonSingleton.getInstance(); |
|
|
|
|
|
|
|
|
|
private DaemonSingleton.StateUpdateListener daemonStateUpdatedListener = |
|
|
|
|
private final DaemonSingleton.StateUpdateListener daemonStateUpdatedListener = |
|
|
|
|
new DaemonSingleton.StateUpdateListener() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@ -42,8 +43,11 @@ public class I2PDActivity extends Activity {
@@ -42,8 +43,11 @@ public class I2PDActivity extends Activity {
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
DaemonSingleton.State state = daemon.getState(); |
|
|
|
|
textView.setText(String.valueOf(state)+ |
|
|
|
|
(DaemonSingleton.State.startFailed.equals(state)?": "+daemon.getDaemonStartResult():"")); |
|
|
|
|
textView.setText( |
|
|
|
|
String.valueOf(state)+ |
|
|
|
|
(DaemonSingleton.State.startFailed.equals(state)?": "+daemon.getDaemonStartResult():"")+ |
|
|
|
|
(DaemonSingleton.State.gracefulShutdownInProgress.equals(state)?": "+formatGraceTimeRemaining()+" "+getText(R.string.remaining):"") |
|
|
|
|
); |
|
|
|
|
} catch (Throwable tr) { |
|
|
|
|
Log.e(TAG,"error ignored",tr); |
|
|
|
|
} |
|
|
|
@ -51,6 +55,18 @@ public class I2PDActivity extends Activity {
@@ -51,6 +55,18 @@ 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) { |
|
|
|
@ -58,32 +74,41 @@ public class I2PDActivity extends Activity {
@@ -58,32 +74,41 @@ public class I2PDActivity extends Activity {
|
|
|
|
|
|
|
|
|
|
textView = new TextView(this); |
|
|
|
|
setContentView(textView); |
|
|
|
|
DaemonSingleton.getInstance().addStateChangeListener(daemonStateUpdatedListener); |
|
|
|
|
daemon.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(); |
|
|
|
|
localDestroy(); |
|
|
|
|
textView = null; |
|
|
|
|
daemon.removeStateChangeListener(daemonStateUpdatedListener); |
|
|
|
|
//cancelGracefulStop();
|
|
|
|
|
try{ |
|
|
|
|
doUnbindService(); |
|
|
|
|
}catch(Throwable tr){ |
|
|
|
|
Log.e(TAG, "", tr); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void localDestroy() { |
|
|
|
|
textView = null; |
|
|
|
|
DaemonSingleton.getInstance().removeStateChangeListener(daemonStateUpdatedListener); |
|
|
|
|
private static void cancelGracefulStop() { |
|
|
|
|
Timer gracefulQuitTimer = getGracefulQuitTimer(); |
|
|
|
|
if(gracefulQuitTimer!=null) { |
|
|
|
|
gracefulQuitTimer.cancel(); |
|
|
|
|
setGracefulQuitTimer(null); |
|
|
|
|
} |
|
|
|
|
// try{
|
|
|
|
|
// doUnbindService();
|
|
|
|
|
// }catch(Throwable tr){
|
|
|
|
|
// Log.e(TAG, "", tr);
|
|
|
|
|
// }
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private CharSequence throwableToString(Throwable tr) { |
|
|
|
@ -122,26 +147,29 @@ public class I2PDActivity extends Activity {
@@ -122,26 +147,29 @@ public class I2PDActivity extends Activity {
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean mIsBound; |
|
|
|
|
private static volatile boolean mIsBound; |
|
|
|
|
|
|
|
|
|
private synchronized void doBindService() { |
|
|
|
|
if(mIsBound)return; |
|
|
|
|
private void doBindService() { |
|
|
|
|
synchronized (I2PDActivity.class) { |
|
|
|
|
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) { |
|
|
|
@ -170,6 +198,12 @@ public class I2PDActivity extends Activity {
@@ -170,6 +198,12 @@ 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) { |
|
|
|
@ -177,9 +211,12 @@ public class I2PDActivity extends Activity {
@@ -177,9 +211,12 @@ public class I2PDActivity extends Activity {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Timer gracefulQuitTimer; |
|
|
|
|
private final Object gracefulQuitTimerLock = new Object(); |
|
|
|
|
private synchronized void i2pdGracefulStop() { |
|
|
|
|
},"stop").start(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static volatile Timer gracefulQuitTimer; |
|
|
|
|
|
|
|
|
|
private void i2pdGracefulStop() { |
|
|
|
|
if(daemon.getState()==DaemonSingleton.State.stopped){ |
|
|
|
|
Toast.makeText(this, R.string.already_stopped, |
|
|
|
|
Toast.LENGTH_SHORT).show(); |
|
|
|
@ -200,16 +237,12 @@ public class I2PDActivity extends Activity {
@@ -200,16 +237,12 @@ public class I2PDActivity extends Activity {
|
|
|
|
|
Log.d(TAG, "grac stopping"); |
|
|
|
|
if(daemon.isStartedOkay()) { |
|
|
|
|
daemon.stopAcceptingTunnels(); |
|
|
|
|
Timer gracefulQuitTimer = new Timer(true); |
|
|
|
|
setGracefulQuitTimer(gracefulQuitTimer); |
|
|
|
|
gracefulQuitTimer.schedule(new TimerTask(){ |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void run() { |
|
|
|
|
i2pdStop(); |
|
|
|
|
long gracefulStopAtMillis; |
|
|
|
|
synchronized (graceStartedMillis_LOCK) { |
|
|
|
|
graceStartedMillis = System.currentTimeMillis(); |
|
|
|
|
gracefulStopAtMillis = graceStartedMillis + GRACEFUL_DELAY_MILLIS; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}, 10*60*1000/*milliseconds*/); |
|
|
|
|
rescheduleGraceStop(null,gracefulStopAtMillis); |
|
|
|
|
}else{ |
|
|
|
|
i2pdStop(); |
|
|
|
|
} |
|
|
|
@ -218,18 +251,35 @@ public class I2PDActivity extends Activity {
@@ -218,18 +251,35 @@ public class I2PDActivity extends Activity {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
},"gracQuitInit").start(); |
|
|
|
|
},"gracInit").start(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Timer getGracefulQuitTimer() { |
|
|
|
|
synchronized (gracefulQuitTimerLock) { |
|
|
|
|
return gracefulQuitTimer; |
|
|
|
|
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(); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
gracefulQuitTimer.scheduleAtFixedRate(tickerTask,0/*start delay*/,1000/*millis period*/); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void setGracefulQuitTimer(Timer gracefulQuitTimer) { |
|
|
|
|
synchronized (gracefulQuitTimerLock) { |
|
|
|
|
this.gracefulQuitTimer = gracefulQuitTimer; |
|
|
|
|
private static Timer getGracefulQuitTimer() { |
|
|
|
|
return gracefulQuitTimer; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static void setGracefulQuitTimer(Timer gracefulQuitTimer) { |
|
|
|
|
I2PDActivity.gracefulQuitTimer = gracefulQuitTimer; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|