blob: dde733cd16ec76e2a8f06d5bba4d4adc474af8d8 [file] [log] [blame]
Winson Chung88f33452012-02-23 15:23:44 -08001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung88f33452012-02-23 15:23:44 -080018
19import android.view.View;
20
Adam Cohen091440a2015-03-18 14:16:05 -070021import com.android.launcher3.util.Thunk;
22
Winson Chung88f33452012-02-23 15:23:44 -080023public class CheckLongPressHelper {
Winson Chung7501adf2015-06-02 11:24:28 -070024
Tony Wickham1bce7fd2016-04-28 17:39:03 -070025 public static final int DEFAULT_LONG_PRESS_TIMEOUT = 300;
26
Adam Cohen091440a2015-03-18 14:16:05 -070027 @Thunk View mView;
Winson Chung6b276142015-05-13 15:44:26 -070028 @Thunk View.OnLongClickListener mListener;
Adam Cohen091440a2015-03-18 14:16:05 -070029 @Thunk boolean mHasPerformedLongPress;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070030 private int mLongPressTimeout = DEFAULT_LONG_PRESS_TIMEOUT;
Winson Chung88f33452012-02-23 15:23:44 -080031 private CheckForLongPress mPendingCheckForLongPress;
32
33 class CheckForLongPress implements Runnable {
34 public void run() {
35 if ((mView.getParent() != null) && mView.hasWindowFocus()
36 && !mHasPerformedLongPress) {
Winson Chung6b276142015-05-13 15:44:26 -070037 boolean handled;
38 if (mListener != null) {
39 handled = mListener.onLongClick(mView);
40 } else {
41 handled = mView.performLongClick();
42 }
43 if (handled) {
Winson Chung401d3a52012-03-08 12:10:40 -080044 mView.setPressed(false);
Winson Chung88f33452012-02-23 15:23:44 -080045 mHasPerformedLongPress = true;
46 }
47 }
48 }
49 }
50
51 public CheckLongPressHelper(View v) {
52 mView = v;
53 }
54
Winson Chung6b276142015-05-13 15:44:26 -070055 public CheckLongPressHelper(View v, View.OnLongClickListener listener) {
56 mView = v;
57 mListener = listener;
58 }
59
Winson Chung7501adf2015-06-02 11:24:28 -070060 /**
61 * Overrides the default long press timeout.
62 */
63 public void setLongPressTimeout(int longPressTimeout) {
64 mLongPressTimeout = longPressTimeout;
65 }
66
Winson Chung88f33452012-02-23 15:23:44 -080067 public void postCheckForLongPress() {
68 mHasPerformedLongPress = false;
69
70 if (mPendingCheckForLongPress == null) {
71 mPendingCheckForLongPress = new CheckForLongPress();
72 }
Winson Chung7501adf2015-06-02 11:24:28 -070073 mView.postDelayed(mPendingCheckForLongPress, mLongPressTimeout);
Winson Chung88f33452012-02-23 15:23:44 -080074 }
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}