Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f80abd35 authored by jasonwshsu's avatar jasonwshsu Committed by Jason Hsu
Browse files

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
parent fa48db8c
Loading
Loading
Loading
Loading
+19 −19
Original line number Diff line number Diff line
@@ -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) {
            // 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 member device "
                                        + activeDevice.getDevice().getAnonymizedAddress()
                                        + ". change activeDevice as main device "
                        "The active device is the sub/member device "
                                + targetDevice.getDevice().getAnonymizedAddress()
                                + ". change targetDevice as main device "
                                + cachedDevice.getDevice().getAnonymizedAddress());
                        activeDevice = cachedDevice;
                        break;
                    }
                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);
        }
    }

+15 −0
Original line number Diff line number Diff line
@@ -411,6 +411,21 @@ public class BluetoothEventManagerTest {
                BluetoothProfile.HEARING_AID);
    }

    @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);