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

Commit 0383c21d authored by DvTonder's avatar DvTonder
Browse files

Framework: Audio service - add missing pieces from CM9 port

Author: Phil Tunstall
Fix ring/notification volume not restoring correctly on headset (dis)connect

We weren't checking the alias of each stream when restoring, which meant that
for the notification stream volume:
1. The wrong volume level would be stored.
2. The volume level to restore would be retrieved from the wrong place, and then
overwrite the correctly restored ring stream volume.

As well as fixing that I have improved the readability of the code a bit.

 + Change-Id: I95234c2b7ad7dfd4dbd455a56c8a69975c514b50

Author:  Phil Tunstall
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

Author: dalingrin
Add separate headset and speaker volumes
This is a simple implementation of keeping separate volume
settings for the wired headset and the speaker without
overhauling the audio system.

This allows a person with sensitive headphones to keep music, ringer
and other stream volumes lower than they would usually keep the
speaker volumes.

 + Change-Id: Ia614c1e3ac548ff4e0104a090cda13b9daef09cb

Author: gsarrica
Allow unlinking of notification sound and ringtone volume.
Thanks to http://forum.aokp.co/ for this.

 + Change-Id: I55db003cc1691fbca5d3e8ea79e088d0622d38ee

Settings side has been merged

Change-Id: I19deecf2346bcf0691dc3a1480791df7b00afb06
parent aadd9c84
Loading
Loading
Loading
Loading
+117 −2
Original line number Diff line number Diff line
@@ -67,6 +67,7 @@ import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.VolumePanel;
import android.provider.Settings.SettingNotFoundException;

import com.android.internal.telephony.ITelephony;

@@ -270,6 +271,29 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
            "STREAM_TTS"
    };

    // Add separate headset and speaker volumes
    private static final String[] STREAM_VOLUME_HEADSET_SETTINGS = new String[] {
        "AudioService.SAVED_VOICE_CALL_HEADSET_VOL",
        "AudioService.SAVED_SYSTEM_HEADSET_VOL",
        "AudioService.SAVED_RING_HEADSET_VOL",
        "AudioService.SAVED_MUSIC_HEADSET_VOL",
        "AudioService.SAVED_ALARM_HEADSET_VOL",
        "AudioService.SAVED_NOTIFICATION_HEADSET_VOL",
    };

    private static final String[] STREAM_VOLUME_SPEAKER_SETTINGS = new String[] {
        "AudioService.SAVED_VOICE_CALL_SPEAKER_VOL",
        "AudioService.SAVED_SYSTEM_SPEAKER_VOL",
        "AudioService.SAVED_RING_SPEAKER_VOL",
        "AudioService.SAVED_MUSIC_SPEAKER_VOL",
        "AudioService.SAVED_ALARM_SPEAKER_VOL",
        "AudioService.SAVED_NOTIFICATION_SPEAKER_VOL",
    };

    private static final int HEADSET_VOLUME_RESTORE_CAP_VOICE_CALL = 3; // Out of 5
    private static final int HEADSET_VOLUME_RESTORE_CAP_MUSIC = 8; // Out of 15
    private static final int HEADSET_VOLUME_RESTORE_CAP_OTHER = 4; // Out of 7

     private final AudioSystem.ErrorCallback mAudioSystemCallback = new AudioSystem.ErrorCallback() {
        public void onError(int error) {
            switch (error) {
@@ -402,7 +426,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished {

    private int mDeviceOrientation = Configuration.ORIENTATION_UNDEFINED;

    // Request to override default use of A2DP for media.
    // Request to override default use of A2DP for media
    private boolean mBluetoothA2dpEnabled;
    private final Object mBluetoothA2dpEnabledLock = new Object();

@@ -469,6 +493,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
        intentFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        intentFilter.addAction(Intent.ACTION_HEADSET_PLUG);

        // Register a configuration change listener only if requested by system properties
        // to monitor orientation changes (off by default)
@@ -636,6 +661,18 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
        } else {
            mRingerModeAffectedStreams |= (1 << AudioSystem.STREAM_MUSIC);
        }

        boolean linkNotificationWithVolume = Settings.System.getInt(mContentResolver,
                Settings.System.VOLUME_LINK_NOTIFICATION, 1) == 1;
        if (linkNotificationWithVolume) {
            STREAM_VOLUME_ALIAS[AudioSystem.STREAM_NOTIFICATION] = AudioSystem.STREAM_RING;
            STREAM_VOLUME_ALIAS_NON_VOICE[AudioSystem.STREAM_NOTIFICATION] = AudioSystem.STREAM_RING;
        } else {
            STREAM_VOLUME_ALIAS[AudioSystem.STREAM_NOTIFICATION] = AudioSystem.STREAM_NOTIFICATION;
            STREAM_VOLUME_ALIAS_NON_VOICE[AudioSystem.STREAM_NOTIFICATION] = AudioSystem.STREAM_NOTIFICATION;
        }
        updateStreamVolumeAlias(false);

        Settings.System.putInt(cr,
                Settings.System.MODE_RINGER_STREAMS_AFFECTED, mRingerModeAffectedStreams);

@@ -3121,6 +3158,8 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
            super(new Handler());
            mContentResolver.registerContentObserver(Settings.System.getUriFor(
                Settings.System.MODE_RINGER_STREAMS_AFFECTED), false, this);
            mContentResolver.registerContentObserver(Settings.System.getUriFor(
                Settings.System.VOLUME_LINK_NOTIFICATION), false, this);
        }

        @Override
@@ -3148,6 +3187,18 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                    mRingerModeAffectedStreams = ringerModeAffectedStreams;
                    setRingerModeInt(getRingerMode(), false);
                }

                boolean linkNotificationWithVolume = Settings.System.getInt(mContentResolver,
                        Settings.System.VOLUME_LINK_NOTIFICATION, 1) == 1;
                if (linkNotificationWithVolume) {
                    STREAM_VOLUME_ALIAS[AudioSystem.STREAM_NOTIFICATION] = AudioSystem.STREAM_RING;
                    STREAM_VOLUME_ALIAS_NON_VOICE[AudioSystem.STREAM_NOTIFICATION] = AudioSystem.STREAM_RING;

                } else {
                    STREAM_VOLUME_ALIAS[AudioSystem.STREAM_NOTIFICATION] = AudioSystem.STREAM_NOTIFICATION;
                    STREAM_VOLUME_ALIAS_NON_VOICE[AudioSystem.STREAM_NOTIFICATION] = AudioSystem.STREAM_NOTIFICATION;
                }
                updateStreamVolumeAlias(false);
            }
        }
    }
@@ -3450,6 +3501,70 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                        }
                    }
                }
            } else if (action.equals(Intent.ACTION_HEADSET_PLUG)) {
                //Save and restore volumes for headset and speaker
                state = intent.getIntExtra("state", 0);
                int lastVolume;
                if (state == 1) {
                    // Headset plugged in
                    final boolean capVolumeRestore = Settings.System.getInt(mContentResolver,
                            Settings.System.SAFE_HEADSET_VOLUME_RESTORE, 1) == 1;
                    for (int stream = 0; stream < STREAM_VOLUME_HEADSET_SETTINGS.length; stream++) {
                        final int streamAlias = mStreamVolumeAlias[stream];
                        // Save speaker volume
                        System.putInt(mContentResolver, STREAM_VOLUME_SPEAKER_SETTINGS[stream],
                                getStreamVolume(streamAlias));
                        // Restore headset volume
                        try {
                            lastVolume = System.getInt(mContentResolver,
                                    STREAM_VOLUME_HEADSET_SETTINGS[streamAlias]);
                        } catch (SettingNotFoundException e) {
                            lastVolume = -1;
                        }
                        if (lastVolume >= 0) {
                            if (capVolumeRestore) {
                                final int volumeCap;
                                switch (streamAlias) {
                                    case AudioSystem.STREAM_VOICE_CALL:
                                        volumeCap = HEADSET_VOLUME_RESTORE_CAP_VOICE_CALL;
                                        break;
                                    case AudioSystem.STREAM_MUSIC:
                                        volumeCap = HEADSET_VOLUME_RESTORE_CAP_MUSIC;
                                        break;
                                    case AudioSystem.STREAM_SYSTEM:
                                    case AudioSystem.STREAM_RING:
                                    case AudioSystem.STREAM_ALARM:
                                    case AudioSystem.STREAM_NOTIFICATION:
                                    default:
                                        volumeCap = HEADSET_VOLUME_RESTORE_CAP_OTHER;
                                        break;
                                }
                                setStreamVolume(streamAlias, Math.min(volumeCap, lastVolume), 0);
                            } else {
                                setStreamVolume(streamAlias, lastVolume, 0);
                            }
                        }
                    }
                } else {
                    // Headset disconnected
                    for (int stream = 0; stream < STREAM_VOLUME_SPEAKER_SETTINGS.length; stream++) {
                        final int streamAlias = mStreamVolumeAlias[stream];
                        // Save headset volume
                        System.putInt(mContentResolver, STREAM_VOLUME_HEADSET_SETTINGS[stream],
                                getStreamVolume(streamAlias));
                        // Restore speaker volume
                        try {
                            lastVolume = System.getInt(mContentResolver,
                                    STREAM_VOLUME_SPEAKER_SETTINGS[streamAlias]);
                        } catch (SettingNotFoundException e) {
                            lastVolume = -1;
                        }
                        System.putInt(mContentResolver, STREAM_VOLUME_HEADSET_SETTINGS[stream],
                                getStreamVolume(stream));
                        if (lastVolume >= 0)
                            setStreamVolume(streamAlias, lastVolume, 0);
                    }
                }
            } else if (action.equals(Intent.ACTION_USB_AUDIO_ACCESSORY_PLUG) ||
                           action.equals(Intent.ACTION_USB_AUDIO_DEVICE_PLUG)) {
                state = intent.getIntExtra("state", 0);