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

Commit 15687091 authored by Phil Tunstall's avatar Phil Tunstall
Browse files

Audio: Restore volume for all streams on headset connect/disconnect

Audio streams which play sound from both speaker and a connected device would
report to AudioService only the speaker as the playback device, meaning volumes
would not be saved for a separate device state. This patch makes it so the
connected device will be reported instead, fixing volume restore on device
change for those streams (alarm, ring, notifications).

Readded safe headset restore capping for all streams.

Patch Set 3: Changed cap to 10/15

Change-Id: Id2e690f7894475ba0086f62342b8eeb163d24da1
parent 834f7050
Loading
Loading
Loading
Loading
+15 −8
Original line number Diff line number Diff line
@@ -279,7 +279,9 @@ public class AudioService extends IAudioService.Stub implements OnFinished {

    private boolean mLinkNotificationWithVolume;

    private static final int HEADSET_VOLUME_RESTORE_CAP_MUSIC = 8; // Out of 15
    // Cap used for safe headset volume restore. The value directly applies
    // to AudioSystem.STREAM_MUSIC volume and is rescaled for other streams.
    private static final int HEADSET_VOLUME_RESTORE_CAP = 10;

    private final AudioSystem.ErrorCallback mAudioSystemCallback = new AudioSystem.ErrorCallback() {
        public void onError(int error) {
@@ -2392,12 +2394,12 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
        int device = AudioSystem.getDevicesForStream(stream);
        if ((device & (device - 1)) != 0) {
            // Multiple device selection is either:
            //  - speaker + one other device: give priority to speaker in this case.
            //  - speaker + one other device: give priority to the non-speaker device in this case.
            //  - one A2DP device + another device: happens with duplicated output. In this case
            // retain the device on the A2DP output as the other must not correspond to an active
            // selection if not the speaker.
            if ((device & AudioSystem.DEVICE_OUT_SPEAKER) != 0) {
                device = AudioSystem.DEVICE_OUT_SPEAKER;
                device ^= AudioSystem.DEVICE_OUT_SPEAKER;
            } else {
                device &= AudioSystem.DEVICE_OUT_ALL_A2DP;
            }
@@ -3543,14 +3545,19 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                        setBluetoothA2dpOnInt(false);
                    }

                    // Media volume restore capping
                    // Volume restore capping
                    final boolean capVolumeRestore = Settings.System.getInt(mContentResolver,
                            Settings.System.SAFE_HEADSET_VOLUME_RESTORE, 1) == 1;
                    if (capVolumeRestore) {
                        final int volume = getStreamVolume(AudioSystem.STREAM_MUSIC);
                        if (volume > HEADSET_VOLUME_RESTORE_CAP_MUSIC) {
                            setStreamVolume(AudioSystem.STREAM_MUSIC,
                                    HEADSET_VOLUME_RESTORE_CAP_MUSIC, 0);
                        for (int stream = 0; stream < AudioSystem.getNumStreamTypes(); stream++) {
                            if (stream == mStreamVolumeAlias[stream]) {
                                final int volume = getStreamVolume(stream);
                                final int restoreCap = rescaleIndex(HEADSET_VOLUME_RESTORE_CAP,
                                        AudioSystem.STREAM_MUSIC, stream);
                                if (volume > restoreCap) {
                                    setStreamVolume(stream, restoreCap, 0);
                                }
                            }
                        }
                    }
                } else {