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 | |
Fengjiang Li | 9f90efb | 2022-12-01 16:53:17 -0800 | [diff] [blame] | 19 | import android.os.Handler; |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 20 | import android.view.MotionEvent; |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 21 | import android.view.View; |
Tony | 2ca999c | 2018-09-24 17:24:51 -0400 | [diff] [blame] | 22 | import android.view.ViewConfiguration; |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 23 | |
Fengjiang Li | a6a67e3 | 2022-12-06 21:53:25 -0800 | [diff] [blame] | 24 | import com.android.launcher3.util.TouchUtil; |
| 25 | |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 26 | /** |
Fengjiang Li | 9f90efb | 2022-12-01 16:53:17 -0800 | [diff] [blame] | 27 | * Utility class to handle tripper long press or right click on a view with custom timeout and |
| 28 | * stylus event |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 29 | */ |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 30 | public class CheckLongPressHelper { |
Winson Chung | 7501adf | 2015-06-02 11:24:28 -0700 | [diff] [blame] | 31 | |
Tony | 2ca999c | 2018-09-24 17:24:51 -0400 | [diff] [blame] | 32 | public static final float DEFAULT_LONG_PRESS_TIMEOUT_FACTOR = 0.75f; |
Tony Wickham | 1bce7fd | 2016-04-28 17:39:03 -0700 | [diff] [blame] | 33 | |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 34 | private final View mView; |
| 35 | private final View.OnLongClickListener mListener; |
| 36 | private final float mSlop; |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 37 | |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 38 | private float mLongPressTimeoutFactor = DEFAULT_LONG_PRESS_TIMEOUT_FACTOR; |
| 39 | |
| 40 | private boolean mHasPerformedLongPress; |
Fengjiang Li | 9f90efb | 2022-12-01 16:53:17 -0800 | [diff] [blame] | 41 | private boolean mIsInMouseRightClick; |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 42 | |
| 43 | private Runnable mPendingCheckForLongPress; |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 44 | |
| 45 | public CheckLongPressHelper(View v) { |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 46 | this(v, null); |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Winson Chung | 6b27614 | 2015-05-13 15:44:26 -0700 | [diff] [blame] | 49 | public CheckLongPressHelper(View v, View.OnLongClickListener listener) { |
| 50 | mView = v; |
| 51 | mListener = listener; |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 52 | mSlop = ViewConfiguration.get(mView.getContext()).getScaledTouchSlop(); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Handles the touch event on a view |
| 57 | * |
| 58 | * @see View#onTouchEvent(MotionEvent) |
| 59 | */ |
| 60 | public void onTouchEvent(MotionEvent ev) { |
| 61 | switch (ev.getAction()) { |
| 62 | case MotionEvent.ACTION_DOWN: { |
| 63 | // Just in case the previous long press hasn't been cleared, we make sure to |
| 64 | // start fresh on touch down. |
| 65 | cancelLongPress(); |
| 66 | |
Fengjiang Li | 9f90efb | 2022-12-01 16:53:17 -0800 | [diff] [blame] | 67 | // Mouse right click should immediately trigger a long press |
Fengjiang Li | a6a67e3 | 2022-12-06 21:53:25 -0800 | [diff] [blame] | 68 | if (TouchUtil.isMouseRightClickDownOrMove(ev)) { |
Fengjiang Li | 9f90efb | 2022-12-01 16:53:17 -0800 | [diff] [blame] | 69 | mIsInMouseRightClick = true; |
| 70 | triggerLongPress(); |
| 71 | final Handler handler = mView.getHandler(); |
| 72 | if (handler != null) { |
| 73 | // Send an ACTION_UP to end this click gesture to avoid user dragging with |
| 74 | // mouse's right button. Note that we need to call |
| 75 | // {@link Handler#postAtFrontOfQueue()} instead of {@link View#post()} to |
| 76 | // make sure ACTION_UP is sent before any ACTION_MOVE if user is dragging. |
| 77 | final MotionEvent actionUpEvent = MotionEvent.obtain(ev); |
| 78 | actionUpEvent.setAction(MotionEvent.ACTION_UP); |
| 79 | handler.postAtFrontOfQueue(() -> { |
| 80 | mView.getRootView().dispatchTouchEvent(actionUpEvent); |
| 81 | actionUpEvent.recycle(); |
| 82 | }); |
| 83 | } |
| 84 | break; |
| 85 | } |
| 86 | |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 87 | postCheckForLongPress(); |
| 88 | if (isStylusButtonPressed(ev)) { |
| 89 | triggerLongPress(); |
| 90 | } |
| 91 | break; |
| 92 | } |
| 93 | case MotionEvent.ACTION_CANCEL: |
| 94 | case MotionEvent.ACTION_UP: |
| 95 | cancelLongPress(); |
| 96 | break; |
| 97 | case MotionEvent.ACTION_MOVE: |
Fengjiang Li | 9f90efb | 2022-12-01 16:53:17 -0800 | [diff] [blame] | 98 | if (mIsInMouseRightClick |
| 99 | || !Utilities.pointInView(mView, ev.getX(), ev.getY(), mSlop)) { |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 100 | cancelLongPress(); |
| 101 | } else if (mPendingCheckForLongPress != null && isStylusButtonPressed(ev)) { |
| 102 | // Only trigger long press if it has not been cancelled before |
| 103 | triggerLongPress(); |
| 104 | } |
| 105 | break; |
| 106 | } |
Winson Chung | 6b27614 | 2015-05-13 15:44:26 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Winson Chung | 7501adf | 2015-06-02 11:24:28 -0700 | [diff] [blame] | 109 | /** |
| 110 | * Overrides the default long press timeout. |
| 111 | */ |
Tony | 2ca999c | 2018-09-24 17:24:51 -0400 | [diff] [blame] | 112 | public void setLongPressTimeoutFactor(float longPressTimeoutFactor) { |
| 113 | mLongPressTimeoutFactor = longPressTimeoutFactor; |
Winson Chung | 7501adf | 2015-06-02 11:24:28 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 116 | private void postCheckForLongPress() { |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 117 | mHasPerformedLongPress = false; |
| 118 | |
| 119 | if (mPendingCheckForLongPress == null) { |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 120 | mPendingCheckForLongPress = this::triggerLongPress; |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 121 | } |
Tony | 2ca999c | 2018-09-24 17:24:51 -0400 | [diff] [blame] | 122 | mView.postDelayed(mPendingCheckForLongPress, |
| 123 | (long) (ViewConfiguration.getLongPressTimeout() * mLongPressTimeoutFactor)); |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 126 | /** |
Fengjiang Li | 9f90efb | 2022-12-01 16:53:17 -0800 | [diff] [blame] | 127 | * Cancels any pending long press and right click |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 128 | */ |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 129 | public void cancelLongPress() { |
Fengjiang Li | 9f90efb | 2022-12-01 16:53:17 -0800 | [diff] [blame] | 130 | mIsInMouseRightClick = false; |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 131 | mHasPerformedLongPress = false; |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 132 | clearCallbacks(); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Returns true if long press has been performed in the current touch gesture |
| 137 | */ |
| 138 | public boolean hasPerformedLongPress() { |
| 139 | return mHasPerformedLongPress; |
| 140 | } |
| 141 | |
| 142 | private void triggerLongPress() { |
Jon Miranda | a1567d5 | 2020-04-15 16:00:01 -0700 | [diff] [blame] | 143 | if ((mView.getParent() != null) |
| 144 | && mView.hasWindowFocus() |
Jon Miranda | 6fa6347 | 2021-01-25 15:30:26 -0500 | [diff] [blame] | 145 | && (!mView.isPressed() || mListener != null) |
Jon Miranda | a1567d5 | 2020-04-15 16:00:01 -0700 | [diff] [blame] | 146 | && !mHasPerformedLongPress) { |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 147 | boolean handled; |
| 148 | if (mListener != null) { |
| 149 | handled = mListener.onLongClick(mView); |
| 150 | } else { |
| 151 | handled = mView.performLongClick(); |
| 152 | } |
| 153 | if (handled) { |
| 154 | mView.setPressed(false); |
| 155 | mHasPerformedLongPress = true; |
| 156 | } |
| 157 | clearCallbacks(); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | private void clearCallbacks() { |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 162 | if (mPendingCheckForLongPress != null) { |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 163 | mView.removeCallbacks(mPendingCheckForLongPress); |
| 164 | mPendingCheckForLongPress = null; |
| 165 | } |
| 166 | } |
| 167 | |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 168 | |
| 169 | /** |
| 170 | * Identifies if the provided {@link MotionEvent} is a stylus with the primary stylus button |
| 171 | * pressed. |
| 172 | * |
| 173 | * @param event The event to check. |
| 174 | * @return Whether a stylus button press occurred. |
| 175 | */ |
| 176 | private static boolean isStylusButtonPressed(MotionEvent event) { |
| 177 | return event.getToolType(0) == MotionEvent.TOOL_TYPE_STYLUS |
| 178 | && event.isButtonPressed(MotionEvent.BUTTON_SECONDARY); |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 179 | } |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 180 | } |