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

Commit dd141e0d authored by Chen Bai's avatar Chen Bai
Browse files

haptic: add VibratorManager API to make haptic feedback based on input

- In VibratorManager, add a hidden API that perform haptic feedback
  based on the input information specifically, input device id and input
  source. Note that for the scope of current feature, only input source
  is taken into consideration. Input device id is reserved for future
  extension.
- Apply the new API in to View's scroll feedback.

Flag: android.os.vibrator.haptic_feedback_input_source_customization_enabled
Test: atest HapticFeedbackVibrationProviderTest
Test: atest VibratorManagerServiceTest
Bug: 353625893
Change-Id: I2f319cc955a0a3070c8a6c69961a656320580e8f
parent ecbdc5f1
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -42,4 +42,12 @@ interface IVibratorManagerService {
    // vibrate/isVibrating/cancel.
    oneway void performHapticFeedback(int uid, int deviceId, String opPkg, int constant,
            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
    // same as performHapticFeedback when no customization is provided for a given constant and
    // device.
    oneway void performHapticFeedbackForInputDevice(int uid, int deviceId, String opPkg,
            int constant, int inputDeviceId, int inputSource, String reason, int flags,
            int privFlags);
}
+11 −0
Original line number Diff line number Diff line
@@ -214,6 +214,17 @@ public class SystemVibrator extends Vibrator {
        mVibratorManager.performHapticFeedback(constant, reason, flags, privFlags);
    }

    @Override
    public void performHapticFeedbackForInputDevice(int constant, int inputDeviceId,
            int inputSource, String reason, int flags, int privFlags) {
        if (mVibratorManager == null) {
            Log.w(TAG, "Failed to perform haptic feedback for input device; no vibrator manager.");
            return;
        }
        mVibratorManager.performHapticFeedbackForInputDevice(constant, inputDeviceId, inputSource,
                reason, flags, privFlags);
    }

    @Override
    public void cancel() {
        if (mVibratorManager == null) {
+16 −0
Original line number Diff line number Diff line
@@ -160,6 +160,22 @@ public class SystemVibratorManager extends VibratorManager {
        }
    }

    @Override
    public void performHapticFeedbackForInputDevice(int constant, int inputDeviceId,
            int inputSource, String reason, int flags, int privFlags) {
        if (mService == null) {
            Log.w(TAG, "Failed to perform haptic feedback for input device;"
                            + " no vibrator manager service.");
            return;
        }
        try {
            mService.performHapticFeedbackForInputDevice(mUid, mContext.getDeviceId(), mPackageName,
                    constant, inputDeviceId, inputSource, reason, flags, privFlags);
        } catch (RemoteException e) {
            Log.w(TAG, "Failed to perform haptic feedback for input device.", e);
        }
    }

    @Override
    public void cancel() {
        cancelVibration(VibrationAttributes.USAGE_FILTER_MATCH_ALL);
+25 −0
Original line number Diff line number Diff line
@@ -552,6 +552,31 @@ public abstract class Vibrator {
        Log.w(TAG, "performHapticFeedback is not supported");
    }

    /**
     * Performs a haptic feedback. Similar to {@link #performHapticFeedback} but also take into the
     * consideration the {@link InputDevice} that triggered the haptic
     *
     * <p>A haptic feedback is a short vibration feedback. The type of feedback is identified via
     * the {@code constant}, which should be one of the effect constants provided in
     * {@link HapticFeedbackConstants}. The haptic feedback provided for a given effect ID is
     * consistent across all usages on the same device.
     *
     * @param constant      the ID for the haptic feedback. This should be one of the constants
     *                      defined in {@link HapticFeedbackConstants}.
     * @param inputDeviceId the integer id of the input device that triggered the haptic feedback.
     * @param inputSource   the {@link InputDevice.Source} that triggered the haptic feedback.
     * @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 performHapticFeedbackForInputDevice(
            int constant, int inputDeviceId, int inputSource, String reason,
            @HapticFeedbackConstants.Flags int flags,
            @HapticFeedbackConstants.PrivateFlags int privFlags) {
        Log.w(TAG, "performHapticFeedbackForInputDevice is not supported");
    }

    /**
     * Query whether the vibrator natively supports the given effects.
     *
+21 −0
Original line number Diff line number Diff line
@@ -154,6 +154,27 @@ public abstract class VibratorManager {
        Log.w(TAG, "performHapticFeedback is not supported");
    }

    /**
     * Performs a haptic feedback. Similar to {@link #performHapticFeedback} but also take input
     * into consideration.
     *
     * @param constant      the ID of the requested haptic feedback. Should be one of the constants
     *                      defined in {@link HapticFeedbackConstants}.
     * @param inputDeviceId the integer id of the input device that customizes the haptic feedback
     *                      corresponding to the {@code constant}.
     * @param inputSource   the {@link InputDevice.Source} that customizes the haptic feedback
     *                      corresponding to the {@code constant}.
     * @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 performHapticFeedbackForInputDevice(int constant, int inputDeviceId,
            int inputSource, String reason, @HapticFeedbackConstants.Flags int flags,
            @HapticFeedbackConstants.PrivateFlags int privFlags) {
        Log.w(TAG, "performHapticFeedbackForInputDevice is not supported");
    }

    /**
     * Turn all the vibrators off.
     */
Loading