From a3286ebac3932c83e7e93787e06cb51a3d79f300 Mon Sep 17 00:00:00 2001 From: hypnosis-i2p Date: Wed, 13 Jul 2016 19:36:18 +0800 Subject: [PATCH] updated --- .../org/purplei2p/i2pd/DaemonSingleton.java | 84 +++++++++++-------- android/src/org/purplei2p/i2pd/I2PD.java | 7 +- 2 files changed, 53 insertions(+), 38 deletions(-) diff --git a/android/src/org/purplei2p/i2pd/DaemonSingleton.java b/android/src/org/purplei2p/i2pd/DaemonSingleton.java index 7f9aae75..5e0ac4d0 100644 --- a/android/src/org/purplei2p/i2pd/DaemonSingleton.java +++ b/android/src/org/purplei2p/i2pd/DaemonSingleton.java @@ -18,7 +18,7 @@ public class DaemonSingleton { public synchronized void addStateChangeListener(StateChangeListener listener) { stateChangeListeners.add(listener); } public synchronized void removeStateChangeListener(StateChangeListener listener) { stateChangeListeners.remove(listener); } - public void stopAcceptingTunnels() { + public synchronized void stopAcceptingTunnels() { if(isStartedOkay()){ state=State.gracefulShutdownInProgress; fireStateChange(); @@ -39,35 +39,45 @@ public class DaemonSingleton { public State getState() { return state; } { - fireStateChange(); - new Thread(new Runnable(){ - - @Override - public void run() { - try { - I2PD_JNI.loadLibraries(); - state = State.jniLibraryLoaded; - fireStateChange(); - } catch (Throwable tr) { - lastThrowable=tr; - state = State.startFailed; - fireStateChange(); - return; + synchronized(this){ + fireStateChange(); + new Thread(new Runnable(){ + + @Override + public void run() { + try { + I2PD_JNI.loadLibraries(); + synchronized (DaemonSingleton.this) { + state = State.jniLibraryLoaded; + fireStateChange(); + } + } catch (Throwable tr) { + lastThrowable=tr; + synchronized (DaemonSingleton.this) { + state = State.startFailed; + fireStateChange(); + } + return; + } + try { + synchronized (DaemonSingleton.this) { + daemonStartResult = I2PD_JNI.startDaemon(); + if("ok".equals(daemonStartResult)){state=State.startedOkay;setStartedOkay(true);} + else state=State.startFailed; + fireStateChange(); + } + } catch (Throwable tr) { + lastThrowable=tr; + synchronized (DaemonSingleton.this) { + state = State.startFailed; + fireStateChange(); + } + return; + } } - try { - daemonStartResult = I2PD_JNI.startDaemon(); - if("ok".equals(daemonStartResult)){state=State.startedOkay;setStartedOkay(true);} - else state=State.startFailed; - fireStateChange(); - } catch (Throwable tr) { - lastThrowable=tr; - state = State.startFailed; - fireStateChange(); - return; - } - } - - }, "i2pdDaemonStart").start(); + + }, "i2pdDaemonStart").start(); + } } private Throwable lastThrowable; private String daemonStartResult="N/A"; @@ -90,16 +100,22 @@ public class DaemonSingleton { public String getDaemonStartResult() { return daemonStartResult; } + + private final Object startedOkayLock = new Object(); - public synchronized boolean isStartedOkay() { - return startedOkay; + public boolean isStartedOkay() { + synchronized (startedOkayLock) { + return startedOkay; + } } - private synchronized void setStartedOkay(boolean startedOkay) { - this.startedOkay = startedOkay; + private void setStartedOkay(boolean startedOkay) { + synchronized (startedOkayLock) { + this.startedOkay = startedOkay; + } } - public void stopDaemon() { + public synchronized void stopDaemon() { if(isStartedOkay()){ try {I2PD_JNI.stopDaemon();}catch(Throwable tr){Log.e(TAG, "", tr);} setStartedOkay(false); diff --git a/android/src/org/purplei2p/i2pd/I2PD.java b/android/src/org/purplei2p/i2pd/I2PD.java index 5d8322ed..58912bf5 100755 --- a/android/src/org/purplei2p/i2pd/I2PD.java +++ b/android/src/org/purplei2p/i2pd/I2PD.java @@ -5,8 +5,6 @@ import java.io.StringWriter; import java.util.Timer; import java.util.TimerTask; -import org.purplei2p.i2pd.DaemonSingleton.State; - import android.annotation.SuppressLint; import android.app.Activity; import android.content.ComponentName; @@ -76,8 +74,9 @@ public class I2PD extends Activity { private void localDestroy() { textView = null; daemon.removeStateChangeListener(daemonStateChangeListener); - if(getGracefulQuitTimer()!=null) { - getGracefulQuitTimer().cancel(); + Timer gracefulQuitTimer = getGracefulQuitTimer(); + if(gracefulQuitTimer!=null) { + gracefulQuitTimer.cancel(); setGracefulQuitTimer(null); } try{