From 21f2c4d59af6cd6dfc4e5ee1f2b11e058a05ebb9 Mon Sep 17 00:00:00 2001 From: Oscar Azucena Date: Tue, 26 Oct 2021 18:06:54 -0700 Subject: [PATCH] Changed user check for play sound effect API Changed the way the user is checked for audio manager play sound effect API. Instead of capturing the process user, the current user is used to determined if the audio should be played. Also moved the should sound play check to audio service, as the check requires interact across users permission, instead of adding permission to the API this can be faciliated in the audio service. Bug: 165038414 Test: atest AudioManagerTest#testSoundEffects Test: atest AudioManagerTest# testCheckingZenModeBlockDoesNotRequireNotificationPolicyAccess Test: atest VolumeDialogControllerImplTest Test: manually disable audio effect sounds settings and play audio NoNonSdkCheck: IAudioService#playSoundEffect is internal API Change-Id: Ib0e3001fd76e3a5fdd831551d16f091029d03422 --- media/java/android/media/AudioManager.java | 31 ++----------------- media/java/android/media/IAudioService.aidl | 2 +- .../volume/VolumeDialogControllerImpl.java | 3 +- .../android/server/audio/AudioService.java | 16 ++++++++-- 4 files changed, 18 insertions(+), 34 deletions(-) diff --git a/media/java/android/media/AudioManager.java b/media/java/android/media/AudioManager.java index 669bfc5a1b32d..b84a4334e0402 100644 --- a/media/java/android/media/AudioManager.java +++ b/media/java/android/media/AudioManager.java @@ -55,12 +55,10 @@ import android.os.Handler; import android.os.IBinder; import android.os.Looper; import android.os.Message; -import android.os.Process; import android.os.RemoteException; import android.os.ServiceManager; import android.os.SystemClock; import android.os.UserHandle; -import android.provider.Settings; import android.text.TextUtils; import android.util.ArrayMap; import android.util.Log; @@ -3508,20 +3506,7 @@ public class AudioManager { * whether sounds are heard or not. */ public void playSoundEffect(@SystemSoundEffect int effectType) { - if (effectType < 0 || effectType >= NUM_SOUND_EFFECTS) { - return; - } - - if (!querySoundEffectsEnabled(Process.myUserHandle().getIdentifier())) { - return; - } - - final IAudioService service = getService(); - try { - service.playSoundEffect(effectType); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + playSoundEffect(effectType, UserHandle.USER_CURRENT); } /** @@ -3537,13 +3522,9 @@ public class AudioManager { return; } - if (!querySoundEffectsEnabled(userId)) { - return; - } - final IAudioService service = getService(); try { - service.playSoundEffect(effectType); + service.playSoundEffect(effectType, userId); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } @@ -3571,14 +3552,6 @@ public class AudioManager { } } - /** - * Settings has an in memory cache, so this is fast. - */ - private boolean querySoundEffectsEnabled(int user) { - return Settings.System.getIntForUser(getContext().getContentResolver(), - Settings.System.SOUND_EFFECTS_ENABLED, 0, user) != 0; - } - /** * Load Sound effects. * This method must be called when sound effects are enabled. diff --git a/media/java/android/media/IAudioService.aidl b/media/java/android/media/IAudioService.aidl index 067f8215829c4..3921cd42ad402 100755 --- a/media/java/android/media/IAudioService.aidl +++ b/media/java/android/media/IAudioService.aidl @@ -158,7 +158,7 @@ interface IAudioService { int getMode(); - oneway void playSoundEffect(int effectType); + oneway void playSoundEffect(int effectType, int userId); oneway void playSoundEffectVolume(int effectType, float volume); diff --git a/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogControllerImpl.java b/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogControllerImpl.java index e57059894786a..5aa3617b16511 100644 --- a/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogControllerImpl.java +++ b/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogControllerImpl.java @@ -377,7 +377,8 @@ public class VolumeDialogControllerImpl implements VolumeDialogController, Dumpa private void playTouchFeedback() { if (System.currentTimeMillis() - mLastToggledRingerOn < TOUCH_FEEDBACK_TIMEOUT_MS) { try { - mAudioService.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD); + mAudioService.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, + UserHandle.USER_CURRENT); } catch (RemoteException e) { // ignore } diff --git a/services/core/java/com/android/server/audio/AudioService.java b/services/core/java/com/android/server/audio/AudioService.java index e8b0e08ed710b..266f9d35a8a28 100644 --- a/services/core/java/com/android/server/audio/AudioService.java +++ b/services/core/java/com/android/server/audio/AudioService.java @@ -5013,9 +5013,19 @@ public class AudioService extends IAudioService.Stub } } - /** @see AudioManager#playSoundEffect(int) */ - public void playSoundEffect(int effectType) { - playSoundEffectVolume(effectType, -1.0f); + /** @see AudioManager#playSoundEffect(int, int) */ + public void playSoundEffect(int effectType, int userId) { + if (querySoundEffectsEnabled(userId)) { + playSoundEffectVolume(effectType, -1.0f); + } + } + + /** + * Settings has an in memory cache, so this is fast. + */ + private boolean querySoundEffectsEnabled(int user) { + return Settings.System.getIntForUser(getContentResolver(), + Settings.System.SOUND_EFFECTS_ENABLED, 0, user) != 0; } /** @see AudioManager#playSoundEffect(int, float) */