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

Commit 7b633e61 authored by Jason Hsu's avatar Jason Hsu Committed by Android (Google) Code Review
Browse files

Merge "Fix did not show active status when changing the active device" into main

parents 1d5285fb f80abd35
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);