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

Commit 18fb6cdb authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Fix UI incorrect when switch active device to hearing aid"

parents 356c5dfd 2a3151d2
Loading
Loading
Loading
Loading
+28 −3
Original line number Diff line number Diff line
@@ -277,14 +277,39 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall
    public void onActiveDeviceChanged(CachedBluetoothDevice activeDevice, int bluetoothProfile) {
        Log.d(TAG, "onActiveDeviceChanged : device : "
                + activeDevice + ", profile : " + bluetoothProfile);
        if (BluetoothProfile.HEARING_AID == bluetoothProfile
                || BluetoothProfile.A2DP == bluetoothProfile) {

        if (BluetoothProfile.HEARING_AID == bluetoothProfile) {
            if (activeDevice != null) {
                dispatchConnectedDeviceChanged(MediaDeviceUtils.getId(activeDevice));
            }
        } else if (BluetoothProfile.A2DP == bluetoothProfile) {
            // When active device change to Hearing Aid,
            // BluetoothEventManager also send onActiveDeviceChanged() to notify that active device
            // of A2DP profile is null. To handle this case, check hearing aid device
            // is active device or not
            final MediaDevice activeHearingAidDevice = findActiveHearingAidDevice();
            final String id = activeDevice == null
                    ? PhoneMediaDevice.ID : MediaDeviceUtils.getId(activeDevice);
                    ? activeHearingAidDevice == null
                    ? PhoneMediaDevice.ID : activeHearingAidDevice.getId()
                    : MediaDeviceUtils.getId(activeDevice);
            dispatchConnectedDeviceChanged(id);
        }
    }

    private MediaDevice findActiveHearingAidDevice() {
        final HearingAidProfile hearingAidProfile = mProfileManager.getHearingAidProfile();

        if (hearingAidProfile != null) {
            final List<BluetoothDevice> activeDevices = hearingAidProfile.getActiveDevices();
            for (BluetoothDevice btDevice : activeDevices) {
                if (btDevice != null) {
                    return findMediaDevice(MediaDeviceUtils.getId(btDevice));
                }
            }
        }
        return null;
    }

    @Override
    public void onServiceConnected() {
        if (!mIsA2dpProfileReady || !mIsHearingAidProfileReady) {
+12 −0
Original line number Diff line number Diff line
@@ -15,6 +15,8 @@
 */
package com.android.settingslib.media;

import android.bluetooth.BluetoothDevice;

import androidx.mediarouter.media.MediaRouter;

import com.android.settingslib.bluetooth.CachedBluetoothDevice;
@@ -33,6 +35,16 @@ public class MediaDeviceUtils {
        return cachedDevice.getAddress();
    }

    /**
     * Use BluetoothDevice address to represent unique id
     *
     * @param bluetoothDevice the BluetoothDevice
     * @return BluetoothDevice address
     */
    public static String getId(BluetoothDevice bluetoothDevice) {
        return bluetoothDevice.getAddress();
    }

    /**
     * Use RouteInfo id to represent unique id
     *
+30 −0
Original line number Diff line number Diff line
@@ -425,4 +425,34 @@ public class BluetoothMediaManagerTest {

        verify(mCallback, never()).onConnectedDeviceChanged(any());
    }

    @Test
    public void onActiveDeviceChanged_hearingAidDeviceIsActive_returnHearingAidDeviceId() {
        final BluetoothDevice bluetoothDevice = mock(BluetoothDevice.class);
        final List<BluetoothDevice> devices = new ArrayList<>();
        devices.add(bluetoothDevice);
        final BluetoothMediaDevice bluetoothMediaDevice = mock(BluetoothMediaDevice.class);
        mMediaManager.mMediaDevices.add(bluetoothMediaDevice);

        when(bluetoothDevice.getAddress()).thenReturn(TEST_ADDRESS);
        when(mHapProfile.getActiveDevices()).thenReturn(devices);
        when(bluetoothMediaDevice.getId()).thenReturn(TEST_ADDRESS);

        mMediaManager.registerCallback(mCallback);
        mMediaManager.onActiveDeviceChanged(null, BluetoothProfile.A2DP);

        verify(mCallback).onConnectedDeviceChanged(TEST_ADDRESS);
    }

    @Test
    public void onActiveDeviceChanged_hearingAidDeviceNotActive_returnPhoneDeviceId() {
        final List<BluetoothDevice> devices = new ArrayList<>();

        when(mHapProfile.getActiveDevices()).thenReturn(devices);

        mMediaManager.registerCallback(mCallback);
        mMediaManager.onActiveDeviceChanged(null, BluetoothProfile.A2DP);

        verify(mCallback).onConnectedDeviceChanged(PhoneMediaDevice.ID);
    }
}
+16 −3
Original line number Diff line number Diff line
@@ -20,6 +20,8 @@ import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.when;

import android.bluetooth.BluetoothDevice;

import androidx.mediarouter.media.MediaRouter;

import com.android.settingslib.bluetooth.CachedBluetoothDevice;
@@ -38,7 +40,9 @@ public class MediaDeviceUtilsTest {
    private static final String TEST_ROUTE_ID = "test_route_id";

    @Mock
    private CachedBluetoothDevice mDevice;
    private CachedBluetoothDevice mCachedDevice;
    @Mock
    private BluetoothDevice mBluetoothDevice;
    @Mock
    private MediaRouter.RouteInfo mRouteInfo;

@@ -47,11 +51,20 @@ public class MediaDeviceUtilsTest {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void getId_returnCachedBluetoothDeviceAddress() {
        when(mCachedDevice.getAddress()).thenReturn(TEST_ADDRESS);

        final String id = MediaDeviceUtils.getId(mCachedDevice);

        assertThat(id).isEqualTo(TEST_ADDRESS);
    }

    @Test
    public void getId_returnBluetoothDeviceAddress() {
        when(mDevice.getAddress()).thenReturn(TEST_ADDRESS);
        when(mBluetoothDevice.getAddress()).thenReturn(TEST_ADDRESS);

        final String id = MediaDeviceUtils.getId(mDevice);
        final String id = MediaDeviceUtils.getId(mBluetoothDevice);

        assertThat(id).isEqualTo(TEST_ADDRESS);
    }