Browse Source

android work 1

pull/1095/head
hypnosis-i2p 7 years ago
parent
commit
62d9a47c3d
  1. 58
      android/src/org/purplei2p/i2pd/DaemonSingleton.java
  2. 8
      android/src/org/purplei2p/i2pd/ForegroundService.java
  3. 79
      android/src/org/purplei2p/i2pd/I2PDActivity.java

58
android/src/org/purplei2p/i2pd/DaemonSingleton.java

@ -8,8 +8,8 @@ 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 StateUpdateListener { void daemonStateUpdate(); } public interface StateUpdateListener { void daemonStateUpdate(); }
private final Set<StateUpdateListener> stateUpdateListeners = new HashSet<StateUpdateListener>(); private final Set<StateUpdateListener> stateUpdateListeners = new HashSet<>();
public static DaemonSingleton getInstance() { public static DaemonSingleton getInstance() {
return instance; return instance;
@ -18,63 +18,54 @@ public class DaemonSingleton {
public synchronized void addStateChangeListener(StateUpdateListener listener) { stateUpdateListeners.add(listener); } public synchronized void addStateChangeListener(StateUpdateListener listener) { stateUpdateListeners.add(listener); }
public synchronized void removeStateChangeListener(StateUpdateListener listener) { stateUpdateListeners.remove(listener); } public synchronized void removeStateChangeListener(StateUpdateListener listener) { stateUpdateListeners.remove(listener); }
private synchronized void setState(State newState) {
if(newState==null)throw new NullPointerException();
State oldState = state;
if(oldState==null)throw new NullPointerException();
if(oldState.equals(newState))return;
state=newState;
fireStateUpdate1();
}
public synchronized void stopAcceptingTunnels() { public synchronized void stopAcceptingTunnels() {
if(isStartedOkay()){ if(isStartedOkay()){
state=State.gracefulShutdownInProgress; setState(State.gracefulShutdownInProgress);
fireStateUpdate();
I2PD_JNI.stopAcceptingTunnels(); I2PD_JNI.stopAcceptingTunnels();
} }
} }
public void onNetworkStateChange(boolean isConnected) { private volatile boolean startedOkay;
I2PD_JNI.onNetworkStateChanged(isConnected);
}
private boolean startedOkay;
public static enum State {uninitialized,starting,jniLibraryLoaded,startedOkay,startFailed,gracefulShutdownInProgress,stopped}; public enum State {uninitialized,starting,jniLibraryLoaded,startedOkay,startFailed,gracefulShutdownInProgress,stopped};
private State state = State.uninitialized; private volatile State state = State.uninitialized;
public State getState() { return state; } public State getState() { return state; }
public synchronized void start() { {
if(state != State.uninitialized)return; setState(State.starting);
state = State.starting;
fireStateUpdate();
new Thread(new Runnable(){ new Thread(new Runnable(){
@Override @Override
public void run() { public void run() {
try { try {
I2PD_JNI.loadLibraries(); I2PD_JNI.loadLibraries();
synchronized (DaemonSingleton.this) { setState(State.jniLibraryLoaded);
state = State.jniLibraryLoaded;
fireStateUpdate();
}
} catch (Throwable tr) { } catch (Throwable tr) {
lastThrowable=tr; lastThrowable=tr;
synchronized (DaemonSingleton.this) { setState(State.startFailed);
state = State.startFailed;
fireStateUpdate();
}
return; return;
} }
try { try {
synchronized (DaemonSingleton.this) { synchronized (DaemonSingleton.this) {
daemonStartResult = I2PD_JNI.startDaemon(); daemonStartResult = I2PD_JNI.startDaemon();
if("ok".equals(daemonStartResult)){ if("ok".equals(daemonStartResult)){
state=State.startedOkay; setState(State.startedOkay);
setStartedOkay(true); setStartedOkay(true);
}else state=State.startFailed; }else setState(State.startFailed);
fireStateUpdate();
} }
} catch (Throwable tr) { } catch (Throwable tr) {
lastThrowable=tr; lastThrowable=tr;
synchronized (DaemonSingleton.this) { setState(State.startFailed);
state = State.startFailed;
fireStateUpdate();
}
return; return;
} }
} }
@ -84,7 +75,7 @@ public class DaemonSingleton {
private Throwable lastThrowable; private Throwable lastThrowable;
private String daemonStartResult="N/A"; private String daemonStartResult="N/A";
private synchronized void fireStateUpdate() { private void fireStateUpdate1() {
Log.i(TAG, "daemon state change: "+state); Log.i(TAG, "daemon state change: "+state);
for(StateUpdateListener listener : stateUpdateListeners) { for(StateUpdateListener listener : stateUpdateListeners) {
try { try {
@ -121,10 +112,7 @@ public class DaemonSingleton {
if(isStartedOkay()){ if(isStartedOkay()){
try {I2PD_JNI.stopDaemon();}catch(Throwable tr){Log.e(TAG, "", tr);} try {I2PD_JNI.stopDaemon();}catch(Throwable tr){Log.e(TAG, "", tr);}
setStartedOkay(false); setStartedOkay(false);
synchronized (DaemonSingleton.this) { setState(State.stopped);
state = State.stopped;
fireStateUpdate();
}
} }
} }
} }

8
android/src/org/purplei2p/i2pd/ForegroundService.java

@ -34,15 +34,13 @@ public class ForegroundService extends 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. // 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();
} }
@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);
daemon.start();
return START_STICKY; return START_STICKY;
} }
@ -54,7 +52,7 @@ public class ForegroundService extends Service {
stopForeground(true); stopForeground(true);
// 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();
} }
@Override @Override
@ -92,6 +90,6 @@ public class ForegroundService extends Service {
startForeground(NOTIFICATION, notification); startForeground(NOTIFICATION, notification);
} }
private final DaemonSingleton daemon = DaemonSingleton.getInstance(); private static final DaemonSingleton daemon = DaemonSingleton.getInstance();
} }

79
android/src/org/purplei2p/i2pd/I2PDActivity.java

@ -23,9 +23,9 @@ public class I2PDActivity extends Activity {
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
@ -58,7 +58,7 @@ 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
@ -68,22 +68,18 @@ public class I2PDActivity extends Activity {
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
localDestroy(); textView = null;
} daemon.removeStateChangeListener(daemonStateUpdatedListener);
Timer gracefulQuitTimer = getGracefulQuitTimer();
private void localDestroy() { if(gracefulQuitTimer!=null) {
textView = null; gracefulQuitTimer.cancel();
DaemonSingleton.getInstance().removeStateChangeListener(daemonStateUpdatedListener); setGracefulQuitTimer(null);
Timer gracefulQuitTimer = getGracefulQuitTimer(); }
if(gracefulQuitTimer!=null) { try{
gracefulQuitTimer.cancel(); doUnbindService();
setGracefulQuitTimer(null); }catch(Throwable tr){
Log.e(TAG, "", tr);
} }
// try{
// doUnbindService();
// }catch(Throwable tr){
// Log.e(TAG, "", tr);
// }
} }
private CharSequence throwableToString(Throwable tr) { private CharSequence throwableToString(Throwable tr) {
@ -122,24 +118,27 @@ 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) {
// Establish a connection with the service. We use an explicit if (mIsBound) return;
// class name because we want a specific service implementation that // Establish a connection with the service. We use an explicit
// we know will be running in our own process (and thus won't be // class name because we want a specific service implementation that
// supporting component replacement by other applications). // we know will be running in our own process (and thus won't be
bindService(new Intent(this, // supporting component replacement by other applications).
ForegroundService.class), mConnection, Context.BIND_AUTO_CREATE); bindService(new Intent(this, ForegroundService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true; mIsBound = true;
}
} }
private void doUnbindService() { private void doUnbindService() {
if (mIsBound) { synchronized (I2PDActivity.class) {
// Detach our existing connection. if (mIsBound) {
unbindService(mConnection); // Detach our existing connection.
mIsBound = false; unbindService(mConnection);
mIsBound = false;
}
} }
} }
@ -177,9 +176,9 @@ public class I2PDActivity extends Activity {
} }
} }
private Timer gracefulQuitTimer; private volatile Timer gracefulQuitTimer;
private final Object gracefulQuitTimerLock = new Object();
private synchronized void i2pdGracefulStop() { 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();
@ -218,18 +217,14 @@ public class I2PDActivity extends Activity {
} }
} }
},"gracQuitInit").start(); },"gracInit").start();
} }
private Timer getGracefulQuitTimer() { private Timer getGracefulQuitTimer() {
synchronized (gracefulQuitTimerLock) { return gracefulQuitTimer;
return gracefulQuitTimer;
}
} }
private void setGracefulQuitTimer(Timer gracefulQuitTimer) { private void setGracefulQuitTimer(Timer gracefulQuitTimer) {
synchronized (gracefulQuitTimerLock) { this.gracefulQuitTimer = gracefulQuitTimer;
this.gracefulQuitTimer = gracefulQuitTimer;
}
} }
} }

Loading…
Cancel
Save