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

Commit e79af72c authored by Vlad Popa's avatar Vlad Popa
Browse files

Add mute state to the absolute volume callback

We need this information for the user to know whether the stream is
muted or not. With the absolute volume changes this is not reflected
with the index anymore

Test: atest AudioDeviceVolumeManagerTest
Bug: 391252779
Bug: 393657380
Flag: android.media.audio.unify_absolute_volume_management
Change-Id: I94ba9aafa50935d71c3b058e292f7b637927539b
parent 28cf8606
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -1473,7 +1473,7 @@ public class AudioDeviceBroker {
    }
    }


    /*package*/ int getVolumeForDeviceIgnoreMute(int streamType, int device) {
    /*package*/ int getVolumeForDeviceIgnoreMute(int streamType, int device) {
        return mAudioService.getVolumeForDeviceIgnoreMute(streamType, device);
        return mAudioService.getVolumeForDevice(streamType, device).first;
    }
    }


    /*package*/ int getMaxVssVolumeForStream(int streamType) {
    /*package*/ int getMaxVssVolumeForStream(int streamType) {
+28 −16
Original line number Original line Diff line number Diff line
@@ -246,6 +246,7 @@ import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.ArraySet;
import android.util.IntArray;
import android.util.IntArray;
import android.util.Log;
import android.util.Log;
import android.util.Pair;
import android.util.PrintWriterPrinter;
import android.util.PrintWriterPrinter;
import android.util.Slog;
import android.util.Slog;
import android.util.SparseArray;
import android.util.SparseArray;
@@ -542,9 +543,13 @@ public class AudioService extends IAudioService.Stub
     */
     */
    private InputDeviceVolumeHelper mInputDeviceVolumeHelper;
    private InputDeviceVolumeHelper mInputDeviceVolumeHelper;
    /*package*/ int getVolumeForDeviceIgnoreMute(int stream, int device) {
    /** Returns a pair of volume and mute state for the given stream and device. */
    /*package*/ Pair<Integer, Boolean> getVolumeForDevice(int stream, int device) {
        final VolumeStreamState streamState = mStreamStates.get(stream);
        final VolumeStreamState streamState = mStreamStates.get(stream);
        return streamState != null ? streamState.getIndex(device) : -1;
        if (streamState == null) {
            return new Pair<>(-1, false);
        }
        return new Pair<>(streamState.getIndex(device), streamState.mIsMuted);
    }
    }
    /**
    /**
@@ -4260,9 +4265,9 @@ public class AudioService extends IAudioService.Stub
                        0);
                        0);
            }
            }
            handleAbsoluteVolume(streamType, streamTypeAlias, deviceAttr,
            final boolean muted = streamState.mIsMuted;
                    getVssForStreamOrDefault(streamType).getIndex(deviceType),
            final int index = streamState.getIndex(deviceType);
                    flags);
            handleAbsoluteVolume(streamType, streamTypeAlias, deviceAttr, index, muted, flags);
        }
        }
        final int newIndex = getVssForStreamOrDefault(streamType).getIndex(deviceType);
        final int newIndex = getVssForStreamOrDefault(streamType).getIndex(deviceType);
@@ -4336,7 +4341,7 @@ public class AudioService extends IAudioService.Stub
    }
    }
    private boolean handleAbsoluteVolume(int streamType, int streamTypeAlias,
    private boolean handleAbsoluteVolume(int streamType, int streamTypeAlias,
            @NonNull AudioDeviceAttributes ada, int newIndex, int flags) {
            @NonNull AudioDeviceAttributes ada, int newIndex, boolean muted, int flags) {
            // Check if volume update should be handled by an external volume controller
            // Check if volume update should be handled by an external volume controller
        boolean registeredAsAbsoluteVolume = false;
        boolean registeredAsAbsoluteVolume = false;
        boolean volumeHandled = false;
        boolean volumeHandled = false;
@@ -4351,7 +4356,7 @@ public class AudioService extends IAudioService.Stub
                info = getAbsoluteVolumeDeviceInfo(deviceType);
                info = getAbsoluteVolumeDeviceInfo(deviceType);
            }
            }
            if (info != null) {
            if (info != null) {
                dispatchAbsoluteVolumeChanged(streamType, info, newIndex);
                dispatchAbsoluteVolumeChanged(streamType, info, newIndex, muted);
                registeredAsAbsoluteVolume = true;
                registeredAsAbsoluteVolume = true;
                volumeHandled = true;
                volumeHandled = true;
            }
            }
@@ -5344,8 +5349,10 @@ public class AudioService extends IAudioService.Stub
        final AudioDeviceAttributes device = absVolumeDevices.toArray(
        final AudioDeviceAttributes device = absVolumeDevices.toArray(
                new AudioDeviceAttributes[0])[0];
                new AudioDeviceAttributes[0])[0];
        final int index = (getVolumeForDeviceIgnoreMute(streamType, device.getInternalType()) + 5)
        final Pair<Integer, Boolean> volumePair = getVolumeForDevice(streamType,
                / 10;
                device.getInternalType());
        final int index = (volumePair.first + 5) / 10;
        final boolean muted = volumePair.second;
        if (DEBUG_VOL) {
        if (DEBUG_VOL) {
            Slog.i(TAG, "onUpdateContextualVolumes streamType: " + streamType
            Slog.i(TAG, "onUpdateContextualVolumes streamType: " + streamType
@@ -5355,7 +5362,8 @@ public class AudioService extends IAudioService.Stub
        final int streamTypeAlias = sStreamVolumeAlias.get(streamType, /*valueIfKeyNotFound*/
        final int streamTypeAlias = sStreamVolumeAlias.get(streamType, /*valueIfKeyNotFound*/
                streamType);
                streamType);
        if (!handleAbsoluteVolume(streamType, streamTypeAlias, device, index * 10, /*flags=*/0)) {
        if (!handleAbsoluteVolume(streamType, streamTypeAlias, device, index * 10, muted, /*flags=*/
                0)) {
            return;
            return;
        }
        }
@@ -5446,16 +5454,17 @@ public class AudioService extends IAudioService.Stub
            }
            }
        }
        }
        final boolean muted = streamState.mIsMuted;
        if (!mSoundDoseHelper.willDisplayWarningAfterCheckVolume(streamType, index,
        if (!mSoundDoseHelper.willDisplayWarningAfterCheckVolume(streamType, index,
                deviceType, flags)) {
                deviceType, flags)) {
            onSetStreamVolume(streamType, index, flags, deviceType, caller, hasModifyAudioSettings,
            onSetStreamVolume(streamType, index, flags, deviceType, caller, hasModifyAudioSettings,
                    // ada is non-null when called from setDeviceVolume,
                    // ada is non-null when called from setDeviceVolume,
                    // which shouldn't update the mute state
                    // which shouldn't update the mute state
                    canChangeMuteAndUpdateController /*canChangeMute*/);
                    canChangeMuteAndUpdateController /*canChangeMute*/);
            index = getVssForStreamOrDefault(streamType).getIndex(deviceType);
            index = streamState.getIndex(deviceType);
        }
        }
        handleAbsoluteVolume(streamType, streamTypeAlias, deviceAttr, index, flags);
        handleAbsoluteVolume(streamType, streamTypeAlias, deviceAttr, index, muted, flags);
        synchronized (mHdmiClientLock) {
        synchronized (mHdmiClientLock) {
            if (streamTypeAlias == AudioSystem.STREAM_MUSIC
            if (streamTypeAlias == AudioSystem.STREAM_MUSIC
@@ -5471,14 +5480,17 @@ public class AudioService extends IAudioService.Stub
    }
    }
    private void dispatchAbsoluteVolumeChanged(int streamType, AbsoluteVolumeDeviceInfo deviceInfo,
    private void dispatchAbsoluteVolumeChanged(int streamType, AbsoluteVolumeDeviceInfo deviceInfo,
            int index) {
            int index, boolean muted) {
        VolumeInfo volumeInfo = deviceInfo.getMatchingVolumeInfoForStream(streamType);
        VolumeInfo volumeInfo = deviceInfo.getMatchingVolumeInfoForStream(streamType);
        if (volumeInfo != null) {
        if (volumeInfo != null) {
            try {
            try {
                VolumeInfo.Builder volInfoBuilder = new VolumeInfo.Builder(volumeInfo)
                        .setVolumeIndex(rescaleIndex(index, streamType, volumeInfo));
                if (unifyAbsoluteVolumeManagement()) {
                    volInfoBuilder.setMuted(muted);
                }
                deviceInfo.mCallback.dispatchDeviceVolumeChanged(deviceInfo.mDevice,
                deviceInfo.mCallback.dispatchDeviceVolumeChanged(deviceInfo.mDevice,
                        new VolumeInfo.Builder(volumeInfo)
                        volInfoBuilder.build());
                                .setVolumeIndex(rescaleIndex(index, streamType, volumeInfo))
                                .build());
            } catch (RemoteException e) {
            } catch (RemoteException e) {
                Log.w(TAG, "Couldn't dispatch absolute volume behavior volume change");
                Log.w(TAG, "Couldn't dispatch absolute volume behavior volume change");
            }
            }
+4 −2
Original line number Original line Diff line number Diff line
@@ -50,6 +50,7 @@ import android.provider.Settings;
import android.text.TextUtils;
import android.text.TextUtils;
import android.util.Log;
import android.util.Log;
import android.util.MathUtils;
import android.util.MathUtils;
import android.util.Pair;
import android.util.SparseIntArray;
import android.util.SparseIntArray;


import com.android.internal.R;
import com.android.internal.R;
@@ -724,8 +725,9 @@ public class SoundDoseHelper {
                int device = mAudioService.getDeviceForStream(AudioSystem.STREAM_MUSIC);
                int device = mAudioService.getDeviceForStream(AudioSystem.STREAM_MUSIC);
                if (safeDevicesContains(device) && isStreamActive) {
                if (safeDevicesContains(device) && isStreamActive) {
                    scheduleMusicActiveCheck();
                    scheduleMusicActiveCheck();
                    int index = mAudioService.getVolumeForDeviceIgnoreMute(AudioSystem.STREAM_MUSIC,
                    final Pair<Integer, Boolean> volumePair = mAudioService.getVolumeForDevice(
                            device);
                            AudioSystem.STREAM_MUSIC, device);
                    final int index = volumePair.second ? 0 : volumePair.first;
                    if (index > safeMediaVolumeIndex(device)) {
                    if (index > safeMediaVolumeIndex(device)) {
                        // Approximate cumulative active music time
                        // Approximate cumulative active music time
                        long curTimeMs = SystemClock.elapsedRealtime();
                        long curTimeMs = SystemClock.elapsedRealtime();
+3 −2
Original line number Original line Diff line number Diff line
@@ -253,7 +253,7 @@ public class AbsoluteVolumeBehaviorTest {


        // Dispatched volume index is scaled to the range in the initial VolumeInfo
        // Dispatched volume index is scaled to the range in the initial VolumeInfo
        verify(mMockDispatcher).dispatchDeviceVolumeChanged(DEVICE_SPEAKER_OUT,
        verify(mMockDispatcher).dispatchDeviceVolumeChanged(DEVICE_SPEAKER_OUT,
                new VolumeInfo.Builder(volumeInfo).setVolumeIndex(250).build());
                new VolumeInfo.Builder(volumeInfo).setVolumeIndex(250).setMuted(false).build());
    }
    }


    @Test
    @Test
@@ -401,6 +401,7 @@ public class AbsoluteVolumeBehaviorTest {
        verify(mMockDispatcher, never()).dispatchDeviceVolumeChanged(eq(DEVICE_SPEAKER_OUT), any());
        verify(mMockDispatcher, never()).dispatchDeviceVolumeChanged(eq(DEVICE_SPEAKER_OUT), any());
        // Volume changed dispatched for adjust-only absolute volume listener
        // Volume changed dispatched for adjust-only absolute volume listener
        verify(mMockAdjustOnlyAbsoluteVolumeDispatcher).dispatchDeviceVolumeChanged(
        verify(mMockAdjustOnlyAbsoluteVolumeDispatcher).dispatchDeviceVolumeChanged(
                DEVICE_SPEAKER_OUT, new VolumeInfo.Builder(volumeInfo).setVolumeIndex(250).build());
                DEVICE_SPEAKER_OUT, new VolumeInfo.Builder(volumeInfo).setVolumeIndex(250).setMuted(
                        false).build());
    }
    }
}
}