Merge "Added utility function to VibratorWrapper for generating Assist hapic" into udc-qpr-dev

This commit is contained in:
Andreas Agvard
2023-06-09 10:29:17 +00:00
committed by Android (Google) Code Review
@@ -17,6 +17,7 @@ package com.android.launcher3.util;
import static android.os.VibrationEffect.createPredefined; import static android.os.VibrationEffect.createPredefined;
import static android.provider.Settings.System.HAPTIC_FEEDBACK_ENABLED; import static android.provider.Settings.System.HAPTIC_FEEDBACK_ENABLED;
import static com.android.launcher3.util.Executors.MAIN_EXECUTOR; import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR; import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
@@ -68,6 +69,9 @@ public class VibratorWrapper {
@Nullable @Nullable
private final VibrationEffect mBumpEffect; private final VibrationEffect mBumpEffect;
@Nullable
private final VibrationEffect mAssistEffect;
private long mLastDragTime; private long mLastDragTime;
private final int mThresholdUntilNextDragCallMillis; private final int mThresholdUntilNextDragCallMillis;
@@ -125,12 +129,25 @@ public class VibratorWrapper {
mBumpEffect = null; mBumpEffect = null;
mThresholdUntilNextDragCallMillis = 0; mThresholdUntilNextDragCallMillis = 0;
} }
if (Utilities.ATLEAST_R && mVibrator.areAllPrimitivesSupported(
VibrationEffect.Composition.PRIMITIVE_QUICK_RISE,
VibrationEffect.Composition.PRIMITIVE_TICK)) {
// quiet ramp, short pause, then sharp tick
mAssistEffect = VibrationEffect.startComposition()
.addPrimitive(VibrationEffect.Composition.PRIMITIVE_QUICK_RISE, 0.25f)
.addPrimitive(VibrationEffect.Composition.PRIMITIVE_TICK, 1f, 50)
.compose();
} else {
// fallback for devices without composition support
mAssistEffect = VibrationEffect.createPredefined(VibrationEffect.EFFECT_HEAVY_CLICK);
}
} }
/** /**
* This is called when the user swipes to/from all apps. This is meant to be used in between * This is called when the user swipes to/from all apps. This is meant to be used in between
* long animation progresses so that it gives a dragging texture effect. For a better * long animation progresses so that it gives a dragging texture effect. For a better
* experience, this should be used in combination with vibrateForDragCommit(). * experience, this should be used in combination with vibrateForDragCommit().
*/ */
public void vibrateForDragTexture() { public void vibrateForDragTexture() {
if (mDragEffect == null) { if (mDragEffect == null) {
@@ -145,7 +162,7 @@ public class VibratorWrapper {
} }
/** /**
* This is used when user reaches the commit threshold when swiping to/from from all apps. * This is used when user reaches the commit threshold when swiping to/from from all apps.
*/ */
public void vibrateForDragCommit() { public void vibrateForDragCommit() {
if (mCommitEffect != null) { if (mCommitEffect != null) {
@@ -156,9 +173,9 @@ public class VibratorWrapper {
} }
/** /**
* The bump haptic is used to be called at the end of a swipe and only if it the gesture is a * The bump haptic is used to be called at the end of a swipe and only if it the gesture is a
* FLING going to/from all apps. Client can just call this method elsewhere just for the * FLING going to/from all apps. Client can just call this method elsewhere just for the
* effect. * effect.
*/ */
public void vibrateForDragBump() { public void vibrateForDragBump() {
if (mBumpEffect != null) { if (mBumpEffect != null) {
@@ -166,6 +183,15 @@ public class VibratorWrapper {
} }
} }
/**
* The assist haptic is used to be called when an assistant is invoked
*/
public void vibrateForAssist() {
if (mAssistEffect != null) {
vibrate(mAssistEffect);
}
}
/** /**
* This should be used to cancel a haptic in case where the haptic shouldn't be vibrating. For * This should be used to cancel a haptic in case where the haptic shouldn't be vibrating. For
* example, when no animation is happening but a vibrator happens to be vibrating still. Need * example, when no animation is happening but a vibrator happens to be vibrating still. Need
@@ -176,6 +202,7 @@ public class VibratorWrapper {
// reset dragTexture timestamp to be able to play dragTexture again whenever cancelled // reset dragTexture timestamp to be able to play dragTexture again whenever cancelled
mLastDragTime = 0; mLastDragTime = 0;
} }
private boolean isHapticFeedbackEnabled(ContentResolver resolver) { private boolean isHapticFeedbackEnabled(ContentResolver resolver) {
return Settings.System.getInt(resolver, HAPTIC_FEEDBACK_ENABLED, 0) == 1; return Settings.System.getInt(resolver, HAPTIC_FEEDBACK_ENABLED, 0) == 1;
} }