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:
Jake Hamby
2011-02-07 18:21:25 -08:00
parent 2adae4e274
commit 436b29e68e
44 changed files with 3536 additions and 3041 deletions

View File

@@ -35,17 +35,15 @@ import android.os.PowerManager;
* confirmation entry dialog. Otherwise it puts a Notification in the status bar, which can
* be clicked to bring up the Pairing entry dialog.
*/
public class BluetoothPairingRequest extends BroadcastReceiver {
public final class BluetoothPairingRequest extends BroadcastReceiver {
public static final int NOTIFICATION_ID = android.R.drawable.stat_sys_data_bluetooth;
private static final int NOTIFICATION_ID = android.R.drawable.stat_sys_data_bluetooth;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
LocalBluetoothManager localManager = LocalBluetoothManager.getInstance(context);
// convert broadcast intent into activity intent (same action string)
BluetoothDevice device =
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
@@ -68,39 +66,35 @@ public class BluetoothPairingRequest extends BroadcastReceiver {
(PowerManager)context.getSystemService(Context.POWER_SERVICE);
String deviceAddress = device != null ? device.getAddress() : null;
if (powerManager.isScreenOn() &&
localManager.shouldShowDialogInForeground(deviceAddress)) {
LocalBluetoothPreferences.shouldShowDialogInForeground(context, deviceAddress)) {
// Since the screen is on and the BT-related activity is in the foreground,
// just open the dialog
context.startActivity(pairingIntent);
} else {
// Put up a notification that leads to the dialog
Resources res = context.getResources();
Notification notification = new Notification(
android.R.drawable.stat_sys_data_bluetooth,
res.getString(R.string.bluetooth_notif_ticker),
System.currentTimeMillis());
Notification.Builder builder = new Notification.Builder(context)
.setSmallIcon(android.R.drawable.stat_sys_data_bluetooth)
.setTicker(res.getString(R.string.bluetooth_notif_ticker));
PendingIntent pending = PendingIntent.getActivity(context, 0,
pairingIntent, PendingIntent.FLAG_ONE_SHOT);
String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
if (TextUtils.isEmpty(name)) {
name = (device != null ? device.getName() :
context.getString(android.R.string.unknownName));
name = device != null ? device.getName() :
context.getString(android.R.string.unknownName);
}
notification.setLatestEventInfo(context,
res.getString(R.string.bluetooth_notif_title),
res.getString(R.string.bluetooth_notif_message, name),
pending);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
builder.setContentTitle(res.getString(R.string.bluetooth_notif_title))
.setContentText(res.getString(R.string.bluetooth_notif_message, name))
.setContentIntent(pending)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND);
NotificationManager manager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, notification);
manager.notify(NOTIFICATION_ID, builder.getNotification());
}
} else if (action.equals(BluetoothDevice.ACTION_PAIRING_CANCEL)) {