mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 20:29:52 +01:00
Update semantics of Bluetooth setting.
The setting now enables/disables the plugin, not just contact connections. The key agreement UI will need to be updated to change the setting if the user agrees to use Bluetooth.
This commit is contained in:
@@ -10,4 +10,7 @@ public interface BluetoothConstants {
|
|||||||
String PROP_UUID = "uuid";
|
String PROP_UUID = "uuid";
|
||||||
|
|
||||||
String PREF_BT_ENABLE = "enable";
|
String PREF_BT_ENABLE = "enable";
|
||||||
|
|
||||||
|
int REASON_USER = 1;
|
||||||
|
int REASON_NO_BT_ADAPTER = 2;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import org.briarproject.bramble.api.keyagreement.KeyAgreementConnection;
|
|||||||
import org.briarproject.bramble.api.keyagreement.KeyAgreementListener;
|
import org.briarproject.bramble.api.keyagreement.KeyAgreementListener;
|
||||||
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementListeningEvent;
|
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementListeningEvent;
|
||||||
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementStoppedListeningEvent;
|
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementStoppedListeningEvent;
|
||||||
|
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
@@ -49,6 +50,7 @@ import static org.briarproject.bramble.api.plugin.BluetoothConstants.ID;
|
|||||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PREF_BT_ENABLE;
|
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PREF_BT_ENABLE;
|
||||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PROP_ADDRESS;
|
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PROP_ADDRESS;
|
||||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PROP_UUID;
|
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PROP_UUID;
|
||||||
|
import static org.briarproject.bramble.api.plugin.BluetoothConstants.REASON_NO_BT_ADAPTER;
|
||||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.UUID_BYTES;
|
import static org.briarproject.bramble.api.plugin.BluetoothConstants.UUID_BYTES;
|
||||||
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
||||||
import static org.briarproject.bramble.api.plugin.Plugin.State.DISABLED;
|
import static org.briarproject.bramble.api.plugin.Plugin.State.DISABLED;
|
||||||
@@ -78,7 +80,6 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
|
|
||||||
protected final PluginState state = new PluginState();
|
protected final PluginState state = new PluginState();
|
||||||
|
|
||||||
private volatile boolean contactConnections = false;
|
|
||||||
private volatile String contactConnectionsUuid = null;
|
private volatile String contactConnectionsUuid = null;
|
||||||
|
|
||||||
abstract void initialiseAdapter() throws IOException;
|
abstract void initialiseAdapter() throws IOException;
|
||||||
@@ -131,16 +132,18 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
LOG.info("Bluetooth enabled");
|
LOG.info("Bluetooth enabled");
|
||||||
// We may not have been able to get the local address before
|
// We may not have been able to get the local address before
|
||||||
ioExecutor.execute(this::updateProperties);
|
ioExecutor.execute(this::updateProperties);
|
||||||
if (shouldAllowContactConnections()) bind();
|
if (getState() == INACTIVE) bind();
|
||||||
callback.pluginStateChanged(getState());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void onAdapterDisabled() {
|
void onAdapterDisabled() {
|
||||||
LOG.info("Bluetooth disabled");
|
LOG.info("Bluetooth disabled");
|
||||||
connectionLimiter.allConnectionsClosed();
|
connectionLimiter.allConnectionsClosed();
|
||||||
// The server socket may not have been closed automatically
|
// The server socket may not have been closed automatically
|
||||||
tryToClose(state.clearServerSocket());
|
SS ss = state.clearServerSocket();
|
||||||
callback.pluginStateChanged(getState());
|
if (ss != null) {
|
||||||
|
LOG.info("Closing server socket");
|
||||||
|
tryToClose(ss);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -164,29 +167,22 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
try {
|
try {
|
||||||
initialiseAdapter();
|
initialiseAdapter();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
state.setNoAdapter();
|
||||||
throw new PluginException(e);
|
throw new PluginException(e);
|
||||||
}
|
}
|
||||||
updateProperties();
|
updateProperties();
|
||||||
state.setStarted();
|
Settings settings = callback.getSettings();
|
||||||
loadSettings(callback.getSettings());
|
boolean enabledByUser = settings.getBoolean(PREF_BT_ENABLE, false);
|
||||||
if (shouldAllowContactConnections()) {
|
state.setStarted(enabledByUser);
|
||||||
|
if (enabledByUser) {
|
||||||
if (isAdapterEnabled()) bind();
|
if (isAdapterEnabled()) bind();
|
||||||
else enableAdapter();
|
else enableAdapter();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadSettings(Settings settings) {
|
|
||||||
contactConnections = settings.getBoolean(PREF_BT_ENABLE, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean shouldAllowContactConnections() {
|
|
||||||
return contactConnections;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void bind() {
|
private void bind() {
|
||||||
ioExecutor.execute(() -> {
|
ioExecutor.execute(() -> {
|
||||||
if (!shouldAllowContactConnections() || getState() != ACTIVE)
|
if (getState() != INACTIVE) return;
|
||||||
return;
|
|
||||||
// Bind a server socket to accept connections from contacts
|
// Bind a server socket to accept connections from contacts
|
||||||
SS ss;
|
SS ss;
|
||||||
try {
|
try {
|
||||||
@@ -195,8 +191,7 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
logException(LOG, WARNING, e);
|
logException(LOG, WARNING, e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!shouldAllowContactConnections() ||
|
if (!state.setServerSocket(ss)) {
|
||||||
!state.setServerSocket(ss)) {
|
|
||||||
LOG.info("Closing redundant server socket");
|
LOG.info("Closing redundant server socket");
|
||||||
tryToClose(ss);
|
tryToClose(ss);
|
||||||
return;
|
return;
|
||||||
@@ -263,7 +258,7 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getReasonDisabled() {
|
public int getReasonDisabled() {
|
||||||
return getState() == DISABLED ? REASON_STARTING_STOPPING : -1;
|
return state.getReasonDisabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -279,7 +274,7 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
@Override
|
@Override
|
||||||
public void poll(Collection<Pair<TransportProperties, ConnectionHandler>>
|
public void poll(Collection<Pair<TransportProperties, ConnectionHandler>>
|
||||||
properties) {
|
properties) {
|
||||||
if (!shouldAllowContactConnections() || getState() != ACTIVE) return;
|
if (getState() != ACTIVE) return;
|
||||||
backoff.increment();
|
backoff.increment();
|
||||||
for (Pair<TransportProperties, ConnectionHandler> p : properties) {
|
for (Pair<TransportProperties, ConnectionHandler> p : properties) {
|
||||||
connect(p.getFirst(), p.getSecond());
|
connect(p.getFirst(), p.getSecond());
|
||||||
@@ -333,8 +328,7 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DuplexTransportConnection createConnection(TransportProperties p) {
|
public DuplexTransportConnection createConnection(TransportProperties p) {
|
||||||
if (!shouldAllowContactConnections() || getState() != ACTIVE)
|
if (getState() != ACTIVE) return null;
|
||||||
return null;
|
|
||||||
if (!connectionLimiter.canOpenContactConnection()) return null;
|
if (!connectionLimiter.canOpenContactConnection()) return null;
|
||||||
String address = p.get(PROP_ADDRESS);
|
String address = p.get(PROP_ADDRESS);
|
||||||
if (isNullOrEmpty(address)) return null;
|
if (isNullOrEmpty(address)) return null;
|
||||||
@@ -438,16 +432,18 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@IoExecutor
|
||||||
private void onSettingsUpdated(Settings settings) {
|
private void onSettingsUpdated(Settings settings) {
|
||||||
boolean wasAllowed = shouldAllowContactConnections();
|
boolean enabledByUser = settings.getBoolean(PREF_BT_ENABLE, false);
|
||||||
loadSettings(settings);
|
SS ss = state.setEnabledByUser(enabledByUser);
|
||||||
boolean isAllowed = shouldAllowContactConnections();
|
State s = getState();
|
||||||
if (wasAllowed && !isAllowed) {
|
callback.pluginStateChanged(s);
|
||||||
LOG.info("Contact connections disabled");
|
if (ss != null) {
|
||||||
tryToClose(state.clearServerSocket());
|
LOG.info("Disabled by user, closing server socket");
|
||||||
|
tryToClose(ss);
|
||||||
disableAdapterIfEnabledByUs();
|
disableAdapterIfEnabledByUs();
|
||||||
} else if (!wasAllowed && isAllowed) {
|
} else if (s == INACTIVE) {
|
||||||
LOG.info("Contact connections enabled");
|
LOG.info("Enabled by user, opening server socket");
|
||||||
if (isAdapterEnabled()) bind();
|
if (isAdapterEnabled()) bind();
|
||||||
else enableAdapter();
|
else enableAdapter();
|
||||||
}
|
}
|
||||||
@@ -481,13 +477,18 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
protected class PluginState {
|
protected class PluginState {
|
||||||
|
|
||||||
@GuardedBy("this")
|
@GuardedBy("this")
|
||||||
private boolean started = false, stopped = false;
|
private boolean started = false,
|
||||||
|
stopped = false,
|
||||||
|
noAdapter = false,
|
||||||
|
enabledByUser = false;
|
||||||
|
|
||||||
@GuardedBy("this")
|
@GuardedBy("this")
|
||||||
@Nullable
|
@Nullable
|
||||||
private SS serverSocket = null;
|
private SS serverSocket = null;
|
||||||
|
|
||||||
synchronized void setStarted() {
|
synchronized void setStarted(boolean enabledByUser) {
|
||||||
started = true;
|
started = true;
|
||||||
|
this.enabledByUser = enabledByUser;
|
||||||
callback.pluginStateChanged(getState());
|
callback.pluginStateChanged(getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -500,6 +501,23 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
return ss;
|
return ss;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
synchronized void setNoAdapter() {
|
||||||
|
noAdapter = true;
|
||||||
|
callback.pluginStateChanged(getState());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
synchronized SS setEnabledByUser(boolean enabledByUser) {
|
||||||
|
this.enabledByUser = enabledByUser;
|
||||||
|
SS ss = null;
|
||||||
|
if (!enabledByUser) {
|
||||||
|
ss = serverSocket;
|
||||||
|
serverSocket = null;
|
||||||
|
}
|
||||||
|
callback.pluginStateChanged(getState());
|
||||||
|
return ss;
|
||||||
|
}
|
||||||
|
|
||||||
synchronized boolean setServerSocket(SS ss) {
|
synchronized boolean setServerSocket(SS ss) {
|
||||||
if (stopped || serverSocket != null) return false;
|
if (stopped || serverSocket != null) return false;
|
||||||
serverSocket = ss;
|
serverSocket = ss;
|
||||||
@@ -511,12 +529,19 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
synchronized SS clearServerSocket() {
|
synchronized SS clearServerSocket() {
|
||||||
SS ss = serverSocket;
|
SS ss = serverSocket;
|
||||||
serverSocket = null;
|
serverSocket = null;
|
||||||
|
callback.pluginStateChanged(getState());
|
||||||
return ss;
|
return ss;
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized State getState() {
|
synchronized State getState() {
|
||||||
if (!started || stopped) return DISABLED;
|
if (!started || stopped || !enabledByUser) return DISABLED;
|
||||||
return isAdapterEnabled() ? ACTIVE : INACTIVE;
|
return serverSocket == null ? INACTIVE : ACTIVE;
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized int getReasonDisabled() {
|
||||||
|
if (noAdapter && !stopped) return REASON_NO_BT_ADAPTER;
|
||||||
|
if (!started || stopped) return REASON_STARTING_STOPPING;
|
||||||
|
return enabledByUser ? -1 : REASON_USER;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user