- Move network waiter from a thread to the service handler
- Allow quitting while waiting - Move connected detection to utility class - Use connected info for displaying buttons
This commit is contained in:
@ -14,6 +14,7 @@ import android.view.MenuItem;
|
|||||||
import net.i2p.android.router.R;
|
import net.i2p.android.router.R;
|
||||||
import net.i2p.android.router.binder.RouterBinder;
|
import net.i2p.android.router.binder.RouterBinder;
|
||||||
import net.i2p.android.router.service.RouterService;
|
import net.i2p.android.router.service.RouterService;
|
||||||
|
import net.i2p.android.router.util.Util;
|
||||||
import net.i2p.router.CommSystemFacade;
|
import net.i2p.router.CommSystemFacade;
|
||||||
import net.i2p.router.NetworkDatabaseFacade;
|
import net.i2p.router.NetworkDatabaseFacade;
|
||||||
import net.i2p.router.Router;
|
import net.i2p.router.Router;
|
||||||
@ -145,7 +146,8 @@ public abstract class I2PActivityBase extends Activity {
|
|||||||
public boolean onPrepareOptionsMenu(Menu menu) {
|
public boolean onPrepareOptionsMenu(Menu menu) {
|
||||||
// add/hide items here
|
// add/hide items here
|
||||||
RouterService svc = _routerService;
|
RouterService svc = _routerService;
|
||||||
boolean showStart = (svc == null) || (!_isBound) || svc.canManualStart();
|
boolean showStart = ((svc == null) || (!_isBound) || svc.canManualStart()) &&
|
||||||
|
Util.isConnected(this);
|
||||||
MenuItem start = menu.findItem(R.id.menu_start);
|
MenuItem start = menu.findItem(R.id.menu_start);
|
||||||
start.setVisible(showStart);
|
start.setVisible(showStart);
|
||||||
start.setEnabled(showStart);
|
start.setEnabled(showStart);
|
||||||
|
@ -199,7 +199,8 @@ public class MainActivity extends I2PActivityBase {
|
|||||||
|
|
||||||
private void updateVisibility() {
|
private void updateVisibility() {
|
||||||
RouterService svc = _routerService;
|
RouterService svc = _routerService;
|
||||||
boolean showStart = (svc == null) || (!_isBound) || svc.canManualStart();
|
boolean showStart = ((svc == null) || (!_isBound) || svc.canManualStart()) &&
|
||||||
|
Util.isConnected(this);
|
||||||
Button start = (Button) findViewById(R.id.router_start_button);
|
Button start = (Button) findViewById(R.id.router_start_button);
|
||||||
start.setVisibility(showStart ? View.VISIBLE : View.INVISIBLE);
|
start.setVisibility(showStart ? View.VISIBLE : View.INVISIBLE);
|
||||||
|
|
||||||
@ -214,7 +215,10 @@ public class MainActivity extends I2PActivityBase {
|
|||||||
private void updateStatus() {
|
private void updateStatus() {
|
||||||
RouterContext ctx = getRouterContext();
|
RouterContext ctx = getRouterContext();
|
||||||
TextView tv = (TextView) findViewById(R.id.main_status_text);
|
TextView tv = (TextView) findViewById(R.id.main_status_text);
|
||||||
if (ctx != null) {
|
if (!Util.isConnected(this)) {
|
||||||
|
tv.setText("No Internet connection is available");
|
||||||
|
tv.setVisibility(View.VISIBLE);
|
||||||
|
} else if (ctx != null) {
|
||||||
int active = ctx.commSystem().countActivePeers();
|
int active = ctx.commSystem().countActivePeers();
|
||||||
int known = Math.max(ctx.netDb().getKnownRouters() - 1, 0);
|
int known = Math.max(ctx.netDb().getKnownRouters() - 1, 0);
|
||||||
int inEx = ctx.tunnelManager().getFreeTunnelCount();
|
int inEx = ctx.tunnelManager().getFreeTunnelCount();
|
||||||
|
@ -8,11 +8,11 @@ import android.content.IntentFilter;
|
|||||||
import android.content.ServiceConnection;
|
import android.content.ServiceConnection;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
import android.net.NetworkInfo;
|
import android.net.NetworkInfo;
|
||||||
import android.os.Build;
|
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
|
||||||
import net.i2p.android.router.binder.RouterBinder;
|
import net.i2p.android.router.binder.RouterBinder;
|
||||||
import net.i2p.android.router.service.RouterService;
|
import net.i2p.android.router.service.RouterService;
|
||||||
|
import net.i2p.android.router.util.Util;
|
||||||
|
|
||||||
public class I2PReceiver extends BroadcastReceiver {
|
public class I2PReceiver extends BroadcastReceiver {
|
||||||
private final Context _context;
|
private final Context _context;
|
||||||
@ -21,7 +21,6 @@ public class I2PReceiver extends BroadcastReceiver {
|
|||||||
private int _unconnectedCount;
|
private int _unconnectedCount;
|
||||||
private RouterService _routerService;
|
private RouterService _routerService;
|
||||||
private ServiceConnection _connection;
|
private ServiceConnection _connection;
|
||||||
private static final boolean _isEmulator = Build.MODEL.equals("sdk");
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers itself
|
* Registers itself
|
||||||
@ -29,13 +28,12 @@ public class I2PReceiver extends BroadcastReceiver {
|
|||||||
public I2PReceiver(Context context) {
|
public I2PReceiver(Context context) {
|
||||||
super();
|
super();
|
||||||
_context = context;
|
_context = context;
|
||||||
getInfo();
|
|
||||||
IntentFilter intents = new IntentFilter();
|
IntentFilter intents = new IntentFilter();
|
||||||
intents.addAction(Intent.ACTION_TIME_CHANGED);
|
intents.addAction(Intent.ACTION_TIME_CHANGED);
|
||||||
intents.addAction(Intent.ACTION_TIME_TICK); // once per minute, for testing
|
intents.addAction(Intent.ACTION_TIME_TICK); // once per minute, for testing
|
||||||
intents.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
|
intents.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
|
||||||
context.registerReceiver(this, intents);
|
context.registerReceiver(this, intents);
|
||||||
_wasConnected = isConnected();
|
_wasConnected = Util.isConnected(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
@ -59,7 +57,7 @@ public class I2PReceiver extends BroadcastReceiver {
|
|||||||
|
|
||||||
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION) ||
|
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION) ||
|
||||||
action.equals(Intent.ACTION_TIME_TICK)) {
|
action.equals(Intent.ACTION_TIME_TICK)) {
|
||||||
boolean connected = isConnected();
|
boolean connected = Util.isConnected(context);
|
||||||
if (_wasConnected && !connected) {
|
if (_wasConnected && !connected) {
|
||||||
// notify + 2 timer ticks
|
// notify + 2 timer ticks
|
||||||
if (++_unconnectedCount >= 3) {
|
if (++_unconnectedCount >= 3) {
|
||||||
@ -80,22 +78,7 @@ public class I2PReceiver extends BroadcastReceiver {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isConnected() {
|
/****
|
||||||
// emulator always returns null NetworkInfo
|
|
||||||
if (_isEmulator)
|
|
||||||
return true;
|
|
||||||
NetworkInfo current = getInfo();
|
|
||||||
return current != null && current.isConnected();
|
|
||||||
}
|
|
||||||
|
|
||||||
private NetworkInfo getInfo() {
|
|
||||||
ConnectivityManager cm = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
||||||
NetworkInfo current = cm.getActiveNetworkInfo();
|
|
||||||
System.err.println("Current network info:");
|
|
||||||
printInfo(current);
|
|
||||||
return current;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void printInfo(NetworkInfo ni) {
|
private static void printInfo(NetworkInfo ni) {
|
||||||
if (ni == null) {
|
if (ni == null) {
|
||||||
System.err.println("Network info is null");
|
System.err.println("Network info is null");
|
||||||
@ -113,6 +96,7 @@ public class I2PReceiver extends BroadcastReceiver {
|
|||||||
" failover: " + ni.isFailover());
|
" failover: " + ni.isFailover());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
****/
|
||||||
|
|
||||||
private boolean bindRouter() {
|
private boolean bindRouter() {
|
||||||
Intent intent = new Intent();
|
Intent intent = new Intent();
|
||||||
|
@ -15,6 +15,7 @@ import java.util.List;
|
|||||||
import net.i2p.android.router.R;
|
import net.i2p.android.router.R;
|
||||||
import net.i2p.android.router.binder.RouterBinder;
|
import net.i2p.android.router.binder.RouterBinder;
|
||||||
import net.i2p.android.router.receiver.I2PReceiver;
|
import net.i2p.android.router.receiver.I2PReceiver;
|
||||||
|
import net.i2p.android.router.util.Util;
|
||||||
import net.i2p.data.DataHelper;
|
import net.i2p.data.DataHelper;
|
||||||
import net.i2p.router.Job;
|
import net.i2p.router.Job;
|
||||||
import net.i2p.router.Router;
|
import net.i2p.router.Router;
|
||||||
@ -77,7 +78,7 @@ public class RouterService extends Service {
|
|||||||
//return START_STICKY;
|
//return START_STICKY;
|
||||||
return START_NOT_STICKY;
|
return START_NOT_STICKY;
|
||||||
_receiver = new I2PReceiver(this);
|
_receiver = new I2PReceiver(this);
|
||||||
if (_receiver.isConnected()) {
|
if (Util.isConnected(this)) {
|
||||||
_statusBar.update("I2P is starting up");
|
_statusBar.update("I2P is starting up");
|
||||||
_state = State.STARTING;
|
_state = State.STARTING;
|
||||||
_starterThread = new Thread(new Starter());
|
_starterThread = new Thread(new Starter());
|
||||||
@ -99,28 +100,22 @@ public class RouterService extends Service {
|
|||||||
/** maybe this goes away when the receiver can bind to us */
|
/** maybe this goes away when the receiver can bind to us */
|
||||||
private class Waiter implements Runnable {
|
private class Waiter implements Runnable {
|
||||||
public void run() {
|
public void run() {
|
||||||
System.err.println(MARKER + this + " waiter thread" +
|
System.err.println(MARKER + this + " waiter handler" +
|
||||||
" Current state is: " + _state);
|
" Current state is: " + _state);
|
||||||
while (_state == State.WAITING) {
|
if (_state == State.WAITING) {
|
||||||
try {
|
if (Util.isConnected(RouterService.this)) {
|
||||||
Thread.sleep(30*1000);
|
|
||||||
} catch (InterruptedException ie) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_receiver.isConnected()) {
|
|
||||||
synchronized (_stateLock) {
|
synchronized (_stateLock) {
|
||||||
if (_state != State.WAITING)
|
if (_state != State.WAITING)
|
||||||
break;
|
return;
|
||||||
_statusBar.update("Network connected, I2P is starting up");
|
_statusBar.update("Network connected, I2P is starting up");
|
||||||
_state = State.STARTING;
|
_state = State.STARTING;
|
||||||
_starterThread = new Thread(new Starter());
|
_starterThread = new Thread(new Starter());
|
||||||
_starterThread.start();
|
_starterThread.start();
|
||||||
}
|
}
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
|
_handler.postDelayed(this, 15*1000);
|
||||||
}
|
}
|
||||||
System.err.println("waiter finished");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,7 +231,7 @@ public class RouterService extends Service {
|
|||||||
synchronized (_stateLock) {
|
synchronized (_stateLock) {
|
||||||
if (!canManualStop())
|
if (!canManualStop())
|
||||||
return;
|
return;
|
||||||
if (_state == State.WAITING || _state == State.STARTING)
|
if (_state == State.STARTING)
|
||||||
_starterThread.interrupt();
|
_starterThread.interrupt();
|
||||||
if (_state == State.STARTING || _state == State.RUNNING) {
|
if (_state == State.STARTING || _state == State.RUNNING) {
|
||||||
_statusBar.update("Stopping I2P");
|
_statusBar.update("Stopping I2P");
|
||||||
@ -255,12 +250,15 @@ public class RouterService extends Service {
|
|||||||
synchronized (_stateLock) {
|
synchronized (_stateLock) {
|
||||||
if (!canManualStop())
|
if (!canManualStop())
|
||||||
return;
|
return;
|
||||||
if (_state == State.WAITING || _state == State.STARTING)
|
if (_state == State.STARTING)
|
||||||
_starterThread.interrupt();
|
_starterThread.interrupt();
|
||||||
if (_state == State.STARTING || _state == State.RUNNING) {
|
if (_state == State.STARTING || _state == State.RUNNING) {
|
||||||
_statusBar.update("Quitting I2P");
|
_statusBar.update("Quitting I2P");
|
||||||
Thread stopperThread = new Thread(new Stopper(State.MANUAL_QUITTING, State.MANUAL_QUITTED));
|
Thread stopperThread = new Thread(new Stopper(State.MANUAL_QUITTING, State.MANUAL_QUITTED));
|
||||||
stopperThread.start();
|
stopperThread.start();
|
||||||
|
} else if (_state == State.WAITING) {
|
||||||
|
_state = State.MANUAL_QUITTING;
|
||||||
|
(new FinalShutdownHook()).run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -272,7 +270,7 @@ public class RouterService extends Service {
|
|||||||
System.err.println("networkStop called" +
|
System.err.println("networkStop called" +
|
||||||
" Current state is: " + _state);
|
" Current state is: " + _state);
|
||||||
synchronized (_stateLock) {
|
synchronized (_stateLock) {
|
||||||
if (_state == State.WAITING || _state == State.STARTING)
|
if (_state == State.STARTING)
|
||||||
_starterThread.interrupt();
|
_starterThread.interrupt();
|
||||||
if (_state == State.STARTING || _state == State.RUNNING) {
|
if (_state == State.STARTING || _state == State.RUNNING) {
|
||||||
_statusBar.update("Network disconnected, stopping I2P");
|
_statusBar.update("Network disconnected, stopping I2P");
|
||||||
@ -327,7 +325,7 @@ public class RouterService extends Service {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
synchronized (_stateLock) {
|
synchronized (_stateLock) {
|
||||||
if (_state == State.WAITING || _state == State.STARTING)
|
if (_state == State.STARTING)
|
||||||
_starterThread.interrupt();
|
_starterThread.interrupt();
|
||||||
if (_state == State.STARTING || _state == State.RUNNING) {
|
if (_state == State.STARTING || _state == State.RUNNING) {
|
||||||
// should this be in a thread?
|
// should this be in a thread?
|
||||||
@ -394,7 +392,7 @@ public class RouterService extends Service {
|
|||||||
synchronized (_stateLock) {
|
synchronized (_stateLock) {
|
||||||
// null out to release the memory
|
// null out to release the memory
|
||||||
_context = null;
|
_context = null;
|
||||||
if (_state == State.WAITING || _state == State.STARTING)
|
if (_state == State.STARTING)
|
||||||
_starterThread.interrupt();
|
_starterThread.interrupt();
|
||||||
if (_state == State.WAITING || _state == State.STARTING ||
|
if (_state == State.WAITING || _state == State.STARTING ||
|
||||||
_state == State.RUNNING)
|
_state == State.RUNNING)
|
||||||
@ -421,15 +419,14 @@ public class RouterService extends Service {
|
|||||||
synchronized (_stateLock) {
|
synchronized (_stateLock) {
|
||||||
// null out to release the memory
|
// null out to release the memory
|
||||||
_context = null;
|
_context = null;
|
||||||
if (_state == State.WAITING || _state == State.STARTING)
|
if (_state == State.STARTING)
|
||||||
_starterThread.interrupt();
|
_starterThread.interrupt();
|
||||||
if (_state == State.MANUAL_STOPPING) {
|
if (_state == State.MANUAL_STOPPING) {
|
||||||
_state = State.MANUAL_STOPPED;
|
_state = State.MANUAL_STOPPED;
|
||||||
} else if (_state == State.NETWORK_STOPPING) {
|
} else if (_state == State.NETWORK_STOPPING) {
|
||||||
// start waiter thread
|
// start waiter handler
|
||||||
_state = State.WAITING;
|
_state = State.WAITING;
|
||||||
_starterThread = new Thread(new Waiter());
|
_handler.postDelayed(new Waiter(), 10*1000);
|
||||||
_starterThread.start();
|
|
||||||
} else if (_state == State.STARTING || _state == State.RUNNING ||
|
} else if (_state == State.STARTING || _state == State.RUNNING ||
|
||||||
_state == State.STOPPING) {
|
_state == State.STOPPING) {
|
||||||
System.err.println(this + " died of unknown causes");
|
System.err.println(this + " died of unknown causes");
|
||||||
|
@ -3,8 +3,12 @@ package net.i2p.android.router.util;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.pm.PackageInfo;
|
import android.content.pm.PackageInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
import android.net.ConnectivityManager;
|
||||||
|
import android.net.NetworkInfo;
|
||||||
|
import android.os.Build;
|
||||||
|
|
||||||
public abstract class Util {
|
public abstract class Util {
|
||||||
|
private static final boolean _isEmulator = Build.MODEL.equals("sdk");
|
||||||
|
|
||||||
public static String getOurVersion(Context ctx) {
|
public static String getOurVersion(Context ctx) {
|
||||||
PackageManager pm = ctx.getPackageManager();
|
PackageManager pm = ctx.getPackageManager();
|
||||||
@ -20,4 +24,19 @@ public abstract class Util {
|
|||||||
} catch (Exception e) {}
|
} catch (Exception e) {}
|
||||||
return "??";
|
return "??";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean isConnected(Context ctx) {
|
||||||
|
// emulator always returns null NetworkInfo
|
||||||
|
if (_isEmulator)
|
||||||
|
return true;
|
||||||
|
NetworkInfo current = getNetworkInfo(ctx);
|
||||||
|
return current != null && current.isConnected();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static NetworkInfo getNetworkInfo(Context ctx) {
|
||||||
|
ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
|
NetworkInfo current = cm.getActiveNetworkInfo();
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user