2019-09-24 16:17:40 -07:00
|
|
|
/*
|
2021-06-04 18:20:30 +01:00
|
|
|
* Copyright (C) 2021 The Android Open Source Project
|
2019-09-24 16:17:40 -07:00
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
2022-12-20 10:26:19 -08:00
|
|
|
package com.android.launcher3.util;
|
2019-09-24 16:17:40 -07:00
|
|
|
|
2023-11-28 16:07:44 -08:00
|
|
|
import static android.os.VibrationEffect.Composition.PRIMITIVE_LOW_TICK;
|
2019-09-24 16:17:40 -07:00
|
|
|
import static android.os.VibrationEffect.createPredefined;
|
|
|
|
|
import static android.provider.Settings.System.HAPTIC_FEEDBACK_ENABLED;
|
2023-06-07 20:36:15 +02:00
|
|
|
|
2023-11-16 15:06:46 +01:00
|
|
|
import static com.android.launcher3.LauncherPrefs.LONG_PRESS_NAV_HANDLE_HAPTIC_HINT_DELAY;
|
2023-10-16 15:49:56 +02:00
|
|
|
import static com.android.launcher3.LauncherPrefs.LONG_PRESS_NAV_HANDLE_HAPTIC_HINT_END_SCALE_PERCENT;
|
|
|
|
|
import static com.android.launcher3.LauncherPrefs.LONG_PRESS_NAV_HANDLE_HAPTIC_HINT_ITERATIONS;
|
|
|
|
|
import static com.android.launcher3.LauncherPrefs.LONG_PRESS_NAV_HANDLE_HAPTIC_HINT_SCALE_EXPONENT;
|
|
|
|
|
import static com.android.launcher3.LauncherPrefs.LONG_PRESS_NAV_HANDLE_HAPTIC_HINT_START_SCALE_PERCENT;
|
2019-09-24 16:17:40 -07:00
|
|
|
import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
|
|
|
|
|
import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
|
|
|
|
|
|
2021-06-04 18:20:30 +01:00
|
|
|
import android.annotation.SuppressLint;
|
2019-09-24 16:17:40 -07:00
|
|
|
import android.annotation.TargetApi;
|
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.database.ContentObserver;
|
2021-06-04 18:20:30 +01:00
|
|
|
import android.media.AudioAttributes;
|
2019-09-24 16:17:40 -07:00
|
|
|
import android.os.Build;
|
2022-10-31 14:28:38 -07:00
|
|
|
import android.os.SystemClock;
|
2019-09-24 16:17:40 -07:00
|
|
|
import android.os.VibrationEffect;
|
|
|
|
|
import android.os.Vibrator;
|
|
|
|
|
import android.provider.Settings;
|
|
|
|
|
|
2022-10-31 14:28:38 -07:00
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
|
2023-10-16 15:49:56 +02:00
|
|
|
import com.android.launcher3.LauncherPrefs;
|
2021-06-04 18:20:30 +01:00
|
|
|
import com.android.launcher3.Utilities;
|
2023-10-16 15:49:56 +02:00
|
|
|
import com.android.launcher3.config.FeatureFlags;
|
2021-06-04 18:20:30 +01:00
|
|
|
|
2019-09-24 16:17:40 -07:00
|
|
|
/**
|
|
|
|
|
* Wrapper around {@link Vibrator} to easily perform haptic feedback where necessary.
|
|
|
|
|
*/
|
|
|
|
|
@TargetApi(Build.VERSION_CODES.Q)
|
|
|
|
|
public class VibratorWrapper {
|
|
|
|
|
|
|
|
|
|
public static final MainThreadInitializedObject<VibratorWrapper> INSTANCE =
|
|
|
|
|
new MainThreadInitializedObject<>(VibratorWrapper::new);
|
|
|
|
|
|
2021-06-04 18:20:30 +01:00
|
|
|
public static final AudioAttributes VIBRATION_ATTRS = new AudioAttributes.Builder()
|
|
|
|
|
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
|
|
|
|
|
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
|
|
|
|
|
.build();
|
|
|
|
|
|
2020-03-18 21:47:46 +00:00
|
|
|
public static final VibrationEffect EFFECT_CLICK =
|
2019-09-24 16:17:40 -07:00
|
|
|
createPredefined(VibrationEffect.EFFECT_CLICK);
|
|
|
|
|
|
2023-11-28 16:07:44 -08:00
|
|
|
private static final float LOW_TICK_SCALE = 0.7f;
|
2022-10-31 14:28:38 -07:00
|
|
|
private static final float DRAG_TEXTURE_SCALE = 0.03f;
|
|
|
|
|
private static final float DRAG_COMMIT_SCALE = 0.5f;
|
|
|
|
|
private static final float DRAG_BUMP_SCALE = 0.4f;
|
|
|
|
|
private static final int DRAG_TEXTURE_EFFECT_SIZE = 200;
|
|
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
|
private final VibrationEffect mDragEffect;
|
|
|
|
|
@Nullable
|
|
|
|
|
private final VibrationEffect mCommitEffect;
|
|
|
|
|
@Nullable
|
|
|
|
|
private final VibrationEffect mBumpEffect;
|
|
|
|
|
|
2023-06-07 20:36:15 +02:00
|
|
|
@Nullable
|
2023-10-16 15:49:56 +02:00
|
|
|
private final VibrationEffect mSearchEffect;
|
2023-06-07 20:36:15 +02:00
|
|
|
|
2022-10-31 14:28:38 -07:00
|
|
|
private long mLastDragTime;
|
|
|
|
|
private final int mThresholdUntilNextDragCallMillis;
|
|
|
|
|
|
2019-09-24 16:17:40 -07:00
|
|
|
/**
|
|
|
|
|
* Haptic when entering overview.
|
|
|
|
|
*/
|
|
|
|
|
public static final VibrationEffect OVERVIEW_HAPTIC = EFFECT_CLICK;
|
|
|
|
|
|
2023-10-16 15:49:56 +02:00
|
|
|
private final Context mContext;
|
2019-09-24 16:17:40 -07:00
|
|
|
private final Vibrator mVibrator;
|
|
|
|
|
private final boolean mHasVibrator;
|
|
|
|
|
|
|
|
|
|
private boolean mIsHapticFeedbackEnabled;
|
|
|
|
|
|
2022-10-31 14:28:38 -07:00
|
|
|
private VibratorWrapper(Context context) {
|
2023-10-16 15:49:56 +02:00
|
|
|
mContext = context;
|
2019-09-24 16:17:40 -07:00
|
|
|
mVibrator = context.getSystemService(Vibrator.class);
|
|
|
|
|
mHasVibrator = mVibrator.hasVibrator();
|
|
|
|
|
if (mHasVibrator) {
|
|
|
|
|
final ContentResolver resolver = context.getContentResolver();
|
|
|
|
|
mIsHapticFeedbackEnabled = isHapticFeedbackEnabled(resolver);
|
|
|
|
|
final ContentObserver observer = new ContentObserver(MAIN_EXECUTOR.getHandler()) {
|
|
|
|
|
@Override
|
|
|
|
|
public void onChange(boolean selfChange) {
|
|
|
|
|
mIsHapticFeedbackEnabled = isHapticFeedbackEnabled(resolver);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
resolver.registerContentObserver(Settings.System.getUriFor(HAPTIC_FEEDBACK_ENABLED),
|
2022-10-31 14:28:38 -07:00
|
|
|
false /* notifyForDescendants */, observer);
|
2019-09-24 16:17:40 -07:00
|
|
|
} else {
|
|
|
|
|
mIsHapticFeedbackEnabled = false;
|
|
|
|
|
}
|
2022-10-31 14:28:38 -07:00
|
|
|
|
|
|
|
|
if (Utilities.ATLEAST_S && mVibrator.areAllPrimitivesSupported(
|
2023-11-28 16:07:44 -08:00
|
|
|
PRIMITIVE_LOW_TICK)) {
|
2022-10-31 14:28:38 -07:00
|
|
|
|
|
|
|
|
// Drag texture, Commit, and Bump should only be used for premium phones.
|
|
|
|
|
// Before using these haptics make sure check if the device can use it
|
|
|
|
|
VibrationEffect.Composition dragEffect = VibrationEffect.startComposition();
|
|
|
|
|
for (int i = 0; i < DRAG_TEXTURE_EFFECT_SIZE; i++) {
|
|
|
|
|
dragEffect.addPrimitive(
|
2023-11-28 16:07:44 -08:00
|
|
|
PRIMITIVE_LOW_TICK, DRAG_TEXTURE_SCALE);
|
2022-10-31 14:28:38 -07:00
|
|
|
}
|
|
|
|
|
mDragEffect = dragEffect.compose();
|
|
|
|
|
mCommitEffect = VibrationEffect.startComposition().addPrimitive(
|
|
|
|
|
VibrationEffect.Composition.PRIMITIVE_TICK, DRAG_COMMIT_SCALE).compose();
|
|
|
|
|
mBumpEffect = VibrationEffect.startComposition().addPrimitive(
|
2023-11-28 16:07:44 -08:00
|
|
|
PRIMITIVE_LOW_TICK, DRAG_BUMP_SCALE).compose();
|
2022-10-31 14:28:38 -07:00
|
|
|
int primitiveDuration = mVibrator.getPrimitiveDurations(
|
2023-11-28 16:07:44 -08:00
|
|
|
PRIMITIVE_LOW_TICK)[0];
|
2022-10-31 14:28:38 -07:00
|
|
|
|
|
|
|
|
mThresholdUntilNextDragCallMillis =
|
|
|
|
|
DRAG_TEXTURE_EFFECT_SIZE * primitiveDuration + 100;
|
|
|
|
|
} else {
|
|
|
|
|
mDragEffect = null;
|
|
|
|
|
mCommitEffect = null;
|
|
|
|
|
mBumpEffect = null;
|
|
|
|
|
mThresholdUntilNextDragCallMillis = 0;
|
|
|
|
|
}
|
2023-06-07 20:36:15 +02:00
|
|
|
|
|
|
|
|
if (Utilities.ATLEAST_R && mVibrator.areAllPrimitivesSupported(
|
|
|
|
|
VibrationEffect.Composition.PRIMITIVE_QUICK_RISE,
|
|
|
|
|
VibrationEffect.Composition.PRIMITIVE_TICK)) {
|
2023-10-16 15:49:56 +02:00
|
|
|
if (FeatureFlags.ENABLE_SEARCH_HAPTIC_HINT.get()) {
|
|
|
|
|
mSearchEffect = VibrationEffect.startComposition()
|
|
|
|
|
.addPrimitive(VibrationEffect.Composition.PRIMITIVE_TICK, 1f)
|
|
|
|
|
.compose();
|
|
|
|
|
} else {
|
|
|
|
|
// quiet ramp, short pause, then sharp tick
|
|
|
|
|
mSearchEffect = VibrationEffect.startComposition()
|
|
|
|
|
.addPrimitive(VibrationEffect.Composition.PRIMITIVE_QUICK_RISE, 0.25f)
|
|
|
|
|
.addPrimitive(VibrationEffect.Composition.PRIMITIVE_TICK, 1f, 50)
|
|
|
|
|
.compose();
|
|
|
|
|
}
|
2023-06-07 20:36:15 +02:00
|
|
|
} else {
|
|
|
|
|
// fallback for devices without composition support
|
2023-10-16 15:49:56 +02:00
|
|
|
mSearchEffect = VibrationEffect.createPredefined(VibrationEffect.EFFECT_HEAVY_CLICK);
|
2023-06-07 20:36:15 +02:00
|
|
|
}
|
2022-10-31 14:28:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-06-07 20:36:15 +02:00
|
|
|
* 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
|
|
|
|
|
* experience, this should be used in combination with vibrateForDragCommit().
|
2022-10-31 14:28:38 -07:00
|
|
|
*/
|
|
|
|
|
public void vibrateForDragTexture() {
|
|
|
|
|
if (mDragEffect == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
long currentTime = SystemClock.elapsedRealtime();
|
|
|
|
|
long elapsedTimeSinceDrag = currentTime - mLastDragTime;
|
|
|
|
|
if (elapsedTimeSinceDrag >= mThresholdUntilNextDragCallMillis) {
|
|
|
|
|
vibrate(mDragEffect);
|
|
|
|
|
mLastDragTime = currentTime;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-06-07 20:36:15 +02:00
|
|
|
* This is used when user reaches the commit threshold when swiping to/from from all apps.
|
2022-10-31 14:28:38 -07:00
|
|
|
*/
|
|
|
|
|
public void vibrateForDragCommit() {
|
|
|
|
|
if (mCommitEffect != null) {
|
|
|
|
|
vibrate(mCommitEffect);
|
|
|
|
|
}
|
|
|
|
|
// resetting dragTexture timestamp to be able to play dragTexture again
|
|
|
|
|
mLastDragTime = 0;
|
2019-09-24 16:17:40 -07:00
|
|
|
}
|
|
|
|
|
|
2022-10-31 14:28:38 -07:00
|
|
|
/**
|
2023-06-07 20:36:15 +02:00
|
|
|
* 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
|
|
|
|
|
* effect.
|
2022-10-31 14:28:38 -07:00
|
|
|
*/
|
|
|
|
|
public void vibrateForDragBump() {
|
|
|
|
|
if (mBumpEffect != null) {
|
|
|
|
|
vibrate(mBumpEffect);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This should be used to cancel a haptic in case where the haptic shouldn't be vibrating. For
|
2023-10-16 15:49:56 +02:00
|
|
|
* example, when no animation is happening but a vibrator happens to be vibrating still.
|
2022-10-31 14:28:38 -07:00
|
|
|
*/
|
2023-10-16 15:49:56 +02:00
|
|
|
public void cancelVibrate() {
|
2022-10-31 14:28:38 -07:00
|
|
|
UI_HELPER_EXECUTOR.execute(mVibrator::cancel);
|
|
|
|
|
// reset dragTexture timestamp to be able to play dragTexture again whenever cancelled
|
|
|
|
|
mLastDragTime = 0;
|
|
|
|
|
}
|
2023-06-07 20:36:15 +02:00
|
|
|
|
2019-09-24 16:17:40 -07:00
|
|
|
private boolean isHapticFeedbackEnabled(ContentResolver resolver) {
|
|
|
|
|
return Settings.System.getInt(resolver, HAPTIC_FEEDBACK_ENABLED, 0) == 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Vibrates with the given effect if haptic feedback is available and enabled. */
|
|
|
|
|
public void vibrate(VibrationEffect vibrationEffect) {
|
|
|
|
|
if (mHasVibrator && mIsHapticFeedbackEnabled) {
|
2021-06-04 18:20:30 +01:00
|
|
|
UI_HELPER_EXECUTOR.execute(() -> mVibrator.vibrate(vibrationEffect, VIBRATION_ATTRS));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-09-24 16:17:40 -07:00
|
|
|
}
|
|
|
|
|
}
|
2023-10-16 15:49:56 +02:00
|
|
|
|
|
|
|
|
/** Indicates that search has been invoked. */
|
|
|
|
|
public void vibrateForSearch() {
|
2023-12-02 11:04:51 +01:00
|
|
|
if (mSearchEffect != null) {
|
2023-10-16 15:49:56 +02:00
|
|
|
vibrate(mSearchEffect);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-28 16:07:44 -08:00
|
|
|
/** Indicates that Taskbar has been invoked. */
|
|
|
|
|
public void vibrateForTaskbarUnstash() {
|
|
|
|
|
if (Utilities.ATLEAST_S && mVibrator.areAllPrimitivesSupported(PRIMITIVE_LOW_TICK)) {
|
|
|
|
|
VibrationEffect primitiveLowTickEffect = VibrationEffect
|
|
|
|
|
.startComposition()
|
|
|
|
|
.addPrimitive(PRIMITIVE_LOW_TICK, LOW_TICK_SCALE)
|
|
|
|
|
.compose();
|
|
|
|
|
|
|
|
|
|
vibrate(primitiveLowTickEffect);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-16 15:49:56 +02:00
|
|
|
/** Indicates that search will be invoked if the current gesture is maintained. */
|
|
|
|
|
public void vibrateForSearchHint() {
|
|
|
|
|
if (FeatureFlags.ENABLE_SEARCH_HAPTIC_HINT.get() && Utilities.ATLEAST_S
|
2023-11-28 16:07:44 -08:00
|
|
|
&& mVibrator.areAllPrimitivesSupported(PRIMITIVE_LOW_TICK)) {
|
2023-11-16 15:06:46 +01:00
|
|
|
LauncherPrefs launcherPrefs = LauncherPrefs.get(mContext);
|
|
|
|
|
float startScale = launcherPrefs.get(
|
2023-10-16 15:49:56 +02:00
|
|
|
LONG_PRESS_NAV_HANDLE_HAPTIC_HINT_START_SCALE_PERCENT) / 100f;
|
2023-11-16 15:06:46 +01:00
|
|
|
float endScale = launcherPrefs.get(
|
2023-10-16 15:49:56 +02:00
|
|
|
LONG_PRESS_NAV_HANDLE_HAPTIC_HINT_END_SCALE_PERCENT) / 100f;
|
2023-11-16 15:06:46 +01:00
|
|
|
int scaleExponent = launcherPrefs.get(
|
2023-10-16 15:49:56 +02:00
|
|
|
LONG_PRESS_NAV_HANDLE_HAPTIC_HINT_SCALE_EXPONENT);
|
2023-11-16 15:06:46 +01:00
|
|
|
int iterations = launcherPrefs.get(
|
2023-10-16 15:49:56 +02:00
|
|
|
LONG_PRESS_NAV_HANDLE_HAPTIC_HINT_ITERATIONS);
|
2023-11-16 15:06:46 +01:00
|
|
|
int delayMs = launcherPrefs.get(
|
|
|
|
|
LONG_PRESS_NAV_HANDLE_HAPTIC_HINT_DELAY);
|
2023-10-16 15:49:56 +02:00
|
|
|
|
|
|
|
|
VibrationEffect.Composition composition = VibrationEffect.startComposition();
|
|
|
|
|
for (int i = 0; i < iterations; i++) {
|
|
|
|
|
float t = i / (iterations - 1f);
|
|
|
|
|
float scale = (float) Math.pow((1 - t) * startScale + t * endScale,
|
|
|
|
|
scaleExponent);
|
2023-11-16 15:06:46 +01:00
|
|
|
if (i == 0) {
|
|
|
|
|
// Adds a delay before the ramp starts
|
2023-11-28 16:07:44 -08:00
|
|
|
composition.addPrimitive(PRIMITIVE_LOW_TICK, scale,
|
2023-11-16 15:06:46 +01:00
|
|
|
delayMs);
|
|
|
|
|
} else {
|
2023-11-28 16:07:44 -08:00
|
|
|
composition.addPrimitive(PRIMITIVE_LOW_TICK, scale);
|
2023-11-16 15:06:46 +01:00
|
|
|
}
|
2023-10-16 15:49:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vibrate(composition.compose());
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-24 16:17:40 -07:00
|
|
|
}
|