Merge branch '156-turn-off-bluetooth' into 'master'

Turn off Bluetooth again when not required anymore

Turn off Bluetooth again when not required anymore and when errors occur.
Also do not turn off Bluetooth when just changing device orientation.

If I didn't oversee something, this closes #156 

See merge request !19
This commit is contained in:
akwizgran
2015-12-15 10:21:20 +00:00
2 changed files with 22 additions and 9 deletions

View File

@@ -58,7 +58,7 @@ implements InvitationListener {
// Fields that are accessed from background threads must be volatile // Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db; @Inject private volatile DatabaseComponent db;
private volatile boolean enableBluetooth = true; private volatile boolean leaveBluetoothEnabled = true;
@Override @Override
public void onCreate(Bundle state) { public void onCreate(Bundle state) {
@@ -66,6 +66,9 @@ implements InvitationListener {
if (state == null) { if (state == null) {
// This is a new activity // This is a new activity
setView(new ChooseIdentityView(this)); setView(new ChooseIdentityView(this));
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter != null) bluetoothWasEnabled = adapter.isEnabled();
} else { } else {
// Restore the activity's state // Restore the activity's state
byte[] b = state.getByteArray("briar.LOCAL_AUTHOR_ID"); byte[] b = state.getByteArray("briar.LOCAL_AUTHOR_ID");
@@ -73,6 +76,8 @@ implements InvitationListener {
taskHandle = state.getLong("briar.TASK_HANDLE", -1); taskHandle = state.getLong("briar.TASK_HANDLE", -1);
task = referenceManager.getReference(taskHandle, task = referenceManager.getReference(taskHandle,
InvitationTask.class); InvitationTask.class);
bluetoothWasEnabled = state.getBoolean("briar.BLUETOOTH_WAS_ENABLED");
if (task == null) { if (task == null) {
// No background task - we must be in an initial or final state // No background task - we must be in an initial or final state
localInvitationCode = state.getInt("briar.LOCAL_CODE"); localInvitationCode = state.getInt("briar.LOCAL_CODE");
@@ -139,8 +144,6 @@ implements InvitationListener {
} }
} }
} }
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter != null) bluetoothWasEnabled = adapter.isEnabled();
} }
private void showToastAndFinish() { private void showToastAndFinish() {
@@ -166,7 +169,7 @@ implements InvitationListener {
long duration = System.currentTimeMillis() - now; long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO)) if (LOG.isLoggable(INFO))
LOG.info("Loading setting took " + duration + " ms"); LOG.info("Loading setting took " + duration + " ms");
enableBluetooth = c.getBoolean("enable", true); leaveBluetoothEnabled = c.getBoolean("enable", true);
} catch (DbException e) { } catch (DbException e) {
if (LOG.isLoggable(WARNING)) if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e); LOG.log(WARNING, e.toString(), e);
@@ -187,16 +190,15 @@ implements InvitationListener {
state.putBoolean("briar.FAILED", connectionFailed); state.putBoolean("briar.FAILED", connectionFailed);
state.putString("briar.CONTACT_NAME", contactName); state.putString("briar.CONTACT_NAME", contactName);
if (task != null) state.putLong("briar.TASK_HANDLE", taskHandle); if (task != null) state.putLong("briar.TASK_HANDLE", taskHandle);
state.putBoolean("briar.BLUETOOTH_WAS_ENABLED", bluetoothWasEnabled);
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
if (task != null) task.removeListener(this); if (task != null) task.removeListener(this);
if (!bluetoothWasEnabled && !enableBluetooth) { if (isFinishing()) disableBluetooth();
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter != null) adapter.disable();
}
} }
@Override @Override
@@ -286,7 +288,7 @@ implements InvitationListener {
setView(new InvitationCodeView(this, true)); setView(new InvitationCodeView(this, true));
task = invitationTaskFactory.createTask(localAuthorId, task = invitationTaskFactory.createTask(localAuthorId,
localInvitationCode, code, enableBluetooth); localInvitationCode, code, leaveBluetoothEnabled);
taskHandle = referenceManager.putReference(task, InvitationTask.class); taskHandle = referenceManager.putReference(task, InvitationTask.class);
task.addListener(AddContactActivity.this); task.addListener(AddContactActivity.this);
// Add a second listener so we can remove the first in onDestroy(), // Add a second listener so we can remove the first in onDestroy(),
@@ -315,6 +317,15 @@ implements InvitationListener {
} }
} }
public void disableBluetooth() {
if (!bluetoothWasEnabled && !leaveBluetoothEnabled) {
if (LOG.isLoggable(INFO)) LOG.info("Turning off Bluetooth again.");
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter != null) adapter.disable();
}
}
String getContactName() { String getContactName() {
return contactName; return contactName;
} }

View File

@@ -39,6 +39,8 @@ class ErrorView extends AddContactView implements OnClickListener {
removeAllViews(); removeAllViews();
Context ctx = getContext(); Context ctx = getContext();
container.disableBluetooth();
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService LayoutInflater inflater = (LayoutInflater) ctx.getSystemService
(Context.LAYOUT_INFLATER_SERVICE); (Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.invitation_error, this); View view = inflater.inflate(R.layout.invitation_error, this);