Java Code Examples for android.view.MotionEvent#obtainNoHistory()

The following examples show how to use android.view.MotionEvent#obtainNoHistory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TouchInterceptionFrameLayout.java    From droidddle with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (mTouchInterceptionListener == null) {
        return super.onInterceptTouchEvent(ev);
    }
    switch (ev.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
            mInitialY = ev.getY();
            mPendingDownMotionEvent = MotionEvent.obtainNoHistory(ev);
            mBeganFromDownMotionEvent = true;
            mDownMotionEventPended = true;
            mIntercepting = mTouchInterceptionListener.shouldInterceptTouchEvent(ev, false, 0);
            return mIntercepting;
    }
    return super.onInterceptTouchEvent(ev);
}
 
Example 2
Source File: HeadsUpNotificationView.java    From HeadsUp with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
    // Do not let notification to be timed-out while
    // we are touching it.
    handleTimeout(event);

    // Translate touch event too to correspond with
    // view's translation changes and prevent lags
    // while swiping.
    final MotionEvent ev = MotionEvent.obtainNoHistory(event);
    ev.offsetLocation(getTranslationX(), getTranslationY());
    boolean handled = mSwipeHelperX.onTouchEvent(ev);
    ev.recycle();

    return handled || super.onTouchEvent(event);
}
 
Example 3
Source File: WallpaperService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void onInputEvent(InputEvent event, int displayId) {
    boolean handled = false;
    try {
        if (event instanceof MotionEvent
                && (event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
            MotionEvent dup = MotionEvent.obtainNoHistory((MotionEvent)event);
            dispatchPointer(dup);
            handled = true;
        }
    } finally {
        finishInputEvent(event, handled);
    }
}
 
Example 4
Source File: ForwardingListener.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Handles 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 ShowableListMenu popup = getPopup();
    if (popup == null || !popup.isShowing()) {
        return false;
    }

    final DropDownListView dst = (DropDownListView) popup.getListView();
    if (dst == null || !dst.isShown()) {
        return false;
    }

    // Convert event to destination-local coordinates.
    final MotionEvent dstEvent = MotionEvent.obtainNoHistory(srcEvent);
    src.toGlobalMotionEvent(dstEvent);
    dst.toLocalMotionEvent(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 = srcEvent.getActionMasked();
    final boolean keepForwarding = action != MotionEvent.ACTION_UP
            && action != MotionEvent.ACTION_CANCEL;

    return handled && keepForwarding;
}
 
Example 5
Source File: ListPopupWindow.java    From MDPreference with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: TouchInterceptionLayout.java    From UltimateRecyclerView with Apache License 2.0 5 votes vote down vote up
/**
 * Duplicate touch events to child views.
 * We want to dispatch a down motion event and the move events to
 * child views, but calling dispatchTouchEvent() causes StackOverflowError.
 * Therefore we do it manually.
 *
 * @param ev            motion event to be passed to children
 * @param pendingEvents pending events like ACTION_DOWN. This will be passed to the children before ev
 */
private void duplicateTouchEventForChildren(MotionEvent ev, MotionEvent... pendingEvents) {
    if (ev == null) {
        return;
    }
    for (int i = getChildCount() - 1; 0 <= i; i--) {
        View childView = getChildAt(i);
        if (childView != null) {
            Rect childRect = new Rect();
            childView.getHitRect(childRect);
            MotionEvent event = MotionEvent.obtainNoHistory(ev);
            if (!childRect.contains((int) event.getX(), (int) event.getY())) {
                continue;
            }
            float offsetX = -childView.getLeft();
            float offsetY = -childView.getTop();
            boolean consumed = false;
            if (pendingEvents != null) {
                for (MotionEvent pe : pendingEvents) {
                    if (pe != null) {
                        MotionEvent peAdjusted = MotionEvent.obtainNoHistory(pe);
                        peAdjusted.offsetLocation(offsetX, offsetY);
                        consumed |= childView.dispatchTouchEvent(peAdjusted);
                    }
                }
            }
            event.offsetLocation(offsetX, offsetY);
            consumed |= childView.dispatchTouchEvent(event);
            if (consumed) {
                break;
            }
        }
    }
}
 
Example 7
Source File: ListPopupWindow.java    From material with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 8
Source File: AcDisplayFragment.java    From AcDisplay with GNU General Public License v2.0 5 votes vote down vote up
private void populateStdMotion(@NonNull MotionEvent srcEvent) {
    // Track current movement to be able to handle
    // flings correctly.
    MotionEvent dstEvent = MotionEvent.obtainNoHistory(srcEvent);
    mVelocityTracker.addMovement(MotionEvent.obtainNoHistory(srcEvent));
    dstEvent.recycle();

    // No need to handle swipe-to-dismiss if the
    // widget is not dismissible.
    if (!isDismissible(mSelectedWidget)) {
        return;
    }

    final float y = srcEvent.getY() - mIconsContainer.getHeight();

    if (y <= 0) {
        if (mSceneContainer.getTranslationY() != 0) {
            resetSceneContainerParams();
        }
        return;
    }

    // Populate current animation
    float height = getSceneView().getHeight();
    float progress = MathUtils.range(y / height, 0f, 1f);
    populateStdAnimation(progress);
}
 
Example 9
Source File: FlexibleSpaceWithImageWithViewPagerTab2Activity.java    From Android-ObservableScrollView with Apache License 2.0 5 votes vote down vote up
@Override
public void onMoveMotionEvent(MotionEvent ev, float diffX, float diffY) {
    int flexibleSpace = mFlexibleSpaceHeight - mTabHeight;
    float translationY = ScrollUtils.getFloat(ViewHelper.getTranslationY(mInterceptionLayout) + diffY, -flexibleSpace, 0);
    MotionEvent e = MotionEvent.obtainNoHistory(ev);
    e.offsetLocation(0, translationY - mBaseTranslationY);
    mVelocityTracker.addMovement(e);
    updateFlexibleSpace(translationY);
}
 
Example 10
Source File: TouchInterceptionFrameLayout.java    From Android-ObservableScrollView with Apache License 2.0 5 votes vote down vote up
/**
 * Duplicate touch events to child views.
 * We want to dispatch a down motion event and the move events to
 * child views, but calling dispatchTouchEvent() causes StackOverflowError.
 * Therefore we do it manually.
 *
 * @param ev            Motion event to be passed to children.
 * @param pendingEvents Pending events like ACTION_DOWN. This will be passed to the children before ev.
 */
private void duplicateTouchEventForChildren(MotionEvent ev, MotionEvent... pendingEvents) {
    if (ev == null) {
        return;
    }
    for (int i = getChildCount() - 1; 0 <= i; i--) {
        View childView = getChildAt(i);
        if (childView != null) {
            Rect childRect = new Rect();
            childView.getHitRect(childRect);
            MotionEvent event = MotionEvent.obtainNoHistory(ev);
            if (!childRect.contains((int) event.getX(), (int) event.getY())) {
                continue;
            }
            float offsetX = -childView.getLeft();
            float offsetY = -childView.getTop();
            boolean consumed = false;
            if (pendingEvents != null) {
                for (MotionEvent pe : pendingEvents) {
                    if (pe != null) {
                        MotionEvent peAdjusted = MotionEvent.obtainNoHistory(pe);
                        peAdjusted.offsetLocation(offsetX, offsetY);
                        consumed |= childView.dispatchTouchEvent(peAdjusted);
                    }
                }
            }
            event.offsetLocation(offsetX, offsetY);
            consumed |= childView.dispatchTouchEvent(event);
            if (consumed) {
                break;
            }
        }
    }
}
 
Example 11
Source File: TouchInterceptionFrameLayout.java    From WayHoo with Apache License 2.0 5 votes vote down vote up
/**
 * Duplicate touch events to child views.
 * We want to dispatch a down motion event and the move events to
 * child views, but calling dispatchTouchEvent() causes StackOverflowError.
 * Therefore we do it manually.
 *
 * @param ev            motion event to be passed to children
 * @param pendingEvents pending events like ACTION_DOWN. This will be passed to the children before ev
 */
private void duplicateTouchEventForChildren(MotionEvent ev, MotionEvent... pendingEvents) {
    if (ev == null) {
        return;
    }
    for (int i = getChildCount() - 1; 0 <= i; i--) {
        View childView = getChildAt(i);
        if (childView != null) {
            Rect childRect = new Rect();
            childView.getHitRect(childRect);
            MotionEvent event = MotionEvent.obtainNoHistory(ev);
            if (!childRect.contains((int) event.getX(), (int) event.getY())) {
                continue;
            }
            float offsetX = -childView.getLeft();
            float offsetY = -childView.getTop();
            boolean consumed = false;
            if (pendingEvents != null) {
                for (MotionEvent pe : pendingEvents) {
                    if (pe != null) {
                        MotionEvent peAdjusted = MotionEvent.obtainNoHistory(pe);
                        peAdjusted.offsetLocation(offsetX, offsetY);
                        consumed |= childView.dispatchTouchEvent(peAdjusted);
                    }
                }
            }
            event.offsetLocation(offsetX, offsetY);
            consumed |= childView.dispatchTouchEvent(event);
            if (consumed) {
                break;
            }
        }
    }
}
 
Example 12
Source File: TouchInterceptionLayout.java    From UltimateRecyclerView with Apache License 2.0 4 votes vote down vote up
private MotionEvent obtainMotionEvent(MotionEvent base, int action) {
    MotionEvent ev = MotionEvent.obtainNoHistory(base);
    ev.setAction(action);
    return ev;
}
 
Example 13
Source File: OverviewStackViewTouchHandler.java    From StackOverView with MIT License 4 votes vote down vote up
/** Constructs a simulated motion event for the current stack scroll. */
MotionEvent createMotionEventForStackScroll(MotionEvent ev) {
    MotionEvent pev = MotionEvent.obtainNoHistory(ev);
    pev.setLocation(0, mScroller.progressToScrollRange(mScroller.getStackScroll()));
    return pev;
}
 
Example 14
Source File: TouchInterceptionFrameLayout.java    From Android-ObservableScrollView with Apache License 2.0 4 votes vote down vote up
private MotionEvent obtainMotionEvent(MotionEvent base, int action) {
    MotionEvent ev = MotionEvent.obtainNoHistory(base);
    ev.setAction(action);
    return ev;
}
 
Example 15
Source File: TouchInterceptionFrameLayout.java    From WayHoo with Apache License 2.0 4 votes vote down vote up
private MotionEvent obtainMotionEvent(MotionEvent base, int action) {
    MotionEvent ev = MotionEvent.obtainNoHistory(base);
    ev.setAction(action);
    return ev;
}