blob: 381b678f91a165a151ba477abd4f33f07f70b07a [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 {
Adam Cohen091440a2015-03-18 14:16:05 -070024 @Thunk View mView;
Winson Chung6b276142015-05-13 15:44:26 -070025 @Thunk View.OnLongClickListener mListener;
Adam Cohen091440a2015-03-18 14:16:05 -070026 @Thunk boolean mHasPerformedLongPress;
Winson Chung88f33452012-02-23 15:23:44 -080027 private CheckForLongPress mPendingCheckForLongPress;
28
29 class CheckForLongPress implements Runnable {
30 public void run() {
31 if ((mView.getParent() != null) && mView.hasWindowFocus()
32 && !mHasPerformedLongPress) {
Winson Chung6b276142015-05-13 15:44:26 -070033 boolean handled;
34 if (mListener != null) {
35 handled = mListener.onLongClick(mView);
36 } else {
37 handled = mView.performLongClick();
38 }
39 if (handled) {
Winson Chung401d3a52012-03-08 12:10:40 -080040 mView.setPressed(false);
Winson Chung88f33452012-02-23 15:23:44 -080041 mHasPerformedLongPress = true;
42 }
43 }
44 }
45 }
46
47 public CheckLongPressHelper(View v) {
48 mView = v;
49 }
50
Winson Chung6b276142015-05-13 15:44:26 -070051 public CheckLongPressHelper(View v, View.OnLongClickListener listener) {
52 mView = v;
53 mListener = listener;
54 }
55
Winson Chung88f33452012-02-23 15:23:44 -080056 public void postCheckForLongPress() {
57 mHasPerformedLongPress = false;
58
59 if (mPendingCheckForLongPress == null) {
60 mPendingCheckForLongPress = new CheckForLongPress();
61 }
Daniel Sandlere4f98912013-06-25 15:13:26 -040062 mView.postDelayed(mPendingCheckForLongPress,
63 LauncherAppState.getInstance().getLongPressTimeout());
Winson Chung88f33452012-02-23 15:23:44 -080064 }
65
66 public void cancelLongPress() {
67 mHasPerformedLongPress = false;
68 if (mPendingCheckForLongPress != null) {
69 mView.removeCallbacks(mPendingCheckForLongPress);
70 mPendingCheckForLongPress = null;
71 }
72 }
73
74 public boolean hasPerformedLongPress() {
75 return mHasPerformedLongPress;
76 }
77}