From a7880d4bb7784c1b72def92a6b7a7aaf489b3971 Mon Sep 17 00:00:00 2001 From: Jean-Michel Trivi Date: Sat, 15 Apr 2017 12:41:05 -0700 Subject: [PATCH] Vol key presses: active stream has priority of set control stream When deciding which volume stream is to be controlled by key presses, give priority to the stream that is actually playing, unless the user has selected a another volume to be controlled by the key presses. Test: play some music over headset, pause, go to home screen. Press vol up, it changes ringer volume, immediately press play and press vol up again, it should now change music volume (and UI updates to indicate that). Bug: 33355229 Change-Id: Ibad9b59a692e1c6375487b739c55fdbf07f107dd --- .../android/server/audio/AudioService.java | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/services/core/java/com/android/server/audio/AudioService.java b/services/core/java/com/android/server/audio/AudioService.java index 268724283351a..0dee7f7a2492f 100644 --- a/services/core/java/com/android/server/audio/AudioService.java +++ b/services/core/java/com/android/server/audio/AudioService.java @@ -520,7 +520,11 @@ public class AudioService extends IAudioService.Stub private int mPrevVolDirection = AudioManager.ADJUST_SAME; // mVolumeControlStream is set by VolumePanel to temporarily force the stream type which volume // is controlled by Vol keys. - private int mVolumeControlStream = -1; + private int mVolumeControlStream = -1; + // interpretation of whether the volume stream has been selected by the user by clicking on a + // volume slider to change which volume is controlled by the volume keys. Is false + // when mVolumeControlStream is -1. + private boolean mUserSelectedVolumeControlStream = false; private final Object mForceControlStreamLock = new Object(); // VolumePanel is currently the only client of forceVolumeControlStream() and runs in system // server process so in theory it is not necessary to monitor the client death. @@ -1226,14 +1230,29 @@ public class AudioService extends IAudioService.Stub private void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags, String callingPackage, String caller, int uid) { if (DEBUG_VOL) Log.d(TAG, "adjustSuggestedStreamVolume() stream=" + suggestedStreamType - + ", flags=" + flags + ", caller=" + caller); - int streamType; - boolean isMute = isMuteAdjust(direction); - if (mVolumeControlStream != -1) { + + ", flags=" + flags + ", caller=" + caller + + ", volControlStream=" + mVolumeControlStream + + ", userSelect=" + mUserSelectedVolumeControlStream); + final int streamType; + if (mUserSelectedVolumeControlStream) { // implies mVolumeControlStream != -1 streamType = mVolumeControlStream; } else { - streamType = getActiveStreamType(suggestedStreamType); + final int maybeActiveStreamType = getActiveStreamType(suggestedStreamType); + final boolean activeForReal; + if (maybeActiveStreamType == AudioSystem.STREAM_MUSIC) { + activeForReal = isAfMusicActiveRecently(0); + } else { + activeForReal = AudioSystem.isStreamActive(maybeActiveStreamType, 0); + } + if (activeForReal || mVolumeControlStream == -1) { + streamType = maybeActiveStreamType; + } else { + streamType = mVolumeControlStream; + } } + + final boolean isMute = isMuteAdjust(direction); + ensureValidStreamType(streamType); final int resolvedStream = mStreamVolumeAlias[streamType]; @@ -1709,13 +1728,18 @@ public class AudioService extends IAudioService.Stub /** @see AudioManager#forceVolumeControlStream(int) */ public void forceVolumeControlStream(int streamType, IBinder cb) { + if (DEBUG_VOL) { Log.d(TAG, String.format("forceVolumeControlStream(%d)", streamType)); } synchronized(mForceControlStreamLock) { + if (mVolumeControlStream != -1 && streamType != -1) { + mUserSelectedVolumeControlStream = true; + } mVolumeControlStream = streamType; if (mVolumeControlStream == -1) { if (mForceControlStreamClient != null) { mForceControlStreamClient.release(); mForceControlStreamClient = null; } + mUserSelectedVolumeControlStream = false; } else { mForceControlStreamClient = new ForceControlStreamClient(cb); } @@ -1746,6 +1770,7 @@ public class AudioService extends IAudioService.Stub } else { mForceControlStreamClient = null; mVolumeControlStream = -1; + mUserSelectedVolumeControlStream = false; } } }