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

Commit 3ef75498 authored by Eric Laurent's avatar Eric Laurent
Browse files

Allow mute when playing over fixed volume device

Modified AudioService to add the possibility to mute/unmute music
with volume keys while playing over fixed volume devices (HDMI, digital dock).
VOL- forces volume to 0 and VOL+ forces volume to maximum.

Bug 7377764.

Change-Id: Ibaf805d76c30e3c0e395547cd3ce0087dbfb9f30
parent b38f8ea5
Loading
Loading
Loading
Loading
+125 −104
Original line number Diff line number Diff line
@@ -821,28 +821,42 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
        VolumeStreamState streamState = mStreamStates[streamTypeAlias];

        final int device = getDeviceForStream(streamTypeAlias);

        // get last audible index if stream is muted, current index otherwise
        final int aliasIndex = streamState.getIndex(device,
        int aliasIndex = streamState.getIndex(device,
                        (streamState.muteCount() != 0) /* lastAudible */);
        boolean adjustVolume = true;

        // convert one UI step (+/-1) into a number of internal units on the stream alias
        int step = rescaleIndex(10, streamType, streamTypeAlias);

        int step;
        int index;
        int oldIndex;

        if ((direction == AudioManager.ADJUST_RAISE) &&
                !checkSafeMediaVolume(streamTypeAlias, aliasIndex + step, device)) {
            index = mStreamStates[streamType].getIndex(device,
                                                 (streamState.muteCount() != 0)  /* lastAudible */);
            oldIndex = index;
        } else {
        flags &= ~AudioManager.FLAG_FIXED_VOLUME;
        if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) &&
               ((device & mFixedVolumeDevices) != 0)) {
            flags |= AudioManager.FLAG_FIXED_VOLUME;
                index = mStreamStates[streamType].getMaxIndex();

            // Always toggle between max safe volume and 0 for fixed volume devices where safe
            // volume is enforced, and max and 0 for the others.
            // This is simulated by stepping by the full allowed volume range
            if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE &&
                    (device & mSafeMediaVolumeDevices) != 0) {
                step = mSafeMediaVolumeIndex;
            } else {
                step = streamState.getMaxIndex();
            }
            if (aliasIndex != 0) {
                aliasIndex = step;
            }
        } else {
            // convert one UI step (+/-1) into a number of internal units on the stream alias
            step = rescaleIndex(10, streamType, streamTypeAlias);
        }

        if ((direction == AudioManager.ADJUST_RAISE) &&
                !checkSafeMediaVolume(streamTypeAlias, aliasIndex + step, device)) {
            index = mStreamStates[streamType].getIndex(device,
                                                 (streamState.muteCount() != 0)  /* lastAudible */);
            oldIndex = index;
        } else {
            // If either the client forces allowing ringer modes for this adjustment,
@@ -896,7 +910,6 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                index = mStreamStates[streamType].getIndex(device, false  /* lastAudible */);
            }
        }
        }
        sendVolumeUpdate(streamType, oldIndex, index, flags);
    }

@@ -924,19 +937,27 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
        final int device = getDeviceForStream(streamType);
        int oldIndex;

        flags &= ~AudioManager.FLAG_FIXED_VOLUME;
        if ((mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) &&
                ((device & mFixedVolumeDevices) != 0)) {
            flags |= AudioManager.FLAG_FIXED_VOLUME;
            index = mStreamStates[streamType].getMaxIndex();
            oldIndex = index;
        } else {
        // get last audible index if stream is muted, current index otherwise
        oldIndex = streamState.getIndex(device,
                                        (streamState.muteCount() != 0) /* lastAudible */);

        index = rescaleIndex(index * 10, streamType, mStreamVolumeAlias[streamType]);

        flags &= ~AudioManager.FLAG_FIXED_VOLUME;
        if ((mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) &&
                ((device & mFixedVolumeDevices) != 0)) {
            flags |= AudioManager.FLAG_FIXED_VOLUME;
            // volume is either 0 or max allowed for fixed volume devices
            if (index != 0) {
                if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE &&
                        (device & mSafeMediaVolumeDevices) != 0) {
                    index = mSafeMediaVolumeIndex;
                } else {
                    index = streamState.getMaxIndex();
                }
            }
        }

        if (!checkSafeMediaVolume(mStreamVolumeAlias[streamType], index, device)) {
            return;
        }
@@ -963,7 +984,6 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
        // get last audible index if stream is muted, current index otherwise
        index = mStreamStates[streamType].getIndex(device,
                                (mStreamStates[streamType].muteCount() != 0) /* lastAudible */);
        }
        sendVolumeUpdate(streamType, oldIndex, index, flags);
    }

@@ -1202,13 +1222,11 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
    public int getStreamVolume(int streamType) {
        ensureValidStreamType(streamType);
        int device = getDeviceForStream(streamType);
        int index;
        int index = mStreamStates[streamType].getIndex(device, false  /* lastAudible */);

        if ((mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) &&
        if (index != 0 && (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) &&
                (device & mFixedVolumeDevices) != 0) {
            index = mStreamStates[streamType].getMaxIndex();
        } else {
            index = mStreamStates[streamType].getIndex(device, false  /* lastAudible */);
        }
        return (index + 5) / 10;
    }
@@ -2702,15 +2720,6 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                }
                remainingDevices &= ~device;

                // ignore settings for fixed volume devices: volume should always be at max
                if ((mStreamVolumeAlias[mStreamType] == AudioSystem.STREAM_MUSIC) &&
                        ((device & mFixedVolumeDevices) != 0)) {
                    if (muteCount() == 0) {
                        mIndex.put(device, mIndexMax);
                    }
                    mLastAudibleIndex.put(device, mIndexMax);
                    continue;
                }
                // retrieve current volume for device
                String name = getSettingNameForDevice(false /* lastAudible */, device);
                // if no volume stored for current stream and device, use default volume if default
@@ -2723,6 +2732,18 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                    continue;
                }

                // ignore settings for fixed volume devices: volume should always be at max or 0
                if ((mStreamVolumeAlias[mStreamType] == AudioSystem.STREAM_MUSIC) &&
                        ((device & mFixedVolumeDevices) != 0)) {
                    if ((muteCount()) == 0 && (index != 0)) {
                        mIndex.put(device, mIndexMax);
                    } else {
                        mIndex.put(device, 0);
                    }
                    mLastAudibleIndex.put(device, mIndexMax);
                    continue;
                }

                // retrieve last audible volume for device
                name = getSettingNameForDevice(true  /* lastAudible */, device);
                // use stored last audible index if present, otherwise use current index if not 0