blob: 639c173dcc0e6928b49867dd6d1972084428e389 [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;
Tony2ca999c2018-09-24 17:24:51 -040020import android.view.ViewConfiguration;
Winson Chung88f33452012-02-23 15:23:44 -080021
Adam Cohen091440a2015-03-18 14:16:05 -070022import com.android.launcher3.util.Thunk;
23
Winson Chung88f33452012-02-23 15:23:44 -080024public class CheckLongPressHelper {
Winson Chung7501adf2015-06-02 11:24:28 -070025
Tony2ca999c2018-09-24 17:24:51 -040026 public static final float DEFAULT_LONG_PRESS_TIMEOUT_FACTOR = 0.75f;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070027
Adam Cohen091440a2015-03-18 14:16:05 -070028 @Thunk View mView;
Winson Chung6b276142015-05-13 15:44:26 -070029 @Thunk View.OnLongClickListener mListener;
Adam Cohen091440a2015-03-18 14:16:05 -070030 @Thunk boolean mHasPerformedLongPress;
Tony2ca999c2018-09-24 17:24:51 -040031 private float mLongPressTimeoutFactor = DEFAULT_LONG_PRESS_TIMEOUT_FACTOR;
Winson Chung88f33452012-02-23 15:23:44 -080032 private CheckForLongPress mPendingCheckForLongPress;
33
34 class CheckForLongPress implements Runnable {
35 public void run() {
36 if ((mView.getParent() != null) && mView.hasWindowFocus()
37 && !mHasPerformedLongPress) {
Winson Chung6b276142015-05-13 15:44:26 -070038 boolean handled;
39 if (mListener != null) {
40 handled = mListener.onLongClick(mView);
41 } else {
42 handled = mView.performLongClick();
43 }
44 if (handled) {
Winson Chung401d3a52012-03-08 12:10:40 -080045 mView.setPressed(false);
Winson Chung88f33452012-02-23 15:23:44 -080046 mHasPerformedLongPress = true;
47 }
48 }
49 }
50 }
51
52 public CheckLongPressHelper(View v) {
53 mView = v;
54 }
55
Winson Chung6b276142015-05-13 15:44:26 -070056 public CheckLongPressHelper(View v, View.OnLongClickListener listener) {
57 mView = v;
58 mListener = listener;
59 }
60
Winson Chung7501adf2015-06-02 11:24:28 -070061 /**
62 * Overrides the default long press timeout.
63 */
Tony2ca999c2018-09-24 17:24:51 -040064 public void setLongPressTimeoutFactor(float longPressTimeoutFactor) {
65 mLongPressTimeoutFactor = longPressTimeoutFactor;
Winson Chung7501adf2015-06-02 11:24:28 -070066 }
67
Winson Chung88f33452012-02-23 15:23:44 -080068 public void postCheckForLongPress() {
69 mHasPerformedLongPress = false;
70
71 if (mPendingCheckForLongPress == null) {
72 mPendingCheckForLongPress = new CheckForLongPress();
73 }
Tony2ca999c2018-09-24 17:24:51 -040074 mView.postDelayed(mPendingCheckForLongPress,
75 (long) (ViewConfiguration.getLongPressTimeout() * mLongPressTimeoutFactor));
Winson Chung88f33452012-02-23 15:23:44 -080076 }
77
78 public void cancelLongPress() {
79 mHasPerformedLongPress = false;
80 if (mPendingCheckForLongPress != null) {
Winson Chung88f33452012-02-23 15:23:44 -080081 mView.removeCallbacks(mPendingCheckForLongPress);
82 mPendingCheckForLongPress = null;
83 }
84 }
85
86 public boolean hasPerformedLongPress() {
87 return mHasPerformedLongPress;
88 }
89}