From 1e5c3566d0d9712978d6a838ac23a9e88b61db74 Mon Sep 17 00:00:00 2001 From: Juan Sebastian Martinez Date: Wed, 19 Jul 2023 15:33:32 +0000 Subject: [PATCH] Extension to VibratorHelper to use the new oneway haptics API Two wrapper methods are added to the helper that use the new oneway API from haptics framework. The methods rely on calling the new API from a View passed as a parameter Test: Unit tests testPerformHapticFeedback and testPerformHapticFeedback_withFlags test that the wrappers call the corresponding view methods Bug: 245528624 Change-Id: I0d84b4f41dc25eef9daeec9392376f165aa0490f --- .../systemui/statusbar/VibratorHelper.java | 17 +++++++++++++++++ .../systemui/statusbar/VibratorHelperTest.kt | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/VibratorHelper.java b/packages/SystemUI/src/com/android/systemui/statusbar/VibratorHelper.java index 324e97294f4ea..645595c1f7bf7 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/VibratorHelper.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/VibratorHelper.java @@ -23,6 +23,7 @@ import android.os.Process; import android.os.VibrationAttributes; import android.os.VibrationEffect; import android.os.Vibrator; +import android.view.View; import androidx.annotation.VisibleForTesting; @@ -151,4 +152,20 @@ public class VibratorHelper { BIOMETRIC_ERROR_VIBRATION_EFFECT, reason, HARDWARE_FEEDBACK_VIBRATION_ATTRIBUTES); } + + /** + * Perform a vibration using a view and the one-way API with flags + * @see View#performHapticFeedback(int feedbackConstant, int flags) + */ + public void performHapticFeedback(@NonNull View view, int feedbackConstant, int flags) { + view.performHapticFeedback(feedbackConstant, flags); + } + + /** + * Perform a vibration using a view and the one-way API + * @see View#performHapticFeedback(int feedbackConstant) + */ + public void performHapticFeedback(@NonNull View view, int feedbackConstant) { + view.performHapticFeedback(feedbackConstant); + } } diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/VibratorHelperTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/VibratorHelperTest.kt index ad908e7f80003..aab4bc361d7b6 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/VibratorHelperTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/VibratorHelperTest.kt @@ -6,6 +6,8 @@ import android.os.VibrationAttributes import android.os.VibrationEffect import android.os.Vibrator import android.testing.AndroidTestingRunner +import android.view.HapticFeedbackConstants +import android.view.View import androidx.test.filters.SmallTest import com.android.systemui.SysuiTestCase import com.android.systemui.util.mockito.eq @@ -33,6 +35,7 @@ class VibratorHelperTest : SysuiTestCase() { @Mock lateinit var vibrator: Vibrator @Mock lateinit var executor: Executor + @Mock lateinit var view: View @Captor lateinit var backgroundTaskCaptor: ArgumentCaptor lateinit var vibratorHelper: VibratorHelper @@ -71,6 +74,21 @@ class VibratorHelperTest : SysuiTestCase() { verifyAsync().vibrate(any(VibrationEffect::class.java)) } + @Test + fun testPerformHapticFeedback() { + val constant = HapticFeedbackConstants.CONFIRM + vibratorHelper.performHapticFeedback(view, constant) + verify(view).performHapticFeedback(eq(constant)) + } + + @Test + fun testPerformHapticFeedback_withFlags() { + val constant = HapticFeedbackConstants.CONFIRM + val flag = HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING + vibratorHelper.performHapticFeedback(view, constant, flag) + verify(view).performHapticFeedback(eq(constant), eq(flag)) + } + @Test fun testHasVibrator() { assertThat(vibratorHelper.hasVibrator()).isTrue()