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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 18 | |
| 19 | import android.view.View; |
| 20 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 21 | import com.android.launcher3.util.Thunk; |
| 22 | |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 23 | public class CheckLongPressHelper { |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 24 | @Thunk View mView; |
Winson Chung | 6b27614 | 2015-05-13 15:44:26 -0700 | [diff] [blame^] | 25 | @Thunk View.OnLongClickListener mListener; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 26 | @Thunk boolean mHasPerformedLongPress; |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 27 | private CheckForLongPress mPendingCheckForLongPress; |
| 28 | |
| 29 | class CheckForLongPress implements Runnable { |
| 30 | public void run() { |
| 31 | if ((mView.getParent() != null) && mView.hasWindowFocus() |
| 32 | && !mHasPerformedLongPress) { |
Winson Chung | 6b27614 | 2015-05-13 15:44:26 -0700 | [diff] [blame^] | 33 | boolean handled; |
| 34 | if (mListener != null) { |
| 35 | handled = mListener.onLongClick(mView); |
| 36 | } else { |
| 37 | handled = mView.performLongClick(); |
| 38 | } |
| 39 | if (handled) { |
Winson Chung | 401d3a5 | 2012-03-08 12:10:40 -0800 | [diff] [blame] | 40 | mView.setPressed(false); |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 41 | mHasPerformedLongPress = true; |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public CheckLongPressHelper(View v) { |
| 48 | mView = v; |
| 49 | } |
| 50 | |
Winson Chung | 6b27614 | 2015-05-13 15:44:26 -0700 | [diff] [blame^] | 51 | public CheckLongPressHelper(View v, View.OnLongClickListener listener) { |
| 52 | mView = v; |
| 53 | mListener = listener; |
| 54 | } |
| 55 | |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 56 | public void postCheckForLongPress() { |
| 57 | mHasPerformedLongPress = false; |
| 58 | |
| 59 | if (mPendingCheckForLongPress == null) { |
| 60 | mPendingCheckForLongPress = new CheckForLongPress(); |
| 61 | } |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 62 | mView.postDelayed(mPendingCheckForLongPress, |
| 63 | LauncherAppState.getInstance().getLongPressTimeout()); |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | public void cancelLongPress() { |
| 67 | mHasPerformedLongPress = false; |
| 68 | if (mPendingCheckForLongPress != null) { |
| 69 | mView.removeCallbacks(mPendingCheckForLongPress); |
| 70 | mPendingCheckForLongPress = null; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | public boolean hasPerformedLongPress() { |
| 75 | return mHasPerformedLongPress; |
| 76 | } |
| 77 | } |