Fix did not show active status when changing the active device

Root Cause: The reported active device might be sub device or member device which are not listed in CachedBluetoothDevice list, so the main CachedBluetoothDevice on the UI will not reflect its correct status.

Solution: When searching in the list, also search their sub device and member device then report active on the main CachedBluetoothDevice.

Bug: 300220339
Test: make RunSettingsLibRoboTests ROBOTEST_FILTER=BluetoothEventManagerTest
Change-Id: I5740e61a7c8ac498c63e6aeaf11f355643ab42ed
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java
index f5bacb6..c97445f 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 @@
 
     @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 8c316d1..13635c3 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 @@
     }
 
     @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);