mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 06:09:55 +01:00
Ask for Bluetooth discoverability before adding a contact.
This commit is contained in:
@@ -11,7 +11,7 @@ public interface RequestCodes {
|
|||||||
int REQUEST_RINGTONE = 7;
|
int REQUEST_RINGTONE = 7;
|
||||||
int REQUEST_PERMISSION_CAMERA = 8;
|
int REQUEST_PERMISSION_CAMERA = 8;
|
||||||
int REQUEST_DOZE_WHITELISTING = 9;
|
int REQUEST_DOZE_WHITELISTING = 9;
|
||||||
int REQUEST_ENABLE_BLUETOOTH = 10;
|
int REQUEST_BLUETOOTH_DISCOVERABLE = 10;
|
||||||
int REQUEST_UNLOCK = 11;
|
int REQUEST_UNLOCK = 11;
|
||||||
int REQUEST_KEYGUARD_UNLOCK = 12;
|
int REQUEST_KEYGUARD_UNLOCK = 12;
|
||||||
|
|
||||||
|
|||||||
@@ -38,14 +38,17 @@ import javax.annotation.Nullable;
|
|||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import static android.Manifest.permission.CAMERA;
|
import static android.Manifest.permission.CAMERA;
|
||||||
import static android.bluetooth.BluetoothAdapter.ACTION_REQUEST_ENABLE;
|
import static android.bluetooth.BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE;
|
||||||
|
import static android.bluetooth.BluetoothAdapter.ACTION_SCAN_MODE_CHANGED;
|
||||||
import static android.bluetooth.BluetoothAdapter.ACTION_STATE_CHANGED;
|
import static android.bluetooth.BluetoothAdapter.ACTION_STATE_CHANGED;
|
||||||
|
import static android.bluetooth.BluetoothAdapter.EXTRA_SCAN_MODE;
|
||||||
import static android.bluetooth.BluetoothAdapter.EXTRA_STATE;
|
import static android.bluetooth.BluetoothAdapter.EXTRA_STATE;
|
||||||
|
import static android.bluetooth.BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE;
|
||||||
import static android.bluetooth.BluetoothAdapter.STATE_ON;
|
import static android.bluetooth.BluetoothAdapter.STATE_ON;
|
||||||
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
|
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
|
||||||
import static android.os.Build.VERSION.SDK_INT;
|
import static android.os.Build.VERSION.SDK_INT;
|
||||||
import static android.widget.Toast.LENGTH_LONG;
|
import static android.widget.Toast.LENGTH_LONG;
|
||||||
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_ENABLE_BLUETOOTH;
|
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_BLUETOOTH_DISCOVERABLE;
|
||||||
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_PERMISSION_CAMERA;
|
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_PERMISSION_CAMERA;
|
||||||
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@@ -55,7 +58,7 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
|||||||
KeyAgreementEventListener {
|
KeyAgreementEventListener {
|
||||||
|
|
||||||
private enum BluetoothState {
|
private enum BluetoothState {
|
||||||
UNKNOWN, NO_ADAPTER, WAITING, REFUSED, ENABLED
|
UNKNOWN, NO_ADAPTER, WAITING, REFUSED, ENABLED, DISCOVERABLE
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
@@ -64,7 +67,7 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
|||||||
@Inject
|
@Inject
|
||||||
EventBus eventBus;
|
EventBus eventBus;
|
||||||
|
|
||||||
private boolean isResumed = false, enableWasRequested = false;
|
private boolean isResumed = false, wasAdapterEnabled = false;
|
||||||
private boolean continueClicked, gotCameraPermission;
|
private boolean continueClicked, gotCameraPermission;
|
||||||
private BluetoothState bluetoothState = BluetoothState.UNKNOWN;
|
private BluetoothState bluetoothState = BluetoothState.UNKNOWN;
|
||||||
private BroadcastReceiver bluetoothReceiver = null;
|
private BroadcastReceiver bluetoothReceiver = null;
|
||||||
@@ -85,7 +88,9 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
|||||||
if (state == null) {
|
if (state == null) {
|
||||||
showInitialFragment(IntroFragment.newInstance());
|
showInitialFragment(IntroFragment.newInstance());
|
||||||
}
|
}
|
||||||
IntentFilter filter = new IntentFilter(ACTION_STATE_CHANGED);
|
IntentFilter filter = new IntentFilter();
|
||||||
|
filter.addAction(ACTION_STATE_CHANGED);
|
||||||
|
filter.addAction(ACTION_SCAN_MODE_CHANGED);
|
||||||
bluetoothReceiver = new BluetoothStateReceiver();
|
bluetoothReceiver = new BluetoothStateReceiver();
|
||||||
registerReceiver(bluetoothReceiver, filter);
|
registerReceiver(bluetoothReceiver, filter);
|
||||||
}
|
}
|
||||||
@@ -133,45 +138,48 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
|||||||
public void showNextScreen() {
|
public void showNextScreen() {
|
||||||
continueClicked = true;
|
continueClicked = true;
|
||||||
if (checkPermissions()) {
|
if (checkPermissions()) {
|
||||||
if (shouldRequestEnableBluetooth()) requestEnableBluetooth();
|
if (shouldRequestBluetoothDiscoverable()) {
|
||||||
else if (canShowQrCodeFragment()) showQrCodeFragment();
|
requestBluetoothDiscoverable();
|
||||||
|
} else if (canShowQrCodeFragment()) {
|
||||||
|
showQrCodeFragment();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean shouldRequestEnableBluetooth() {
|
private boolean shouldRequestBluetoothDiscoverable() {
|
||||||
return bluetoothState == BluetoothState.UNKNOWN
|
return bluetoothState == BluetoothState.UNKNOWN
|
||||||
|| bluetoothState == BluetoothState.REFUSED;
|
|| bluetoothState == BluetoothState.REFUSED;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void requestEnableBluetooth() {
|
private void requestBluetoothDiscoverable() {
|
||||||
BluetoothAdapter bt = BluetoothAdapter.getDefaultAdapter();
|
BluetoothAdapter bt = BluetoothAdapter.getDefaultAdapter();
|
||||||
if (bt == null) {
|
if (bt == null) {
|
||||||
setBluetoothState(BluetoothState.NO_ADAPTER);
|
setBluetoothState(BluetoothState.NO_ADAPTER);
|
||||||
} else if (bt.isEnabled()) {
|
return;
|
||||||
setBluetoothState(BluetoothState.ENABLED);
|
|
||||||
} else {
|
|
||||||
enableWasRequested = true;
|
|
||||||
setBluetoothState(BluetoothState.WAITING);
|
|
||||||
Intent i = new Intent(ACTION_REQUEST_ENABLE);
|
|
||||||
startActivityForResult(i, REQUEST_ENABLE_BLUETOOTH);
|
|
||||||
}
|
}
|
||||||
|
setBluetoothState(BluetoothState.WAITING);
|
||||||
|
wasAdapterEnabled = bt.isEnabled();
|
||||||
|
Intent i = new Intent(ACTION_REQUEST_DISCOVERABLE);
|
||||||
|
startActivityForResult(i, REQUEST_BLUETOOTH_DISCOVERABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setBluetoothState(BluetoothState bluetoothState) {
|
private void setBluetoothState(BluetoothState bluetoothState) {
|
||||||
LOG.info("Setting Bluetooth state to " + bluetoothState);
|
LOG.info("Setting Bluetooth state to " + bluetoothState);
|
||||||
this.bluetoothState = bluetoothState;
|
this.bluetoothState = bluetoothState;
|
||||||
if (enableWasRequested && bluetoothState == BluetoothState.ENABLED) {
|
if (!wasAdapterEnabled && bluetoothState == BluetoothState.ENABLED) {
|
||||||
eventBus.broadcast(new BluetoothEnabledEvent());
|
eventBus.broadcast(new BluetoothEnabledEvent());
|
||||||
enableWasRequested = false;
|
wasAdapterEnabled = true;
|
||||||
}
|
}
|
||||||
if (canShowQrCodeFragment()) showQrCodeFragment();
|
if (canShowQrCodeFragment()) showQrCodeFragment();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onActivityResult(int request, int result, Intent data) {
|
public void onActivityResult(int request, int result, Intent data) {
|
||||||
// If the request was granted we'll catch the state change event
|
if (request == REQUEST_BLUETOOTH_DISCOVERABLE) {
|
||||||
if (request == REQUEST_ENABLE_BLUETOOTH && result == RESULT_CANCELED)
|
// If the request was granted we'll catch the state change event
|
||||||
setBluetoothState(BluetoothState.REFUSED);
|
if (result == RESULT_CANCELED)
|
||||||
|
setBluetoothState(BluetoothState.REFUSED);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showQrCodeFragment() {
|
private void showQrCodeFragment() {
|
||||||
@@ -259,9 +267,18 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
int state = intent.getIntExtra(EXTRA_STATE, 0);
|
String action = intent.getAction();
|
||||||
if (state == STATE_ON) setBluetoothState(BluetoothState.ENABLED);
|
if (ACTION_STATE_CHANGED.equals(action)) {
|
||||||
else setBluetoothState(BluetoothState.UNKNOWN);
|
int state = intent.getIntExtra(EXTRA_STATE, 0);
|
||||||
|
if (state == STATE_ON)
|
||||||
|
setBluetoothState(BluetoothState.ENABLED);
|
||||||
|
else setBluetoothState(BluetoothState.UNKNOWN);
|
||||||
|
} else if (ACTION_SCAN_MODE_CHANGED.equals(action)) {
|
||||||
|
int scanMode = intent.getIntExtra(EXTRA_SCAN_MODE, -1);
|
||||||
|
if (scanMode == SCAN_MODE_CONNECTABLE_DISCOVERABLE)
|
||||||
|
setBluetoothState(BluetoothState.DISCOVERABLE);
|
||||||
|
else setBluetoothState(BluetoothState.UNKNOWN);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user