blob: 483c62249e379ad3c17833e91c8610ebbb14786c [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
Adam Cohen091440a2015-03-18 14:16:05 -070025 @Thunk View mView;
Winson Chung6b276142015-05-13 15:44:26 -070026 @Thunk View.OnLongClickListener mListener;
Adam Cohen091440a2015-03-18 14:16:05 -070027 @Thunk boolean mHasPerformedLongPress;
Winson Chung7501adf2015-06-02 11:24:28 -070028 private int mLongPressTimeout = 300;
Winson Chung88f33452012-02-23 15:23:44 -080029 private CheckForLongPress mPendingCheckForLongPress;
30
31 class CheckForLongPress implements Runnable {
32 public void run() {
33 if ((mView.getParent() != null) && mView.hasWindowFocus()
34 && !mHasPerformedLongPress) {
Winson Chung6b276142015-05-13 15:44:26 -070035 boolean handled;
36 if (mListener != null) {
37 handled = mListener.onLongClick(mView);
38 } else {
39 handled = mView.performLongClick();
40 }
41 if (handled) {
Winson Chung401d3a52012-03-08 12:10:40 -080042 mView.setPressed(false);
Winson Chung88f33452012-02-23 15:23:44 -080043 mHasPerformedLongPress = true;
44 }
45 }
46 }
47 }
48
49 public CheckLongPressHelper(View v) {
50 mView = v;
51 }
52
Winson Chung6b276142015-05-13 15:44:26 -070053 public CheckLongPressHelper(View v, View.OnLongClickListener listener) {
54 mView = v;
55 mListener = listener;
56 }
57
Winson Chung7501adf2015-06-02 11:24:28 -070058 /**
59 * Overrides the default long press timeout.
60 */
61 public void setLongPressTimeout(int longPressTimeout) {
62 mLongPressTimeout = longPressTimeout;
63 }
64
Winson Chung88f33452012-02-23 15:23:44 -080065 public void postCheckForLongPress() {
66 mHasPerformedLongPress = false;
67
68 if (mPendingCheckForLongPress == null) {
69 mPendingCheckForLongPress = new CheckForLongPress();
70 }
Winson Chung7501adf2015-06-02 11:24:28 -070071 mView.postDelayed(mPendingCheckForLongPress, mLongPressTimeout);
Winson Chung88f33452012-02-23 15:23:44 -080072 }
73
74 public void cancelLongPress() {
75 mHasPerformedLongPress = false;
76 if (mPendingCheckForLongPress != null) {
77 mView.removeCallbacks(mPendingCheckForLongPress);
78 mPendingCheckForLongPress = null;
79 }
80 }
81
82 public boolean hasPerformedLongPress() {
83 return mHasPerformedLongPress;
84 }
85}