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.keyguard; 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 { run()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 CheckLongPressHelper(View v)43 public CheckLongPressHelper(View v) { 44 mScaledTouchSlop = ViewConfiguration.get(v.getContext()).getScaledTouchSlop(); 45 mLongPressTimeout = ViewConfiguration.getLongPressTimeout(); 46 mView = v; 47 } 48 postCheckForLongPress(MotionEvent ev)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 onMove(MotionEvent ev)60 public void onMove(MotionEvent ev) { 61 float x = ev.getX(); 62 float y = ev.getY(); 63 boolean xMoved = Math.abs(mDownX - x) > mScaledTouchSlop; 64 boolean yMoved = Math.abs(mDownY - y) > mScaledTouchSlop; 65 66 if (xMoved || yMoved) { 67 cancelLongPress(); 68 } 69 } 70 cancelLongPress()71 public void cancelLongPress() { 72 mHasPerformedLongPress = false; 73 if (mPendingCheckForLongPress != null) { 74 mView.removeCallbacks(mPendingCheckForLongPress); 75 mPendingCheckForLongPress = null; 76 } 77 } 78 hasPerformedLongPress()79 public boolean hasPerformedLongPress() { 80 return mHasPerformedLongPress; 81 } 82 } 83