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

Commit 33268e6a authored by Vlad Popa's avatar Vlad Popa Committed by Android (Google) Code Review
Browse files

Merge "Add possibility to mute and set index with setVolumeForDevice" into main

parents 60734847 7fdde867
Loading
Loading
Loading
Loading
+9 −1
Original line number Original line Diff line number Diff line
@@ -734,7 +734,15 @@ public class AudioDeviceVolumeManager {
     * @hide
     * @hide
     * Sets the volume on the given audio device
     * 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
     * @param ada the device for which volume is to be modified
     */
     */
    @SystemApi
    @SystemApi
+42 −18
Original line number Original line 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");
            Log.e(TAG, "VSS for stream type " + streamType + " is null");
            return;
            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,
                new DeviceVolumeEvent(streamType, direction, ada, /*deviceForStream=*/-1,
                        callingPackage, /*skipped=*/false, /*event=*/"adjustVolumeForDevice"));
                        callingPackage, /*skipped=*/false, /*event=*/"adjustVolumeForDevice"));
        adjustStreamVolume(streamType, direction, /*flags=*/0, ada, callingPackage,
        adjustStreamVolume(streamType, direction, /*flags=*/0, ada, callingPackage,
                "adjustVolumeForDevice", Binder.getCallingUid(), Binder.getCallingPid(),
                "adjustVolumeForDevice", Binder.getCallingUid(), Binder.getCallingPid(),
                /*attributionTag=*/null, /*hasModifyAudioSettings=*/true,
                /*attributionTag=*/null, /*hasModifyAudioSettings=*/true,
@@ -4961,15 +4963,16 @@ public class AudioService extends IAudioService.Stub
            return;
            return;
        }
        }
        final int currDev = getDeviceForStream(streamType, /*selectAbsoluteDevices=*/true);
        final AudioDeviceAttributes currDev = getDeviceAttributesForStream(
                streamType, /*selectAbsoluteDevices=*/true);
        final boolean currDevIsAda = currDev.equalTypeAddress(ada);
        AudioService.sVolumeLogger.enqueue(
        AudioService.sVolumeLogger.enqueue(
                new DeviceVolumeEvent(streamType, vi.getVolumeIndex(), ada, callingPackage,
                new DeviceVolumeEvent(streamType, vi.getVolumeIndex(), ada, callingPackage,
                        currDev == ada.getInternalType()));
                        currDevIsAda));
        setDeviceVolumeInt(vi, vss, ada, callingPackage,
        setDeviceVolumeInt(vi, vss, ada, callingPackage, FLAG_ABSOLUTE_VOLUME, /*changeMute=*/
                FLAG_ABSOLUTE_VOLUME, currDev == ada.getInternalType(),
                currDevIsAda, "notifyAbsoluteVolumeChanged");
                "notifyAbsoluteVolumeChanged");
    }
    }
    private static boolean isVolumeInfoValid(VolumeInfo vi, boolean forAdjust) {
    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) {
            String callingPackage, int flags, boolean changeMute, String caller) {
        int streamType = vss.getStreamType();
        int streamType = vss.getStreamType();
        int index = vi.getVolumeIndex();
        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
        // if a stream is not muted but the VolumeInfo is for muting, set the volume index
        // for the device to min volume
        // for the device to min volume
        boolean hasMuteState = deviceVolumeApis() ? vi.hasMuteState() : vi.hasMuteCommand();
        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,
                setStreamVolumeWithAttributionInt(streamType,
                        vss.getMinIndex(),
                        vss.getMinIndex(),
                        flags,
                        flags,
@@ -5006,12 +5018,24 @@ public class AudioService extends IAudioService.Stub
                        changeMute);
                        changeMute);
                return;
                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
        if (vi.getMinVolumeIndex() == VolumeInfo.INDEX_NOT_SET
                || vi.getMaxVolumeIndex() == VolumeInfo.INDEX_NOT_SET) {
                || vi.getMaxVolumeIndex() == VolumeInfo.INDEX_NOT_SET) {
            // assume index meant to be in stream type range, validate
            // assume index meant to be in stream type range, validate
            if ((index * 10) < vss.getMinIndex()
            if ((index * 10) < vss.getMinIndex()
                    || (index * 10) > vss.getMaxIndex()) {
                    || (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
                throw new IllegalArgumentException("invalid volume index " + index
                        + " not between min/max for stream " + vi.getStreamType());
                        + " 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,
        if (!mSoundDoseHelper.willDisplayWarningAfterCheckVolume(streamType, index,
                deviceType, flags)) {
                deviceType, flags)) {
            onSetStreamVolume(streamType, index, flags, deviceType, caller, hasModifyAudioSettings,
            onSetStreamVolume(streamType, index, flags, deviceType, caller, hasModifyAudioSettings,
@@ -5660,7 +5683,8 @@ public class AudioService extends IAudioService.Stub
            index = streamState.getIndex(deviceType);
            index = streamState.getIndex(deviceType);
        }
        }
        handleAbsoluteVolume(streamType, streamTypeAlias, deviceAttr, index, muted, flags);
        handleAbsoluteVolume(streamType, streamTypeAlias, deviceAttr, index, streamState.mIsMuted,
                flags);
        synchronized (mHdmiClientLock) {
        synchronized (mHdmiClientLock) {
            if (streamTypeAlias == AudioSystem.STREAM_MUSIC
            if (streamTypeAlias == AudioSystem.STREAM_MUSIC
@@ -6151,7 +6175,7 @@ public class AudioService extends IAudioService.Stub
                index = (vss.getIndex(ada.getInternalType()) + 5) / 10;
                index = (vss.getIndex(ada.getInternalType()) + 5) / 10;
            }
            }
            vib.setVolumeIndex(index);
            vib.setVolumeIndex(index);
            // only set as a mute command if stream muted
            // only set as a mute state if stream muted
            if (vss.mIsMuted) {
            if (vss.mIsMuted) {
                vib.setMuted(true);
                vib.setMuted(true);
            }
            }