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

Commit 29ee1883 authored by Ricardo Cerqueira's avatar Ricardo Cerqueira
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

Conflicts:
	media/java/android/media/AudioService.java
parent 476fbee8
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
@@ -543,6 +543,7 @@ public class AudioService extends IAudioService.Stub {
        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        intentFilter.addAction(Intent.ACTION_USER_BACKGROUND);
        intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
        intentFilter.addAction(Intent.ACTION_HEADSET_PLUG);

        intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
        // TODO merge orientation and rotation
@@ -4107,6 +4108,16 @@ public class AudioService extends IAudioService.Stub {
                        }
                    }
                }
            } 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);
@@ -4197,6 +4208,7 @@ public class AudioService extends IAudioService.Stub {
                        0,
                        null,
                        SAFE_VOLUME_CONFIGURE_TIMEOUT_MS);
                adjustCurrentStreamVolume();
            } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
                AudioSystem.setParameters("screen_state=on");
            } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
@@ -4227,6 +4239,20 @@ public class AudioService extends IAudioService.Stub {
                        mStreamStates[AudioSystem.STREAM_MUSIC], 0);
            }
        }

        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 showVolumeChangeUi(final int streamType, final int flags) {