diff options
author | 2023-07-18 15:58:14 +0200 | |
---|---|---|
committer | 2023-10-11 11:27:21 +0200 | |
commit | f178449bbca13b430d9b4a4c0b3864773c310fac (patch) | |
tree | b5406194267c20fa96e3b9575dfffa97d9656eb3 | |
parent | 84282d807f5a657aadbbfb096b6206d1bf172ecd (diff) |
Add volume to IRingtonePlayer.playAsync
Add volume param to IRingtonePlayer so that notification
alerts can have their volume adjusted.
Test: atest FrameworksUiServicesTests
Bug: 270456865
Change-Id: Iec72c74642ade5a4b8d110abf21f6e713814e7f1
7 files changed, 35 insertions, 18 deletions
diff --git a/media/java/android/media/IRingtonePlayer.aidl b/media/java/android/media/IRingtonePlayer.aidl index 73f15f21596c..1e57be2c1e22 100644 --- a/media/java/android/media/IRingtonePlayer.aidl +++ b/media/java/android/media/IRingtonePlayer.aidl @@ -49,7 +49,7 @@ interface IRingtonePlayer { oneway void setHapticGeneratorEnabled(IBinder token, boolean hapticGeneratorEnabled); /** Used for Notification sound playback. */ - oneway void playAsync(in Uri uri, in UserHandle user, boolean looping, in AudioAttributes aa); + oneway void playAsync(in Uri uri, in UserHandle user, boolean looping, in AudioAttributes aa, float volume); oneway void stopAsync(); /** Return the title of the media. */ diff --git a/packages/SystemUI/src/com/android/systemui/media/NotificationPlayer.java b/packages/SystemUI/src/com/android/systemui/media/NotificationPlayer.java index 3d4fca1b8945..1b3b47350197 100644 --- a/packages/SystemUI/src/com/android/systemui/media/NotificationPlayer.java +++ b/packages/SystemUI/src/com/android/systemui/media/NotificationPlayer.java @@ -51,11 +51,12 @@ public class NotificationPlayer implements OnCompletionListener, OnErrorListener Uri uri; boolean looping; AudioAttributes attributes; + float volume; long requestTime; public String toString() { return "{ code=" + code + " looping=" + looping + " attributes=" + attributes - + " uri=" + uri + " }"; + + " volume=" + volume + " uri=" + uri + " }"; } } @@ -101,6 +102,7 @@ public class NotificationPlayer implements OnCompletionListener, OnErrorListener player.setAudioAttributes(mCmd.attributes); player.setDataSource(mCmd.context, mCmd.uri); player.setLooping(mCmd.looping); + player.setVolume(mCmd.volume); player.setOnCompletionListener(NotificationPlayer.this); player.setOnErrorListener(NotificationPlayer.this); player.prepare(); @@ -401,10 +403,11 @@ public class NotificationPlayer implements OnCompletionListener, OnErrorListener * (see {@link MediaPlayer#setLooping(boolean)}) * @param stream the AudioStream to use. * (see {@link MediaPlayer#setAudioStreamType(int)}) + * @param volume the volume for the audio with values in range [0.0, 1.0] * @deprecated use {@link #play(Context, Uri, boolean, AudioAttributes)} instead. */ @Deprecated - public void play(Context context, Uri uri, boolean looping, int stream) { + public void play(Context context, Uri uri, boolean looping, int stream, float volume) { if (DEBUG) { Log.d(mTag, "play uri=" + uri.toString()); } PlayerBase.deprecateStreamTypeForPlayback(stream, "NotificationPlayer", "play"); Command cmd = new Command(); @@ -414,6 +417,7 @@ public class NotificationPlayer implements OnCompletionListener, OnErrorListener cmd.uri = uri; cmd.looping = looping; cmd.attributes = new AudioAttributes.Builder().setInternalLegacyStreamType(stream).build(); + cmd.volume = volume; synchronized (mCmdQueue) { enqueueLocked(cmd); mState = PLAY; @@ -432,8 +436,10 @@ public class NotificationPlayer implements OnCompletionListener, OnErrorListener * (see {@link MediaPlayer#setLooping(boolean)}) * @param attributes the AudioAttributes to use. * (see {@link MediaPlayer#setAudioAttributes(AudioAttributes)}) + * @param volume the volume for the audio with values in range [0.0, 1.0] */ - public void play(Context context, Uri uri, boolean looping, AudioAttributes attributes) { + public void play(Context context, Uri uri, boolean looping, AudioAttributes attributes, + float volume) { if (DEBUG) { Log.d(mTag, "play uri=" + uri.toString()); } Command cmd = new Command(); cmd.requestTime = SystemClock.uptimeMillis(); @@ -442,6 +448,7 @@ public class NotificationPlayer implements OnCompletionListener, OnErrorListener cmd.uri = uri; cmd.looping = looping; cmd.attributes = attributes; + cmd.volume = volume; synchronized (mCmdQueue) { enqueueLocked(cmd); mState = PLAY; diff --git a/packages/SystemUI/src/com/android/systemui/media/RingtonePlayer.java b/packages/SystemUI/src/com/android/systemui/media/RingtonePlayer.java index 80be76661098..7a488365c740 100644 --- a/packages/SystemUI/src/com/android/systemui/media/RingtonePlayer.java +++ b/packages/SystemUI/src/com/android/systemui/media/RingtonePlayer.java @@ -285,7 +285,8 @@ public class RingtonePlayer implements CoreStartable { } @Override - public void playAsync(Uri uri, UserHandle user, boolean looping, AudioAttributes aa) { + public void playAsync(Uri uri, UserHandle user, boolean looping, AudioAttributes aa, + float volume) { if (LOGD) Log.d(TAG, "playAsync(uri=" + uri + ", user=" + user + ")"); if (Binder.getCallingUid() != Process.SYSTEM_UID) { throw new SecurityException("Async playback only available from system UID."); @@ -293,7 +294,7 @@ public class RingtonePlayer implements CoreStartable { if (UserHandle.ALL.equals(user)) { user = UserHandle.SYSTEM; } - mAsyncPlayer.play(getContextForUser(user), uri, looping, aa); + mAsyncPlayer.play(getContextForUser(user), uri, looping, aa, volume); } @Override diff --git a/services/core/java/com/android/server/notification/NotificationAttentionHelper.java b/services/core/java/com/android/server/notification/NotificationAttentionHelper.java index 75a0cf521a1d..219b660eec7d 100644 --- a/services/core/java/com/android/server/notification/NotificationAttentionHelper.java +++ b/services/core/java/com/android/server/notification/NotificationAttentionHelper.java @@ -468,7 +468,7 @@ public final class NotificationAttentionHelper { + record.getAudioAttributes()); } player.playAsync(soundUri, record.getSbn().getUser(), looping, - record.getAudioAttributes()); + record.getAudioAttributes(), 1.0f); return true; } } catch (RemoteException e) { diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index 87c30674bfb7..520e1ef3c1ec 100755 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -8757,7 +8757,7 @@ public class NotificationManagerService extends SystemService { if (DBG) Slog.v(TAG, "Playing sound " + soundUri + " with attributes " + record.getAudioAttributes()); player.playAsync(soundUri, record.getSbn().getUser(), looping, - record.getAudioAttributes()); + record.getAudioAttributes(), 1.0f); return true; } } catch (RemoteException e) { diff --git a/services/tests/uiservicestests/src/com/android/server/notification/BuzzBeepBlinkTest.java b/services/tests/uiservicestests/src/com/android/server/notification/BuzzBeepBlinkTest.java index 974238427587..42ad73a23f0e 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/BuzzBeepBlinkTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/BuzzBeepBlinkTest.java @@ -32,6 +32,7 @@ import static junit.framework.Assert.assertTrue; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import static org.mockito.ArgumentMatchers.anyFloat; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyBoolean; import static org.mockito.Matchers.anyInt; @@ -147,6 +148,7 @@ public class BuzzBeepBlinkTest extends UiServiceTestCase { private static final int CUSTOM_LIGHT_ON = 10000; private static final int CUSTOM_LIGHT_OFF = 10000; private static final int MAX_VIBRATION_DELAY = 1000; + private static final float DEFAULT_VOLUME = 1.0f; @Before public void setUp() throws Exception { @@ -397,19 +399,22 @@ public class BuzzBeepBlinkTest extends UiServiceTestCase { // private void verifyNeverBeep() throws RemoteException { - verify(mRingtonePlayer, never()).playAsync(any(), any(), anyBoolean(), any()); + verify(mRingtonePlayer, never()).playAsync(any(), any(), anyBoolean(), any(), anyFloat()); } private void verifyBeepUnlooped() throws RemoteException { - verify(mRingtonePlayer, times(1)).playAsync(any(), any(), eq(false), any()); + verify(mRingtonePlayer, times(1)).playAsync(any(), any(), eq(false), any(), + eq(DEFAULT_VOLUME)); } private void verifyBeepLooped() throws RemoteException { - verify(mRingtonePlayer, times(1)).playAsync(any(), any(), eq(true), any()); + verify(mRingtonePlayer, times(1)).playAsync(any(), any(), eq(true), any(), + eq(DEFAULT_VOLUME)); } private void verifyBeep(int times) throws RemoteException { - verify(mRingtonePlayer, times(times)).playAsync(any(), any(), anyBoolean(), any()); + verify(mRingtonePlayer, times(times)).playAsync(any(), any(), anyBoolean(), any(), + eq(DEFAULT_VOLUME)); } private void verifyNeverStopAudio() throws RemoteException { @@ -905,7 +910,7 @@ public class BuzzBeepBlinkTest extends UiServiceTestCase { verifyDelayedVibrate( mService.getVibratorHelper().createFallbackVibration(/* insistent= */ false)); verify(mRingtonePlayer, never()).playAsync - (anyObject(), anyObject(), anyBoolean(), anyObject()); + (anyObject(), anyObject(), anyBoolean(), anyObject(), anyFloat()); assertTrue(r.isInterruptive()); assertNotEquals(-1, r.getLastAudiblyAlertedMs()); } diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationAttentionHelperTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationAttentionHelperTest.java index 81867df74abd..98bb01b50319 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationAttentionHelperTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationAttentionHelperTest.java @@ -151,6 +151,7 @@ public class NotificationAttentionHelperTest extends UiServiceTestCase { private static final int CUSTOM_LIGHT_ON = 10000; private static final int CUSTOM_LIGHT_OFF = 10000; private static final int MAX_VIBRATION_DELAY = 1000; + private static final float DEFAULT_VOLUME = 1.0f; @Before public void setUp() throws Exception { @@ -410,19 +411,22 @@ public class NotificationAttentionHelperTest extends UiServiceTestCase { // private void verifyNeverBeep() throws RemoteException { - verify(mRingtonePlayer, never()).playAsync(any(), any(), anyBoolean(), any()); + verify(mRingtonePlayer, never()).playAsync(any(), any(), anyBoolean(), any(), anyFloat()); } private void verifyBeepUnlooped() throws RemoteException { - verify(mRingtonePlayer, times(1)).playAsync(any(), any(), eq(false), any()); + verify(mRingtonePlayer, times(1)).playAsync(any(), any(), eq(false), any(), + eq(DEFAULT_VOLUME)); } private void verifyBeepLooped() throws RemoteException { - verify(mRingtonePlayer, times(1)).playAsync(any(), any(), eq(true), any()); + verify(mRingtonePlayer, times(1)).playAsync(any(), any(), eq(true), any(), + eq(DEFAULT_VOLUME)); } private void verifyBeep(int times) throws RemoteException { - verify(mRingtonePlayer, times(times)).playAsync(any(), any(), anyBoolean(), any()); + verify(mRingtonePlayer, times(times)).playAsync(any(), any(), anyBoolean(), any(), + eq(DEFAULT_VOLUME)); } private void verifyNeverStopAudio() throws RemoteException { @@ -921,7 +925,7 @@ public class NotificationAttentionHelperTest extends UiServiceTestCase { mAttentionHelper.getVibratorHelper().createFallbackVibration( /* insistent= */ false)); verify(mRingtonePlayer, never()).playAsync(anyObject(), anyObject(), anyBoolean(), - anyObject()); + anyObject(), anyFloat()); assertTrue(r.isInterruptive()); assertNotEquals(-1, r.getLastAudiblyAlertedMs()); } |