mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 12:19:54 +01:00
add option to force enable or disable bluetooth adapter
This commit is contained in:
@@ -83,6 +83,7 @@ class DroidtoothPlugin implements DuplexPlugin, EventListener {
|
|||||||
|
|
||||||
private volatile boolean running = false;
|
private volatile boolean running = false;
|
||||||
private volatile boolean wasEnabledByUs = false;
|
private volatile boolean wasEnabledByUs = false;
|
||||||
|
private volatile boolean forceEnabled = false;
|
||||||
private volatile BluetoothStateReceiver receiver = null;
|
private volatile BluetoothStateReceiver receiver = null;
|
||||||
private volatile BluetoothServerSocket socket = null;
|
private volatile BluetoothServerSocket socket = null;
|
||||||
|
|
||||||
@@ -154,7 +155,7 @@ class DroidtoothPlugin implements DuplexPlugin, EventListener {
|
|||||||
} else {
|
} else {
|
||||||
// Enable Bluetooth if settings allow
|
// Enable Bluetooth if settings allow
|
||||||
if (callback.getSettings().getBoolean(PREF_BT_ENABLE, false)) {
|
if (callback.getSettings().getBoolean(PREF_BT_ENABLE, false)) {
|
||||||
enableAdapter();
|
enableAdapter(true);
|
||||||
} else {
|
} else {
|
||||||
LOG.info("Not enabling Bluetooth");
|
LOG.info("Not enabling Bluetooth");
|
||||||
}
|
}
|
||||||
@@ -245,11 +246,12 @@ class DroidtoothPlugin implements DuplexPlugin, EventListener {
|
|||||||
return new DroidtoothTransportConnection(this, s);
|
return new DroidtoothTransportConnection(this, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void enableAdapter() {
|
private void enableAdapter(boolean force) {
|
||||||
if (adapter != null && !adapter.isEnabled()) {
|
if (adapter != null && !adapter.isEnabled()) {
|
||||||
if (adapter.enable()) {
|
if (adapter.enable()) {
|
||||||
LOG.info("Enabling Bluetooth");
|
LOG.info("Enabling Bluetooth");
|
||||||
wasEnabledByUs = true;
|
wasEnabledByUs = true;
|
||||||
|
if(force) forceEnabled = true;
|
||||||
} else {
|
} else {
|
||||||
LOG.info("Could not enable Bluetooth");
|
LOG.info("Could not enable Bluetooth");
|
||||||
}
|
}
|
||||||
@@ -261,11 +263,14 @@ class DroidtoothPlugin implements DuplexPlugin, EventListener {
|
|||||||
running = false;
|
running = false;
|
||||||
if (receiver != null) appContext.unregisterReceiver(receiver);
|
if (receiver != null) appContext.unregisterReceiver(receiver);
|
||||||
tryToClose(socket);
|
tryToClose(socket);
|
||||||
disableAdapter();
|
disableAdapter(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void disableAdapter() {
|
private void disableAdapter(boolean force) {
|
||||||
if (adapter != null && adapter.isEnabled() && wasEnabledByUs) {
|
if (adapter != null && adapter.isEnabled() && wasEnabledByUs
|
||||||
|
&& (!forceEnabled || force)) {
|
||||||
|
if(force) forceEnabled = false;
|
||||||
|
|
||||||
if (adapter.disable()) LOG.info("Disabling Bluetooth");
|
if (adapter.disable()) LOG.info("Disabling Bluetooth");
|
||||||
else LOG.info("Could not disable Bluetooth");
|
else LOG.info("Could not disable Bluetooth");
|
||||||
}
|
}
|
||||||
@@ -431,26 +436,28 @@ class DroidtoothPlugin implements DuplexPlugin, EventListener {
|
|||||||
@Override
|
@Override
|
||||||
public void eventOccurred(Event e) {
|
public void eventOccurred(Event e) {
|
||||||
if (e instanceof EnableBluetoothEvent) {
|
if (e instanceof EnableBluetoothEvent) {
|
||||||
enableAdapterAsync();
|
EnableBluetoothEvent enable = (EnableBluetoothEvent) e;
|
||||||
|
enableAdapterAsync(enable.isForced());
|
||||||
} else if (e instanceof DisableBluetoothEvent) {
|
} 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() {
|
ioExecutor.execute(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
enableAdapter();
|
enableAdapter(force);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void disableAdapterAsync() {
|
private void disableAdapterAsync(final boolean force) {
|
||||||
ioExecutor.execute(new Runnable() {
|
ioExecutor.execute(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
disableAdapter();
|
disableAdapter(force);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,5 +11,12 @@ import javax.annotation.concurrent.Immutable;
|
|||||||
*/
|
*/
|
||||||
@Immutable
|
@Immutable
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public class DisableBluetoothEvent extends Event {
|
public class DisableBluetoothEvent extends BluetoothEvent {
|
||||||
|
public DisableBluetoothEvent(){
|
||||||
|
super(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DisableBluetoothEvent(boolean force) {
|
||||||
|
super(force);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,5 +10,12 @@ import javax.annotation.concurrent.Immutable;
|
|||||||
*/
|
*/
|
||||||
@Immutable
|
@Immutable
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public class EnableBluetoothEvent extends Event {
|
public class EnableBluetoothEvent extends BluetoothEvent {
|
||||||
|
public EnableBluetoothEvent(){
|
||||||
|
super(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public EnableBluetoothEvent(boolean force){
|
||||||
|
super(force);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -326,8 +326,8 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void enableOrDisableBluetooth(boolean enable) {
|
private void enableOrDisableBluetooth(boolean enable) {
|
||||||
if (enable) eventBus.broadcast(new EnableBluetoothEvent());
|
if (enable) eventBus.broadcast(new EnableBluetoothEvent(true));
|
||||||
else eventBus.broadcast(new DisableBluetoothEvent());
|
else eventBus.broadcast(new DisableBluetoothEvent(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void storeTorSettings(final int torSetting) {
|
private void storeTorSettings(final int torSetting) {
|
||||||
|
|||||||
Reference in New Issue
Block a user