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

Commit 7fdde867 authored by Vlad Popa's avatar Vlad Popa
Browse files

Add possibility to mute and set index with setVolumeForDevice

Test: atest AudioDeviceVolumeManagerTest
Bug: 404313846
Bug: 396216663
Flag: android.media.audio.device_volume_apis
Change-Id: I0eccafd03230b287dbe107cab7d50cb4c23dd39e
parent 22cdf6cc
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -734,7 +734,15 @@ public class AudioDeviceVolumeManager {
     * @hide
     * Sets the volume on the given audio device
     *
     * @param vi the volume information, only stream-based volumes are supported
     * @param vi the volume information, only stream-based volumes are supported for now.
     * <ul>If vi doesn't contain a mute state: the index is set to vi's index </ul>
     * <ul>If vi contains a mute state:
     * <li> if the ada device is the current device for the VolumeInfo routing strategy, the mute
     *          state will be applied to the stream type</li>
     * <li> otherwise, the mute state is left unchanged: if vi is muted, the index is set to its
     *           min value; if vi is unmuted, the index is set to vi's index.</li>
     * </ul>
     *
     * @param ada the device for which volume is to be modified
     */
    @SystemApi
+42 −18
Original line number Diff line number Diff line
@@ -4893,8 +4893,11 @@ public class AudioService extends IAudioService.Stub
            Log.e(TAG, "VSS for stream type " + streamType + " is null");
            return;
        }
        setDeviceVolumeInt(vi, vss, ada, callingPackage, /*flags=*/0, /*changeMute=*/false,
                "setVolumeForDevice");
        final AudioDeviceAttributes currDev = getDeviceAttributesForStream(
                streamType, /*selectAbsoluteDevices=*/true);
        setDeviceVolumeInt(vi, vss, ada, callingPackage, /*flags=*/0, /*changeMute=*/
                ada.equalTypeAddress(currDev), "setVolumeForDevice");
    }
    /**
@@ -4921,7 +4924,6 @@ public class AudioService extends IAudioService.Stub
                new DeviceVolumeEvent(streamType, direction, ada, /*deviceForStream=*/-1,
                        callingPackage, /*skipped=*/false, /*event=*/"adjustVolumeForDevice"));
        adjustStreamVolume(streamType, direction, /*flags=*/0, ada, callingPackage,
                "adjustVolumeForDevice", Binder.getCallingUid(), Binder.getCallingPid(),
                /*attributionTag=*/null, /*hasModifyAudioSettings=*/true,
@@ -4961,15 +4963,16 @@ public class AudioService extends IAudioService.Stub
            return;
        }
        final int currDev = getDeviceForStream(streamType, /*selectAbsoluteDevices=*/true);
        final AudioDeviceAttributes currDev = getDeviceAttributesForStream(
                streamType, /*selectAbsoluteDevices=*/true);
        final boolean currDevIsAda = currDev.equalTypeAddress(ada);
        AudioService.sVolumeLogger.enqueue(
                new DeviceVolumeEvent(streamType, vi.getVolumeIndex(), ada, callingPackage,
                        currDev == ada.getInternalType()));
                        currDevIsAda));
        setDeviceVolumeInt(vi, vss, ada, callingPackage,
                FLAG_ABSOLUTE_VOLUME, currDev == ada.getInternalType(),
                "notifyAbsoluteVolumeChanged");
        setDeviceVolumeInt(vi, vss, ada, callingPackage, FLAG_ABSOLUTE_VOLUME, /*changeMute=*/
                currDevIsAda, "notifyAbsoluteVolumeChanged");
    }
    private static boolean isVolumeInfoValid(VolumeInfo vi, boolean forAdjust) {
@@ -4995,10 +4998,19 @@ public class AudioService extends IAudioService.Stub
            String callingPackage, int flags, boolean changeMute, String caller) {
        int streamType = vss.getStreamType();
        int index = vi.getVolumeIndex();
        final boolean streamMuted = isStreamMute(streamType);
        // if a stream is not muted but the VolumeInfo is for muting, set the volume index
        // for the device to min volume
        boolean hasMuteState = deviceVolumeApis() ? vi.hasMuteState() : vi.hasMuteCommand();
        if (hasMuteState && vi.isMuted() && !isStreamMute(streamType)) {
        Slog.v(TAG,
                "setDeviceVolumeInt from caller " + caller + " changeMute " + changeMute + " index "
                        + index + " muteState " + (hasMuteState ? vi.isMuted() : "no state")
                        + " streamType " + streamType + " streamMuted " + streamMuted + " device "
                        + Integer.toHexString(ada.getInternalType()));
        if (hasMuteState) {
            // if not allowed to change mute we are only setting the index to min
            if (vi.isMuted() && !streamMuted && !changeMute) {
                setStreamVolumeWithAttributionInt(streamType,
                        vss.getMinIndex(),
                        flags,
@@ -5006,12 +5018,24 @@ public class AudioService extends IAudioService.Stub
                        changeMute);
                return;
            }
            if (deviceVolumeApis() && changeMute && vi.isMuted() != streamMuted) {
                adjustStreamVolumeWithAttribution(streamType,
                        vi.isMuted() ? AudioManager.ADJUST_MUTE : AudioManager.ADJUST_UNMUTE, flags,
                        getPackageNameForUid(Binder.getCallingUid()), /*attributionTag=*/null);
                // do not change the mute if we also set an index
                changeMute = false;
            }
        }
        if (vi.getMinVolumeIndex() == VolumeInfo.INDEX_NOT_SET
                || vi.getMaxVolumeIndex() == VolumeInfo.INDEX_NOT_SET) {
            // assume index meant to be in stream type range, validate
            if ((index * 10) < vss.getMinIndex()
                    || (index * 10) > vss.getMaxIndex()) {
                if (hasMuteState) {
                    // no need to set an index if we have a mute state
                    return;
                }
                throw new IllegalArgumentException("invalid volume index " + index
                        + " not between min/max for stream " + vi.getStreamType());
            }
@@ -5650,7 +5674,6 @@ public class AudioService extends IAudioService.Stub
            }
        }
        final boolean muted = streamState.mIsMuted;
        if (!mSoundDoseHelper.willDisplayWarningAfterCheckVolume(streamType, index,
                deviceType, flags)) {
            onSetStreamVolume(streamType, index, flags, deviceType, caller, hasModifyAudioSettings,
@@ -5660,7 +5683,8 @@ public class AudioService extends IAudioService.Stub
            index = streamState.getIndex(deviceType);
        }
        handleAbsoluteVolume(streamType, streamTypeAlias, deviceAttr, index, muted, flags);
        handleAbsoluteVolume(streamType, streamTypeAlias, deviceAttr, index, streamState.mIsMuted,
                flags);
        synchronized (mHdmiClientLock) {
            if (streamTypeAlias == AudioSystem.STREAM_MUSIC
@@ -6151,7 +6175,7 @@ public class AudioService extends IAudioService.Stub
                index = (vss.getIndex(ada.getInternalType()) + 5) / 10;
            }
            vib.setVolumeIndex(index);
            // only set as a mute command if stream muted
            // only set as a mute state if stream muted
            if (vss.mIsMuted) {
                vib.setMuted(true);
            }