Add haptic feedback to Launcher recent apps scrolling
Add light LOW_TICK effects when the scrolling on RecentsView snaps to a page, and a stronger TICK effect when the edges absorb the scroll (by scrolling fast towards one of the edges). Fix: 182382085 Test: manual Change-Id: Ifb917ae499f73607707773f9870eb39cd6fe16ef
This commit is contained in:
@@ -479,9 +479,11 @@ public abstract class PagedView<T extends View & PageIndicator> extends ViewGrou
|
||||
if (newPos < mMinScroll && oldPos >= mMinScroll) {
|
||||
mEdgeGlowLeft.onAbsorb((int) mScroller.getCurrVelocity());
|
||||
mScroller.abortAnimation();
|
||||
onEdgeAbsorbingScroll();
|
||||
} else if (newPos > mMaxScroll && oldPos <= mMaxScroll) {
|
||||
mEdgeGlowRight.onAbsorb((int) mScroller.getCurrVelocity());
|
||||
mScroller.abortAnimation();
|
||||
onEdgeAbsorbingScroll();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1332,6 +1334,13 @@ public abstract class PagedView<T extends View & PageIndicator> extends ViewGrou
|
||||
|
||||
protected void onNotSnappingToPageInFreeScroll() { }
|
||||
|
||||
/**
|
||||
* Called when the view edges absorb part of the scroll. Subclasses can override this
|
||||
* to provide custom behavior during animation.
|
||||
*/
|
||||
protected void onEdgeAbsorbingScroll() {
|
||||
}
|
||||
|
||||
protected boolean shouldFlingForVelocity(int velocity) {
|
||||
float threshold = mAllowEasyFling ? mEasyFlingThresholdVelocity : mFlingThresholdVelocity;
|
||||
return Math.abs(velocity) > threshold;
|
||||
|
||||
@@ -21,15 +21,19 @@ 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.UI_HELPER_EXECUTOR;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.database.ContentObserver;
|
||||
import android.media.AudioAttributes;
|
||||
import android.os.Build;
|
||||
import android.os.VibrationEffect;
|
||||
import android.os.Vibrator;
|
||||
import android.provider.Settings;
|
||||
|
||||
import com.android.launcher3.Utilities;
|
||||
|
||||
/**
|
||||
* Wrapper around {@link Vibrator} to easily perform haptic feedback where necessary.
|
||||
*/
|
||||
@@ -39,6 +43,11 @@ public class VibratorWrapper {
|
||||
public static final MainThreadInitializedObject<VibratorWrapper> INSTANCE =
|
||||
new MainThreadInitializedObject<>(VibratorWrapper::new);
|
||||
|
||||
public static final AudioAttributes VIBRATION_ATTRS = new AudioAttributes.Builder()
|
||||
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
|
||||
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
|
||||
.build();
|
||||
|
||||
public static final VibrationEffect EFFECT_CLICK =
|
||||
createPredefined(VibrationEffect.EFFECT_CLICK);
|
||||
|
||||
@@ -81,4 +90,24 @@ public class VibratorWrapper {
|
||||
UI_HELPER_EXECUTOR.execute(() -> mVibrator.vibrate(vibrationEffect));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Vibrates with a single primitive, if supported, or use a fallback effect instead. This only
|
||||
* vibrates if haptic feedback is available and enabled.
|
||||
*/
|
||||
@SuppressLint("NewApi")
|
||||
public void vibrate(int primitiveId, float primitiveScale, VibrationEffect fallbackEffect) {
|
||||
if (mHasVibrator && mIsHapticFeedbackEnabled) {
|
||||
UI_HELPER_EXECUTOR.execute(() -> {
|
||||
if (Utilities.ATLEAST_R && primitiveId >= 0
|
||||
&& mVibrator.areAllPrimitivesSupported(primitiveId)) {
|
||||
mVibrator.vibrate(VibrationEffect.startComposition()
|
||||
.addPrimitive(primitiveId, primitiveScale)
|
||||
.compose(), VIBRATION_ATTRS);
|
||||
} else {
|
||||
mVibrator.vibrate(fallbackEffect, VIBRATION_ATTRS);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user