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

Commit 7619ca4e authored by Phil Tunstall's avatar Phil Tunstall Committed by Steve Kondik
Browse files

Sound preference: Safe volume restore when plugging in a headset (1/2)

When enabled, volumes that were above a threshold (~55%) the previous time
something was connected to the audio jack will be restored to that threshold
rather than their full level.

Affects all sound streams apart from voice call audio.

Change-Id: I0007a09ce75b55c95986e38a4cbb24fe4e8200a9
parent 4f2fa524
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -1626,6 +1626,11 @@ public final class Settings {
         */
        public static final String VOLUME_BLUETOOTH_SCO = "volume_bluetooth_sco";

        /**
         * Whether to prevent loud volume levels when headset is first plugged in.
         */
        public static final String SAFE_HEADSET_VOLUME_RESTORE = "safe_headset_volume_restore";

        /**
         * Whether the notifications should use the ring volume (value of 1) or a separate
         * notification volume (value of 0). In most cases, users will have this enabled so the
+28 −1
Original line number Diff line number Diff line
@@ -2572,6 +2572,15 @@ public class AudioService extends IAudioService.Stub {
                //Save and restore volumes for headset and speaker
                int lastVolume;
                if (state == 1) {
                    // Headset plugged in
                    int volumeRestoreCap;
                    if (Settings.System.getInt(mContentResolver,
                            Settings.System.SAFE_HEADSET_VOLUME_RESTORE, 1) == 0) {
                        // Don't cap
                        volumeRestoreCap = 8;
                    } else {
                        volumeRestoreCap = 4;
                    }
                    for (int stream = 0; stream < STREAM_VOLUME_HEADSET_SETTINGS.length; stream++) {
                        try {
                            lastVolume = System.getInt(mContentResolver,
@@ -2582,9 +2591,27 @@ public class AudioService extends IAudioService.Stub {
                        System.putInt(mContentResolver, STREAM_VOLUME_SPEAKER_SETTINGS[stream],
                                getStreamVolume(stream));
                        if (lastVolume >= 0)
                            if (stream == 0) {
                                // Don't touch voice call volume
                                setStreamVolume(stream, lastVolume, 0);
                            } else if (stream != 3) {
                                if (lastVolume > volumeRestoreCap) {
                                    setStreamVolume(stream, volumeRestoreCap, 0);
                                } else {
                                    setStreamVolume(stream, lastVolume, 0);
                                }
                            } else {
                                // For media volume the cap is doubled to correspond
                                // with its finer granularity
                                if (lastVolume > (volumeRestoreCap * 2)) {
                                    setStreamVolume(stream, (volumeRestoreCap * 2), 0);
                                } else {
                                    setStreamVolume(stream, lastVolume, 0);
                                }
                            }
                    }
                } else {
                    // Headset disconnected
                    for (int stream = 0; stream < STREAM_VOLUME_SPEAKER_SETTINGS.length; stream++) {
                        try {
                            lastVolume = System.getInt(mContentResolver,