@Override public boolean onTouchEventInternal(MotionEvent e) { final int action = e.getActionMasked(); // This path mimics the Android long press detection while still allowing // other touch events to come through the gesture detector. if (!mUseDefaultLongPress) { if (e.getPointerCount() > 1) { // If there's more than one pointer ignore the long press. if (mLongPressRunnable.isPending()) { cancelLongPress(); } } else if (action == MotionEvent.ACTION_DOWN) { // If there was a pending event kill it off. if (mLongPressRunnable.isPending()) { cancelLongPress(); } mLongPressRunnable.init(e); mLongPressHandler.postDelayed(mLongPressRunnable, mLongPressTimeoutMs); } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) { cancelLongPress(); } else if (mLongPressRunnable.isPending()) { // Allow for a little bit of touch slop. MotionEvent initialEvent = mLongPressRunnable.getInitialEvent(); float distanceX = initialEvent.getX() - e.getX(); float distanceY = initialEvent.getY() - e.getY(); float distance = distanceX * distanceX + distanceY * distanceY; // Save a square root here by comparing to squared touch slop if (distance > mScaledTouchSlop * mScaledTouchSlop) { cancelLongPress(); } } } // Sends the pinch event if two or more fingers touch the screen. According to test // Android handles the fingers order pretty consistently so always requesting // index 0 and 1 works here. // This might need some rework if 3 fingers event are supported. if (e.getPointerCount() > 1) { mHandler.onPinch( e.getX(0) * mPxToDp, e.getY(0) * mPxToDp, e.getX(1) * mPxToDp, e.getY(1) * mPxToDp, action == MotionEvent.ACTION_POINTER_DOWN); mDetector.setIsLongpressEnabled(false); mSingleInput = false; } else { mDetector.setIsLongpressEnabled(mUseDefaultLongPress); mSingleInput = true; } mDetector.onTouchEvent(e); // Propagate the up event after any gesture events. if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) { mHandler.onUpOrCancel(); } return true; }
private void longPress(MotionEvent e) { if (mHandler != null && mSingleInput) { mInLongPress = true; mHandler.onLongPress(e.getX() * mPxToDp, e.getY() * mPxToDp); } }