From 6eb84ac6de9eeacc1e199d74310cf91f5a98daf2 Mon Sep 17 00:00:00 2001 From: Jaikumar Ganesh Date: Fri, 4 Mar 2011 13:49:37 -0800 Subject: [PATCH] Settings app: Run Bluetooth Event receiver when not in fg. Also fix connection indication issues. Fixes bugs 3510336 and 3513352 Change-Id: Iaa47c9d7fd04fa5dd2700f55993ba2fd1a78fd70 --- .../bluetooth/BluetoothEventManager.java | 39 +++++++------------ .../bluetooth/LocalBluetoothManager.java | 4 +- .../LocalBluetoothProfileManager.java | 3 +- 3 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/com/android/settings/bluetooth/BluetoothEventManager.java b/src/com/android/settings/bluetooth/BluetoothEventManager.java index 70e35f984ec..6f83766f310 100644 --- a/src/com/android/settings/bluetooth/BluetoothEventManager.java +++ b/src/com/android/settings/bluetooth/BluetoothEventManager.java @@ -44,8 +44,9 @@ final class BluetoothEventManager { private final LocalBluetoothAdapter mLocalAdapter; private final CachedBluetoothDeviceManager mDeviceManager; private LocalBluetoothProfileManager mProfileManager; - private final IntentFilter mIntentFilter; + private final IntentFilter mAdapterIntentFilter, mProfileIntentFilter; private final Map mHandlerMap; + private Context mContext; private final Collection mCallbacks = new ArrayList(); @@ -56,7 +57,12 @@ final class BluetoothEventManager { void addHandler(String action, Handler handler) { mHandlerMap.put(action, handler); - mIntentFilter.addAction(action); + mAdapterIntentFilter.addAction(action); + } + + void addProfileHandler(String action, Handler handler) { + mHandlerMap.put(action, handler); + mProfileIntentFilter.addAction(action); } // Set profile manager after construction due to circular dependency @@ -65,11 +71,13 @@ final class BluetoothEventManager { } BluetoothEventManager(LocalBluetoothAdapter adapter, - CachedBluetoothDeviceManager deviceManager) { + CachedBluetoothDeviceManager deviceManager, Context context) { mLocalAdapter = adapter; mDeviceManager = deviceManager; - mIntentFilter = new IntentFilter(); + mAdapterIntentFilter = new IntentFilter(); + mProfileIntentFilter = new IntentFilter(); mHandlerMap = new HashMap(); + mContext = context; // Bluetooth on/off broadcasts addHandler(BluetoothAdapter.ACTION_STATE_CHANGED, new AdapterStateChangedHandler()); @@ -91,28 +99,11 @@ final class BluetoothEventManager { // Dock event broadcasts addHandler(Intent.ACTION_DOCK_EVENT, new DockEventHandler()); + mContext.registerReceiver(mBroadcastReceiver, mAdapterIntentFilter); } - /** - * A Bluetooth-related activity is now in the foreground. Register to - * start receiving Bluetooth events. - * @param context a Context object for the current Activity - */ - void resume(Context context) { - if (mLocalAdapter.syncBluetoothState()) { - // adapter state changed while we were paused: send callbacks - int newState = mLocalAdapter.getState(); - synchronized (mCallbacks) { - for (BluetoothCallback callback : mCallbacks) { - callback.onBluetoothStateChanged(newState); - } - } - } - context.registerReceiver(mBroadcastReceiver, mIntentFilter); - } - - void pause(Context context) { - context.unregisterReceiver(mBroadcastReceiver); + void registerProfileIntentReceiver() { + mContext.registerReceiver(mBroadcastReceiver, mProfileIntentFilter); } /** Register to start receiving callbacks for Bluetooth events. */ diff --git a/src/com/android/settings/bluetooth/LocalBluetoothManager.java b/src/com/android/settings/bluetooth/LocalBluetoothManager.java index 0c04e2273cb..63b8b7ca23b 100644 --- a/src/com/android/settings/bluetooth/LocalBluetoothManager.java +++ b/src/com/android/settings/bluetooth/LocalBluetoothManager.java @@ -66,7 +66,7 @@ public final class LocalBluetoothManager { mCachedDeviceManager = new CachedBluetoothDeviceManager(); mEventManager = new BluetoothEventManager(mLocalAdapter, - mCachedDeviceManager); + mCachedDeviceManager, context); mProfileManager = new LocalBluetoothProfileManager(context, mLocalAdapter, mCachedDeviceManager, mEventManager); } @@ -87,11 +87,9 @@ public final class LocalBluetoothManager { if (context != null) { Log.d(TAG, "setting foreground activity to non-null context"); mForegroundActivity = context; - mEventManager.resume(context); } else { if (mForegroundActivity != null) { Log.d(TAG, "setting foreground activity to null"); - mEventManager.pause(mForegroundActivity); mForegroundActivity = null; } } diff --git a/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java b/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java index 0bb6f115b95..ee3cb66a067 100644 --- a/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java +++ b/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java @@ -159,6 +159,7 @@ final class LocalBluetoothProfileManager { } else if (mOppProfile != null) { Log.w(TAG, "Warning: OPP profile was previously added but the UUID is now missing."); } + mEventManager.registerProfileIntentReceiver(); // There is no local SDP record for HID and Settings app doesn't control PBAP } @@ -168,7 +169,7 @@ final class LocalBluetoothProfileManager { private void addProfile(LocalBluetoothProfile profile, String profileName, String stateChangedAction) { - mEventManager.addHandler(stateChangedAction, new StateChangedHandler(profile)); + mEventManager.addProfileHandler(stateChangedAction, new StateChangedHandler(profile)); mProfileNameMap.put(profileName, profile); }