Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 18 | |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 19 | import android.view.MotionEvent; |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 20 | import android.view.View; |
Tony | 2ca999c | 2018-09-24 17:24:51 -0400 | [diff] [blame] | 21 | import android.view.ViewConfiguration; |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 22 | |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 23 | /** |
| 24 | * Utility class to handle tripper long press on a view with custom timeout and stylus event |
| 25 | */ |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 26 | public class CheckLongPressHelper { |
Winson Chung | 7501adf | 2015-06-02 11:24:28 -0700 | [diff] [blame] | 27 | |
Tony | 2ca999c | 2018-09-24 17:24:51 -0400 | [diff] [blame] | 28 | public static final float DEFAULT_LONG_PRESS_TIMEOUT_FACTOR = 0.75f; |
Tony Wickham | 1bce7fd | 2016-04-28 17:39:03 -0700 | [diff] [blame] | 29 | |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 30 | private final View mView; |
| 31 | private final View.OnLongClickListener mListener; |
| 32 | private final float mSlop; |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 33 | |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 34 | private float mLongPressTimeoutFactor = DEFAULT_LONG_PRESS_TIMEOUT_FACTOR; |
| 35 | |
| 36 | private boolean mHasPerformedLongPress; |
| 37 | |
| 38 | private Runnable mPendingCheckForLongPress; |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 39 | |
| 40 | public CheckLongPressHelper(View v) { |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 41 | this(v, null); |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Winson Chung | 6b27614 | 2015-05-13 15:44:26 -0700 | [diff] [blame] | 44 | public CheckLongPressHelper(View v, View.OnLongClickListener listener) { |
| 45 | mView = v; |
| 46 | mListener = listener; |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 47 | mSlop = ViewConfiguration.get(mView.getContext()).getScaledTouchSlop(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Handles the touch event on a view |
| 52 | * |
| 53 | * @see View#onTouchEvent(MotionEvent) |
| 54 | */ |
| 55 | public void onTouchEvent(MotionEvent ev) { |
| 56 | switch (ev.getAction()) { |
| 57 | case MotionEvent.ACTION_DOWN: { |
| 58 | // Just in case the previous long press hasn't been cleared, we make sure to |
| 59 | // start fresh on touch down. |
| 60 | cancelLongPress(); |
| 61 | |
| 62 | postCheckForLongPress(); |
| 63 | if (isStylusButtonPressed(ev)) { |
| 64 | triggerLongPress(); |
| 65 | } |
| 66 | break; |
| 67 | } |
| 68 | case MotionEvent.ACTION_CANCEL: |
| 69 | case MotionEvent.ACTION_UP: |
| 70 | cancelLongPress(); |
| 71 | break; |
| 72 | case MotionEvent.ACTION_MOVE: |
| 73 | if (!Utilities.pointInView(mView, ev.getX(), ev.getY(), mSlop)) { |
| 74 | cancelLongPress(); |
| 75 | } else if (mPendingCheckForLongPress != null && isStylusButtonPressed(ev)) { |
| 76 | // Only trigger long press if it has not been cancelled before |
| 77 | triggerLongPress(); |
| 78 | } |
| 79 | break; |
| 80 | } |
Winson Chung | 6b27614 | 2015-05-13 15:44:26 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Winson Chung | 7501adf | 2015-06-02 11:24:28 -0700 | [diff] [blame] | 83 | /** |
| 84 | * Overrides the default long press timeout. |
| 85 | */ |
Tony | 2ca999c | 2018-09-24 17:24:51 -0400 | [diff] [blame] | 86 | public void setLongPressTimeoutFactor(float longPressTimeoutFactor) { |
| 87 | mLongPressTimeoutFactor = longPressTimeoutFactor; |
Winson Chung | 7501adf | 2015-06-02 11:24:28 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 90 | private void postCheckForLongPress() { |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 91 | mHasPerformedLongPress = false; |
| 92 | |
| 93 | if (mPendingCheckForLongPress == null) { |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 94 | mPendingCheckForLongPress = this::triggerLongPress; |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 95 | } |
Tony | 2ca999c | 2018-09-24 17:24:51 -0400 | [diff] [blame] | 96 | mView.postDelayed(mPendingCheckForLongPress, |
| 97 | (long) (ViewConfiguration.getLongPressTimeout() * mLongPressTimeoutFactor)); |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 100 | /** |
| 101 | * Cancels any pending long press |
| 102 | */ |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 103 | public void cancelLongPress() { |
| 104 | mHasPerformedLongPress = false; |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 105 | clearCallbacks(); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Returns true if long press has been performed in the current touch gesture |
| 110 | */ |
| 111 | public boolean hasPerformedLongPress() { |
| 112 | return mHasPerformedLongPress; |
| 113 | } |
| 114 | |
| 115 | private void triggerLongPress() { |
Jon Miranda | a1567d5 | 2020-04-15 16:00:01 -0700 | [diff] [blame^] | 116 | if ((mView.getParent() != null) |
| 117 | && mView.hasWindowFocus() |
| 118 | && (!mView.isPressed() || mListener == null) |
| 119 | && !mHasPerformedLongPress) { |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 120 | boolean handled; |
| 121 | if (mListener != null) { |
| 122 | handled = mListener.onLongClick(mView); |
| 123 | } else { |
| 124 | handled = mView.performLongClick(); |
| 125 | } |
| 126 | if (handled) { |
| 127 | mView.setPressed(false); |
| 128 | mHasPerformedLongPress = true; |
| 129 | } |
| 130 | clearCallbacks(); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | private void clearCallbacks() { |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 135 | if (mPendingCheckForLongPress != null) { |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 136 | mView.removeCallbacks(mPendingCheckForLongPress); |
| 137 | mPendingCheckForLongPress = null; |
| 138 | } |
| 139 | } |
| 140 | |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * Identifies if the provided {@link MotionEvent} is a stylus with the primary stylus button |
| 144 | * pressed. |
| 145 | * |
| 146 | * @param event The event to check. |
| 147 | * @return Whether a stylus button press occurred. |
| 148 | */ |
| 149 | private static boolean isStylusButtonPressed(MotionEvent event) { |
| 150 | return event.getToolType(0) == MotionEvent.TOOL_TYPE_STYLUS |
| 151 | && event.isButtonPressed(MotionEvent.BUTTON_SECONDARY); |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 152 | } |
| 153 | } |