Better connectivity detection for the Tor plugin.

This commit is contained in:
akwizgran
2014-04-04 19:54:39 +01:00
parent a5ac325ae8
commit a563fcc192

View File

@@ -1,5 +1,6 @@
package org.briarproject.plugins.tor; package org.briarproject.plugins.tor;
import static android.content.Context.CONNECTIVITY_SERVICE;
import static android.content.Context.MODE_PRIVATE; import static android.content.Context.MODE_PRIVATE;
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
import static android.net.ConnectivityManager.EXTRA_NO_CONNECTIVITY; import static android.net.ConnectivityManager.EXTRA_NO_CONNECTIVITY;
@@ -50,6 +51,8 @@ import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build; import android.os.Build;
import android.os.FileObserver; import android.os.FileObserver;
@@ -492,7 +495,7 @@ class TorPlugin implements DuplexPlugin, EventHandler {
} }
private void acceptContactConnections(ServerSocket ss) { private void acceptContactConnections(ServerSocket ss) {
while(true) { while(running) {
Socket s; Socket s;
try { try {
s = ss.accept(); s = ss.accept();
@@ -505,7 +508,6 @@ class TorPlugin implements DuplexPlugin, EventHandler {
LOG.info("Connection received"); LOG.info("Connection received");
TorTransportConnection conn = new TorTransportConnection(this, s); TorTransportConnection conn = new TorTransportConnection(this, s);
callback.incomingConnectionCreated(conn); callback.incomingConnectionCreated(conn);
if(!running) return;
} }
} }
@@ -518,7 +520,7 @@ class TorPlugin implements DuplexPlugin, EventHandler {
public void stop() throws IOException { public void stop() throws IOException {
running = false; running = false;
if(socket != null) tryToClose(socket); tryToClose(socket);
if(networkStateReceiver != null) if(networkStateReceiver != null)
appContext.unregisterReceiver(networkStateReceiver); appContext.unregisterReceiver(networkStateReceiver);
try { try {
@@ -552,7 +554,7 @@ class TorPlugin implements DuplexPlugin, EventHandler {
} }
public void poll(Collection<ContactId> connected) { public void poll(Collection<ContactId> connected) {
if(!running) return; if(!isRunning()) return;
Map<ContactId, TransportProperties> remote = Map<ContactId, TransportProperties> remote =
callback.getRemoteProperties(); callback.getRemoteProperties();
for(final ContactId c : remote.keySet()) { for(final ContactId c : remote.keySet()) {
@@ -571,7 +573,7 @@ class TorPlugin implements DuplexPlugin, EventHandler {
} }
public DuplexTransportConnection createConnection(ContactId c) { public DuplexTransportConnection createConnection(ContactId c) {
if(!running) return null; if(!isRunning()) return null;
TransportProperties p = callback.getRemoteProperties().get(c); TransportProperties p = callback.getRemoteProperties().get(c);
if(p == null) return null; if(p == null) return null;
String onion = p.get("onion"); String onion = p.get("onion");
@@ -646,10 +648,17 @@ class TorPlugin implements DuplexPlugin, EventHandler {
@Override @Override
public void onReceive(Context ctx, Intent i) { public void onReceive(Context ctx, Intent i) {
// Note: Some devices fail to set this extra if(!running) return;
boolean online = !i.getBooleanExtra(EXTRA_NO_CONNECTIVITY, false); boolean online = !i.getBooleanExtra(EXTRA_NO_CONNECTIVITY, false);
if(online) {
// Some devices fail to set EXTRA_NO_CONNECTIVITY, double check
Object o = ctx.getSystemService(CONNECTIVITY_SERVICE);
ConnectivityManager cm = (ConnectivityManager) o;
NetworkInfo net = cm.getActiveNetworkInfo();
if(net == null || !net.isConnected()) online = false;
}
String country = locationUtils.getCurrentCountry(); String country = locationUtils.getCurrentCountry();
if(LOG.isLoggable(INFO)){ if(LOG.isLoggable(INFO)) {
LOG.info("Online: " + online); LOG.info("Online: " + online);
if("".equals(country)) LOG.info("Country code unknown"); if("".equals(country)) LOG.info("Country code unknown");
else LOG.info("Country code: " + country); else LOG.info("Country code: " + country);