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

Commit 67d0d36b authored by Camden Bickel's avatar Camden Bickel Committed by Android (Google) Code Review
Browse files

Merge "audio: Only replace route info when Bluetooth device is bonded" into main

parents c8843080 aa04336b
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -420,7 +420,7 @@ import java.util.Objects;
        // to derive a name ourselves from the type instead.
        String deviceName = audioDeviceInfo.getPort().name();

        if (!TextUtils.isEmpty(address)) {
        if (mBluetoothRouteController.containsBondedDeviceWithAddress(address)) {
            routeId = mBluetoothRouteController.getRouteIdForBluetoothAddress(address);
            deviceName = mBluetoothRouteController.getNameForBluetoothAddress(address);
        }
+5 −0
Original line number Diff line number Diff line
@@ -141,6 +141,11 @@ import java.util.stream.Collectors;
        mContext.unregisterReceiver(mDeviceStateChangedReceiver);
    }

    /** Returns true if the given address corresponds to a currently-bonded Bluetooth device. */
    public synchronized boolean containsBondedDeviceWithAddress(@Nullable String address) {
        return mAddressToBondedDevice.containsKey(address);
    }

    @Nullable
    public synchronized String getRouteIdForBluetoothAddress(@Nullable String address) {
        BluetoothDevice bluetoothDevice = mAddressToBondedDevice.get(address);
+54 −0
Original line number Diff line number Diff line
@@ -82,6 +82,11 @@ public class AudioManagerRouteControllerTest {
    private static final AudioDeviceInfo FAKE_AUDIO_DEVICE_INFO_WIRED_HEADSET =
            createAudioDeviceInfo(
                    AudioSystem.DEVICE_OUT_WIRED_HEADSET, "name_wired_hs", /* address= */ null);
    private static final AudioDeviceInfo FAKE_AUDIO_DEVICE_INFO_WIRED_HEADSET_WITH_ADDRESS =
            createAudioDeviceInfo(
                    AudioSystem.DEVICE_OUT_WIRED_HEADSET,
                    "name_wired_hs_with_address",
                    /* address= */ "card=1;device=0");
    private static final AudioDeviceInfo FAKE_AUDIO_DEVICE_INFO_BLUETOOTH_A2DP =
            createAudioDeviceInfo(
                    AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, "name_a2dp", /* address= */ "12:34:45");
@@ -304,6 +309,55 @@ public class AudioManagerRouteControllerTest {
        assertThat(selectedRoute.getName().toString()).isEqualTo(FAKE_ROUTE_NAME);
    }

    @Test
    public void getAvailableRoutes_whenAddressIsPopulatedForNonBluetoothDevice_usesCorrectName() {
        addAvailableAudioDeviceInfo(
                /* newSelectedDevice= */ FAKE_AUDIO_DEVICE_INFO_WIRED_HEADSET_WITH_ADDRESS,
                /* newAvailableDevices...= */ FAKE_AUDIO_DEVICE_INFO_WIRED_HEADSET_WITH_ADDRESS,
                FAKE_AUDIO_DEVICE_INFO_BLUETOOTH_A2DP);

        List<MediaRoute2Info> availableRoutes = mControllerUnderTest.getAvailableRoutes();
        assertThat(availableRoutes.size()).isEqualTo(3);

        assertThat(
                        getAvailableRouteWithType(MediaRoute2Info.TYPE_WIRED_HEADSET)
                                .getName()
                                .toString())
                .isEqualTo(
                        FAKE_AUDIO_DEVICE_INFO_WIRED_HEADSET_WITH_ADDRESS
                                .getProductName()
                                .toString());

        assertThat(
                        getAvailableRouteWithType(MediaRoute2Info.TYPE_BLUETOOTH_A2DP)
                                .getName()
                                .toString())
                .isEqualTo(FAKE_AUDIO_DEVICE_INFO_BLUETOOTH_A2DP.getProductName().toString());
    }

    @Test
    public void
            getAvailableRoutes_whenAddressIsNotPopulatedForNonBluetoothDevice_usesCorrectName() {
        addAvailableAudioDeviceInfo(
                /* newSelectedDevice= */ FAKE_AUDIO_DEVICE_INFO_WIRED_HEADSET,
                /* newAvailableDevices...= */ FAKE_AUDIO_DEVICE_INFO_WIRED_HEADSET);

        List<MediaRoute2Info> availableRoutes = mControllerUnderTest.getAvailableRoutes();
        assertThat(availableRoutes.size()).isEqualTo(2);

        assertThat(
                        getAvailableRouteWithType(MediaRoute2Info.TYPE_BUILTIN_SPEAKER)
                                .getName()
                                .toString())
                .isEqualTo(FAKE_AUDIO_DEVICE_INFO_BUILTIN_SPEAKER.getProductName().toString());

        assertThat(
                        getAvailableRouteWithType(MediaRoute2Info.TYPE_WIRED_HEADSET)
                                .getName()
                                .toString())
                .isEqualTo(FAKE_AUDIO_DEVICE_INFO_WIRED_HEADSET.getProductName().toString());
    }

    // Internal methods.

    @NonNull