From a5dd120dfaf559b716a7681825c3e131228e518e Mon Sep 17 00:00:00 2001 From: Lais Andrade Date: Fri, 1 Apr 2022 18:09:41 +0100 Subject: [PATCH] Fix NPE on VibratorManagerService The service fixes the VibrationAttributes extracted from the external vibration AudioAttributes to apply usage and flags, and thus need to check for permissions before applying them. The check had a NPE error for unknown external vibrations. Fix: 227677233 Test: VibratorManagerServiceTest Change-Id: I864a0538c90d65b96ec319ce75cb64f962138bb9 --- .../vibrator/VibratorManagerService.java | 5 +++-- .../vibrator/VibratorManagerServiceTest.java | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/services/core/java/com/android/server/vibrator/VibratorManagerService.java b/services/core/java/com/android/server/vibrator/VibratorManagerService.java index 3ffca96600985..bf3298558cd64 100644 --- a/services/core/java/com/android/server/vibrator/VibratorManagerService.java +++ b/services/core/java/com/android/server/vibrator/VibratorManagerService.java @@ -989,12 +989,13 @@ public class VibratorManagerService extends IVibratorManagerService.Stub { */ @NonNull private VibrationAttributes fixupVibrationAttributes(@Nullable VibrationAttributes attrs, - CombinedVibration effect) { + @Nullable CombinedVibration effect) { if (attrs == null) { attrs = DEFAULT_ATTRIBUTES; } int usage = attrs.getUsage(); - if ((usage == VibrationAttributes.USAGE_UNKNOWN) && effect.isHapticFeedbackCandidate()) { + if ((usage == VibrationAttributes.USAGE_UNKNOWN) + && (effect != null) && effect.isHapticFeedbackCandidate()) { usage = VibrationAttributes.USAGE_TOUCH; } int flags = attrs.getFlags(); diff --git a/services/tests/servicestests/src/com/android/server/vibrator/VibratorManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/vibrator/VibratorManagerServiceTest.java index 9c72ce22c857c..f3d494d160f28 100644 --- a/services/tests/servicestests/src/com/android/server/vibrator/VibratorManagerServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/vibrator/VibratorManagerServiceTest.java @@ -1315,6 +1315,24 @@ public class VibratorManagerServiceTest { assertNotEquals(IExternalVibratorService.SCALE_MUTE, scale); } + @Test + public void onExternalVibration_withUnknownUsage_appliesMediaSettings() { + mockVibrators(1); + mVibratorProviders.get(1).setCapabilities(IVibrator.CAP_EXTERNAL_CONTROL); + setUserSetting(Settings.System.MEDIA_VIBRATION_INTENSITY, + Vibrator.VIBRATION_INTENSITY_OFF); + AudioAttributes flaggedAudioAttrs = new AudioAttributes.Builder() + .setUsage(AudioAttributes.USAGE_UNKNOWN) + .setFlags(AudioAttributes.FLAG_BYPASS_MUTE) + .build(); + createSystemReadyService(); + + int scale = mExternalVibratorService.onExternalVibrationStart( + new ExternalVibration(/* uid= */ 123, PACKAGE_NAME, flaggedAudioAttrs, + mock(IExternalVibrationController.class))); + assertEquals(IExternalVibratorService.SCALE_MUTE, scale); + } + private VibrationEffectSegment expectedPrebaked(int effectId) { return expectedPrebaked(effectId, VibrationEffect.EFFECT_STRENGTH_MEDIUM); }