From 49433621b038a3b021db3a0cef9c130d62b48f0d Mon Sep 17 00:00:00 2001 From: Phil Tunstall Date: Sun, 22 Jul 2012 12:26:54 +0100 Subject: 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 --- media/java/android/media/AudioService.java | 70 +++++++++++++++++------------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/media/java/android/media/AudioService.java b/media/java/android/media/AudioService.java index b557dd9..8cfed1b 100644 --- a/media/java/android/media/AudioService.java +++ b/media/java/android/media/AudioService.java @@ -226,6 +226,10 @@ public class AudioService extends IAudioService.Stub { "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 AudioSystem.ErrorCallback mAudioSystemCallback = new AudioSystem.ErrorCallback() { public void onError(int error) { switch (error) { @@ -2573,56 +2577,60 @@ public class AudioService extends IAudioService.Stub { int lastVolume; if (state == 1) { // Headset plugged in - int volumeRestoreCap; - if (Settings.System.getInt(mContentResolver, - Settings.System.SAFE_HEADSET_VOLUME_RESTORE, 1) == 0) { - // Don't cap - volumeRestoreCap = 8; - } else { - volumeRestoreCap = 4; - } + 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 = STREAM_VOLUME_ALIAS[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[stream]); + STREAM_VOLUME_HEADSET_SETTINGS[streamAlias]); } catch (SettingNotFoundException e) { lastVolume = -1; } - System.putInt(mContentResolver, STREAM_VOLUME_SPEAKER_SETTINGS[stream], - getStreamVolume(stream)); - if (lastVolume >= 0) - if (stream == 0) { - // Don't touch voice call volume - setStreamVolume(stream, lastVolume, 0); - } else if (stream != 3) { - if (lastVolume > volumeRestoreCap) { - setStreamVolume(stream, volumeRestoreCap, 0); - } else { - setStreamVolume(stream, lastVolume, 0); + 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 { - // For media volume the cap is doubled to correspond - // with its finer granularity - if (lastVolume > (volumeRestoreCap * 2)) { - setStreamVolume(stream, (volumeRestoreCap * 2), 0); - } else { - setStreamVolume(stream, lastVolume, 0); - } + setStreamVolume(streamAlias, lastVolume, 0); } + } } } else { // Headset disconnected for (int stream = 0; stream < STREAM_VOLUME_SPEAKER_SETTINGS.length; stream++) { + final int streamAlias = STREAM_VOLUME_ALIAS[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[stream]); + STREAM_VOLUME_SPEAKER_SETTINGS[streamAlias]); } catch (SettingNotFoundException e) { lastVolume = -1; } - System.putInt(mContentResolver, STREAM_VOLUME_HEADSET_SETTINGS[stream], - getStreamVolume(stream)); if (lastVolume >= 0) - setStreamVolume(stream, lastVolume, 0); + setStreamVolume(streamAlias, lastVolume, 0); } } -- cgit v1.1