|
|
@ -19,13 +19,14 @@ 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 = "i2pd"; |
|
|
|
private static final String TAG = "i2pdActvt"; |
|
|
|
|
|
|
|
public static final int GRACEFUL_DELAY_MILLIS = 10 * 60 * 1000; |
|
|
|
|
|
|
|
|
|
|
|
private TextView textView; |
|
|
|
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() { |
|
|
|
new DaemonSingleton.StateUpdateListener() { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
@ -42,8 +43,11 @@ public class I2PDActivity extends Activity { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
DaemonSingleton.State state = daemon.getState(); |
|
|
|
DaemonSingleton.State state = daemon.getState(); |
|
|
|
textView.setText(String.valueOf(state)+ |
|
|
|
textView.setText( |
|
|
|
(DaemonSingleton.State.startFailed.equals(state)?": "+daemon.getDaemonStartResult():"")); |
|
|
|
String.valueOf(state)+ |
|
|
|
|
|
|
|
(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); |
|
|
|
} |
|
|
|
} |
|
|
@ -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 |
|
|
|
@Override |
|
|
|
public void onCreate(Bundle savedInstanceState) { |
|
|
|
public void onCreate(Bundle savedInstanceState) { |
|
|
@ -58,32 +74,41 @@ public class I2PDActivity extends Activity { |
|
|
|
|
|
|
|
|
|
|
|
textView = new TextView(this); |
|
|
|
textView = new TextView(this); |
|
|
|
setContentView(textView); |
|
|
|
setContentView(textView); |
|
|
|
DaemonSingleton.getInstance().addStateChangeListener(daemonStateUpdatedListener); |
|
|
|
daemon.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(); |
|
|
|
localDestroy(); |
|
|
|
textView = null; |
|
|
|
|
|
|
|
daemon.removeStateChangeListener(daemonStateUpdatedListener); |
|
|
|
|
|
|
|
//cancelGracefulStop();
|
|
|
|
|
|
|
|
try{ |
|
|
|
|
|
|
|
doUnbindService(); |
|
|
|
|
|
|
|
}catch(Throwable tr){ |
|
|
|
|
|
|
|
Log.e(TAG, "", tr); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void localDestroy() { |
|
|
|
private static void cancelGracefulStop() { |
|
|
|
textView = null; |
|
|
|
|
|
|
|
DaemonSingleton.getInstance().removeStateChangeListener(daemonStateUpdatedListener); |
|
|
|
|
|
|
|
Timer gracefulQuitTimer = getGracefulQuitTimer(); |
|
|
|
Timer gracefulQuitTimer = getGracefulQuitTimer(); |
|
|
|
if(gracefulQuitTimer!=null) { |
|
|
|
if(gracefulQuitTimer!=null) { |
|
|
|
gracefulQuitTimer.cancel(); |
|
|
|
gracefulQuitTimer.cancel(); |
|
|
|
setGracefulQuitTimer(null); |
|
|
|
setGracefulQuitTimer(null); |
|
|
|
} |
|
|
|
} |
|
|
|
// try{
|
|
|
|
|
|
|
|
// doUnbindService();
|
|
|
|
|
|
|
|
// }catch(Throwable tr){
|
|
|
|
|
|
|
|
// Log.e(TAG, "", tr);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private CharSequence throwableToString(Throwable tr) { |
|
|
|
private CharSequence throwableToString(Throwable tr) { |
|
|
@ -122,26 +147,29 @@ public class I2PDActivity extends Activity { |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean mIsBound; |
|
|
|
private static volatile boolean mIsBound; |
|
|
|
|
|
|
|
|
|
|
|
private synchronized void doBindService() { |
|
|
|
private void doBindService() { |
|
|
|
if(mIsBound)return; |
|
|
|
synchronized (I2PDActivity.class) { |
|
|
|
|
|
|
|
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; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public boolean onCreateOptionsMenu(Menu menu) { |
|
|
|
public boolean onCreateOptionsMenu(Menu menu) { |
|
|
@ -170,6 +198,12 @@ public class I2PDActivity extends Activity { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void i2pdStop() { |
|
|
|
private void i2pdStop() { |
|
|
|
|
|
|
|
cancelGracefulStop(); |
|
|
|
|
|
|
|
new Thread(new Runnable(){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void run() { |
|
|
|
|
|
|
|
Log.d(TAG, "stopping"); |
|
|
|
try{ |
|
|
|
try{ |
|
|
|
daemon.stopDaemon(); |
|
|
|
daemon.stopDaemon(); |
|
|
|
}catch (Throwable tr) { |
|
|
|
}catch (Throwable tr) { |
|
|
@ -177,9 +211,12 @@ public class I2PDActivity extends Activity { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Timer gracefulQuitTimer; |
|
|
|
},"stop").start(); |
|
|
|
private final Object gracefulQuitTimerLock = new Object(); |
|
|
|
} |
|
|
|
private synchronized void i2pdGracefulStop() { |
|
|
|
|
|
|
|
|
|
|
|
private static volatile Timer gracefulQuitTimer; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private 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(); |
|
|
@ -200,16 +237,12 @@ 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(); |
|
|
|
Timer gracefulQuitTimer = new Timer(true); |
|
|
|
long gracefulStopAtMillis; |
|
|
|
setGracefulQuitTimer(gracefulQuitTimer); |
|
|
|
synchronized (graceStartedMillis_LOCK) { |
|
|
|
gracefulQuitTimer.schedule(new TimerTask(){ |
|
|
|
graceStartedMillis = System.currentTimeMillis(); |
|
|
|
|
|
|
|
gracefulStopAtMillis = graceStartedMillis + GRACEFUL_DELAY_MILLIS; |
|
|
|
@Override |
|
|
|
|
|
|
|
public void run() { |
|
|
|
|
|
|
|
i2pdStop(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
rescheduleGraceStop(null,gracefulStopAtMillis); |
|
|
|
}, 10*60*1000/*milliseconds*/); |
|
|
|
|
|
|
|
}else{ |
|
|
|
}else{ |
|
|
|
i2pdStop(); |
|
|
|
i2pdStop(); |
|
|
|
} |
|
|
|
} |
|
|
@ -218,18 +251,35 @@ public class I2PDActivity extends Activity { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
},"gracQuitInit").start(); |
|
|
|
},"gracInit").start(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Timer getGracefulQuitTimer() { |
|
|
|
private void rescheduleGraceStop(Timer gracefulQuitTimerOld, long gracefulStopAtMillis) { |
|
|
|
synchronized (gracefulQuitTimerLock) { |
|
|
|
if(gracefulQuitTimerOld!=null)gracefulQuitTimerOld.cancel(); |
|
|
|
return gracefulQuitTimer; |
|
|
|
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) { |
|
|
|
private static Timer getGracefulQuitTimer() { |
|
|
|
synchronized (gracefulQuitTimerLock) { |
|
|
|
return gracefulQuitTimer; |
|
|
|
this.gracefulQuitTimer = gracefulQuitTimer; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void setGracefulQuitTimer(Timer gracefulQuitTimer) { |
|
|
|
|
|
|
|
I2PDActivity.gracefulQuitTimer = gracefulQuitTimer; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|