blob: 10ca6a371aa6a049ed683b9d1e6d95718a2864a7 [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;
25 @Thunk boolean mHasPerformedLongPress;
Winson Chung88f33452012-02-23 15:23:44 -080026 private CheckForLongPress mPendingCheckForLongPress;
27
28 class CheckForLongPress implements Runnable {
29 public void run() {
30 if ((mView.getParent() != null) && mView.hasWindowFocus()
31 && !mHasPerformedLongPress) {
32 if (mView.performLongClick()) {
Winson Chung401d3a52012-03-08 12:10:40 -080033 mView.setPressed(false);
Winson Chung88f33452012-02-23 15:23:44 -080034 mHasPerformedLongPress = true;
35 }
36 }
37 }
38 }
39
40 public CheckLongPressHelper(View v) {
41 mView = v;
42 }
43
44 public void postCheckForLongPress() {
45 mHasPerformedLongPress = false;
46
47 if (mPendingCheckForLongPress == null) {
48 mPendingCheckForLongPress = new CheckForLongPress();
49 }
Daniel Sandlere4f98912013-06-25 15:13:26 -040050 mView.postDelayed(mPendingCheckForLongPress,
51 LauncherAppState.getInstance().getLongPressTimeout());
Winson Chung88f33452012-02-23 15:23:44 -080052 }
53
54 public void cancelLongPress() {
55 mHasPerformedLongPress = false;
56 if (mPendingCheckForLongPress != null) {
57 mView.removeCallbacks(mPendingCheckForLongPress);
58 mPendingCheckForLongPress = null;
59 }
60 }
61
62 public boolean hasPerformedLongPress() {
63 return mHasPerformedLongPress;
64 }
65}