mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 04:04:16 +00:00
fixes 1094; fixes grac stop
This commit is contained in:
parent
1b56d66fc8
commit
33735b343d
@ -1,12 +1,17 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resources>
|
<resources>
|
||||||
<string name="app_name">i2pd</string>
|
<string name="app_name">i2pd</string>
|
||||||
<string name="i2pd_started">i2pd started</string>
|
|
||||||
<string name="i2pd_service_started">i2pd service started</string>
|
|
||||||
<string name="i2pd_service_stopped">i2pd service stopped</string>
|
|
||||||
<string name="action_stop">Stop</string>
|
<string name="action_stop">Stop</string>
|
||||||
<string name="action_graceful_stop">Graceful Stop</string>
|
<string name="action_graceful_stop">Graceful Stop</string>
|
||||||
<string name="graceful_stop_is_already_in_progress">Graceful stop is already in progress</string>
|
<string name="graceful_stop_is_already_in_progress">Graceful stop is already in progress</string>
|
||||||
<string name="graceful_stop_is_in_progress">Graceful stop is in progress</string>
|
<string name="graceful_stop_is_in_progress">Graceful stop is in progress</string>
|
||||||
<string name="already_stopped">Already stopped</string>
|
<string name="already_stopped">Already stopped</string>
|
||||||
|
<string name="uninitialized">i2pd initializing</string>
|
||||||
|
<string name="starting">i2pd is starting</string>
|
||||||
|
<string name="jniLibraryLoaded">i2pd: loaded JNI libraries</string>
|
||||||
|
<string name="startedOkay">i2pd started</string>
|
||||||
|
<string name="startFailed">i2pd start failed</string>
|
||||||
|
<string name="gracefulShutdownInProgress">i2pd: graceful shutdown in progress</string>
|
||||||
|
<string name="stopped">i2pd has stopped</string>
|
||||||
|
<string name="remaining">remaining</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -35,7 +35,25 @@ public class DaemonSingleton {
|
|||||||
|
|
||||||
private volatile boolean startedOkay;
|
private volatile boolean startedOkay;
|
||||||
|
|
||||||
public enum State {uninitialized,starting,jniLibraryLoaded,startedOkay,startFailed,gracefulShutdownInProgress,stopped};
|
public enum State {
|
||||||
|
uninitialized(R.string.uninitialized),
|
||||||
|
starting(R.string.starting),
|
||||||
|
jniLibraryLoaded(R.string.jniLibraryLoaded),
|
||||||
|
startedOkay(R.string.startedOkay),
|
||||||
|
startFailed(R.string.startFailed),
|
||||||
|
gracefulShutdownInProgress(R.string.gracefulShutdownInProgress),
|
||||||
|
stopped(R.string.stopped);
|
||||||
|
|
||||||
|
State(int statusStringResourceId) {
|
||||||
|
this.statusStringResourceId = statusStringResourceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
private final int statusStringResourceId;
|
||||||
|
|
||||||
|
public int getStatusStringResourceId() {
|
||||||
|
return statusStringResourceId;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
private volatile State state = State.uninitialized;
|
private volatile State state = State.uninitialized;
|
||||||
|
|
||||||
|
@ -11,11 +11,32 @@ import android.util.Log;
|
|||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
public class ForegroundService extends Service {
|
public class ForegroundService extends Service {
|
||||||
|
private static final String TAG="FgService";
|
||||||
|
|
||||||
|
private volatile boolean shown;
|
||||||
|
|
||||||
|
private final DaemonSingleton.StateUpdateListener daemonStateUpdatedListener =
|
||||||
|
new DaemonSingleton.StateUpdateListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void daemonStateUpdate() {
|
||||||
|
try {
|
||||||
|
synchronized (ForegroundService.this) {
|
||||||
|
if (shown) cancelNotification();
|
||||||
|
showNotification();
|
||||||
|
}
|
||||||
|
} catch (Throwable tr) {
|
||||||
|
Log.e(TAG,"error ignored",tr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
private NotificationManager notificationManager;
|
private NotificationManager notificationManager;
|
||||||
|
|
||||||
// Unique Identification Number for the Notification.
|
// Unique Identification Number for the Notification.
|
||||||
// We use it on Notification start, and to cancel it.
|
// We use it on Notification start, and to cancel it.
|
||||||
private int NOTIFICATION = R.string.i2pd_started;
|
private int NOTIFICATION = 1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class for clients to access. Because we know this service always
|
* Class for clients to access. Because we know this service always
|
||||||
@ -32,8 +53,10 @@ public class ForegroundService extends Service {
|
|||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
|
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
|
||||||
|
|
||||||
// Display a notification about us starting. We put an icon in the status bar.
|
synchronized (this) {
|
||||||
showNotification();
|
DaemonSingleton.getInstance().addStateChangeListener(daemonStateUpdatedListener);
|
||||||
|
if (!shown) daemonStateUpdatedListener.daemonStateUpdate();
|
||||||
|
}
|
||||||
// Tell the user we started.
|
// Tell the user we started.
|
||||||
// Toast.makeText(this, R.string.i2pd_service_started, Toast.LENGTH_SHORT).show();
|
// Toast.makeText(this, R.string.i2pd_service_started, Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
@ -46,6 +69,11 @@ public class ForegroundService extends Service {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
|
DaemonSingleton.getInstance().removeStateChangeListener(daemonStateUpdatedListener);
|
||||||
|
cancelNotification();
|
||||||
|
}
|
||||||
|
|
||||||
|
private synchronized void cancelNotification() {
|
||||||
// Cancel the persistent notification.
|
// Cancel the persistent notification.
|
||||||
notificationManager.cancel(NOTIFICATION);
|
notificationManager.cancel(NOTIFICATION);
|
||||||
|
|
||||||
@ -53,6 +81,7 @@ public class ForegroundService extends Service {
|
|||||||
|
|
||||||
// Tell the user we stopped.
|
// Tell the user we stopped.
|
||||||
// Toast.makeText(this, R.string.i2pd_service_stopped, Toast.LENGTH_SHORT).show();
|
// Toast.makeText(this, R.string.i2pd_service_stopped, Toast.LENGTH_SHORT).show();
|
||||||
|
shown=false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -67,9 +96,9 @@ public class ForegroundService extends Service {
|
|||||||
/**
|
/**
|
||||||
* Show a notification while this service is running.
|
* Show a notification while this service is running.
|
||||||
*/
|
*/
|
||||||
private void showNotification() {
|
private synchronized void showNotification() {
|
||||||
// In this sample, we'll use the same text for the ticker and the expanded notification
|
// In this sample, we'll use the same text for the ticker and the expanded notification
|
||||||
CharSequence text = getText(R.string.i2pd_started);
|
CharSequence text = getText(DaemonSingleton.getInstance().getState().getStatusStringResourceId());
|
||||||
|
|
||||||
// The PendingIntent to launch our activity if the user selects this notification
|
// The PendingIntent to launch our activity if the user selects this notification
|
||||||
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
|
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
|
||||||
@ -88,6 +117,7 @@ public class ForegroundService extends Service {
|
|||||||
// Send the notification.
|
// Send the notification.
|
||||||
//mNM.notify(NOTIFICATION, notification);
|
//mNM.notify(NOTIFICATION, notification);
|
||||||
startForeground(NOTIFICATION, notification);
|
startForeground(NOTIFICATION, notification);
|
||||||
|
shown=true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final DaemonSingleton daemon = DaemonSingleton.getInstance();
|
private static final DaemonSingleton daemon = DaemonSingleton.getInstance();
|
||||||
|
@ -19,9 +19,10 @@ 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 static final DaemonSingleton daemon = DaemonSingleton.getInstance();
|
private static final DaemonSingleton daemon = DaemonSingleton.getInstance();
|
||||||
|
|
||||||
@ -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 volatile long graceStartedMillis;
|
||||||
|
private final Object graceStartedMillis_LOCK=new Object();
|
||||||
|
|
||||||
|
private 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) {
|
||||||
@ -70,11 +86,7 @@ public class I2PDActivity extends Activity {
|
|||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
textView = null;
|
textView = null;
|
||||||
daemon.removeStateChangeListener(daemonStateUpdatedListener);
|
daemon.removeStateChangeListener(daemonStateUpdatedListener);
|
||||||
Timer gracefulQuitTimer = getGracefulQuitTimer();
|
cancelGracefulStop();
|
||||||
if(gracefulQuitTimer!=null) {
|
|
||||||
gracefulQuitTimer.cancel();
|
|
||||||
setGracefulQuitTimer(null);
|
|
||||||
}
|
|
||||||
try{
|
try{
|
||||||
doUnbindService();
|
doUnbindService();
|
||||||
}catch(Throwable tr){
|
}catch(Throwable tr){
|
||||||
@ -82,7 +94,15 @@ public class I2PDActivity extends Activity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private CharSequence throwableToString(Throwable tr) {
|
private void cancelGracefulStop() {
|
||||||
|
Timer gracefulQuitTimer = getGracefulQuitTimer();
|
||||||
|
if(gracefulQuitTimer!=null) {
|
||||||
|
gracefulQuitTimer.cancel();
|
||||||
|
setGracefulQuitTimer(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
@ -169,11 +189,20 @@ public class I2PDActivity extends Activity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void i2pdStop() {
|
private void i2pdStop() {
|
||||||
try{
|
cancelGracefulStop();
|
||||||
daemon.stopDaemon();
|
new Thread(new Runnable(){
|
||||||
}catch (Throwable tr) {
|
|
||||||
Log.e(TAG, "", tr);
|
@Override
|
||||||
}
|
public void run() {
|
||||||
|
Log.d(TAG, "stopping");
|
||||||
|
try{
|
||||||
|
daemon.stopDaemon();
|
||||||
|
}catch (Throwable tr) {
|
||||||
|
Log.e(TAG, "", tr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
},"stop").start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private volatile Timer gracefulQuitTimer;
|
private volatile Timer gracefulQuitTimer;
|
||||||
@ -199,8 +228,11 @@ 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);
|
final Timer gracefulQuitTimer = new Timer(true);
|
||||||
setGracefulQuitTimer(gracefulQuitTimer);
|
setGracefulQuitTimer(gracefulQuitTimer);
|
||||||
|
synchronized (graceStartedMillis_LOCK) {
|
||||||
|
graceStartedMillis = System.currentTimeMillis();
|
||||||
|
}
|
||||||
gracefulQuitTimer.schedule(new TimerTask(){
|
gracefulQuitTimer.schedule(new TimerTask(){
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -208,7 +240,14 @@ public class I2PDActivity extends Activity {
|
|||||||
i2pdStop();
|
i2pdStop();
|
||||||
}
|
}
|
||||||
|
|
||||||
}, 10*60*1000/*milliseconds*/);
|
}, GRACEFUL_DELAY_MILLIS);
|
||||||
|
final TimerTask tickerTask = new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
daemonStateUpdatedListener.daemonStateUpdate();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
gracefulQuitTimer.scheduleAtFixedRate(tickerTask,0/*start delay*/,1000/*millis period*/);
|
||||||
}else{
|
}else{
|
||||||
i2pdStop();
|
i2pdStop();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user