Browse Source

Merge pull request #1101 from PurpleI2P/revert-1098-revert-1095-openssl

Revert "Revert "reworked the app, fixed #1094, fixed grace stop""
pull/1102/head
orignal 7 years ago committed by GitHub
parent
commit
d111025012
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      android/.gitignore
  2. 11
      android/res/values/strings.xml
  3. 72
      android/src/org/purplei2p/i2pd/DaemonSingleton.java
  4. 48
      android/src/org/purplei2p/i2pd/ForegroundService.java
  5. 126
      android/src/org/purplei2p/i2pd/I2PDActivity.java

7
android/.gitignore vendored

@ -6,3 +6,10 @@ local.properties
build.sh build.sh
bin bin
log* log*
.gradle
android.iml
build
gradle
gradlew
gradlew.bat

11
android/res/values/strings.xml

@ -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>

72
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,72 @@ 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);
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 boolean startedOkay; private final int statusStringResourceId;
public static enum State {uninitialized,starting,jniLibraryLoaded,startedOkay,startFailed,gracefulShutdownInProgress,stopped}; public int getStatusStringResourceId() {
return statusStringResourceId;
}
};
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 +93,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 +130,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();
}
} }
} }
} }

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

@ -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,29 +53,35 @@ 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);
daemon.start(); 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();
} }
@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;
} }
@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);
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();
shown=false;
} }
@Override @Override
@ -69,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,
@ -90,8 +117,9 @@ 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 final DaemonSingleton daemon = DaemonSingleton.getInstance(); private static final DaemonSingleton daemon = DaemonSingleton.getInstance();
} }

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

@ -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() {
synchronized (I2PDActivity.class) {
if (mIsBound) return; 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;
} }
} }

Loading…
Cancel
Save