mirror of
https://github.com/PurpleI2P/i2pd
synced 2024-11-10 08:00:38 +03:00
rework + now restarts after app kill event
This commit is contained in:
parent
59b3daabc5
commit
9925e2732a
@ -2,6 +2,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_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_quit">Quit</string>
|
<string name="action_quit">Quit</string>
|
||||||
<string name="action_graceful_quit">Graceful Quit</string>
|
<string name="action_graceful_quit">Graceful Quit</string>
|
||||||
<string name="graceful_quit_is_already_in_progress">Graceful quit is already in progress</string>
|
<string name="graceful_quit_is_already_in_progress">Graceful quit is already in progress</string>
|
||||||
|
@ -8,20 +8,20 @@ import android.util.Log;
|
|||||||
public class DaemonSingleton {
|
public class DaemonSingleton {
|
||||||
private static final String TAG="i2pd";
|
private static final String TAG="i2pd";
|
||||||
private static final DaemonSingleton instance = new DaemonSingleton();
|
private static final DaemonSingleton instance = new DaemonSingleton();
|
||||||
public static interface StateChangeListener { void daemonStateChanged(); }
|
public static interface StateUpdateListener { void daemonStateUpdate(); }
|
||||||
private final Set<StateChangeListener> stateChangeListeners = new HashSet<StateChangeListener>();
|
private final Set<StateUpdateListener> stateUpdateListeners = new HashSet<StateUpdateListener>();
|
||||||
|
|
||||||
public static DaemonSingleton getInstance() {
|
public static DaemonSingleton getInstance() {
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void addStateChangeListener(StateChangeListener listener) { stateChangeListeners.add(listener); }
|
public synchronized void addStateChangeListener(StateUpdateListener listener) { stateUpdateListeners.add(listener); }
|
||||||
public synchronized void removeStateChangeListener(StateChangeListener listener) { stateChangeListeners.remove(listener); }
|
public synchronized void removeStateChangeListener(StateUpdateListener listener) { stateUpdateListeners.remove(listener); }
|
||||||
|
|
||||||
public synchronized void stopAcceptingTunnels() {
|
public synchronized void stopAcceptingTunnels() {
|
||||||
if(isStartedOkay()){
|
if(isStartedOkay()){
|
||||||
state=State.gracefulShutdownInProgress;
|
state=State.gracefulShutdownInProgress;
|
||||||
fireStateChange();
|
fireStateUpdate();
|
||||||
I2PD_JNI.stopAcceptingTunnels();
|
I2PD_JNI.stopAcceptingTunnels();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -32,61 +32,63 @@ public class DaemonSingleton {
|
|||||||
|
|
||||||
private boolean startedOkay;
|
private boolean startedOkay;
|
||||||
|
|
||||||
public static enum State {starting,jniLibraryLoaded,startedOkay,startFailed,gracefulShutdownInProgress};
|
public static enum State {uninitialized,starting,jniLibraryLoaded,startedOkay,startFailed,gracefulShutdownInProgress};
|
||||||
|
|
||||||
private State state = State.starting;
|
private State state = State.uninitialized;
|
||||||
|
|
||||||
public State getState() { return state; }
|
public State getState() { return state; }
|
||||||
|
|
||||||
{
|
public synchronized void start() {
|
||||||
synchronized(this){
|
if(state != State.uninitialized)return;
|
||||||
fireStateChange();
|
state = State.starting;
|
||||||
new Thread(new Runnable(){
|
fireStateUpdate();
|
||||||
|
new Thread(new Runnable(){
|
||||||
@Override
|
|
||||||
public void run() {
|
@Override
|
||||||
try {
|
public void run() {
|
||||||
I2PD_JNI.loadLibraries();
|
try {
|
||||||
synchronized (DaemonSingleton.this) {
|
I2PD_JNI.loadLibraries();
|
||||||
state = State.jniLibraryLoaded;
|
synchronized (DaemonSingleton.this) {
|
||||||
fireStateChange();
|
state = State.jniLibraryLoaded;
|
||||||
}
|
fireStateUpdate();
|
||||||
} catch (Throwable tr) {
|
|
||||||
lastThrowable=tr;
|
|
||||||
synchronized (DaemonSingleton.this) {
|
|
||||||
state = State.startFailed;
|
|
||||||
fireStateChange();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
try {
|
} catch (Throwable tr) {
|
||||||
synchronized (DaemonSingleton.this) {
|
lastThrowable=tr;
|
||||||
daemonStartResult = I2PD_JNI.startDaemon();
|
synchronized (DaemonSingleton.this) {
|
||||||
if("ok".equals(daemonStartResult)){state=State.startedOkay;setStartedOkay(true);}
|
state = State.startFailed;
|
||||||
else state=State.startFailed;
|
fireStateUpdate();
|
||||||
fireStateChange();
|
}
|
||||||
}
|
return;
|
||||||
} catch (Throwable tr) {
|
|
||||||
lastThrowable=tr;
|
|
||||||
synchronized (DaemonSingleton.this) {
|
|
||||||
state = State.startFailed;
|
|
||||||
fireStateChange();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
}, "i2pdDaemonStart").start();
|
synchronized (DaemonSingleton.this) {
|
||||||
}
|
daemonStartResult = I2PD_JNI.startDaemon();
|
||||||
|
if("ok".equals(daemonStartResult)){
|
||||||
|
state=State.startedOkay;
|
||||||
|
setStartedOkay(true);
|
||||||
|
}else state=State.startFailed;
|
||||||
|
fireStateUpdate();
|
||||||
|
}
|
||||||
|
} catch (Throwable tr) {
|
||||||
|
lastThrowable=tr;
|
||||||
|
synchronized (DaemonSingleton.this) {
|
||||||
|
state = State.startFailed;
|
||||||
|
fireStateUpdate();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}, "i2pdDaemonStart").start();
|
||||||
}
|
}
|
||||||
private Throwable lastThrowable;
|
private Throwable lastThrowable;
|
||||||
private String daemonStartResult="N/A";
|
private String daemonStartResult="N/A";
|
||||||
|
|
||||||
private synchronized void fireStateChange() {
|
private synchronized void fireStateUpdate() {
|
||||||
Log.i(TAG, "daemon state change: "+state);
|
Log.i(TAG, "daemon state change: "+state);
|
||||||
for(StateChangeListener listener : stateChangeListeners) {
|
for(StateUpdateListener listener : stateUpdateListeners) {
|
||||||
try {
|
try {
|
||||||
listener.daemonStateChanged();
|
listener.daemonStateUpdate();
|
||||||
} catch (Throwable tr) {
|
} catch (Throwable tr) {
|
||||||
Log.e(TAG, "exception in listener ignored", tr);
|
Log.e(TAG, "exception in listener ignored", tr);
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,17 @@
|
|||||||
package org.purplei2p.i2pd;
|
package org.purplei2p.i2pd;
|
||||||
|
|
||||||
import android.app.Notification;
|
import android.app.Notification;
|
||||||
|
import android.app.NotificationManager;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
public class ForegroundService extends Service {
|
public class ForegroundService extends Service {
|
||||||
// private NotificationManager mNM;
|
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.
|
||||||
@ -28,26 +30,31 @@ public class ForegroundService extends Service {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
// mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
|
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
|
||||||
|
|
||||||
// Display a notification about us starting. We put an icon in the status bar.
|
// Display a notification about us starting. We put an icon in the status bar.
|
||||||
showNotification();
|
showNotification();
|
||||||
|
daemon.start();
|
||||||
|
// Tell the user we started.
|
||||||
|
Toast.makeText(this, R.string.i2pd_service_started, Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
Log.i("ForegroundService", "Received start id " + startId + ": " + intent);
|
Log.i("ForegroundService", "Received start id " + startId + ": " + intent);
|
||||||
return START_NOT_STICKY;
|
daemon.start();
|
||||||
|
return START_STICKY;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
// Cancel the persistent notification.
|
// Cancel the persistent notification.
|
||||||
//mNM.cancel(NOTIFICATION);
|
notificationManager.cancel(NOTIFICATION);
|
||||||
|
|
||||||
stopForeground(true);
|
stopForeground(true);
|
||||||
|
|
||||||
// Tell the user we stopped.
|
// Tell the user we stopped.
|
||||||
//Toast.makeText(this, R.string.local_service_stopped, Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, R.string.i2pd_service_stopped, Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -84,4 +91,7 @@ public class ForegroundService extends Service {
|
|||||||
//mNM.notify(NOTIFICATION, notification);
|
//mNM.notify(NOTIFICATION, notification);
|
||||||
startForeground(NOTIFICATION, notification);
|
startForeground(NOTIFICATION, notification);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final DaemonSingleton daemon = DaemonSingleton.getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,12 +22,16 @@ import android.widget.Toast;
|
|||||||
|
|
||||||
public class I2PD extends Activity {
|
public class I2PD extends Activity {
|
||||||
private static final String TAG = "i2pd";
|
private static final String TAG = "i2pd";
|
||||||
private DaemonSingleton daemon = DaemonSingleton.getInstance();
|
|
||||||
private DaemonSingleton.StateChangeListener daemonStateChangeListener =
|
private TextView textView;
|
||||||
new DaemonSingleton.StateChangeListener() {
|
|
||||||
|
private final DaemonSingleton daemon = DaemonSingleton.getInstance();
|
||||||
|
|
||||||
|
private DaemonSingleton.StateUpdateListener daemonStateUpdatedListener =
|
||||||
|
new DaemonSingleton.StateUpdateListener() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void daemonStateChanged() {
|
public void daemonStateUpdate() {
|
||||||
runOnUiThread(new Runnable(){
|
runOnUiThread(new Runnable(){
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -50,19 +54,17 @@ public class I2PD extends Activity {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private TextView textView;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
//set the app be foreground (do not unload when RAM needed)
|
|
||||||
doBindService();
|
|
||||||
|
|
||||||
textView = new TextView(this);
|
textView = new TextView(this);
|
||||||
setContentView(textView);
|
setContentView(textView);
|
||||||
daemonStateChangeListener.daemonStateChanged();
|
DaemonSingleton.getInstance().addStateChangeListener(daemonStateUpdatedListener);
|
||||||
daemon.addStateChangeListener(daemonStateChangeListener);
|
daemonStateUpdatedListener.daemonStateUpdate();
|
||||||
|
|
||||||
|
//set the app be foreground
|
||||||
|
doBindService();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -73,7 +75,7 @@ public class I2PD extends Activity {
|
|||||||
|
|
||||||
private void localDestroy() {
|
private void localDestroy() {
|
||||||
textView = null;
|
textView = null;
|
||||||
daemon.removeStateChangeListener(daemonStateChangeListener);
|
DaemonSingleton.getInstance().removeStateChangeListener(daemonStateUpdatedListener);
|
||||||
Timer gracefulQuitTimer = getGracefulQuitTimer();
|
Timer gracefulQuitTimer = getGracefulQuitTimer();
|
||||||
if(gracefulQuitTimer!=null) {
|
if(gracefulQuitTimer!=null) {
|
||||||
gracefulQuitTimer.cancel();
|
gracefulQuitTimer.cancel();
|
||||||
|
Loading…
Reference in New Issue
Block a user