Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame^] | 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.launcher2; |
| 18 | |
| 19 | import android.view.View; |
| 20 | |
| 21 | public class CheckLongPressHelper { |
| 22 | private View mView; |
| 23 | private boolean mHasPerformedLongPress; |
| 24 | private CheckForLongPress mPendingCheckForLongPress; |
| 25 | |
| 26 | class CheckForLongPress implements Runnable { |
| 27 | public void run() { |
| 28 | if ((mView.getParent() != null) && mView.hasWindowFocus() |
| 29 | && !mHasPerformedLongPress) { |
| 30 | if (mView.performLongClick()) { |
| 31 | mHasPerformedLongPress = true; |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | public CheckLongPressHelper(View v) { |
| 38 | mView = v; |
| 39 | } |
| 40 | |
| 41 | public void postCheckForLongPress() { |
| 42 | mHasPerformedLongPress = false; |
| 43 | |
| 44 | if (mPendingCheckForLongPress == null) { |
| 45 | mPendingCheckForLongPress = new CheckForLongPress(); |
| 46 | } |
| 47 | mView.postDelayed(mPendingCheckForLongPress, LauncherApplication.getLongPressTimeout()); |
| 48 | } |
| 49 | |
| 50 | public void cancelLongPress() { |
| 51 | mHasPerformedLongPress = false; |
| 52 | if (mPendingCheckForLongPress != null) { |
| 53 | mView.removeCallbacks(mPendingCheckForLongPress); |
| 54 | mPendingCheckForLongPress = null; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | public boolean hasPerformedLongPress() { |
| 59 | return mHasPerformedLongPress; |
| 60 | } |
| 61 | } |