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