diff options
2 files changed, 34 insertions, 19 deletions
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java index f5bacb62b6b2..c97445f04eea 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java @@ -230,30 +230,30 @@ public class BluetoothEventManager { @VisibleForTesting void dispatchActiveDeviceChanged( - @Nullable CachedBluetoothDevice activeDevice, - int bluetoothProfile) { + @Nullable CachedBluetoothDevice activeDevice, int bluetoothProfile) { + CachedBluetoothDevice targetDevice = activeDevice; for (CachedBluetoothDevice cachedDevice : mDeviceManager.getCachedDevicesCopy()) { - Set<CachedBluetoothDevice> memberSet = cachedDevice.getMemberDevice(); - boolean isActive = Objects.equals(cachedDevice, activeDevice); - if (!isActive && !memberSet.isEmpty()) { - for (CachedBluetoothDevice memberCachedDevice : memberSet) { - isActive = Objects.equals(memberCachedDevice, activeDevice); - if (isActive) { - Log.d(TAG, - "The active device is the member device " - + activeDevice.getDevice().getAnonymizedAddress() - + ". change activeDevice as main device " - + cachedDevice.getDevice().getAnonymizedAddress()); - activeDevice = cachedDevice; - break; - } - } + // should report isActive from main device or it will cause trouble to other callers. + CachedBluetoothDevice subDevice = cachedDevice.getSubDevice(); + CachedBluetoothDevice finalTargetDevice = targetDevice; + if (targetDevice != null + && ((subDevice != null && subDevice.equals(targetDevice)) + || cachedDevice.getMemberDevice().stream().anyMatch( + memberDevice -> memberDevice.equals(finalTargetDevice)))) { + Log.d(TAG, + "The active device is the sub/member device " + + targetDevice.getDevice().getAnonymizedAddress() + + ". change targetDevice as main device " + + cachedDevice.getDevice().getAnonymizedAddress()); + targetDevice = cachedDevice; } - cachedDevice.onActiveDeviceChanged(isActive, bluetoothProfile); + boolean isActiveDevice = cachedDevice.equals(targetDevice); + cachedDevice.onActiveDeviceChanged(isActiveDevice, bluetoothProfile); mDeviceManager.onActiveDeviceChanged(cachedDevice); } + for (BluetoothCallback callback : mCallbacks) { - callback.onActiveDeviceChanged(activeDevice, bluetoothProfile); + callback.onActiveDeviceChanged(targetDevice, bluetoothProfile); } } diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothEventManagerTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothEventManagerTest.java index 8c316d1c4f21..13635c3a8256 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothEventManagerTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothEventManagerTest.java @@ -412,6 +412,21 @@ public class BluetoothEventManagerTest { } @Test + public void dispatchActiveDeviceChanged_activeFromSubDevice_mainCachedDeviceActive() { + CachedBluetoothDevice subDevice = new CachedBluetoothDevice(mContext, mLocalProfileManager, + mDevice3); + mCachedDevice1.setSubDevice(subDevice); + when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn( + Collections.singletonList(mCachedDevice1)); + mCachedDevice1.onProfileStateChanged(mHearingAidProfile, + BluetoothProfile.STATE_CONNECTED); + + assertThat(mCachedDevice1.isActiveDevice(BluetoothProfile.HEARING_AID)).isFalse(); + mBluetoothEventManager.dispatchActiveDeviceChanged(subDevice, BluetoothProfile.HEARING_AID); + assertThat(mCachedDevice1.isActiveDevice(BluetoothProfile.HEARING_AID)).isTrue(); + } + + @Test public void showUnbondMessage_reasonAuthTimeout_showCorrectedErrorCode() { mIntent = new Intent(BluetoothDevice.ACTION_BOND_STATE_CHANGED); mIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mBluetoothDevice); |