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

Commit 4835ed1e authored by Wenyu Zhang's avatar Wenyu Zhang
Browse files

Only activate device that is not existed previously

Since onAudioDevicesAdded is called not only when new device is hot
plugged, but also when the switcher dialog is opened, adding a check to
make sure only activate if the device does not exist previously.

Change-Id: Icf9756c38ed78be7898e5094f62627025bdb253c
Bug: b/379798051, b/355684672
Test: InputRouteManagerTest
Flag: com.android.media.flags.enable_audio_input_device_routing_and_volume_control
parent b08834b5
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);