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:
@@ -43,7 +43,7 @@ public class RequestPermissionHelperActivity extends AlertActivity implements
|
||||
public static final String ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE =
|
||||
"com.android.settings.bluetooth.ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE";
|
||||
|
||||
private LocalBluetoothManager mLocalManager;
|
||||
private LocalBluetoothAdapter mLocalAdapter;
|
||||
|
||||
private int mTimeout;
|
||||
|
||||
@@ -55,6 +55,7 @@ public class RequestPermissionHelperActivity extends AlertActivity implements
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
// Note: initializes mLocalAdapter and returns true on error
|
||||
if (parseIntent()) {
|
||||
finish();
|
||||
return;
|
||||
@@ -92,32 +93,33 @@ public class RequestPermissionHelperActivity extends AlertActivity implements
|
||||
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
int returnCode;
|
||||
// FIXME: fix this ugly switch logic!
|
||||
switch (which) {
|
||||
case DialogInterface.BUTTON_POSITIVE:
|
||||
case BUTTON_POSITIVE:
|
||||
int btState = 0;
|
||||
|
||||
try {
|
||||
// TODO There's a better way.
|
||||
int retryCount = 30;
|
||||
do {
|
||||
btState = mLocalManager.getBluetoothState();
|
||||
btState = mLocalAdapter.getBluetoothState();
|
||||
Thread.sleep(100);
|
||||
} while (btState == BluetoothAdapter.STATE_TURNING_OFF && --retryCount > 0);
|
||||
} catch (InterruptedException e) {
|
||||
} catch (InterruptedException ignored) {
|
||||
// don't care
|
||||
}
|
||||
|
||||
if (btState == BluetoothAdapter.STATE_TURNING_ON
|
||||
|| btState == BluetoothAdapter.STATE_ON
|
||||
|| mLocalManager.getBluetoothAdapter().enable()) {
|
||||
|| mLocalAdapter.enable()) {
|
||||
returnCode = RequestPermissionActivity.RESULT_BT_STARTING_OR_STARTED;
|
||||
} else {
|
||||
returnCode = Activity.RESULT_CANCELED;
|
||||
returnCode = RESULT_CANCELED;
|
||||
}
|
||||
break;
|
||||
|
||||
case DialogInterface.BUTTON_NEGATIVE:
|
||||
returnCode = Activity.RESULT_CANCELED;
|
||||
case BUTTON_NEGATIVE:
|
||||
returnCode = RESULT_CANCELED;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
@@ -125,6 +127,10 @@ public class RequestPermissionHelperActivity extends AlertActivity implements
|
||||
setResult(returnCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(ACTION_INTERNAL_REQUEST_BT_ON)) {
|
||||
@@ -136,23 +142,24 @@ public class RequestPermissionHelperActivity extends AlertActivity implements
|
||||
mTimeout = intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
|
||||
BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT);
|
||||
} else {
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
setResult(Activity.RESULT_CANCELED);
|
||||
setResult(RESULT_CANCELED);
|
||||
super.onBackPressed();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user