Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3f912b02 authored by Yeabkal Wubshit's avatar Yeabkal Wubshit
Browse files

Create View.performHapticFeedback API that takes vibration usage

The preexisting haptic feedback APIs statically derived the vibration
usage for each feedback constants. This meant that a given constant
could only be played with that statically defined usage.

To enable a more dynamic usage of haptic feedback, we are adding a
performHapticFeedback API that allows specifying the vibration usage for
the feedback. This usage should be of USAGE_CLASS_FEEDBACK. Other than
this difference, this new API works the same way as the existing haptic
feedback APIs.

Bug: 397602072
Bug: 408393305
Test: atest VibratorManagerServiceTest
Test: atest ViewTest
Test: atest HapticFeedbackVibrationProviderTest
Flag: android.os.vibrator.haptic_feedback_with_custom_usage
Test: adb shell cmd vibrator_manager feedback -u 50 0
Change-Id: I7fdeeef0b80c978cacda06cc3d090c5fc827cbd8
parent f225fe41
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -54490,6 +54490,7 @@ package android.view {
    method public boolean performContextClick();
    method public boolean performHapticFeedback(int);
    method public boolean performHapticFeedback(int, int);
    method @FlaggedApi("android.os.vibrator.haptic_feedback_with_custom_usage") public boolean performHapticFeedback(@NonNull android.os.vibrator.HapticFeedbackRequest);
    method public boolean performLongClick();
    method public boolean performLongClick(float, float);
    method @Nullable public android.view.ContentInfo performReceiveContent(@NonNull android.view.ContentInfo);
+1 −1
Original line number Diff line number Diff line
@@ -45,7 +45,7 @@ interface IVibratorManagerService {
    // There is no order guarantee with respect to the two-way APIs above like
    // vibrate/isVibrating/cancel.
    oneway void performHapticFeedback(int uid, int deviceId, String opPkg, int constant,
            String reason, int flags, int privFlags);
            int usage, String reason, int flags, int privFlags);

    // Similar to performHapticFeedback but the effect is customized to the input device. The
    // customization for each constant is defined on a device basis, and the behavior will be the
+3 −2
Original line number Diff line number Diff line
@@ -226,12 +226,13 @@ public class SystemVibrator extends Vibrator {
    }

    @Override
    public void performHapticFeedback(int constant, String reason, int flags, int privFlags) {
    public void performHapticFeedback(int constant, @VibrationAttributes.Usage int usage,
            String reason, int flags, int privFlags) {
        if (mVibratorManager == null) {
            Log.w(TAG, "Failed to perform haptic feedback; no vibrator manager.");
            return;
        }
        mVibratorManager.performHapticFeedback(constant, reason, flags, privFlags);
        mVibratorManager.performHapticFeedback(constant, usage, reason, flags, privFlags);
    }

    @Override
+7 −4
Original line number Diff line number Diff line
@@ -163,7 +163,8 @@ public class SystemVibratorManager extends VibratorManager {
    }

    @Override
    public void performHapticFeedback(int constant, String reason, int flags, int privFlags) {
    public void performHapticFeedback(int constant, @VibrationAttributes.Usage int usage,
            String reason, int flags, int privFlags) {
        if (mService == null) {
            Log.w(TAG, "Failed to perform haptic feedback; no vibrator manager service.");
            return;
@@ -171,7 +172,7 @@ public class SystemVibratorManager extends VibratorManager {
        Trace.traceBegin(TRACE_TAG_VIBRATOR, "performHapticFeedback");
        try {
            mService.performHapticFeedback(mUid, mContext.getDeviceId(), mPackageName, constant,
                    reason, flags, privFlags);
                    usage, reason, flags, privFlags);
        } catch (RemoteException e) {
            Log.w(TAG, "Failed to perform haptic feedback.", e);
        } finally {
@@ -364,8 +365,10 @@ public class SystemVibratorManager extends VibratorManager {
        }

        @Override
        public void performHapticFeedback(int effectId, String reason, int flags, int privFlags) {
            SystemVibratorManager.this.performHapticFeedback(effectId, reason, flags, privFlags);
        public void performHapticFeedback(int effectId, @VibrationAttributes.Usage int usage,
                String reason, int flags, int privFlags) {
            SystemVibratorManager.this.performHapticFeedback(
                    effectId, usage, reason, flags, privFlags);
        }

        @Override
+5 −2
Original line number Diff line number Diff line
@@ -643,14 +643,17 @@ public abstract class Vibrator {
     *
     * @param constant the ID for the haptic feedback. This should be one of the constants defined
     *          in {@link HapticFeedbackConstants}.
     * @param usage one of the {@link VibrationAttributes} usages with the class
     *      {@link USAGE_CLASS_FEEDBACK}. Use {@link USAGE_UNKNOWN} if you are not sure about which
     *      usage to use, and the system will assign a default usage.
     * @param reason the reason for this haptic feedback.
     * @param flags Additional flags as per {@link HapticFeedbackConstants}.
     * @param privFlags Additional private flags as per {@link HapticFeedbackConstants}.
     *
     * @hide
     */
    public void performHapticFeedback(int constant, String reason,
            @HapticFeedbackConstants.Flags int flags,
    public void performHapticFeedback(int constant, @VibrationAttributes.Usage int usage,
            String reason, @HapticFeedbackConstants.Flags int flags,
            @HapticFeedbackConstants.PrivateFlags int privFlags) {
        Log.w(TAG, "performHapticFeedback is not supported");
    }
Loading