• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.launcher3;
18 
19 import android.view.View;
20 import android.view.ViewConfiguration;
21 
22 import com.android.launcher3.util.Thunk;
23 
24 public class CheckLongPressHelper {
25 
26     public static final float DEFAULT_LONG_PRESS_TIMEOUT_FACTOR = 0.75f;
27 
28     @Thunk View mView;
29     @Thunk View.OnLongClickListener mListener;
30     @Thunk boolean mHasPerformedLongPress;
31     private float mLongPressTimeoutFactor = DEFAULT_LONG_PRESS_TIMEOUT_FACTOR;
32     private CheckForLongPress mPendingCheckForLongPress;
33 
34     class CheckForLongPress implements Runnable {
run()35         public void run() {
36             if ((mView.getParent() != null) && mView.hasWindowFocus()
37                     && !mHasPerformedLongPress) {
38                 boolean handled;
39                 if (mListener != null) {
40                     handled = mListener.onLongClick(mView);
41                 } else {
42                     handled = mView.performLongClick();
43                 }
44                 if (handled) {
45                     mView.setPressed(false);
46                     mHasPerformedLongPress = true;
47                 }
48             }
49         }
50     }
51 
CheckLongPressHelper(View v)52     public CheckLongPressHelper(View v) {
53         mView = v;
54     }
55 
CheckLongPressHelper(View v, View.OnLongClickListener listener)56     public CheckLongPressHelper(View v, View.OnLongClickListener listener) {
57         mView = v;
58         mListener = listener;
59     }
60 
61     /**
62      * Overrides the default long press timeout.
63      */
setLongPressTimeoutFactor(float longPressTimeoutFactor)64     public void setLongPressTimeoutFactor(float longPressTimeoutFactor) {
65         mLongPressTimeoutFactor = longPressTimeoutFactor;
66     }
67 
postCheckForLongPress()68     public void postCheckForLongPress() {
69         mHasPerformedLongPress = false;
70 
71         if (mPendingCheckForLongPress == null) {
72             mPendingCheckForLongPress = new CheckForLongPress();
73         }
74         mView.postDelayed(mPendingCheckForLongPress,
75                 (long) (ViewConfiguration.getLongPressTimeout() * mLongPressTimeoutFactor));
76     }
77 
cancelLongPress()78     public void cancelLongPress() {
79         mHasPerformedLongPress = false;
80         if (mPendingCheckForLongPress != null) {
81             mView.removeCallbacks(mPendingCheckForLongPress);
82             mPendingCheckForLongPress = null;
83         }
84     }
85 
hasPerformedLongPress()86     public boolean hasPerformedLongPress() {
87         return mHasPerformedLongPress;
88     }
89 }
90