diff --git a/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPlugin.java b/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPlugin.java index 258085393..71992283e 100644 --- a/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPlugin.java +++ b/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPlugin.java @@ -31,6 +31,7 @@ import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ScheduledExecutorService; import java.util.logging.Logger; import javax.annotation.Nullable; @@ -66,6 +67,7 @@ class AndroidBluetoothPlugin extends BluetoothPlugin { private static final int MAX_DISCOVERY_MS = 10_000; + private final ScheduledExecutorService scheduler; private final AndroidExecutor androidExecutor; private final Context appContext; private final Clock clock; @@ -78,11 +80,13 @@ class AndroidBluetoothPlugin extends BluetoothPlugin { AndroidBluetoothPlugin(BluetoothConnectionLimiter connectionLimiter, TimeoutMonitor timeoutMonitor, Executor ioExecutor, - SecureRandom secureRandom, AndroidExecutor androidExecutor, - Context appContext, Clock clock, Backoff backoff, - PluginCallback callback, int maxLatency, int maxIdleTime) { + SecureRandom secureRandom, ScheduledExecutorService scheduler, + AndroidExecutor androidExecutor, Context appContext, Clock clock, + Backoff backoff, PluginCallback callback, int maxLatency, + int maxIdleTime) { super(connectionLimiter, timeoutMonitor, ioExecutor, secureRandom, backoff, callback, maxLatency, maxIdleTime); + this.scheduler = scheduler; this.androidExecutor = androidExecutor; this.appContext = appContext; this.clock = clock; @@ -177,7 +181,7 @@ class AndroidBluetoothPlugin extends BluetoothPlugin { private DuplexTransportConnection wrapSocket(BluetoothSocket s) throws IOException { return new AndroidBluetoothTransportConnection(this, connectionLimiter, - timeoutMonitor, appContext, s); + timeoutMonitor, appContext, scheduler, s); } @Override diff --git a/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPluginFactory.java b/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPluginFactory.java index 9ebabbd66..54fdde07b 100644 --- a/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPluginFactory.java +++ b/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPluginFactory.java @@ -16,6 +16,7 @@ import org.briarproject.bramble.api.system.Clock; import java.security.SecureRandom; import java.util.concurrent.Executor; +import java.util.concurrent.ScheduledExecutorService; import javax.annotation.concurrent.Immutable; @@ -32,6 +33,7 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory { private static final double BACKOFF_BASE = 1.2; private final Executor ioExecutor; + private final ScheduledExecutorService scheduler; private final AndroidExecutor androidExecutor; private final Context appContext; private final SecureRandom secureRandom; @@ -41,10 +43,12 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory { private final BackoffFactory backoffFactory; public AndroidBluetoothPluginFactory(Executor ioExecutor, + ScheduledExecutorService scheduler, AndroidExecutor androidExecutor, Context appContext, SecureRandom secureRandom, EventBus eventBus, Clock clock, TimeoutMonitor timeoutMonitor, BackoffFactory backoffFactory) { this.ioExecutor = ioExecutor; + this.scheduler = scheduler; this.androidExecutor = androidExecutor; this.appContext = appContext; this.secureRandom = secureRandom; @@ -72,7 +76,7 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory { MAX_POLLING_INTERVAL, BACKOFF_BASE); AndroidBluetoothPlugin plugin = new AndroidBluetoothPlugin( connectionLimiter, timeoutMonitor, ioExecutor, secureRandom, - androidExecutor, appContext, clock, backoff, + scheduler, androidExecutor, appContext, clock, backoff, callback, MAX_LATENCY, MAX_IDLE_TIME); eventBus.addListener(plugin); return plugin; diff --git a/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothTransportConnection.java b/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothTransportConnection.java index 54df326c9..4a8b13f2c 100644 --- a/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothTransportConnection.java +++ b/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothTransportConnection.java @@ -1,51 +1,52 @@ package org.briarproject.bramble.plugin.bluetooth; -import android.annotation.SuppressLint; import android.bluetooth.BluetoothSocket; import android.content.Context; import android.os.PowerManager; -import android.os.PowerManager.WakeLock; import org.briarproject.bramble.api.io.TimeoutMonitor; import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.plugin.Plugin; import org.briarproject.bramble.api.plugin.duplex.AbstractDuplexTransportConnection; +import org.briarproject.bramble.util.RenewableWakeLock; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.concurrent.ScheduledExecutorService; import static android.content.Context.POWER_SERVICE; import static android.os.PowerManager.PARTIAL_WAKE_LOCK; +import static java.util.concurrent.TimeUnit.MINUTES; import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull; import static org.briarproject.bramble.api.plugin.BluetoothConstants.PROP_ADDRESS; +import static org.briarproject.bramble.util.AndroidUtils.getWakeLockTag; import static org.briarproject.bramble.util.AndroidUtils.isValidBluetoothAddress; @NotNullByDefault class AndroidBluetoothTransportConnection extends AbstractDuplexTransportConnection { - private static final String WAKE_LOCK_TAG = - "org.briarproject.briar.android:bluetooth"; - private final BluetoothConnectionLimiter connectionLimiter; + private final RenewableWakeLock wakeLock; private final BluetoothSocket socket; private final InputStream in; - private final WakeLock wakeLock; - @SuppressLint("WakelockTimeout") AndroidBluetoothTransportConnection(Plugin plugin, BluetoothConnectionLimiter connectionLimiter, TimeoutMonitor timeoutMonitor, Context appContext, - BluetoothSocket socket) throws IOException { + ScheduledExecutorService scheduler, BluetoothSocket socket) + throws IOException { super(plugin); this.connectionLimiter = connectionLimiter; this.socket = socket; in = timeoutMonitor.createTimeoutInputStream( socket.getInputStream(), plugin.getMaxIdleTime() * 2); - PowerManager pm = (PowerManager) + PowerManager powerManager = (PowerManager) requireNonNull(appContext.getSystemService(POWER_SERVICE)); - wakeLock = pm.newWakeLock(PARTIAL_WAKE_LOCK, WAKE_LOCK_TAG); + String tag = getWakeLockTag(appContext); + wakeLock = new RenewableWakeLock(powerManager, scheduler, + PARTIAL_WAKE_LOCK, tag, 1, MINUTES); wakeLock.acquire(); String address = socket.getRemoteDevice().getAddress(); if (isValidBluetoothAddress(address)) remote.put(PROP_ADDRESS, address); diff --git a/bramble-android/src/main/java/org/briarproject/bramble/plugin/tor/AndroidTorPlugin.java b/bramble-android/src/main/java/org/briarproject/bramble/plugin/tor/AndroidTorPlugin.java index f36773615..bf1352479 100644 --- a/bramble-android/src/main/java/org/briarproject/bramble/plugin/tor/AndroidTorPlugin.java +++ b/bramble-android/src/main/java/org/briarproject/bramble/plugin/tor/AndroidTorPlugin.java @@ -27,6 +27,7 @@ import static android.content.Context.MODE_PRIVATE; import static android.content.Context.POWER_SERVICE; import static android.os.PowerManager.PARTIAL_WAKE_LOCK; import static java.util.concurrent.TimeUnit.MINUTES; +import static org.briarproject.bramble.util.AndroidUtils.getWakeLockTag; @MethodsNotNullByDefault @ParametersNotNullByDefault @@ -53,7 +54,7 @@ class AndroidTorPlugin extends TorPlugin { appContext.getSystemService(POWER_SERVICE); if (pm == null) throw new AssertionError(); wakeLock = new RenewableWakeLock(pm, scheduler, PARTIAL_WAKE_LOCK, - getWakeLockTag(), 1, MINUTES); + getWakeLockTag(appContext), 1, MINUTES); } @Override @@ -85,17 +86,4 @@ class AndroidTorPlugin extends TorPlugin { super.stop(); wakeLock.release(); } - - private String getWakeLockTag() { - PackageManager pm = appContext.getPackageManager(); - for (PackageInfo info : pm.getInstalledPackages(0)) { - String name = info.packageName.toLowerCase(); - if (name.startsWith("com.huawei.powergenie")) { - return "LocationManagerService"; - } else if (name.startsWith("com.evenwell.powermonitor")) { - return "AudioIn"; - } - } - return getClass().getSimpleName(); - } } diff --git a/bramble-android/src/main/java/org/briarproject/bramble/util/AndroidUtils.java b/bramble-android/src/main/java/org/briarproject/bramble/util/AndroidUtils.java index 4d5c8ea13..00ee3ea19 100644 --- a/bramble-android/src/main/java/org/briarproject/bramble/util/AndroidUtils.java +++ b/bramble-android/src/main/java/org/briarproject/bramble/util/AndroidUtils.java @@ -3,6 +3,8 @@ package org.briarproject.bramble.util; import android.annotation.SuppressLint; import android.bluetooth.BluetoothAdapter; import android.content.Context; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; import android.os.Build; import android.provider.Settings; @@ -117,4 +119,17 @@ public class AndroidUtils { if (SDK_INT < 24) return new String[] {"image/jpeg", "image/png"}; else return new String[] {"image/jpeg", "image/png", "image/gif"}; } + + public static String getWakeLockTag(Context ctx) { + PackageManager pm = ctx.getPackageManager(); + for (PackageInfo info : pm.getInstalledPackages(0)) { + String name = info.packageName.toLowerCase(); + if (name.startsWith("com.huawei.powergenie")) { + return "LocationManagerService"; + } else if (name.startsWith("com.evenwell.powermonitor")) { + return "AudioIn"; + } + } + return ctx.getPackageName(); + } } diff --git a/briar-android/src/main/java/org/briarproject/briar/android/AppModule.java b/briar-android/src/main/java/org/briarproject/briar/android/AppModule.java index 27a720d7f..7231f9260 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/AppModule.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/AppModule.java @@ -134,8 +134,8 @@ public class AppModule { TimeoutMonitor timeoutMonitor) { Context appContext = app.getApplicationContext(); DuplexPluginFactory bluetooth = new AndroidBluetoothPluginFactory( - ioExecutor, androidExecutor, appContext, random, eventBus, - clock, timeoutMonitor, backoffFactory); + ioExecutor, scheduler, androidExecutor, appContext, random, + eventBus, clock, timeoutMonitor, backoffFactory); DuplexPluginFactory tor = new AndroidTorPluginFactory(ioExecutor, scheduler, appContext, networkManager, locationUtils, eventBus, torSocketFactory, backoffFactory, resourceProvider,