Hold a wake lock while Bluetooth connections are open.

This commit is contained in:
akwizgran
2020-06-05 14:53:30 +01:00
parent 53d4b7a0df
commit 69d94c9f29
2 changed files with 20 additions and 3 deletions

View File

@@ -177,7 +177,7 @@ class AndroidBluetoothPlugin extends BluetoothPlugin<BluetoothServerSocket> {
private DuplexTransportConnection wrapSocket(BluetoothSocket s)
throws IOException {
return new AndroidBluetoothTransportConnection(this, connectionLimiter,
timeoutMonitor, s);
timeoutMonitor, appContext, s);
}
@Override

View File

@@ -1,6 +1,10 @@
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;
@@ -11,6 +15,9 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import static android.content.Context.POWER_SERVICE;
import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
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.isValidBluetoothAddress;
@@ -18,19 +25,28 @@ import static org.briarproject.bramble.util.AndroidUtils.isValidBluetoothAddress
class AndroidBluetoothTransportConnection
extends AbstractDuplexTransportConnection {
private static final String WAKE_LOCK_TAG =
"org.briarproject.briar.android:bluetooth";
private final BluetoothConnectionLimiter connectionLimiter;
private final BluetoothSocket socket;
private final InputStream in;
private final WakeLock wakeLock;
@SuppressLint("WakelockTimeout")
AndroidBluetoothTransportConnection(Plugin plugin,
BluetoothConnectionLimiter connectionLimiter,
TimeoutMonitor timeoutMonitor, BluetoothSocket socket)
throws IOException {
TimeoutMonitor timeoutMonitor, Context appContext,
BluetoothSocket socket) throws IOException {
super(plugin);
this.connectionLimiter = connectionLimiter;
this.socket = socket;
in = timeoutMonitor.createTimeoutInputStream(
socket.getInputStream(), plugin.getMaxIdleTime() * 2);
PowerManager pm = (PowerManager)
requireNonNull(appContext.getSystemService(POWER_SERVICE));
wakeLock = pm.newWakeLock(PARTIAL_WAKE_LOCK, WAKE_LOCK_TAG);
wakeLock.acquire();
String address = socket.getRemoteDevice().getAddress();
if (isValidBluetoothAddress(address)) remote.put(PROP_ADDRESS, address);
}
@@ -50,6 +66,7 @@ class AndroidBluetoothTransportConnection
try {
socket.close();
} finally {
wakeLock.release();
connectionLimiter.connectionClosed(this);
}
}