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

Commit 453c9ee2 authored by SongFerngWang's avatar SongFerngWang
Browse files

[Unicast] Won't show "active" when changing active from HS to LEHS

if the 'active device' is in the member device list, then the main
device is active and then updating the main device's state.

Bug: 259892289
Test: build pass.
make RunSettingsLibRoboTests -j40 ROBOTEST_FILTER=BluetoothEventManagerTest

Change-Id: I60d7cb40cc29ea12e5d4f12c10d44eda3e2cf4fe
parent a668ed61
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -233,7 +233,22 @@ public class BluetoothEventManager {
            @Nullable CachedBluetoothDevice activeDevice,
            int bluetoothProfile) {
        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;
                    }
                }
            }
            cachedDevice.onActiveDeviceChanged(isActive, bluetoothProfile);
        }
        for (BluetoothCallback callback : mCallbacks) {
+44 −1
Original line number Diff line number Diff line
@@ -70,10 +70,14 @@ public class BluetoothEventManagerTest {
    @Mock
    private HearingAidProfile mHearingAidProfile;
    @Mock
    private LeAudioProfile mLeAudioProfile;
    @Mock
    private BluetoothDevice mDevice1;
    @Mock
    private BluetoothDevice mDevice2;
    @Mock
    private BluetoothDevice mDevice3;
    @Mock
    private LocalBluetoothProfileManager mLocalProfileManager;
    @Mock
    private BluetoothUtils.ErrorListener mErrorListener;
@@ -83,6 +87,7 @@ public class BluetoothEventManagerTest {
    private BluetoothEventManager mBluetoothEventManager;
    private CachedBluetoothDevice mCachedDevice1;
    private CachedBluetoothDevice mCachedDevice2;
    private CachedBluetoothDevice mCachedDevice3;

    @Before
    public void setUp() {
@@ -95,9 +100,10 @@ public class BluetoothEventManagerTest {
        when(mHfpProfile.isProfileReady()).thenReturn(true);
        when(mA2dpProfile.isProfileReady()).thenReturn(true);
        when(mHearingAidProfile.isProfileReady()).thenReturn(true);

        when(mLeAudioProfile.isProfileReady()).thenReturn(true);
        mCachedDevice1 = new CachedBluetoothDevice(mContext, mLocalProfileManager, mDevice1);
        mCachedDevice2 = new CachedBluetoothDevice(mContext, mLocalProfileManager, mDevice2);
        mCachedDevice3 = new CachedBluetoothDevice(mContext, mLocalProfileManager, mDevice3);
        BluetoothUtils.setErrorListener(mErrorListener);
    }

@@ -293,6 +299,43 @@ public class BluetoothEventManagerTest {
        assertThat(mCachedDevice2.isActiveDevice(BluetoothProfile.HEADSET)).isFalse();
    }

    @Test
    public void dispatchActiveDeviceChanged_connectedMemberDevices_activeDeviceChanged() {
        final List<CachedBluetoothDevice> cachedDevices = new ArrayList<>();
        cachedDevices.add(mCachedDevice1);
        cachedDevices.add(mCachedDevice2);

        int group1 = 1;
        when(mDevice3.getAddress()).thenReturn("testAddress3");
        mCachedDevice1.setGroupId(group1);
        mCachedDevice3.setGroupId(group1);
        mCachedDevice1.addMemberDevice(mCachedDevice3);

        when(mDevice1.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
        when(mDevice2.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
        when(mDevice3.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
        when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(cachedDevices);

        // Connect device1 and device3 for LE and device2 for A2DP and HFP
        mCachedDevice1.onProfileStateChanged(mLeAudioProfile, BluetoothProfile.STATE_CONNECTED);
        mCachedDevice3.onProfileStateChanged(mLeAudioProfile, BluetoothProfile.STATE_CONNECTED);
        mCachedDevice2.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED);
        mCachedDevice2.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED);

        // Verify that both devices are connected and none is Active
        assertThat(mCachedDevice1.isActiveDevice(BluetoothProfile.LE_AUDIO)).isFalse();
        assertThat(mCachedDevice2.isActiveDevice(BluetoothProfile.A2DP)).isFalse();
        assertThat(mCachedDevice2.isActiveDevice(BluetoothProfile.HEADSET)).isFalse();
        assertThat(mCachedDevice3.isActiveDevice(BluetoothProfile.LE_AUDIO)).isFalse();

        // The member device is active.
        mBluetoothEventManager.dispatchActiveDeviceChanged(mCachedDevice3,
                BluetoothProfile.LE_AUDIO);

        // The main device is active since the member is active.
        assertThat(mCachedDevice1.isActiveDevice(BluetoothProfile.LE_AUDIO)).isTrue();
    }

    /**
     * Test to verify onActiveDeviceChanged() with A2DP and Hearing Aid.
     */