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