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

Commit 5500bee7 authored by Jasmine Cha's avatar Jasmine Cha Committed by Android (Google) Code Review
Browse files

Merge "audio: Flag for disabling pre-scale absolute volume." into main

parents 1bbb899e e1528b59
Loading
Loading
Loading
Loading
+25 −14
Original line number Original line Diff line number Diff line
@@ -38,6 +38,7 @@ import static android.provider.Settings.Secure.VOLUME_HUSH_OFF;
import static android.provider.Settings.Secure.VOLUME_HUSH_VIBRATE;
import static android.provider.Settings.Secure.VOLUME_HUSH_VIBRATE;
import static com.android.media.audio.Flags.bluetoothMacAddressAnonymization;
import static com.android.media.audio.Flags.bluetoothMacAddressAnonymization;
import static com.android.media.audio.Flags.disablePrescaleAbsoluteVolume;
import static com.android.server.audio.SoundDoseHelper.ACTION_CHECK_MUSIC_ACTIVE;
import static com.android.server.audio.SoundDoseHelper.ACTION_CHECK_MUSIC_ACTIVE;
import static com.android.server.utils.EventLogger.Event.ALOGE;
import static com.android.server.utils.EventLogger.Event.ALOGE;
import static com.android.server.utils.EventLogger.Event.ALOGI;
import static com.android.server.utils.EventLogger.Event.ALOGI;
@@ -1371,6 +1372,7 @@ public class AudioService extends IAudioService.Stub
                sRingerAndZenModeMutedStreams, "onInitStreamsAndVolumes"));
                sRingerAndZenModeMutedStreams, "onInitStreamsAndVolumes"));
        setRingerModeInt(getRingerModeInternal(), false);
        setRingerModeInt(getRingerModeInternal(), false);
        if (!disablePrescaleAbsoluteVolume()) {
            final float[] preScale = new float[3];
            final float[] preScale = new float[3];
            preScale[0] = mContext.getResources().getFraction(
            preScale[0] = mContext.getResources().getFraction(
                    com.android.internal.R.fraction.config_prescaleAbsoluteVolume_index1,
                    com.android.internal.R.fraction.config_prescaleAbsoluteVolume_index1,
@@ -1386,6 +1388,7 @@ public class AudioService extends IAudioService.Stub
                    mPrescaleAbsoluteVolume[i] = preScale[i];
                    mPrescaleAbsoluteVolume[i] = preScale[i];
                }
                }
            }
            }
        }
        initExternalEventReceivers();
        initExternalEventReceivers();
@@ -8618,7 +8621,7 @@ public class AudioService extends IAudioService.Stub
            if (index == 0) {
            if (index == 0) {
                // 0% for volume 0
                // 0% for volume 0
                index = 0;
                index = 0;
            } else if (index > 0 && index <= 3) {
            } else if (!disablePrescaleAbsoluteVolume() && index > 0 && index <= 3) {
                // Pre-scale for volume steps 1 2 and 3
                // Pre-scale for volume steps 1 2 and 3
                index = (int) (mIndexMax * mPrescaleAbsoluteVolume[index - 1]) / 10;
                index = (int) (mIndexMax * mPrescaleAbsoluteVolume[index - 1]) / 10;
            } else {
            } else {
@@ -11448,6 +11451,14 @@ public class AudioService extends IAudioService.Stub
        pw.print("  adjust-only absolute volume devices="); pw.println(dumpDeviceTypes(
        pw.print("  adjust-only absolute volume devices="); pw.println(dumpDeviceTypes(
                getAbsoluteVolumeDevicesWithBehavior(
                getAbsoluteVolumeDevicesWithBehavior(
                        AudioManager.DEVICE_VOLUME_BEHAVIOR_ABSOLUTE_ADJUST_ONLY)));
                        AudioManager.DEVICE_VOLUME_BEHAVIOR_ABSOLUTE_ADJUST_ONLY)));
        pw.print("  pre-scale for bluetooth absolute volume ");
        if (disablePrescaleAbsoluteVolume()) {
            pw.println("= disabled");
        } else {
            pw.println("=" + mPrescaleAbsoluteVolume[0]
                    + ", " + mPrescaleAbsoluteVolume[1]
                    + ", " + mPrescaleAbsoluteVolume[2]);
        }
        pw.print("  mExtVolumeController="); pw.println(mExtVolumeController);
        pw.print("  mExtVolumeController="); pw.println(mExtVolumeController);
        pw.print("  mHdmiAudioSystemClient="); pw.println(mHdmiAudioSystemClient);
        pw.print("  mHdmiAudioSystemClient="); pw.println(mHdmiAudioSystemClient);
        pw.print("  mHdmiPlaybackClient="); pw.println(mHdmiPlaybackClient);
        pw.print("  mHdmiPlaybackClient="); pw.println(mHdmiPlaybackClient);
+98 −0
Original line number Original line Diff line number Diff line
@@ -16,6 +16,8 @@


package com.android.server.audio;
package com.android.server.audio;


import static com.android.media.audio.Flags.FLAG_DISABLE_PRESCALE_ABSOLUTE_VOLUME;

import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.spy;
@@ -28,13 +30,22 @@ import android.media.AudioManager;
import android.media.AudioSystem;
import android.media.AudioSystem;
import android.media.VolumeInfo;
import android.media.VolumeInfo;
import android.os.test.TestLooper;
import android.os.test.TestLooper;
import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;


import androidx.test.InstrumentationRegistry;
import androidx.test.InstrumentationRegistry;


import org.junit.Before;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.Test;


public class AudioDeviceVolumeManagerTest {
public class AudioDeviceVolumeManagerTest {
    @Rule
    public final CheckFlagsRule mCheckFlagsRule =
            DeviceFlagsValueProvider.createCheckFlagsRule();

    private static final String TAG = "AudioDeviceVolumeManagerTest";
    private static final String TAG = "AudioDeviceVolumeManagerTest";


    private static final AudioDeviceAttributes DEVICE_SPEAKER_OUT = new AudioDeviceAttributes(
    private static final AudioDeviceAttributes DEVICE_SPEAKER_OUT = new AudioDeviceAttributes(
@@ -96,4 +107,91 @@ public class AudioDeviceVolumeManagerTest {
        verify(mSpyAudioSystem, atLeast(1)).setStreamVolumeIndexAS(
        verify(mSpyAudioSystem, atLeast(1)).setStreamVolumeIndexAS(
                AudioManager.STREAM_MUSIC, midIndex, AudioSystem.DEVICE_OUT_USB_DEVICE);
                AudioManager.STREAM_MUSIC, midIndex, AudioSystem.DEVICE_OUT_USB_DEVICE);
    }
    }

    @Test
    @RequiresFlagsDisabled(FLAG_DISABLE_PRESCALE_ABSOLUTE_VOLUME)
    public void testConfigurablePreScaleAbsoluteVolume() throws Exception {
        AudioManager am = mContext.getSystemService(AudioManager.class);
        final int minIndex = am.getStreamMinVolume(AudioManager.STREAM_MUSIC);
        final int maxIndex = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        final VolumeInfo volMedia = new VolumeInfo.Builder(AudioManager.STREAM_MUSIC)
                .setMinVolumeIndex(minIndex)
                .setMaxVolumeIndex(maxIndex)
                .build();
        final AudioDeviceAttributes bleDevice = new AudioDeviceAttributes(
                /*native type*/ AudioSystem.DEVICE_OUT_BLE_HEADSET, /*address*/ "fake_ble");
        final int maxPreScaleIndex = 3;
        final float[] preScale = new float[3];
        preScale[0] = mContext.getResources().getFraction(
                com.android.internal.R.fraction.config_prescaleAbsoluteVolume_index1,
                1, 1);
        preScale[1] = mContext.getResources().getFraction(
                com.android.internal.R.fraction.config_prescaleAbsoluteVolume_index2,
                1, 1);
        preScale[2] = mContext.getResources().getFraction(
                com.android.internal.R.fraction.config_prescaleAbsoluteVolume_index3,
                1, 1);

        for (int i = 0; i < maxPreScaleIndex; i++) {
            final int targetIndex = (int) (preScale[i] * maxIndex);
            final VolumeInfo volCur = new VolumeInfo.Builder(volMedia)
                    .setVolumeIndex(i + 1).build();
            // Adjust stream volume with FLAG_ABSOLUTE_VOLUME set (index:1~3)
            mAudioService.setDeviceVolume(volCur, bleDevice, mPackageName);
            mTestLooper.dispatchAll();

            // Stream volume changes
            verify(mSpyAudioSystem, atLeast(1)).setStreamVolumeIndexAS(
                            AudioManager.STREAM_MUSIC, targetIndex,
                            AudioSystem.DEVICE_OUT_BLE_HEADSET);
        }

        // Adjust stream volume with FLAG_ABSOLUTE_VOLUME set (index:4)
        final VolumeInfo volIndex4 = new VolumeInfo.Builder(volMedia)
                .setVolumeIndex(4).build();
        mAudioService.setDeviceVolume(volIndex4, bleDevice, mPackageName);
        mTestLooper.dispatchAll();

        verify(mSpyAudioSystem, atLeast(1)).setStreamVolumeIndexAS(
                        AudioManager.STREAM_MUSIC, maxIndex,
                        AudioSystem.DEVICE_OUT_BLE_HEADSET);
    }

    @Test
    @RequiresFlagsEnabled(FLAG_DISABLE_PRESCALE_ABSOLUTE_VOLUME)
    public void testDisablePreScaleAbsoluteVolume() throws Exception {
        AudioManager am = mContext.getSystemService(AudioManager.class);
        final int minIndex = am.getStreamMinVolume(AudioManager.STREAM_MUSIC);
        final int maxIndex = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        final VolumeInfo volMedia = new VolumeInfo.Builder(AudioManager.STREAM_MUSIC)
                .setMinVolumeIndex(minIndex)
                .setMaxVolumeIndex(maxIndex)
                .build();
        final AudioDeviceAttributes bleDevice = new AudioDeviceAttributes(
                /*native type*/ AudioSystem.DEVICE_OUT_BLE_HEADSET, /*address*/ "bla");
        final int maxPreScaleIndex = 3;

        for (int i = 0; i < maxPreScaleIndex; i++) {
            final VolumeInfo volCur = new VolumeInfo.Builder(volMedia)
                    .setVolumeIndex(i + 1).build();
            // Adjust stream volume with FLAG_ABSOLUTE_VOLUME set (index:1~3)
            mAudioService.setDeviceVolume(volCur, bleDevice, mPackageName);
            mTestLooper.dispatchAll();

            // Stream volume changes
            verify(mSpyAudioSystem, atLeast(1)).setStreamVolumeIndexAS(
                            AudioManager.STREAM_MUSIC, maxIndex,
                            AudioSystem.DEVICE_OUT_BLE_HEADSET);
        }

        // Adjust stream volume with FLAG_ABSOLUTE_VOLUME set (index:4)
        final VolumeInfo volIndex4 = new VolumeInfo.Builder(volMedia)
                .setVolumeIndex(4).build();
        mAudioService.setDeviceVolume(volIndex4, bleDevice, mPackageName);
        mTestLooper.dispatchAll();

        verify(mSpyAudioSystem, atLeast(1)).setStreamVolumeIndexAS(
                        AudioManager.STREAM_MUSIC, maxIndex,
                        AudioSystem.DEVICE_OUT_BLE_HEADSET);
    }
}
}