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

Commit 788ac357 authored by Wenyu Zhang's avatar Wenyu Zhang Committed by Android (Google) Code Review
Browse files

Merge "Only activate device that is not existed previously" into main

parents 83a945d4 4835ed1e
Loading
Loading
Loading
Loading
+17 −5
Original line number Diff line number Diff line
@@ -80,8 +80,14 @@ public final class InputRouteManager {
                    // behavior.
                    @AudioDeviceType int deviceTypeToActivate = mSelectedInputDeviceType;
                    for (AudioDeviceInfo info : addedDevices) {
                        if (InputMediaDevice.isSupportedInputDevice(info.getType())) {
                            deviceTypeToActivate = info.getType();
                        @AudioDeviceType int type = info.getType();
                        // Since onAudioDevicesAdded is called not only when new device is hot
                        // plugged, but also when the switcher dialog is opened, make sure to check
                        // against existing device list and only activate if the device does not
                        // exist previously.
                        if (InputMediaDevice.isSupportedInputDevice(type)
                                && findDeviceByType(type) == null) {
                            deviceTypeToActivate = type;
                        }
                    }

@@ -140,16 +146,22 @@ public final class InputRouteManager {
    }

    // TODO(b/355684672): handle edge case where there are two devices with the same type. Only
    // using a single mSelectedInputDeviceType might not be enough to recognize the correct device.
    public @Nullable MediaDevice getSelectedInputDevice() {
    // using a single type might not be enough to recognize the correct device.
    @Nullable
    private MediaDevice findDeviceByType(@AudioDeviceType int type) {
        for (MediaDevice device : mInputMediaDevices) {
            if (((InputMediaDevice) device).getAudioDeviceInfoType() == mSelectedInputDeviceType) {
            if (((InputMediaDevice) device).getAudioDeviceInfoType() == type) {
                return device;
            }
        }
        return null;
    }

    @Nullable
    public MediaDevice getSelectedInputDevice() {
        return findDeviceByType(mSelectedInputDeviceType);
    }

    private void applyDefaultSelectedTypeToAllPresets() {
        mSelectedInputDeviceType = retrieveDefaultSelectedDeviceType();
        AudioDeviceAttributes deviceAttributes =
+20 −0
Original line number Diff line number Diff line
@@ -360,6 +360,26 @@ public class InputRouteManagerTest {
        }
    }

    @Test
    public void onAudioDevicesAdded_doNotActivatePreexistingDevice() {
        final AudioManager audioManager = mock(AudioManager.class);
        InputRouteManager inputRouteManager = new InputRouteManager(mContext, audioManager);

        final AudioDeviceInfo info = mockWiredHeadsetInfo();
        InputMediaDevice device = createInputMediaDeviceFromDeviceInfo(info);
        inputRouteManager.mInputMediaDevices.add(device);

        // Trigger onAudioDevicesAdded with a device that already exists in the device list.
        AudioDeviceInfo[] devices = {info};
        inputRouteManager.mAudioDeviceCallback.onAudioDevicesAdded(devices);

        // The device should not be activated.
        for (@MediaRecorder.Source int preset : PRESETS) {
            verify(audioManager, never())
                    .setPreferredDeviceForCapturePreset(preset, getWiredHeadsetDeviceAttributes());
        }
    }

    @Test
    public void onAudioDevicesRemoved_shouldApplyDefaultSelectedDeviceToAllPresets() {
        final AudioManager audioManager = mock(AudioManager.class);