add option to force enable or disable bluetooth adapter

This commit is contained in:
MajorCrazed
2017-10-29 23:30:47 +01:00
parent e6b1597fa7
commit cefe2b09e0
5 changed files with 56 additions and 15 deletions

View File

@@ -83,6 +83,7 @@ class DroidtoothPlugin implements DuplexPlugin, EventListener {
private volatile boolean running = false;
private volatile boolean wasEnabledByUs = false;
private volatile boolean forceEnabled = false;
private volatile BluetoothStateReceiver receiver = null;
private volatile BluetoothServerSocket socket = null;
@@ -154,7 +155,7 @@ class DroidtoothPlugin implements DuplexPlugin, EventListener {
} else {
// Enable Bluetooth if settings allow
if (callback.getSettings().getBoolean(PREF_BT_ENABLE, false)) {
enableAdapter();
enableAdapter(true);
} else {
LOG.info("Not enabling Bluetooth");
}
@@ -245,11 +246,12 @@ class DroidtoothPlugin implements DuplexPlugin, EventListener {
return new DroidtoothTransportConnection(this, s);
}
private void enableAdapter() {
private void enableAdapter(boolean force) {
if (adapter != null && !adapter.isEnabled()) {
if (adapter.enable()) {
LOG.info("Enabling Bluetooth");
wasEnabledByUs = true;
if(force) forceEnabled = true;
} else {
LOG.info("Could not enable Bluetooth");
}
@@ -261,11 +263,14 @@ class DroidtoothPlugin implements DuplexPlugin, EventListener {
running = false;
if (receiver != null) appContext.unregisterReceiver(receiver);
tryToClose(socket);
disableAdapter();
disableAdapter(true);
}
private void disableAdapter() {
if (adapter != null && adapter.isEnabled() && wasEnabledByUs) {
private void disableAdapter(boolean force) {
if (adapter != null && adapter.isEnabled() && wasEnabledByUs
&& (!forceEnabled || force)) {
if(force) forceEnabled = false;
if (adapter.disable()) LOG.info("Disabling Bluetooth");
else LOG.info("Could not disable Bluetooth");
}
@@ -431,26 +436,28 @@ class DroidtoothPlugin implements DuplexPlugin, EventListener {
@Override
public void eventOccurred(Event e) {
if (e instanceof EnableBluetoothEvent) {
enableAdapterAsync();
EnableBluetoothEvent enable = (EnableBluetoothEvent) e;
enableAdapterAsync(enable.isForced());
} else if (e instanceof DisableBluetoothEvent) {
disableAdapterAsync();
DisableBluetoothEvent disable = (DisableBluetoothEvent) e;
disableAdapterAsync(disable.isForced());
}
}
private void enableAdapterAsync() {
private void enableAdapterAsync(final boolean force) {
ioExecutor.execute(new Runnable() {
@Override
public void run() {
enableAdapter();
enableAdapter(force);
}
});
}
private void disableAdapterAsync() {
private void disableAdapterAsync(final boolean force) {
ioExecutor.execute(new Runnable() {
@Override
public void run() {
disableAdapter();
disableAdapter(force);
}
});
}

View File

@@ -0,0 +1,20 @@
package org.briarproject.bramble.api.plugin.event;
import org.briarproject.bramble.api.event.Event;
/**
* to force enable and disable bluetooth
* force enable means that the bluetooth adapter stay enabled until a force DisableBluetoothEvent is called
* force disable stop the bluetooth adapter only when we turned it on
*/
abstract class BluetoothEvent extends Event {
private boolean force;
BluetoothEvent(boolean force){
this.force = force;
}
public boolean isForced(){
return force;
}
}

View File

@@ -11,5 +11,12 @@ import javax.annotation.concurrent.Immutable;
*/
@Immutable
@NotNullByDefault
public class DisableBluetoothEvent extends Event {
public class DisableBluetoothEvent extends BluetoothEvent {
public DisableBluetoothEvent(){
super(false);
}
public DisableBluetoothEvent(boolean force) {
super(force);
}
}

View File

@@ -10,5 +10,12 @@ import javax.annotation.concurrent.Immutable;
*/
@Immutable
@NotNullByDefault
public class EnableBluetoothEvent extends Event {
public class EnableBluetoothEvent extends BluetoothEvent {
public EnableBluetoothEvent(){
super(false);
}
public EnableBluetoothEvent(boolean force){
super(force);
}
}

View File

@@ -326,8 +326,8 @@ public class SettingsFragment extends PreferenceFragmentCompat
}
private void enableOrDisableBluetooth(boolean enable) {
if (enable) eventBus.broadcast(new EnableBluetoothEvent());
else eventBus.broadcast(new DisableBluetoothEvent());
if (enable) eventBus.broadcast(new EnableBluetoothEvent(true));
else eventBus.broadcast(new DisableBluetoothEvent(true));
}
private void storeTorSettings(final int torSetting) {