blob: 52e7cd547efc5df8267f66f3732556a19887ddb0 [file] [log] [blame]
Jim Miller19a52672012-10-23 19:52:04 -07001/*
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 Miller5ecd8112013-01-09 18:50:26 -080017package com.android.keyguard;
Jim Miller19a52672012-10-23 19:52:04 -070018
19import android.view.MotionEvent;
20import android.view.View;
21import android.view.ViewConfiguration;
22
23public 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 Cohene3643132012-10-28 18:29:17 -070063 boolean xMoved = Math.abs(mDownX - x) > mScaledTouchSlop;
64 boolean yMoved = Math.abs(mDownY - y) > mScaledTouchSlop;
Jim Miller19a52672012-10-23 19:52:04 -070065
Adam Cohene3643132012-10-28 18:29:17 -070066 if (xMoved || yMoved) {
Jim Miller19a52672012-10-23 19:52:04 -070067 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 Cohene3643132012-10-28 18:29:17 -070082}