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

Commit a7263e0c authored by Alexandr Shabalin's avatar Alexandr Shabalin Committed by Android (Google) Code Review
Browse files

Merge "Populate `InputMediaDevice#isSelected`." into main

parents a4a15ffc 1135ae6e
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -52,6 +52,8 @@ public class InputMediaDevice extends MediaDevice {

    private final boolean mIsVolumeFixed;

    private final boolean mIsSelected;

    private final String mProductName;

    private InputMediaDevice(
@@ -62,6 +64,7 @@ public class InputMediaDevice extends MediaDevice {
            int maxVolume,
            int currentVolume,
            boolean isVolumeFixed,
            boolean isSelected,
            @Nullable String productName) {
        super(context, /* routeInfo= */ null, /* dynamicRouteAttributes= */ null,
                /* rlpItem= */ null);
@@ -71,6 +74,7 @@ public class InputMediaDevice extends MediaDevice {
        mMaxVolume = maxVolume;
        mCurrentVolume = currentVolume;
        mIsVolumeFixed = isVolumeFixed;
        mIsSelected = isSelected;
        mProductName = productName;
        initDeviceRecord();
    }
@@ -84,6 +88,7 @@ public class InputMediaDevice extends MediaDevice {
            int maxVolume,
            int currentVolume,
            boolean isVolumeFixed,
            boolean isSelected,
            @Nullable String productName) {
        if (!isSupportedInputDevice(audioDeviceInfoType)) {
            return null;
@@ -97,6 +102,7 @@ public class InputMediaDevice extends MediaDevice {
                maxVolume,
                currentVolume,
                isVolumeFixed,
                isSelected,
                productName);
    }

@@ -197,4 +203,14 @@ public class InputMediaDevice extends MediaDevice {
    public boolean isVolumeFixed() {
        return mIsVolumeFixed;
    }

    @Override
    public boolean isSelected() {
        return mIsSelected;
    }

    @Override
    public boolean isInputDevice() {
        return true;
    }
}
+10 −33
Original line number Diff line number Diff line
@@ -209,33 +209,6 @@ public final class InputRouteManager {
        return null;
    }

    @Nullable
    private MediaDevice findDeviceByType(@AudioDeviceType int type) {
        for (MediaDevice device : mInputMediaDevices) {
            if (((InputMediaDevice) device).getAudioDeviceInfoType() == type) {
                return device;
            }
        }
        return null;
    }

    @Nullable
    public MediaDevice getSelectedInputDevice() {
        MediaDevice exactDevice =
                findDeviceByTypeAndAddress(mSelectedInputDeviceType, mSelectedInputDeviceAddr);

        // This can happen because the address can sometimes contain surprising strings
        // such as "bottom" for the default internal mic. In those situations,
        // ignore the address and search by the type only. In any case, this also
        // serves as a sane fallback.
        if (exactDevice == null) {
            MediaDevice device = findDeviceByType(mSelectedInputDeviceType);
            return device;
        }

        return exactDevice;
    }

    private void applyDefaultSelectedTypeToAllPresets() {
        AudioDeviceAttributes deviceAttributes = retrieveDefaultSelectedInputDeviceAttrs();

@@ -278,6 +251,7 @@ public final class InputRouteManager {
                mAudioManager.getDevices(AudioManager.GET_DEVICES_INPUTS);
        mInputMediaDevices.clear();
        for (AudioDeviceInfo info : audioDeviceInfos) {
            boolean isSelected = isSelectedDevice(info.getType(), info.getAddress());
            MediaDevice mediaDevice =
                    InputMediaDevice.create(
                            mContext,
@@ -287,11 +261,10 @@ public final class InputRouteManager {
                            getMaxInputGain(),
                            getCurrentInputGain(),
                            isInputGainFixed(),
                            isSelected,
                            getProductNameFromAudioDeviceInfo(info));
            if (mediaDevice != null) {
                if (info.getType() == mSelectedInputDeviceType
                        && (TextUtils.isEmpty(mSelectedInputDeviceAddr)
                                || info.getAddress() == mSelectedInputDeviceAddr)) {
                if (isSelected) {
                    mInfoMediaManager.setDeviceState(mediaDevice, STATE_SELECTED);
                }
                mInputMediaDevices.add(mediaDevice);
@@ -306,6 +279,11 @@ public final class InputRouteManager {
        }
    }

    private boolean isSelectedDevice(@AudioDeviceType int type, @NonNull String address) {
        return type == mSelectedInputDeviceType && (TextUtils.isEmpty(mSelectedInputDeviceAddr)
                || address.equals(mSelectedInputDeviceAddr));
    }

    /**
     * Gets the product name for the given {@link AudioDeviceInfo}.
     *
@@ -331,9 +309,8 @@ public final class InputRouteManager {
            return;
        }

        if (inputMediaDevice.getAudioDeviceInfoType() == mSelectedInputDeviceType
                && (TextUtils.isEmpty(mSelectedInputDeviceAddr)
                        || inputMediaDevice.getAddress().equals(mSelectedInputDeviceAddr))) {
        if (isSelectedDevice(inputMediaDevice.getAudioDeviceInfoType(),
                inputMediaDevice.getAddress())) {
            Slog.w(TAG, "This device is already selected: " + device.getName());
            return;
        }
+4 −0
Original line number Diff line number Diff line
@@ -408,6 +408,10 @@ public abstract class MediaDevice implements Comparable<MediaDevice> {
        return mRouteInfo.getType() == TYPE_BLE_HEADSET;
    }

    public boolean isInputDevice() {
        return false;
    }

    /**
     * Get application label from MediaDevice.
     *
+10 −0
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ public class InputMediaDeviceTest {
    private final int MAX_VOLUME = 1;
    private final int CURRENT_VOLUME = 0;
    private final boolean IS_VOLUME_FIXED = true;
    private static final boolean IS_SELECTED = true;
    private static final String PRODUCT_NAME_BUILTIN_MIC = "Built-in Mic";
    private static final String PRODUCT_NAME_WIRED_HEADSET = "My Wired Headset";
    private static final String PRODUCT_NAME_USB_HEADSET = "My USB Headset";
@@ -73,6 +74,7 @@ public class InputMediaDeviceTest {
                        MAX_VOLUME,
                        CURRENT_VOLUME,
                        IS_VOLUME_FIXED,
                        IS_SELECTED,
                        PRODUCT_NAME_BUILTIN_MIC);
        assertThat(builtinMediaDevice).isNotNull();
        assertThat(builtinMediaDevice.getDrawableResId()).isEqualTo(R.drawable.ic_media_microphone);
@@ -89,6 +91,7 @@ public class InputMediaDeviceTest {
                        MAX_VOLUME,
                        CURRENT_VOLUME,
                        IS_VOLUME_FIXED,
                        IS_SELECTED,
                        PRODUCT_NAME_BUILTIN_MIC);
        assertThat(builtinMediaDevice).isNotNull();
        assertThat(builtinMediaDevice.getName())
@@ -106,6 +109,7 @@ public class InputMediaDeviceTest {
                        MAX_VOLUME,
                        CURRENT_VOLUME,
                        IS_VOLUME_FIXED,
                        IS_SELECTED,
                        PRODUCT_NAME_WIRED_HEADSET);
        assertThat(wiredMediaDevice).isNotNull();
        assertThat(wiredMediaDevice.getName())
@@ -123,6 +127,7 @@ public class InputMediaDeviceTest {
                        MAX_VOLUME,
                        CURRENT_VOLUME,
                        IS_VOLUME_FIXED,
                        IS_SELECTED,
                        PRODUCT_NAME_USB_HEADSET);
        assertThat(usbMediaDevice).isNotNull();
        assertThat(usbMediaDevice.getName()).isEqualTo(PRODUCT_NAME_USB_HEADSET);
@@ -139,6 +144,7 @@ public class InputMediaDeviceTest {
                        MAX_VOLUME,
                        CURRENT_VOLUME,
                        IS_VOLUME_FIXED,
                        IS_SELECTED,
                        null);
        assertThat(usbMediaDevice).isNotNull();
        assertThat(usbMediaDevice.getName())
@@ -156,6 +162,7 @@ public class InputMediaDeviceTest {
                        MAX_VOLUME,
                        CURRENT_VOLUME,
                        IS_VOLUME_FIXED,
                        IS_SELECTED,
                        PRODUCT_NAME_BT_HEADSET);
        assertThat(btMediaDevice).isNotNull();
        assertThat(btMediaDevice.getName()).isEqualTo(PRODUCT_NAME_BT_HEADSET);
@@ -172,6 +179,7 @@ public class InputMediaDeviceTest {
                        MAX_VOLUME,
                        CURRENT_VOLUME,
                        IS_VOLUME_FIXED,
                        IS_SELECTED,
                        null);
        assertThat(btMediaDevice).isNotNull();
        assertThat(btMediaDevice.getName())
@@ -189,6 +197,7 @@ public class InputMediaDeviceTest {
                        MAX_VOLUME,
                        CURRENT_VOLUME,
                        IS_VOLUME_FIXED,
                        IS_SELECTED,
                        PRODUCT_NAME_BLE_HEADSET);
        assertThat(bleMediaDevice).isNotNull();
        assertThat(bleMediaDevice.getName()).isEqualTo(PRODUCT_NAME_BLE_HEADSET);
@@ -205,6 +214,7 @@ public class InputMediaDeviceTest {
                        MAX_VOLUME,
                        CURRENT_VOLUME,
                        IS_VOLUME_FIXED,
                        IS_SELECTED,
                        null);
        assertThat(bleMediaDevice).isNotNull();
        assertThat(bleMediaDevice.getName())
+14 −9
Original line number Diff line number Diff line
@@ -36,6 +36,8 @@ import android.media.AudioDeviceInfo;
import android.media.AudioManager;
import android.media.MediaRecorder;

import androidx.annotation.Nullable;

import com.android.settingslib.testutils.shadow.ShadowRouter2Manager;

import org.junit.Before;
@@ -256,9 +258,7 @@ public class InputRouteManagerTest {
        onPreferredDevicesForCapturePresetChanged();

        // The selected input device has the same type as the one returned from AudioManager.
        InputMediaDevice selectedInputDevice =
                (InputMediaDevice) mInputRouteManager.getSelectedInputDevice();
        assertThat(selectedInputDevice.getAudioDeviceInfoType())
        assertThat(getSelectedInputDevice().getAudioDeviceInfoType())
                .isEqualTo(AudioDeviceInfo.TYPE_WIRED_HEADSET);
    }

@@ -278,9 +278,7 @@ public class InputRouteManagerTest {
        onPreferredDevicesForCapturePresetChanged();

        // The selected input device has the same type as the first one returned from AudioManager.
        InputMediaDevice selectedInputDevice =
                (InputMediaDevice) mInputRouteManager.getSelectedInputDevice();
        assertThat(selectedInputDevice.getAudioDeviceInfoType())
        assertThat(getSelectedInputDevice().getAudioDeviceInfoType())
                .isEqualTo(AudioDeviceInfo.TYPE_WIRED_HEADSET);
    }

@@ -297,9 +295,7 @@ public class InputRouteManagerTest {
        onPreferredDevicesForCapturePresetChanged();

        // The selected input device has default type AudioDeviceInfo.TYPE_BUILTIN_MIC.
        InputMediaDevice selectedInputDevice =
                (InputMediaDevice) mInputRouteManager.getSelectedInputDevice();
        assertThat(selectedInputDevice.getAudioDeviceInfoType())
        assertThat(getSelectedInputDevice().getAudioDeviceInfoType())
                .isEqualTo(AudioDeviceInfo.TYPE_BUILTIN_MIC);
    }

@@ -314,6 +310,7 @@ public class InputRouteManagerTest {
                        MAX_VOLUME,
                        CURRENT_VOLUME,
                        VOLUME_FIXED_TRUE,
                        /* isSelected= */ false,
                        PRODUCT_NAME_BUILTIN_MIC);
        mInputRouteManager.selectDevice(builtinMicDevice);

@@ -429,6 +426,7 @@ public class InputRouteManagerTest {
                        MAX_VOLUME,
                        CURRENT_VOLUME,
                        VOLUME_FIXED_TRUE,
                        /* isSelected= */ false,
                        PRODUCT_NAME_USB_ACCESSORY);
        AudioDeviceInfo[] devices = {mockUsbHeadsetInfo()};

@@ -519,6 +517,13 @@ public class InputRouteManagerTest {
                MAX_VOLUME,
                CURRENT_VOLUME,
                VOLUME_FIXED_TRUE,
                /* isSelected= */ false,
                info.getProductName() == null ? null : info.getProductName().toString());
    }

    @Nullable
    private InputMediaDevice getSelectedInputDevice() {
        return (InputMediaDevice) mInputRouteManager.mInputMediaDevices.stream().filter(
                MediaDevice::isSelected).findFirst().orElse(null);
    }
}
Loading