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 21 import com.android.launcher3.util.Thunk; 22 23 public class CheckLongPressHelper { 24 25 @Thunk View mView; 26 @Thunk View.OnLongClickListener mListener; 27 @Thunk boolean mHasPerformedLongPress; 28 private int mLongPressTimeout = 300; 29 private CheckForLongPress mPendingCheckForLongPress; 30 31 class CheckForLongPress implements Runnable { 32 public void run() { 33 if ((mView.getParent() != null) && mView.hasWindowFocus() 34 && !mHasPerformedLongPress) { 35 boolean handled; 36 if (mListener != null) { 37 handled = mListener.onLongClick(mView); 38 } else { 39 handled = mView.performLongClick(); 40 } 41 if (handled) { 42 mView.setPressed(false); 43 mHasPerformedLongPress = true; 44 } 45 } 46 } 47 } 48 49 public CheckLongPressHelper(View v) { 50 mView = v; 51 } 52 53 public CheckLongPressHelper(View v, View.OnLongClickListener listener) { 54 mView = v; 55 mListener = listener; 56 } 57 58 /** 59 * Overrides the default long press timeout. 60 */ 61 public void setLongPressTimeout(int longPressTimeout) { 62 mLongPressTimeout = longPressTimeout; 63 } 64 65 public void postCheckForLongPress() { 66 mHasPerformedLongPress = false; 67 68 if (mPendingCheckForLongPress == null) { 69 mPendingCheckForLongPress = new CheckForLongPress(); 70 } 71 mView.postDelayed(mPendingCheckForLongPress, mLongPressTimeout); 72 } 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 } 86