mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 13:49:53 +01:00
Don't enable or disable the Bluetooth adapter automatically.
This commit is contained in:
@@ -71,7 +71,6 @@ class AndroidBluetoothPlugin
|
|||||||
private final Application app;
|
private final Application app;
|
||||||
private final Clock clock;
|
private final Clock clock;
|
||||||
|
|
||||||
private volatile boolean wasEnabledByUs = false;
|
|
||||||
private volatile BluetoothStateReceiver receiver = null;
|
private volatile BluetoothStateReceiver receiver = null;
|
||||||
|
|
||||||
// Non-null if the plugin started successfully
|
// Non-null if the plugin started successfully
|
||||||
@@ -133,38 +132,6 @@ class AndroidBluetoothPlugin
|
|||||||
return adapter != null && adapter.isEnabled();
|
return adapter != null && adapter.isEnabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
void enableAdapter() {
|
|
||||||
if (adapter != null && !adapter.isEnabled()) {
|
|
||||||
if (adapter.enable()) {
|
|
||||||
LOG.info("Enabling Bluetooth");
|
|
||||||
wasEnabledByUs = true;
|
|
||||||
} else {
|
|
||||||
LOG.info("Could not enable Bluetooth");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
void disableAdapterIfEnabledByUs() {
|
|
||||||
if (isAdapterEnabled() && wasEnabledByUs) {
|
|
||||||
if (adapter.disable()) LOG.info("Disabling Bluetooth");
|
|
||||||
else LOG.info("Could not disable Bluetooth");
|
|
||||||
wasEnabledByUs = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
void setEnabledByUs() {
|
|
||||||
wasEnabledByUs = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
void onAdapterDisabled() {
|
|
||||||
super.onAdapterDisabled();
|
|
||||||
wasEnabledByUs = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Nullable
|
@Nullable
|
||||||
String getBluetoothAddress() {
|
String getBluetoothAddress() {
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
package org.briarproject.bramble.api.plugin.event;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.event.Event;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
|
|
||||||
import javax.annotation.concurrent.Immutable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An event that informs the Bluetooth plugin that we have enabled the
|
|
||||||
* Bluetooth adapter.
|
|
||||||
*/
|
|
||||||
@Immutable
|
|
||||||
@NotNullByDefault
|
|
||||||
public class BluetoothEnabledEvent extends Event {
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
package org.briarproject.bramble.api.plugin.event;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.event.Event;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
|
|
||||||
import javax.annotation.concurrent.Immutable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An event that asks the Bluetooth plugin to disable the Bluetooth adapter if
|
|
||||||
* we previously enabled it.
|
|
||||||
*/
|
|
||||||
@Immutable
|
|
||||||
@NotNullByDefault
|
|
||||||
public class DisableBluetoothEvent extends Event {
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
package org.briarproject.bramble.api.plugin.event;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.event.Event;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
|
|
||||||
import javax.annotation.concurrent.Immutable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An event that asks the Bluetooth plugin to enable the Bluetooth adapter.
|
|
||||||
*/
|
|
||||||
@Immutable
|
|
||||||
@NotNullByDefault
|
|
||||||
public class EnableBluetoothEvent extends Event {
|
|
||||||
}
|
|
||||||
@@ -21,9 +21,6 @@ import org.briarproject.bramble.api.plugin.PluginException;
|
|||||||
import org.briarproject.bramble.api.plugin.TransportId;
|
import org.briarproject.bramble.api.plugin.TransportId;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||||
import org.briarproject.bramble.api.plugin.event.BluetoothEnabledEvent;
|
|
||||||
import org.briarproject.bramble.api.plugin.event.DisableBluetoothEvent;
|
|
||||||
import org.briarproject.bramble.api.plugin.event.EnableBluetoothEvent;
|
|
||||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||||
import org.briarproject.bramble.api.properties.event.RemoteTransportPropertiesUpdatedEvent;
|
import org.briarproject.bramble.api.properties.event.RemoteTransportPropertiesUpdatedEvent;
|
||||||
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
|
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
|
||||||
@@ -93,12 +90,6 @@ abstract class BluetoothPlugin<S, SS> implements DuplexPlugin, EventListener {
|
|||||||
|
|
||||||
abstract boolean isAdapterEnabled();
|
abstract boolean isAdapterEnabled();
|
||||||
|
|
||||||
abstract void enableAdapter();
|
|
||||||
|
|
||||||
abstract void disableAdapterIfEnabledByUs();
|
|
||||||
|
|
||||||
abstract void setEnabledByUs();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the local Bluetooth address, or null if no valid address can
|
* Returns the local Bluetooth address, or null if no valid address can
|
||||||
* be found.
|
* be found.
|
||||||
@@ -189,10 +180,7 @@ abstract class BluetoothPlugin<S, SS> implements DuplexPlugin, EventListener {
|
|||||||
throw new PluginException(e);
|
throw new PluginException(e);
|
||||||
}
|
}
|
||||||
updateProperties();
|
updateProperties();
|
||||||
if (enabledByUser) {
|
if (enabledByUser && isAdapterEnabled()) bind();
|
||||||
if (isAdapterEnabled()) bind();
|
|
||||||
else enableAdapter();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void bind() {
|
private void bind() {
|
||||||
@@ -319,7 +307,6 @@ abstract class BluetoothPlugin<S, SS> implements DuplexPlugin, EventListener {
|
|||||||
public void stop() {
|
public void stop() {
|
||||||
SS ss = state.setStopped();
|
SS ss = state.setStopped();
|
||||||
tryToClose(ss);
|
tryToClose(ss);
|
||||||
disableAdapterIfEnabledByUs();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -490,13 +477,7 @@ abstract class BluetoothPlugin<S, SS> implements DuplexPlugin, EventListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void eventOccurred(Event e) {
|
public void eventOccurred(Event e) {
|
||||||
if (e instanceof EnableBluetoothEvent) {
|
if (e instanceof SettingsUpdatedEvent) {
|
||||||
ioExecutor.execute(this::enableAdapter);
|
|
||||||
} else if (e instanceof DisableBluetoothEvent) {
|
|
||||||
ioExecutor.execute(this::disableAdapterIfEnabledByUs);
|
|
||||||
} else if (e instanceof BluetoothEnabledEvent) {
|
|
||||||
setEnabledByUs();
|
|
||||||
} else if (e instanceof SettingsUpdatedEvent) {
|
|
||||||
SettingsUpdatedEvent s = (SettingsUpdatedEvent) e;
|
SettingsUpdatedEvent s = (SettingsUpdatedEvent) e;
|
||||||
if (s.getNamespace().equals(ID.getString()))
|
if (s.getNamespace().equals(ID.getString()))
|
||||||
ioExecutor.execute(() -> onSettingsUpdated(s.getSettings()));
|
ioExecutor.execute(() -> onSettingsUpdated(s.getSettings()));
|
||||||
@@ -522,11 +503,13 @@ abstract class BluetoothPlugin<S, SS> implements DuplexPlugin, EventListener {
|
|||||||
if (ss != null) {
|
if (ss != null) {
|
||||||
LOG.info("Disabled by user, closing server socket");
|
LOG.info("Disabled by user, closing server socket");
|
||||||
tryToClose(ss);
|
tryToClose(ss);
|
||||||
disableAdapterIfEnabledByUs();
|
|
||||||
} else if (s == INACTIVE) {
|
} else if (s == INACTIVE) {
|
||||||
LOG.info("Enabled by user, opening server socket");
|
if (isAdapterEnabled()) {
|
||||||
if (isAdapterEnabled()) bind();
|
LOG.info("Enabled by user, opening server socket");
|
||||||
else enableAdapter();
|
bind();
|
||||||
|
} else {
|
||||||
|
LOG.info("Enabled by user but adapter is disabled");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -62,22 +62,6 @@ class JavaBluetoothPlugin
|
|||||||
return localDevice != null && LocalDevice.isPowerOn();
|
return localDevice != null && LocalDevice.isPowerOn();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
void enableAdapter() {
|
|
||||||
// Nothing we can do on this platform
|
|
||||||
LOG.info("Could not enable Bluetooth");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
void disableAdapterIfEnabledByUs() {
|
|
||||||
// We didn't enable it so we don't need to disable it
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
void setEnabledByUs() {
|
|
||||||
// Irrelevant on this platform
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
String getBluetoothAddress() {
|
String getBluetoothAddress() {
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ import org.briarproject.bramble.api.plugin.LanTcpConstants;
|
|||||||
import org.briarproject.bramble.api.plugin.Plugin;
|
import org.briarproject.bramble.api.plugin.Plugin;
|
||||||
import org.briarproject.bramble.api.plugin.Plugin.State;
|
import org.briarproject.bramble.api.plugin.Plugin.State;
|
||||||
import org.briarproject.bramble.api.plugin.PluginManager;
|
import org.briarproject.bramble.api.plugin.PluginManager;
|
||||||
import org.briarproject.bramble.api.plugin.event.BluetoothEnabledEvent;
|
|
||||||
import org.briarproject.bramble.api.plugin.event.TransportStateEvent;
|
import org.briarproject.bramble.api.plugin.event.TransportStateEvent;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||||
@@ -118,13 +117,6 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
|||||||
*/
|
*/
|
||||||
private boolean continueClicked = false;
|
private boolean continueClicked = false;
|
||||||
|
|
||||||
/**
|
|
||||||
* Records whether the Bluetooth adapter was already enabled before we
|
|
||||||
* asked for Bluetooth discoverability, so we know whether to broadcast a
|
|
||||||
* {@link BluetoothEnabledEvent}.
|
|
||||||
*/
|
|
||||||
private boolean wasAdapterEnabled = false;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Records whether we've enabled the wifi plugin so we don't enable it more
|
* Records whether we've enabled the wifi plugin so we don't enable it more
|
||||||
* than once.
|
* than once.
|
||||||
@@ -274,7 +266,6 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
|||||||
if (i.resolveActivity(getPackageManager()) != null) {
|
if (i.resolveActivity(getPackageManager()) != null) {
|
||||||
LOG.info("Asking for Bluetooth discoverability");
|
LOG.info("Asking for Bluetooth discoverability");
|
||||||
bluetoothDecision = BluetoothDecision.WAITING;
|
bluetoothDecision = BluetoothDecision.WAITING;
|
||||||
wasAdapterEnabled = bt.isEnabled();
|
|
||||||
startActivityForResult(i, REQUEST_BLUETOOTH_DISCOVERABLE);
|
startActivityForResult(i, REQUEST_BLUETOOTH_DISCOVERABLE);
|
||||||
} else {
|
} else {
|
||||||
bluetoothDecision = BluetoothDecision.NO_ADAPTER;
|
bluetoothDecision = BluetoothDecision.NO_ADAPTER;
|
||||||
@@ -320,11 +311,6 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
|||||||
} else {
|
} else {
|
||||||
LOG.info("Bluetooth discoverability was accepted");
|
LOG.info("Bluetooth discoverability was accepted");
|
||||||
bluetoothDecision = BluetoothDecision.ACCEPTED;
|
bluetoothDecision = BluetoothDecision.ACCEPTED;
|
||||||
if (!wasAdapterEnabled) {
|
|
||||||
LOG.info("Bluetooth adapter was enabled by us");
|
|
||||||
eventBus.broadcast(new BluetoothEnabledEvent());
|
|
||||||
wasAdapterEnabled = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
showQrCodeFragmentIfAllowed();
|
showQrCodeFragmentIfAllowed();
|
||||||
} else super.onActivityResult(request, result, data);
|
} else super.onActivityResult(request, result, data);
|
||||||
|
|||||||
Reference in New Issue
Block a user