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

Commit c1419e82 authored by maxwen's avatar maxwen Committed by Thomas Wendt
Browse files

AudioService: Restore volumes after boot and when a headset is plugged

The correct volume levels for <device>_speaker and <device>_headset are not
restored after booting and when a headset device is plugged in or out.

This fixes a glitch where the first volume change event causes the volume to
be reset to the last stored value instead of increasing/decreasting it by
one step.

Change-Id: I6a483af34fd6d9dd706c6979202bf0cf26d80854
parent ef44d827
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
@@ -529,6 +529,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
        intentFilter.addAction(Intent.ACTION_USER_BACKGROUND);
        intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
        intentFilter.addAction(Intent.ACTION_WIFI_DISPLAY_AUDIO);
        intentFilter.addAction(Intent.ACTION_HEADSET_PLUG);

        intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
        // Register a configuration change listener only if requested by system properties
@@ -3941,6 +3942,16 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                        }
                    }
                }
            } else if (action.equals(Intent.ACTION_HEADSET_PLUG)) {
                state = intent.getIntExtra("state", 0);
                if (state == 1) {
                    // Headset plugged in
                    adjustCurrentStreamVolume();
                    // TODO: Cap volume at safe levels
                } else {
                    // Headset disconnected
                    adjustCurrentStreamVolume();
                }
            } else if (action.equals(Intent.ACTION_USB_AUDIO_ACCESSORY_PLUG) ||
                           action.equals(Intent.ACTION_USB_AUDIO_DEVICE_PLUG)) {
                state = intent.getIntExtra("state", 0);
@@ -4031,6 +4042,8 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                        0,
                        null,
                        SAFE_VOLUME_CONFIGURE_TIMEOUT_MS);

                adjustCurrentStreamVolume();
            } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)) {
                if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
                    // a package is being removed, not replaced
@@ -4084,6 +4097,20 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                }
            }
        }

        private void adjustCurrentStreamVolume() {
            VolumeStreamState streamState;
            int device;

            for (int stream = 0; stream < AudioSystem.getNumStreamTypes(); stream++) {
                if (stream == mStreamVolumeAlias[stream]) {
                    streamState = mStreamStates[mStreamVolumeAlias[stream]];
                    device = getDeviceForStream(stream);
                    // apply stored value for device
                    streamState.applyDeviceVolume(device);
                }
            }
        }
    }

    private void masterVolumeChanged(final int flags) {