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 | |
| 19 | import android.view.View; |
Tony | 2ca999c | 2018-09-24 17:24:51 -0400 | [diff] [blame] | 20 | import android.view.ViewConfiguration; |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 21 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 22 | import com.android.launcher3.util.Thunk; |
| 23 | |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 24 | public class CheckLongPressHelper { |
Winson Chung | 7501adf | 2015-06-02 11:24:28 -0700 | [diff] [blame] | 25 | |
Tony | 2ca999c | 2018-09-24 17:24:51 -0400 | [diff] [blame] | 26 | public static final float DEFAULT_LONG_PRESS_TIMEOUT_FACTOR = 0.75f; |
Tony Wickham | 1bce7fd | 2016-04-28 17:39:03 -0700 | [diff] [blame] | 27 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 28 | @Thunk View mView; |
Winson Chung | 6b27614 | 2015-05-13 15:44:26 -0700 | [diff] [blame] | 29 | @Thunk View.OnLongClickListener mListener; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 30 | @Thunk boolean mHasPerformedLongPress; |
Tony | 2ca999c | 2018-09-24 17:24:51 -0400 | [diff] [blame] | 31 | private float mLongPressTimeoutFactor = DEFAULT_LONG_PRESS_TIMEOUT_FACTOR; |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 32 | private CheckForLongPress mPendingCheckForLongPress; |
| 33 | |
| 34 | class CheckForLongPress implements Runnable { |
| 35 | public void run() { |
| 36 | if ((mView.getParent() != null) && mView.hasWindowFocus() |
| 37 | && !mHasPerformedLongPress) { |
Winson Chung | 6b27614 | 2015-05-13 15:44:26 -0700 | [diff] [blame] | 38 | boolean handled; |
| 39 | if (mListener != null) { |
| 40 | handled = mListener.onLongClick(mView); |
| 41 | } else { |
| 42 | handled = mView.performLongClick(); |
| 43 | } |
| 44 | if (handled) { |
Winson Chung | 401d3a5 | 2012-03-08 12:10:40 -0800 | [diff] [blame] | 45 | mView.setPressed(false); |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 46 | mHasPerformedLongPress = true; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | public CheckLongPressHelper(View v) { |
| 53 | mView = v; |
| 54 | } |
| 55 | |
Winson Chung | 6b27614 | 2015-05-13 15:44:26 -0700 | [diff] [blame] | 56 | public CheckLongPressHelper(View v, View.OnLongClickListener listener) { |
| 57 | mView = v; |
| 58 | mListener = listener; |
| 59 | } |
| 60 | |
Winson Chung | 7501adf | 2015-06-02 11:24:28 -0700 | [diff] [blame] | 61 | /** |
| 62 | * Overrides the default long press timeout. |
| 63 | */ |
Tony | 2ca999c | 2018-09-24 17:24:51 -0400 | [diff] [blame] | 64 | public void setLongPressTimeoutFactor(float longPressTimeoutFactor) { |
| 65 | mLongPressTimeoutFactor = longPressTimeoutFactor; |
Winson Chung | 7501adf | 2015-06-02 11:24:28 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 68 | public void postCheckForLongPress() { |
| 69 | mHasPerformedLongPress = false; |
| 70 | |
| 71 | if (mPendingCheckForLongPress == null) { |
| 72 | mPendingCheckForLongPress = new CheckForLongPress(); |
| 73 | } |
Tony | 2ca999c | 2018-09-24 17:24:51 -0400 | [diff] [blame] | 74 | mView.postDelayed(mPendingCheckForLongPress, |
| 75 | (long) (ViewConfiguration.getLongPressTimeout() * mLongPressTimeoutFactor)); |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | public void cancelLongPress() { |
| 79 | mHasPerformedLongPress = false; |
| 80 | if (mPendingCheckForLongPress != null) { |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 81 | mView.removeCallbacks(mPendingCheckForLongPress); |
| 82 | mPendingCheckForLongPress = null; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | public boolean hasPerformedLongPress() { |
| 87 | return mHasPerformedLongPress; |
| 88 | } |
| 89 | } |