diff --git a/briar-android/AndroidManifest.xml b/briar-android/AndroidManifest.xml index c9784c094a4e43de85606aef072033dc337d6921..4ffddcb7941655e914435692aac6c3b0867a5011 100644 --- a/briar-android/AndroidManifest.xml +++ b/briar-android/AndroidManifest.xml @@ -24,6 +24,7 @@ <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_LOGS"/> <uses-permission android:name="android.permission.VIBRATE" /> + <uses-permission android:name="android.permission.WAKE_LOCK" /> <!-- Since API 23, this is needed to add contacts via Bluetooth --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> diff --git a/briar-android/src/org/briarproject/plugins/tor/TorPlugin.java b/briar-android/src/org/briarproject/plugins/tor/TorPlugin.java index 329985202c1c2679c61b9a260c79bfda7decff5b..63ccc6ba57ea7b9d7909c248207d16f81e107bad 100644 --- a/briar-android/src/org/briarproject/plugins/tor/TorPlugin.java +++ b/briar-android/src/org/briarproject/plugins/tor/TorPlugin.java @@ -7,6 +7,7 @@ import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.FileObserver; +import android.os.PowerManager; import net.freehaven.tor.control.EventHandler; import net.freehaven.tor.control.TorControlConnection; @@ -57,9 +58,11 @@ import java.util.zip.ZipInputStream; import static android.content.Context.CONNECTIVITY_SERVICE; import static android.content.Context.MODE_PRIVATE; +import static android.content.Context.POWER_SERVICE; import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; import static android.net.ConnectivityManager.EXTRA_NO_CONNECTIVITY; import static android.net.ConnectivityManager.TYPE_WIFI; +import static android.os.PowerManager.PARTIAL_WAKE_LOCK; import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.logging.Level.INFO; import static java.util.logging.Level.WARNING; @@ -94,6 +97,7 @@ class TorPlugin implements DuplexPlugin, EventHandler, private final File cookieFile, hostnameFile; private final AtomicBoolean circuitBuilt; private final AtomicInteger descriptorsPublished; + private final PowerManager.WakeLock wakeLock; private volatile boolean running = false, networkEnabled = false; private volatile boolean bootstrapped = false; @@ -132,6 +136,10 @@ class TorPlugin implements DuplexPlugin, EventHandler, hostnameFile = new File(torDirectory, "hs/hostname"); circuitBuilt = new AtomicBoolean(false); descriptorsPublished = new AtomicInteger(0); + Object o = appContext.getSystemService(POWER_SERVICE); + PowerManager pm = (PowerManager) o; + wakeLock = pm.newWakeLock(PARTIAL_WAKE_LOCK, "TorPlugin"); + wakeLock.setReferenceCounted(false); } public TransportId getId() { @@ -488,6 +496,7 @@ class TorPlugin implements DuplexPlugin, EventHandler, private void enableNetwork(boolean enable) throws IOException { if (!running) return; if (LOG.isLoggable(INFO)) LOG.info("Enabling network: " + enable); + if (enable) wakeLock.acquire(); if (!enable) { circuitBuilt.set(false); descriptorsPublished.set(0); @@ -496,6 +505,7 @@ class TorPlugin implements DuplexPlugin, EventHandler, } networkEnabled = enable; controlConnection.setConf("DisableNetwork", enable ? "0" : "1"); + if (!enable) wakeLock.release(); } public void stop() throws IOException { @@ -517,6 +527,7 @@ class TorPlugin implements DuplexPlugin, EventHandler, } catch (IOException e) { if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); } + wakeLock.release(); } public boolean isRunning() { @@ -709,15 +720,18 @@ class TorPlugin implements DuplexPlugin, EventHandler, @Override public void onReceive(Context ctx, Intent i) { if (!running) return; - online = !i.getBooleanExtra(EXTRA_NO_CONNECTIVITY, false); - // 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; - connectedToWifi = (net != null && net.getType() == TYPE_WIFI - && net.isConnected()); - updateConnectionStatus(); + if (CONNECTIVITY_ACTION.equals(i.getAction())) { + LOG.info("Detected connectivity change"); + online = !i.getBooleanExtra(EXTRA_NO_CONNECTIVITY, false); + // 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; + connectedToWifi = (net != null && net.getType() == TYPE_WIFI + && net.isConnected()); + updateConnectionStatus(); + } } } }