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

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

AudioService: Reduce volume to safe level when headset is connected (2/2)

This is a safety feature for users who opt to raise volume above the designated
safe level while using a headset.

Currently when a user does this they are provided with no protection against
dangerously high volume levels until the next time the headset volume warning
is displayed.

This patch adds an optional feature to ensure volume is no higher than the safe
level every time a headset is connected.

Change-Id: Idaa9cc9549bc3c6278e1e44490433c5366c29c16
parent a8c90613
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -2053,11 +2053,20 @@ 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.
         * Whether to display a warning dialog when the user attempts to increase media
         * volume above a safe limit while a headset is connected. This feature is enabled
         * by default to comply with safety regulations and the user must agree to a waiver
         * if they wish to disable it.
         * @hide
         */
        public static final String SAFE_HEADSET_VOLUME = "safe_headset_volume";

        /**
         * Whether to reduce media volume to a safe limit each time a headset is plugged in.
         * @hide
         */
        public static final String SAFE_HEADSET_VOLUME_RESTORE = "safe_headset_volume_restore";

        /**
         * Master volume (float in the range 0.0f to 1.0f).
         * @hide
+16 −8
Original line number Diff line number Diff line
@@ -2904,6 +2904,15 @@ public class AudioService extends IAudioService.Stub {

    public void setWiredDeviceConnectionState(int device, int state, String name) {
        synchronized (mConnectedDevices) {
            if ((state == 1) && ((device & mSafeMediaVolumeDevices) != 0)) {
                boolean restoreSafeVolume = Settings.System.getIntForUser(
                        mContentResolver,
                        Settings.System.SAFE_HEADSET_VOLUME_RESTORE,
                        0, UserHandle.USER_CURRENT) != 0;
                if (restoreSafeVolume) {
                    enforceSafeMediaVolume();
                }
            }
            int delay = checkSendBecomingNoisyIntent(device, state);
            queueMsgUnderWakeLock(mAudioHandler,
                    MSG_SET_WIRED_DEVICE_CONNECTION_STATE,
@@ -4254,12 +4263,11 @@ public class AudioService extends IAudioService.Stub {
                    }
                }
            } else if (action.equals(Intent.ACTION_HEADSET_PLUG)) {
                applyCurrentStreamVolume();

                state = intent.getIntExtra("state", 0);
                if (state == 1) {
                    // Headset plugged in
                    adjustCurrentStreamVolume();
                    // TODO: Cap volume at safe levels

                    boolean launchPlayer = Settings.System.getIntForUser(
                            context.getContentResolver(),
                            Settings.System.HEADSET_CONNECT_PLAYER,
@@ -4274,9 +4282,6 @@ public class AudioService extends IAudioService.Stub {
                            Log.w(TAG, "No music player found to start after headset connection");
                        }
                    }
                } else {
                    // Headset disconnected
                    adjustCurrentStreamVolume();
                }
            } else if (action.equals(Intent.ACTION_USB_AUDIO_ACCESSORY_PLUG) ||
                           action.equals(Intent.ACTION_USB_AUDIO_DEVICE_PLUG)) {
@@ -4368,7 +4373,7 @@ public class AudioService extends IAudioService.Stub {
                        0,
                        null,
                        SAFE_VOLUME_CONFIGURE_TIMEOUT_MS);
                adjustCurrentStreamVolume();
                applyCurrentStreamVolume();
            } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
                AudioSystem.setParameters("screen_state=on");
            } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
@@ -4400,7 +4405,10 @@ public class AudioService extends IAudioService.Stub {
            }
        }

        private void adjustCurrentStreamVolume() {
        // Forces volume to update. This is needed by devices affected by a bug
        // where the new volume is not applied when the audio device changes.
        // See: review.cyanogenmod.org/33778
        private void applyCurrentStreamVolume() {
            VolumeStreamState streamState;
            int device;