Refactor Bluetooth settings for readability and performance.
Major refactoring of Bluetooth settings classes. - Moved all functionality from LocalBluetoothManager into new LocalBluetoothAdapter and LocalBluetoothPreferences, and into existing classes. - Refactored functionality from BluetoothEventRedirector into new BluetoothEventManager class, deleting the original version. New version uses a HashMap from action Strings to implementers of the BluetoothEventManager.Handler interface. - Created new BluetoothDiscoveryReceiver to update shared preferences timestamp for Bluetooth discovery start/finish. This is the only event handling we need to do when the settings app is not visible, so it has its own receiver entry in AndroidManifest.xml. Edits are written using QueuedWork.singleThreadExecutor(), which BroadcastReceiver knows about and will wait for completion, eliminating the need for PendingResult. - Miscellaneous cleanups to code style and logic for readability. - Pulled some large switch statement code blocks into new methods. - Changed all Bluetooth state references to the new BluetoothProfile constants. - Changed use of deprecated Notification constructor in BluetoothPairingRequest to use Notification.Builder. - Moved Utf8ByteLengthFilter helper function from BluetoothNamePreference into its own class, and moved test cases into the same package. - Moved all LocalBluetoothProfileManager functionality related to specific profiles into new top-level classes (A2dpProfile, etc.), all implementing the LocalBluetoothProfile interface. - Moved all UI-related methods from CachedBluetoothDevice into the class that uses the method, or into the static Utils class for shared methods. Change-Id: I6d49b7f4ae0c7d7dcf62551ee40b51ecb5fe4f47
This commit is contained in:
@@ -27,7 +27,6 @@ import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
|
||||
@@ -51,7 +50,7 @@ public class RequestPermissionActivity extends Activity implements
|
||||
|
||||
private static final int REQUEST_CODE_START_BT = 1;
|
||||
|
||||
private LocalBluetoothManager mLocalManager;
|
||||
private LocalBluetoothAdapter mLocalAdapter;
|
||||
|
||||
private int mTimeout = BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT;
|
||||
|
||||
@@ -66,18 +65,19 @@ public class RequestPermissionActivity extends Activity implements
|
||||
|
||||
// True if requesting BT to be turned on
|
||||
// False if requesting BT to be turned on + discoverable mode
|
||||
private boolean mEnableOnly = false;
|
||||
private boolean mEnableOnly;
|
||||
|
||||
private boolean mUserConfirmed = false;
|
||||
private boolean mUserConfirmed;
|
||||
|
||||
private AlertDialog mDialog = null;
|
||||
private AlertDialog mDialog;
|
||||
|
||||
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent == null)
|
||||
if (intent == null) {
|
||||
return;
|
||||
}
|
||||
if (mNeededToEnableBluetooth
|
||||
&& BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
|
||||
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothDevice.ERROR);
|
||||
@@ -94,12 +94,13 @@ public class RequestPermissionActivity extends Activity implements
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
// Note: initializes mLocalAdapter and returns true on error
|
||||
if (parseIntent()) {
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
int btState = mLocalManager.getBluetoothState();
|
||||
int btState = mLocalAdapter.getState();
|
||||
|
||||
switch (btState) {
|
||||
case BluetoothAdapter.STATE_OFF:
|
||||
@@ -120,28 +121,29 @@ public class RequestPermissionActivity extends Activity implements
|
||||
*/
|
||||
registerReceiver(mReceiver,
|
||||
new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
|
||||
Intent i = new Intent();
|
||||
i.setClass(this, RequestPermissionHelperActivity.class);
|
||||
Intent intent = new Intent();
|
||||
intent.setClass(this, RequestPermissionHelperActivity.class);
|
||||
if (mEnableOnly) {
|
||||
i.setAction(RequestPermissionHelperActivity.ACTION_INTERNAL_REQUEST_BT_ON);
|
||||
intent.setAction(RequestPermissionHelperActivity.ACTION_INTERNAL_REQUEST_BT_ON);
|
||||
} else {
|
||||
i.setAction(RequestPermissionHelperActivity.
|
||||
intent.setAction(RequestPermissionHelperActivity.
|
||||
ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE);
|
||||
i.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, mTimeout);
|
||||
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, mTimeout);
|
||||
}
|
||||
startActivityForResult(i, REQUEST_CODE_START_BT);
|
||||
startActivityForResult(intent, REQUEST_CODE_START_BT);
|
||||
mNeededToEnableBluetooth = true;
|
||||
break;
|
||||
case BluetoothAdapter.STATE_ON:
|
||||
if (mEnableOnly) {
|
||||
// Nothing to do. Already enabled.
|
||||
proceedAndFinish();
|
||||
return;
|
||||
} else {
|
||||
// Ask the user about enabling discovery mode
|
||||
createDialog();
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
Log.e(TAG, "Unknown adapter state: " + btState);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,8 +178,8 @@ public class RequestPermissionActivity extends Activity implements
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (requestCode != REQUEST_CODE_START_BT) {
|
||||
Log.e(TAG, "Unexpected onActivityResult " + requestCode + " " + resultCode);
|
||||
setResult(Activity.RESULT_CANCELED);
|
||||
Log.e(TAG, "Unexpected onActivityResult " + requestCode + ' ' + resultCode);
|
||||
setResult(RESULT_CANCELED);
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
@@ -191,7 +193,7 @@ public class RequestPermissionActivity extends Activity implements
|
||||
// BT and discoverable mode.
|
||||
mUserConfirmed = true;
|
||||
|
||||
if (mLocalManager.getBluetoothState() == BluetoothAdapter.STATE_ON) {
|
||||
if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
|
||||
proceedAndFinish();
|
||||
} else {
|
||||
// If BT is not up yet, show "Turning on Bluetooth..."
|
||||
@@ -206,7 +208,7 @@ public class RequestPermissionActivity extends Activity implements
|
||||
break;
|
||||
|
||||
case DialogInterface.BUTTON_NEGATIVE:
|
||||
setResult(Activity.RESULT_CANCELED);
|
||||
setResult(RESULT_CANCELED);
|
||||
finish();
|
||||
break;
|
||||
}
|
||||
@@ -217,18 +219,19 @@ public class RequestPermissionActivity extends Activity implements
|
||||
|
||||
if (mEnableOnly) {
|
||||
// BT enabled. Done
|
||||
returnCode = Activity.RESULT_OK;
|
||||
} else if (mLocalManager.getBluetoothAdapter().setScanMode(
|
||||
returnCode = RESULT_OK;
|
||||
} else if (mLocalAdapter.setScanMode(
|
||||
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, mTimeout)) {
|
||||
// If already in discoverable mode, this will extend the timeout.
|
||||
persistDiscoverableEndTimestamp(System.currentTimeMillis() + mTimeout * 1000);
|
||||
LocalBluetoothPreferences.persistDiscoverableEndTimestamp(
|
||||
this, System.currentTimeMillis() + (long) mTimeout * 1000);
|
||||
returnCode = mTimeout;
|
||||
// Activity.RESULT_FIRST_USER should be 1
|
||||
if (returnCode < Activity.RESULT_FIRST_USER) {
|
||||
returnCode = Activity.RESULT_FIRST_USER;
|
||||
if (returnCode < RESULT_FIRST_USER) {
|
||||
returnCode = RESULT_FIRST_USER;
|
||||
}
|
||||
} else {
|
||||
returnCode = Activity.RESULT_CANCELED;
|
||||
returnCode = RESULT_CANCELED;
|
||||
}
|
||||
|
||||
if (mDialog != null) {
|
||||
@@ -239,6 +242,10 @@ public class RequestPermissionActivity extends Activity implements
|
||||
finish();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the received Intent and initialize mLocalBluetoothAdapter.
|
||||
* @return true if an error occurred; false otherwise
|
||||
*/
|
||||
private boolean parseIntent() {
|
||||
Intent intent = getIntent();
|
||||
if (intent != null && intent.getAction().equals(BluetoothAdapter.ACTION_REQUEST_ENABLE)) {
|
||||
@@ -257,16 +264,17 @@ public class RequestPermissionActivity extends Activity implements
|
||||
Log.e(TAG, "Error: this activity may be started only with intent "
|
||||
+ BluetoothAdapter.ACTION_REQUEST_ENABLE + " or "
|
||||
+ BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
|
||||
setResult(Activity.RESULT_CANCELED);
|
||||
setResult(RESULT_CANCELED);
|
||||
return true;
|
||||
}
|
||||
|
||||
mLocalManager = LocalBluetoothManager.getInstance(this);
|
||||
if (mLocalManager == null) {
|
||||
Log.e(TAG, "Error: there's a problem starting bluetooth");
|
||||
setResult(Activity.RESULT_CANCELED);
|
||||
LocalBluetoothManager manager = LocalBluetoothManager.getInstance(this);
|
||||
if (manager == null) {
|
||||
Log.e(TAG, "Error: there's a problem starting Bluetooth");
|
||||
setResult(RESULT_CANCELED);
|
||||
return true;
|
||||
}
|
||||
mLocalAdapter = manager.getBluetoothAdapter();
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -274,20 +282,14 @@ public class RequestPermissionActivity extends Activity implements
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
if (mNeededToEnableBluetooth) unregisterReceiver(mReceiver);
|
||||
}
|
||||
|
||||
private void persistDiscoverableEndTimestamp(long endTimestamp) {
|
||||
SharedPreferences.Editor editor = mLocalManager.getSharedPreferences().edit();
|
||||
editor.putLong(
|
||||
BluetoothDiscoverableEnabler.SHARED_PREFERENCES_KEY_DISCOVERABLE_END_TIMESTAMP,
|
||||
endTimestamp);
|
||||
editor.apply();
|
||||
if (mNeededToEnableBluetooth) {
|
||||
unregisterReceiver(mReceiver);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
setResult(Activity.RESULT_CANCELED);
|
||||
setResult(RESULT_CANCELED);
|
||||
super.onBackPressed();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user