Connect to network immediately for remote router, or if I2P Android is running

This commit is contained in:
str4d
2014-06-11 05:29:15 +00:00
parent 57492ef6d1
commit c0af7f7671

View File

@@ -5,6 +5,9 @@ import java.security.GeneralSecurityException;
import java.util.List;
import javax.mail.MessagingException;
import net.i2p.android.router.service.IRouterState;
import net.i2p.android.router.service.IRouterStateCallback;
import net.i2p.router.Router;
import net.i2p.router.RouterContext;
import net.i2p.router.RouterLaunch;
@@ -21,9 +24,12 @@ import i2p.bote.folder.NewEmailListener;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v4.app.NotificationCompat;
public class BoteService extends Service implements NewEmailListener {
@@ -31,7 +37,6 @@ public class BoteService extends Service implements NewEmailListener {
public static final int NOTIF_ID_NEW_EMAIL = 80739047;
RouterChoice mRouterChoice;
RouterContext mRouterContext;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
@@ -39,9 +44,19 @@ public class BoteService extends Service implements NewEmailListener {
if (mRouterChoice == RouterChoice.INTERNAL)
new Thread(new RouterStarter()).start();
I2PBote.getInstance().startUp();
I2PBote bote = I2PBote.getInstance();
bote.startUp();
bote.addNewEmailListener(this);
I2PBote.getInstance().addNewEmailListener(this);
if (mRouterChoice == RouterChoice.ANDROID) {
// Bind to I2P Android
Intent i2pIntent = new Intent(IRouterState.class.getName());
i2pIntent.setClassName("net.i2p.android.router",
"net.i2p.android.router.service.RouterService");
mTriedBindState = bindService(
i2pIntent, mStateConnection, 0);
} else if (mRouterChoice == RouterChoice.REMOTE)
bote.connectNow();
return START_REDELIVER_INTENT;
}
@@ -53,8 +68,15 @@ public class BoteService extends Service implements NewEmailListener {
@Override
public void onDestroy() {
I2PBote.getInstance().removeNewEmailListener(this);
if (mTriedBindState) {
try {
mStateService.unregisterCallback(mStatusListener);
} catch (RemoteException e) {}
unbindService(mStateConnection);
}
mTriedBindState = false;
I2PBote.getInstance().removeNewEmailListener(this);
I2PBote.getInstance().shutDown();
if (mRouterChoice == RouterChoice.INTERNAL)
@@ -66,6 +88,8 @@ public class BoteService extends Service implements NewEmailListener {
// Internal router helpers
//
private RouterContext mRouterContext;
private class RouterStarter implements Runnable {
public void run() {
RouterLaunch.main(null);
@@ -83,6 +107,47 @@ public class BoteService extends Service implements NewEmailListener {
}
}
//
// I2P Android helpers
//
private IRouterState mStateService = null;
private boolean mTriedBindState;
private ServiceConnection mStateConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
mStateService = IRouterState.Stub.asInterface(service);
try {
mStateService.registerCallback(mStatusListener);
String state = mStateService.getState();
if ("RUNNING".equals(state) ||"ACTIVE".equals(state))
I2PBote.getInstance().connectNow();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
mStateService = null;
}
};
private final IRouterStateCallback.Stub mStatusListener =
new IRouterStateCallback.Stub() {
public void stateChanged(String newState) throws RemoteException {
if ("STOPPING".equals(newState) ||
"MANUAL_STOPPING".equals(newState) ||
"MANUAL_QUITTING".equals(newState) ||
"NETWORK_STOPPING".equals(newState))
stopSelf();
}
};
// NewEmailListener
@Override