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

Commit c8177eb3 authored by Dalingrin's avatar Dalingrin
Browse files

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
parent 901b54ff
Loading
Loading
Loading
Loading
+49 −0
Original line number Diff line number Diff line
@@ -51,6 +51,7 @@ import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.SystemProperties;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.provider.Settings.System;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
@@ -204,6 +205,24 @@ public class AudioService extends IAudioService.Stub {
        AudioSystem.STREAM_MUSIC  // STREAM_TTS
    };

    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 AudioSystem.ErrorCallback mAudioSystemCallback = new AudioSystem.ErrorCallback() {
        public void onError(int error) {
            switch (error) {
@@ -2541,6 +2560,36 @@ public class AudioService extends IAudioService.Stub {
                int state = intent.getIntExtra("state", 0);
                int microphone = intent.getIntExtra("microphone", 0);

                //Save and restore volumes for headset and speaker
                int lastVolume;
                if (state == 1) {
                    for (int stream = 0; stream < STREAM_VOLUME_HEADSET_SETTINGS.length; stream++) {
                        try {
                            lastVolume = System.getInt(mContentResolver,
                                    STREAM_VOLUME_HEADSET_SETTINGS[stream]);
                        } catch (SettingNotFoundException e) {
                            lastVolume = -1;
                        }
                        System.putInt(mContentResolver, STREAM_VOLUME_SPEAKER_SETTINGS[stream],
                                getStreamVolume(stream));
                        if (lastVolume >= 0)
                            setStreamVolume(stream, lastVolume, 0);
                    }
                } else {
                    for (int stream = 0; stream < STREAM_VOLUME_SPEAKER_SETTINGS.length; stream++) {
                        try {
                            lastVolume = System.getInt(mContentResolver,
                                    STREAM_VOLUME_SPEAKER_SETTINGS[stream]);
                        } catch (SettingNotFoundException e) {
                            lastVolume = -1;
                        }
                        System.putInt(mContentResolver, STREAM_VOLUME_HEADSET_SETTINGS[stream],
                                getStreamVolume(stream));
                        if (lastVolume >= 0)
                            setStreamVolume(stream, lastVolume, 0);
                    }
                }

                synchronized (mConnectedDevices) {
                    if (microphone != 0) {
                        boolean isConnected =