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

Commit 06d98a11 authored by Wenyu Zhang's avatar Wenyu Zhang
Browse files

Use empty inputGainIndexMap map when getSupportedDeviceTypes call fails

Change-Id: Id26c48e25c4ebca9eae836dfee578329a8bd5ef8
Bug:b/364923030
Test:AudioServiceTest
Flag:com.android.media.flags.enable_audio_input_device_routing_and_volume_control
parent 3f72bb5d
Loading
Loading
Loading
Loading
+12 −16
Original line number Diff line number Diff line
@@ -23,10 +23,10 @@ import android.annotation.Nullable;
import android.content.ContentResolver;
import android.media.AudioDeviceAttributes;
import android.media.AudioDeviceInfo;
import android.media.AudioManager;
import android.media.AudioSystem;
import android.os.UserHandle;
import android.util.IntArray;
import android.util.Log;
import android.util.SparseIntArray;

import java.util.HashSet;
@@ -48,7 +48,7 @@ import java.util.Set;
    // A map between device internal type (e.g. AudioSystem.DEVICE_IN_BUILTIN_MIC) to its input gain
    // index.
    private final SparseIntArray mInputGainIndexMap;
    private final Set<Integer> mSupportedDeviceTypes;
    private final Set<Integer> mSupportedDeviceTypes = new HashSet<>();

    InputDeviceVolumeHelper(
            SettingsAdapter settings,
@@ -60,20 +60,16 @@ import java.util.Set;

        IntArray internalDeviceTypes = new IntArray();
        int status = AudioSystem.getSupportedDeviceTypes(GET_DEVICES_INPUTS, internalDeviceTypes);
        mInputGainIndexMap =
                new SparseIntArray(
                        status == AudioManager.SUCCESS
                                ? internalDeviceTypes.size()
                                : AudioSystem.DEVICE_IN_ALL_SET.size());

        if (status == AudioManager.SUCCESS) {
            Set<Integer> supportedDeviceTypes = new HashSet<>();
            for (int i = 0; i < internalDeviceTypes.size(); i++) {
                supportedDeviceTypes.add(internalDeviceTypes.get(i));
        if (status != AudioSystem.SUCCESS) {
            Log.e(TAG, "AudioSystem.getSupportedDeviceTypes(GET_DEVICES_INPUTS) failed. status:"
                    + status);
        }
            mSupportedDeviceTypes = supportedDeviceTypes;
        } else {
            mSupportedDeviceTypes = AudioSystem.DEVICE_IN_ALL_SET;

        // Note that in a rare case, if AudioSystem.getSupportedDeviceTypes call fails, both
        // mInputGainIndexMap and mSupportedDeviceTypes will be empty.
        mInputGainIndexMap = new SparseIntArray(internalDeviceTypes.size());
        for (int i = 0; i < internalDeviceTypes.size(); i++) {
            mSupportedDeviceTypes.add(internalDeviceTypes.get(i));
        }

        readSettings();
+26 −3
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
package com.android.server.audio;

import static android.media.AudioDeviceInfo.TYPE_BUILTIN_MIC;
import static android.media.AudioManager.GET_DEVICES_INPUTS;

import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
@@ -34,7 +35,9 @@ import android.media.AudioSystem;
import android.os.Looper;
import android.os.PermissionEnforcer;
import android.os.UserHandle;
import android.os.test.TestLooper;
import android.platform.test.annotations.EnableFlags;
import android.util.IntArray;
import android.util.Log;

import androidx.test.InstrumentationRegistry;
@@ -80,12 +83,15 @@ public class AudioServiceTest {

    private static boolean sLooperPrepared = false;

    private TestLooper mTestLooper;

    @Before
    public void setUp() throws Exception {
        if (!sLooperPrepared) {
            Looper.prepare();
            sLooperPrepared = true;
        }
        mTestLooper = new TestLooper();
        mContext = InstrumentationRegistry.getTargetContext();
        mSpyAudioSystem = spy(new NoOpAudioSystemAdapter());
        mSettingsAdapter = new NoOpSettingsAdapter();
@@ -93,8 +99,11 @@ public class AudioServiceTest {
        when(mMockAppOpsManager.noteOp(anyInt(), anyInt(), anyString(), anyString(), anyString()))
                .thenReturn(AppOpsManager.MODE_ALLOWED);
        mAudioService = new AudioService(mContext, mSpyAudioSystem, mSpySystemServer,
                mSettingsAdapter, mAudioVolumeGroupHelper, mMockAudioPolicy, null,
                mMockAppOpsManager, mMockPermissionEnforcer, mMockPermissionProvider, r -> r.run());
                mSettingsAdapter, mAudioVolumeGroupHelper, mMockAudioPolicy,
                mTestLooper.getLooper(), mMockAppOpsManager, mMockPermissionEnforcer,
                mMockPermissionProvider, r -> r.run());

        mTestLooper.dispatchAll();
    }

    /**
@@ -216,7 +225,19 @@ public class AudioServiceTest {
    public void testInputGainIndex() throws Exception {
        Log.i(TAG, "running testInputGainIndex");
        Assert.assertNotNull(mAudioService);
        Thread.sleep(MAX_MESSAGE_HANDLING_DELAY_MS); // wait for full AudioService initialization

        IntArray internalDeviceTypes = new IntArray();
        int status = AudioSystem.getSupportedDeviceTypes(GET_DEVICES_INPUTS, internalDeviceTypes);
        if (status != AudioSystem.SUCCESS) {
            Log.e(TAG, "AudioSystem.getSupportedDeviceTypes(GET_DEVICES_INPUTS) failed. status:"
                    + status);
        }

        // Make sure TYPE_BUILTIN_MIC, aka DEVICE_IN_BUILTIN_MIC in terms of internal device type,
        // is supported.
        if (!internalDeviceTypes.contains(AudioSystem.DEVICE_IN_BUILTIN_MIC)) {
            return;
        }

        AudioDeviceAttributes ada =
                new AudioDeviceAttributes(
@@ -229,6 +250,8 @@ public class AudioServiceTest {

        int inputGainIndex = 20;
        mAudioService.setInputGainIndex(ada, inputGainIndex);
        mTestLooper.dispatchAll();

        Assert.assertEquals(
                "input gain index reporting wrong value",
                inputGainIndex,