Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [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 | |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 17 | package com.android.keyguard; |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 18 | |
| 19 | import android.view.MotionEvent; |
| 20 | import android.view.View; |
| 21 | import android.view.ViewConfiguration; |
| 22 | |
| 23 | public class CheckLongPressHelper { |
| 24 | private View mView; |
| 25 | private boolean mHasPerformedLongPress; |
| 26 | private CheckForLongPress mPendingCheckForLongPress; |
| 27 | private float mDownX, mDownY; |
| 28 | private int mLongPressTimeout; |
| 29 | private int mScaledTouchSlop; |
| 30 | |
| 31 | class CheckForLongPress implements Runnable { |
| 32 | public void run() { |
| 33 | if ((mView.getParent() != null) && mView.hasWindowFocus() |
| 34 | && !mHasPerformedLongPress) { |
| 35 | if (mView.performLongClick()) { |
| 36 | mView.setPressed(false); |
| 37 | mHasPerformedLongPress = true; |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | public CheckLongPressHelper(View v) { |
| 44 | mScaledTouchSlop = ViewConfiguration.get(v.getContext()).getScaledTouchSlop(); |
| 45 | mLongPressTimeout = ViewConfiguration.getLongPressTimeout(); |
| 46 | mView = v; |
| 47 | } |
| 48 | |
| 49 | public void postCheckForLongPress(MotionEvent ev) { |
| 50 | mDownX = ev.getX(); |
| 51 | mDownY = ev.getY(); |
| 52 | mHasPerformedLongPress = false; |
| 53 | |
| 54 | if (mPendingCheckForLongPress == null) { |
| 55 | mPendingCheckForLongPress = new CheckForLongPress(); |
| 56 | } |
| 57 | mView.postDelayed(mPendingCheckForLongPress, mLongPressTimeout); |
| 58 | } |
| 59 | |
| 60 | public void onMove(MotionEvent ev) { |
| 61 | float x = ev.getX(); |
| 62 | float y = ev.getY(); |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 63 | boolean xMoved = Math.abs(mDownX - x) > mScaledTouchSlop; |
| 64 | boolean yMoved = Math.abs(mDownY - y) > mScaledTouchSlop; |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 65 | |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 66 | if (xMoved || yMoved) { |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 67 | cancelLongPress(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | public void cancelLongPress() { |
| 72 | mHasPerformedLongPress = false; |
| 73 | if (mPendingCheckForLongPress != null) { |
| 74 | mView.removeCallbacks(mPendingCheckForLongPress); |
| 75 | mPendingCheckForLongPress = null; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | public boolean hasPerformedLongPress() { |
| 80 | return mHasPerformedLongPress; |
| 81 | } |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 82 | } |