private void clearTouchTarget() { mTouchTarget = null; if (mDownEvent != null) { mDownEvent.recycle(); mDownEvent = null; } }
public void cancelLongPressDetection() { removeMessages(MSG_LONGPRESS); if (mDownMotionEvent != null) { mDownMotionEvent.recycle(); mDownMotionEvent = null; } }
public void cancelLongPressDetection() { removeMessages(MSG_LONGPRESS); if (mDownMotionEvent != null) { mDownMotionEvent.recycle(); mDownMotionEvent = null; } }
/** * Scale detector is a little buggy when first time scale is occurred. * So we will feed it with fake motion event to warm it up. */ private void warmUpScaleDetector() { long time = System.currentTimeMillis(); MotionEvent event = MotionEvent.obtain(time, time, MotionEvent.ACTION_CANCEL, 0f, 0f, 0); onTouchEvent(event); event.recycle(); }
@Override public boolean dispatchTouchEvent(@NonNull MotionEvent event) { currentMotionEvent = event; // We should remap given event back to original coordinates // so that children can correctly respond to it MotionEvent invertedEvent = applyMatrix(event, matrixInverse); try { return super.dispatchTouchEvent(invertedEvent); } finally { invertedEvent.recycle(); } }
private void onLongPress() { clearCallbacks(); final View src = mSrc; if (!src.isEnabled()) { return; } if (!onForwardingStarted()) { return; } // Don't let the parent intercept our events. mSrc.getParent().requestDisallowInterceptTouchEvent(true); // Make sure we cancel any ongoing source event stream. final long now = SystemClock.uptimeMillis(); final MotionEvent e = MotionEvent.obtain(now, now, MotionEvent.ACTION_CANCEL, 0, 0, 0); mSrc.onTouchEvent(e); e.recycle(); mForwarding = true; mWasLongPress = true; }
final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0); mVelocityTracker.addMovement(ev); ev.recycle(); mFakeDragBeginTime = time; return true;
@Override public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { super.requestDisallowInterceptTouchEvent(disallowIntercept); if (disallowIntercept) { // We should pass "cancel" touch event to make sure controller does not expect // any events anymore. MotionEvent cancel = MotionEvent.obtain(currentMotionEvent); cancel.setAction(MotionEvent.ACTION_CANCEL); controller.onInterceptTouch(this, cancel); cancel.recycle(); } }
/** * Start a fake drag of the pager. * * @return true if the fake drag began successfully, false if it could not be started. */ public boolean beginFakeDrag() { if (mIsBeingDragged) { return false; } mFakeDragging = true; setScrollState(SCROLL_STATE_DRAGGING); if (isHorizontal()) { mInitialMotionX = mLastMotionX = 0; } else { mInitialMotionY = mLastMotionY = 0; } if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } else { mVelocityTracker.clear(); } final long time = SystemClock.uptimeMillis(); final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0); mVelocityTracker.addMovement(ev); ev.recycle(); mFakeDragBeginTime = time; return true; }
final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0); mVelocityTracker.addMovement(ev); ev.recycle(); mFakeDragBeginTime = time; return true;
@Override protected boolean onTouchInternal(@NonNull View view, @NonNull MotionEvent event) { if (viewPager == null) { return super.onTouchInternal(view, event); } else { // Getting motion event in pager coordinates MotionEvent pagerEvent = MotionEvent.obtain(event); transformToPagerEvent(pagerEvent, view, viewPager); handleTouch(pagerEvent); boolean result = super.onTouchInternal(view, pagerEvent); pagerEvent.recycle(); return result; } }
@Test public void testRecycle() { motionEvent2.recycle(); try { motionEvent2.recycle(); fail("recycle() should throw an exception when the event has already been recycled."); } catch (RuntimeException ex) { // expected } motionEvent2 = null; // since it was recycled, don't try to recycle again in tear down }
@Override public boolean onTouch(View v, MotionEvent event) { final boolean wasForwarding = mForwarding; final boolean forwarding; if (wasForwarding) { if (mWasLongPress) { // If we started forwarding as a result of a long-press, // just silently stop forwarding events so that the window // stays open. forwarding = onTouchForwarded(event); } else { forwarding = onTouchForwarded(event) || !onForwardingStopped(); } } else { forwarding = onTouchObserved(event) && onForwardingStarted(); if (forwarding) { // Make sure we cancel any ongoing source event stream. final long now = SystemClock.uptimeMillis(); final MotionEvent e = MotionEvent.obtain(now, now, MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0); mSrc.onTouchEvent(e); e.recycle(); } } mForwarding = forwarding; return forwarding || wasForwarding; }
mLastMotionX, 0, 0); mVelocityTracker.addMovement(ev); ev.recycle();
fakeMotionEvent.recycle();
event.recycle(); mMatrix = mChart.getViewPortHandler().refresh(mMatrix, mChart, false);
trackedEvent.recycle(); break; case MotionEvent.ACTION_POINTER_DOWN:
private void passEventToViewPager(@NonNull MotionEvent event) { if (viewPager == null) { return; } MotionEvent fixedEvent = obtainOnePointerEvent(event); fixedEvent.setLocation(lastViewPagerEventX, 0f); if (isViewPagerInterceptedScroll) { viewPager.onTouchEvent(fixedEvent); } else { isViewPagerInterceptedScroll = viewPager.onInterceptTouchEvent(fixedEvent); } // If ViewPager intercepted touch it will settle itself automatically, // but if touch was not intercepted we should settle it manually if (!isViewPagerInterceptedScroll && hasViewPagerX()) { settleViewPagerIfFinished(viewPager, event); } // Hack: ViewPager has bug when endFakeDrag() does not work properly. But we need to ensure // ViewPager is not in fake drag mode after settleViewPagerIfFinished() try { if (viewPager != null && viewPager.isFakeDragging()) { viewPager.endFakeDrag(); } } catch (Exception ignored) { } fixedEvent.recycle(); }
/** * Handled forwarded motion events and determines when to stop forwarding. * * @param srcEvent motion event in source view coordinates * @return true to continue forwarding motion events, false to cancel */ private boolean onTouchForwarded(MotionEvent srcEvent) { final View src = mSrc; final ListPopupWindow popup = getPopup(); if (popup == null || !popup.isShowing()) { return false; } final DropDownListView dst = popup.mDropDownList; if (dst == null || !dst.isShown()) { return false; } // Convert event to destination-local coordinates. final MotionEvent dstEvent = MotionEvent.obtainNoHistory(srcEvent); toGlobalMotionEvent(src, dstEvent); toLocalMotionEvent(dst, dstEvent); // Forward converted event to destination view, then recycle it. final boolean handled = dst.onForwardedEvent(dstEvent, mActivePointerId); dstEvent.recycle(); // Always cancel forwarding when the touch stream ends. final int action = MotionEventCompat.getActionMasked(srcEvent); final boolean keepForwarding = action != MotionEvent.ACTION_UP && action != MotionEvent.ACTION_CANCEL; return handled && keepForwarding; }