Example usage for android.view MotionEvent ACTION_CANCEL

List of usage examples for android.view MotionEvent ACTION_CANCEL

Introduction

In this page you can find the example usage for android.view MotionEvent ACTION_CANCEL.

Prototype

int ACTION_CANCEL

To view the source code for android.view MotionEvent ACTION_CANCEL.

Click Source Link

Document

Constant for #getActionMasked : The current gesture has been aborted.

Usage

From source file:com.bcgtgjyb.huanwen.meizi.view.widget.MySwipeRefreshLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    ensureTarget();// w ww .  ja  v a  2s  . c  o m

    final int action = MotionEventCompat.getActionMasked(ev);

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;
    }

    //        if (mReturningToStart && action == MotionEvent.ACTION_UP) {
    //            mReturningToStart = false;
    //        }

    if ((!isEnabled() || mReturningToStart || canChildScrollUp() || mRefreshing)) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        Log.i(TAG, "onInterceptTouchEvent  " + mOriginalOffsetTop + "   " + mCircleView.getTop());
        setTargetOffsetTopAndBottom(mOriginalOffsetTop - mCircleView.getTop(), true);
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsBeingDragged = false;
        final float initialDownY = getMotionEventY(ev, mActivePointerId);
        if (initialDownY == -1) {
            return false;
        }
        mInitialDownY = initialDownY;
        break;

    case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER) {
            Log.e(LOG_TAG, "Got ACTION_MOVE event but don't have an active pointer id.");
            return false;
        }

        final float y = getMotionEventY(ev, mActivePointerId);
        if (y == -1) {
            return false;
        }
        final float yDiff = y - mInitialDownY;
        if (yDiff > mTouchSlop && !mIsBeingDragged) {
            mInitialMotionY = mInitialDownY + mTouchSlop;
            mIsBeingDragged = true;
            mProgress.setAlpha(STARTING_PROGRESS_ALPHA);
        }
        break;

    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;

    case MotionEvent.ACTION_UP:
        //guohuanwen
        //                Log.i(TAG, "onInterceptTouchEvent  "+mOriginalOffsetTop +"   "+ mCircleView.getTop());
        //                setTargetOffsetTopAndBottom(mOriginalOffsetTop - mCircleView.getTop(), true);
        //                mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        //                mIsBeingDragged = false;
        //                final float initialDownY2 = getMotionEventY(ev, mActivePointerId);
        //                if (initialDownY2 == -1) {
        //                    return false;
        //                }
        //                mInitialDownY = initialDownY2;
        //                break;
        //guohuanwen

    case MotionEvent.ACTION_CANCEL:
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        break;
    }

    return mIsBeingDragged;
}

From source file:com.comcast.freeflow.core.FreeFlowContainer.java

@Override
public boolean onTouchEvent(MotionEvent event) {

    super.onTouchEvent(event);
    if (layout == null)
        return false;

    boolean canScroll = false;

    if (layout.horizontalScrollEnabled() && this.layout.getContentWidth() > getWidth()) {
        canScroll = true;/*from   w ww  .ja v a2s  .c om*/
    }
    if (layout.verticalScrollEnabled() && layout.getContentHeight() > getHeight()) {
        canScroll = true;
    }

    if (mVelocityTracker == null && canScroll) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    if (mVelocityTracker != null) {
        mVelocityTracker.addMovement(event);
    }

    if (event.getAction() == MotionEvent.ACTION_DOWN) {

        if (mTouchMode == TOUCH_MODE_FLING) {
            // Wait for some time to see if the user is just trying
            // to speed up the scroll
            postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (mTouchMode == TOUCH_MODE_DOWN) {
                        if (mTouchMode == TOUCH_MODE_DOWN) {
                            scroller.forceFinished(true);
                        }
                    }
                }
            }, FLYWHEEL_TIMEOUT);
        }

        beginTouchAt = ViewUtils.getItemAt(frames, (int) (viewPortX + event.getX()),
                (int) (viewPortY + event.getY()));

        if (canScroll) {
            deltaX = event.getX();
            deltaY = event.getY();
        }
        mTouchMode = TOUCH_MODE_DOWN;

        if (mPendingCheckForTap != null) {
            removeCallbacks(mPendingCheckForTap);
            mPendingCheckForLongPress = null;
        }

        if (beginTouchAt != null) {
            mPendingCheckForTap = new CheckForTap();
        }
        postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());

        return true;

    } else if (event.getAction() == MotionEvent.ACTION_MOVE) {

        if (canScroll) {
            float xDiff = event.getX() - deltaX;
            float yDiff = event.getY() - deltaY;

            double distance = Math.sqrt(xDiff * xDiff + yDiff * yDiff);
            if ((mTouchMode == TOUCH_MODE_DOWN || mTouchMode == TOUCH_MODE_REST) && distance > touchSlop) {
                mTouchMode = TOUCH_MODE_SCROLL;

                if (mPendingCheckForTap != null) {
                    removeCallbacks(mPendingCheckForTap);
                    mPendingCheckForTap = null;
                }

            }

            if (mTouchMode == TOUCH_MODE_SCROLL) {
                moveViewportBy(event.getX() - deltaX, event.getY() - deltaY, false);
                invokeOnItemScrollListeners();
                deltaX = event.getX();
                deltaY = event.getY();
            }
        }

        return true;

    } else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
        mTouchMode = TOUCH_MODE_REST;

        if (canScroll) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }

        // requestLayout();

        return true;

    } else if (event.getAction() == MotionEvent.ACTION_UP) {

        if (mTouchMode == TOUCH_MODE_SCROLL) {
            mVelocityTracker.computeCurrentVelocity(1000, maxFlingVelocity);
            if (Math.abs(mVelocityTracker.getXVelocity()) > minFlingVelocity
                    || Math.abs(mVelocityTracker.getYVelocity()) > minFlingVelocity) {

                int maxX = layout.getContentWidth() - getWidth();
                int maxY = layout.getContentHeight() - getHeight();

                scroller.fling(viewPortX, viewPortY, -(int) mVelocityTracker.getXVelocity(),
                        -(int) mVelocityTracker.getYVelocity(), 0, maxX, 0, maxY, overflingDistance,
                        overflingDistance);

                mTouchMode = TOUCH_MODE_FLING;
                post(flingRunnable);

            } else {
                mTouchMode = TOUCH_MODE_REST;
            }

        } else if (mTouchMode == TOUCH_MODE_DOWN || mTouchMode == TOUCH_MODE_DONE_WAITING) {
            if (mTouchModeReset != null) {
                removeCallbacks(mTouchModeReset);
            }
            if (beginTouchAt != null && beginTouchAt.view != null) {
                beginTouchAt.view.setPressed(true);

                mTouchModeReset = new Runnable() {
                    @Override
                    public void run() {
                        mTouchModeReset = null;
                        mTouchMode = TOUCH_MODE_REST;
                        if (beginTouchAt != null && beginTouchAt.view != null) {
                            beginTouchAt.view.setPressed(false);
                        }
                        if (mChoiceActionMode == null && mOnItemSelectedListener != null) {
                            mOnItemSelectedListener.onItemSelected(FreeFlowContainer.this,
                                    selectedFreeFlowItem);
                        }

                        // setPressed(false);
                        // if (!mDataChanged) {
                        mPerformClick = new PerformClick();
                        mPerformClick.run();
                        // }
                    }
                };
                selectedFreeFlowItem = beginTouchAt;
                postDelayed(mTouchModeReset, ViewConfiguration.getPressedStateDuration());

                mTouchMode = TOUCH_MODE_TAP;
            } else {
                mTouchMode = TOUCH_MODE_REST;
            }

        }

        return true;
    }

    return false;

}

From source file:com.dishes.views.stageredggridview.StaggeredGridView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (mFastScroller != null) {
        boolean intercepted = mFastScroller.onTouchEvent(ev);
        if (intercepted) {
            return true;
        }//from  w w  w  .ja va2  s .c o  m
    }
    mVelocityTracker.addMovement(ev);
    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;

    int motionPosition = pointToPosition((int) ev.getX(), (int) ev.getY());

    switch (action) {
    case MotionEvent.ACTION_DOWN:

        mVelocityTracker.clear();
        mScroller.abortAnimation();
        mLastTouchY = ev.getY();
        mLastTouchX = ev.getX();
        motionPosition = pointToPosition((int) mLastTouchX, (int) mLastTouchY);
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mTouchRemainderY = 0;

        if (mTouchMode != TOUCH_MODE_FLINGING && !mDataChanged && motionPosition >= 0
                && getAdapter().isEnabled(motionPosition)) {
            mTouchMode = TOUCH_MODE_DOWN;

            mBeginClick = true;

            if (mPendingCheckForTap == null) {
                mPendingCheckForTap = new CheckForTap();
            }

            postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
        }

        mMotionPosition = motionPosition;
        invalidate();
        break;

    case MotionEvent.ACTION_MOVE: {

        final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (index < 0) {
            Log.e(TAG, "onInterceptTouchEvent could not find pointer with id " + mActivePointerId
                    + " - did StaggeredGridView receive an inconsistent " + "event stream?");
            return false;
        }
        final float y = MotionEventCompat.getY(ev, index);
        final float dy = y - mLastTouchY + mTouchRemainderY;
        final int deltaY = (int) dy;
        mTouchRemainderY = dy - deltaY;

        if (Math.abs(dy) > mTouchSlop) {
            mTouchMode = TOUCH_MODE_DRAGGING;
        }

        if (mTouchMode == TOUCH_MODE_DRAGGING) {
            mLastTouchY = y;

            if (!trackMotionScroll(deltaY, true)) {
                // Break fling velocity if we impacted an edge.
                mVelocityTracker.clear();
            }
        }

        updateSelectorState();
    }
        break;

    case MotionEvent.ACTION_CANCEL:
        mTouchMode = TOUCH_MODE_IDLE;
        updateSelectorState();
        setPressed(false);
        View motionView = this.getChildAt(mMotionPosition - mFirstPosition);
        if (motionView != null) {
            motionView.setPressed(false);
        }
        final Handler handler = getHandler();
        if (handler != null) {
            handler.removeCallbacks(mPendingCheckForLongPress);
        }

        if (mTopEdge != null) {
            mTopEdge.onRelease();
            mBottomEdge.onRelease();
        }

        mTouchMode = TOUCH_MODE_IDLE;
        break;

    case MotionEvent.ACTION_UP: {
        mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
        final float velocity = VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId);
        final int prevTouchMode = mTouchMode;

        if (Math.abs(velocity) > mFlingVelocity) { // TODO
            mTouchMode = TOUCH_MODE_FLINGING;
            mScroller.fling(0, 0, 0, (int) velocity, 0, 0, Integer.MIN_VALUE, Integer.MAX_VALUE);
            mLastTouchY = 0;
            invalidate();
        } else {
            mTouchMode = TOUCH_MODE_IDLE;
        }

        if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
            // TODO : handle
            mTouchMode = TOUCH_MODE_TAP;
        } else {
            mTouchMode = TOUCH_MODE_REST;
        }

        switch (prevTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING:
            final View child = getChildAt(motionPosition - mFirstPosition);
            final float x = ev.getX();
            final boolean inList = x > getPaddingLeft() && x < getWidth() - getPaddingRight();
            if (child != null && !child.hasFocusable() && inList) {
                if (mTouchMode != TOUCH_MODE_DOWN) {
                    child.setPressed(false);
                }

                if (mPerformClick == null) {
                    invalidate();
                    mPerformClick = new PerformClick();
                }

                final PerformClick performClick = mPerformClick;
                performClick.mClickMotionPosition = motionPosition;
                performClick.rememberWindowAttachCount();

                if (mTouchMode == TOUCH_MODE_DOWN || mTouchMode == TOUCH_MODE_TAP) {
                    final Handler handlerTouch = getHandler();
                    if (handlerTouch != null) {
                        handlerTouch.removeCallbacks(mTouchMode == TOUCH_MODE_DOWN ? mPendingCheckForTap
                                : mPendingCheckForLongPress);
                    }
                    mLayoutMode = LAYOUT_NORMAL;

                    if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                        mTouchMode = TOUCH_MODE_TAP;

                        layoutChildren(mDataChanged);
                        child.setPressed(true);
                        positionSelector(mMotionPosition, child);
                        setPressed(true);
                        if (mSelector != null) {
                            Drawable d = mSelector.getCurrent();
                            if (d != null && d instanceof TransitionDrawable) {
                                ((TransitionDrawable) d).resetTransition();
                            }
                        }
                        if (mTouchModeReset != null) {
                            removeCallbacks(mTouchModeReset);
                        }
                        mTouchModeReset = new Runnable() {
                            @Override
                            public void run() {
                                mTouchMode = TOUCH_MODE_REST;
                                child.setPressed(false);
                                setPressed(false);
                                if (!mDataChanged) {
                                    performClick.run();
                                }
                            }
                        };
                        postDelayed(mTouchModeReset, ViewConfiguration.getPressedStateDuration());

                    } else {
                        mTouchMode = TOUCH_MODE_REST;
                    }
                    return true;
                } else if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                    performClick.run();
                }
            }

            mTouchMode = TOUCH_MODE_REST;
        }

        mBeginClick = false;

        updateSelectorState();
    }
        break;
    }
    return true;
}

From source file:com.digreamon.android.widget.SlidingPaneLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!mIsSlidable) {
        return super.onTouchEvent(ev);
    }/*from ww  w.  ja  va  2  s.  co m*/

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction();
    boolean needsInvalidate = false;
    boolean wantTouchEvents = true;

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mInitialMotionX = x;
        mInitialMotionY = y;

        if (isSlideablePaneUnder(x, y)) {
            mScroller.abortAnimation();
            wantTouchEvents = true;
            mLastMotionX = x;
            setScrollState(SCROLL_STATE_DRAGGING);
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (mScrollState != SCROLL_STATE_DRAGGING) {
            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, pointerIndex);
            final float y = MotionEventCompat.getY(ev, pointerIndex);
            final float dx = Math.abs(x - mLastMotionX);
            final float dy = Math.abs(y - mLastMotionY);
            if (dx > mTouchSlop && dx > dy && isSlideablePaneUnder(x, y)) {
                mLastMotionX = x - mInitialMotionX > 0 ? mInitialMotionX + mTouchSlop
                        : mInitialMotionX - mTouchSlop;
                setScrollState(SCROLL_STATE_DRAGGING);
            }
        }
        if (mScrollState == SCROLL_STATE_DRAGGING) {
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final float y = MotionEventCompat.getY(ev, activePointerIndex);
            needsInvalidate |= performDrag(x, y);
        }
        break;
    }

    case MotionEvent.ACTION_UP: {
        if (isDimmed(getCoverView())) {
            final int pi = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, pi);
            final float y = MotionEventCompat.getY(ev, pi);
            final float dx = x - mInitialMotionX;
            final float dy = y - mInitialMotionY;
            if (dx * dx + dy * dy < mTouchSlop * mTouchSlop && isSlideablePaneUnder(x, y)) {
                // Taps close a dimmed open pane.
                closePane(getCoverView(), 0);
                mActivePointerId = INVALID_POINTER;
                break;
            }
        }
        if (mScrollState == SCROLL_STATE_DRAGGING) {
            final VelocityTracker vt = mVelocityTracker;
            vt.computeCurrentVelocity(1000, mMaxVelocity);
            int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(vt, mActivePointerId);

            boolean isPerformClose = false;
            switch (getViewSpec(mCoveredView)) {
            case LEFT:
                isPerformClose = initialVelocity < 0 || (initialVelocity == 0 && mSlideOffset < 0.5f);
                break;
            case RIGHT:
                isPerformClose = initialVelocity > 0 || (initialVelocity == 0 && mSlideOffset < 0.5f);
                break;
            default:
                break;
            }

            if (isPerformClose) {
                closePane(getCoverView(), initialVelocity);
            } else {
                openPane(getCoverView(), initialVelocity);
            }

            mActivePointerId = INVALID_POINTER;
        }
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        if (mScrollState == SCROLL_STATE_DRAGGING) {
            mActivePointerId = INVALID_POINTER;
            if (mSlideOffset < 0.5f) {
                closePane(getCoverView(), 0);
            } else {
                openPane(getCoverView(), 0);
            }
        }
        break;
    }

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = MotionEventCompat.getX(ev, index);
        mLastMotionY = MotionEventCompat.getY(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP: {
        onSecondaryPointerUp(ev);
        break;
    }
    }

    if (needsInvalidate) {
        invalidate();
    }
    return wantTouchEvents;
}

From source file:com.cheng.animationstudy.customview.googleimitatecode.SwipyRefreshLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    ensureTarget();//  w w w .  j a  va  2 s.co m

    final int action = MotionEventCompat.getActionMasked(ev);

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;
    }

    switch (mDirection) {
    case BOTTOM:
        if (!isEnabled() || mReturningToStart || (!mBothDirection && canChildScrollDown()) || mRefreshing) {
            // Fail fast if we're not in a state where a swipe is possible
            return false;
        }
        break;
    case TOP:
    default:
        if (!isEnabled() || mReturningToStart || (!mBothDirection && canChildScrollUp()) || mRefreshing) {
            // Fail fast if we're not in a state where a swipe is possible
            return false;
        }
        break;
    }

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        setTargetOffsetTopAndBottom(mOriginalOffsetTop - mCircleView.getTop(), true);
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsBeingDragged = false;
        final float initialMotionY = getMotionEventY(ev, mActivePointerId);
        if (initialMotionY == -1) {
            return false;
        }
        mInitialMotionY = initialMotionY;

    case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER) {
            return false;
        }

        final float y = getMotionEventY(ev, mActivePointerId);
        if (y == -1) {
            return false;
        }
        if (mBothDirection) {
            if (y > mInitialMotionY) {
                setRawDirection(SwipyRefreshLayoutDirection.TOP);
            } else if (y < mInitialMotionY) {
                setRawDirection(SwipyRefreshLayoutDirection.BOTTOM);
            }
            if ((mDirection == SwipyRefreshLayoutDirection.BOTTOM && canChildScrollDown())
                    || (mDirection == SwipyRefreshLayoutDirection.TOP && canChildScrollUp())) {
                return false;
            }
        }
        float yDiff;
        switch (mDirection) {
        case BOTTOM:
            yDiff = mInitialMotionY - y;
            break;
        case TOP:
        default:
            yDiff = y - mInitialMotionY;
            break;
        }
        if (yDiff > mTouchSlop && !mIsBeingDragged) {
            mIsBeingDragged = true;
            mProgress.setAlpha(STARTING_PROGRESS_ALPHA);
        }
        break;

    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        break;
    }

    return mIsBeingDragged;
}

From source file:caesar.feng.framework.widget.StaggeredGrid.ExtendableListView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    int action = ev.getAction();

    if (!mIsAttached) {
        // Something isn't right.
        // Since we rely on being attached to get data set change notifications,
        // don't risk doing anything where we might try to resync and find things
        // in a bogus state.
        return false;
    }//w  w w.  ja v a2  s .  c o  m

    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        int touchMode = mTouchMode;

        // TODO : overscroll
        //                if (touchMode == TOUCH_MODE_OVERFLING || touchMode == TOUCH_MODE_OVERSCROLL) {
        //                    mMotionCorrection = 0;
        //                    return true;
        //                }

        final int x = (int) ev.getX();
        final int y = (int) ev.getY();
        mActivePointerId = ev.getPointerId(0);

        int motionPosition = findMotionRow(y);
        if (touchMode != TOUCH_MODE_FLINGING && motionPosition >= 0) {
            // User clicked on an actual view (and was not stopping a fling).
            // Remember where the motion event started
            mMotionX = x;
            mMotionY = y;
            mMotionPosition = motionPosition;
            mTouchMode = TOUCH_MODE_DOWN;
        }
        mLastY = Integer.MIN_VALUE;
        initOrResetVelocityTracker();
        mVelocityTracker.addMovement(ev);
        if (touchMode == TOUCH_MODE_FLINGING) {
            return true;
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
            int pointerIndex = ev.findPointerIndex(mActivePointerId);
            if (pointerIndex == -1) {
                pointerIndex = 0;
                mActivePointerId = ev.getPointerId(pointerIndex);
            }
            final int y = (int) ev.getY(pointerIndex);
            initVelocityTrackerIfNotExists();
            mVelocityTracker.addMovement(ev);
            if (startScrollIfNeeded(y)) {
                return true;
            }
            break;
        }
        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP: {
        mTouchMode = TOUCH_MODE_IDLE;
        mActivePointerId = INVALID_POINTER;
        recycleVelocityTracker();
        reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
        break;
    }

    case MotionEvent.ACTION_POINTER_UP: {
        onSecondaryPointerUp(ev);
        break;
    }
    }

    return false;
}

From source file:com.dovar.android.dov.myview.staggered_gridview.ExtendableListView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    int action = ev.getAction();

    if (!mIsAttached) {
        // Something isn't right.
        // Since we rely on being attached to get data set change notifications,
        // don't risk doing anything where we might try to resync and find things
        // in a bogus state.
        return false;
    }//  w w  w.  j  a v  a  2 s. c  o m

    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        int touchMode = mTouchMode;

        // TODO : overscroll
        //                if (touchMode == TOUCH_MODE_OVERFLING || touchMode == TOUCH_MODE_OVERSCROLL) {
        //                    mMotionCorrection = 0;
        //                    return true;
        //                }

        final int x = (int) ev.getX();
        final int y = (int) ev.getY();
        mActivePointerId = ev.getPointerId(0);

        int motionPosition = findMotionRow(y);
        if (touchMode != TOUCH_MODE_FLINGING && motionPosition >= 0) {
            // Dov_user clicked on an actual view (and was not stopping a fling).
            // Remember where the motion event started
            mMotionX = x;
            mMotionY = y;
            mMotionPosition = motionPosition;
            mTouchMode = TOUCH_MODE_DOWN;
        }
        mLastY = Integer.MIN_VALUE;
        initOrResetVelocityTracker();
        mVelocityTracker.addMovement(ev);
        if (touchMode == TOUCH_MODE_FLINGING) {
            return true;
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
            int pointerIndex = ev.findPointerIndex(mActivePointerId);
            if (pointerIndex == -1) {
                pointerIndex = 0;
                mActivePointerId = ev.getPointerId(pointerIndex);
            }
            final int y = (int) ev.getY(pointerIndex);
            initVelocityTrackerIfNotExists();
            mVelocityTracker.addMovement(ev);
            if (startScrollIfNeeded(y)) {
                return true;
            }
            break;
        }
        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP: {
        mTouchMode = TOUCH_MODE_IDLE;
        mActivePointerId = INVALID_POINTER;
        recycleVelocityTracker();
        reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
        break;
    }

    case MotionEvent.ACTION_POINTER_UP: {
        onSecondaryPointerUp(ev);
        break;
    }
    }

    return false;
}

From source file:co.codecrunch.musicplayerlite.slidinguppanelhelper.SlidingUpPanelLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);

    if (!isEnabled() || !isTouchEnabled() || (mIsUnableToDrag && action != MotionEvent.ACTION_DOWN)) {
        mDragHelper.cancel();//from w  w w. j a v  a  2s.co m
        return super.onInterceptTouchEvent(ev);
    }

    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        mDragHelper.cancel();
        return false;
    }

    final float x = ev.getX();
    final float y = ev.getY();

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        mIsUnableToDrag = false;
        mInitialMotionX = x;
        mInitialMotionY = y;
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final float adx = Math.abs(x - mInitialMotionX);
        final float ady = Math.abs(y - mInitialMotionY);
        final int dragSlop = mDragHelper.getTouchSlop();

        // Handle any horizontal scrolling on the drag view.
        if (mIsUsingDragViewTouchEvents && adx > dragSlop && ady < dragSlop) {
            return super.onInterceptTouchEvent(ev);
        }

        if ((ady > dragSlop && adx > ady) || !isDragViewUnder((int) mInitialMotionX, (int) mInitialMotionY)) {
            mDragHelper.cancel();
            mIsUnableToDrag = true;
            return false;
        }
        break;
    }
    }

    return mDragHelper.shouldInterceptTouchEvent(ev);
}

From source file:com.android.launcher3.allapps.FullMergeAlgorithm.java

/**
 * Handles the touch events to dismiss all apps when clicking outside the bounds of the
 * recycler view.//  ww w  .  ja  va2 s.  c o m
 */
private boolean handleTouchEvent(MotionEvent ev) {
    DeviceProfile grid = mLauncher.getDeviceProfile();
    int x = (int) ev.getX();
    int y = (int) ev.getY();

    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        if (!mContentBounds.isEmpty()) {
            // Outset the fixed bounds and check if the touch is outside all apps
            Rect tmpRect = new Rect(mContentBounds);
            tmpRect.inset(-grid.allAppsIconSizePx / 2, 0);
            if (ev.getX() < tmpRect.left || ev.getX() > tmpRect.right) {
                mBoundsCheckLastTouchDownPos.set(x, y);
                return true;
            }
        } else {
            // Check if the touch is outside all apps
            if (ev.getX() < getPaddingLeft() || ev.getX() > (getWidth() - getPaddingRight())) {
                mBoundsCheckLastTouchDownPos.set(x, y);
                return true;
            }
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mBoundsCheckLastTouchDownPos.x > -1) {
            ViewConfiguration viewConfig = ViewConfiguration.get(getContext());
            float dx = ev.getX() - mBoundsCheckLastTouchDownPos.x;
            float dy = ev.getY() - mBoundsCheckLastTouchDownPos.y;
            float distance = (float) Math.hypot(dx, dy);
            if (distance < viewConfig.getScaledTouchSlop()) {
                // The background was clicked, so just go home
                Launcher launcher = Launcher.getLauncher(getContext());
                launcher.showWorkspace(true);
                return true;
            }
        }
        // Fall through
    case MotionEvent.ACTION_CANCEL:
        mBoundsCheckLastTouchDownPos.set(-1, -1);
        break;
    }
    return false;
}

From source file:com.bitfyr.picassoswiperefreshlayout.PicassoSwipeRefreshLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;/* w ww  .  j  av a2s .c om*/
    }

    if (!isEnabled() || mReturningToStart || canChildScrollUp()) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsBeingDragged = false;
        break;

    case MotionEvent.ACTION_MOVE: {
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (pointerIndex < 0) {
            Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id.");
            return false;
        }

        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
        if (mIsBeingDragged) {
            mProgress.showArrow(true);
            float originalDragPercent = overscrollTop / mTotalDragDistance;
            if (originalDragPercent < 0) {
                return false;
            }
            float dragPercent = Math.min(1f, Math.abs(originalDragPercent));
            float adjustedPercent = (float) Math.max(dragPercent - .4, 0) * 5 / 3;
            float extraOS = Math.abs(overscrollTop) - mTotalDragDistance;
            float slingshotDist = mUsingCustomStart ? mSpinnerFinalOffset - mOriginalOffsetTop
                    : mSpinnerFinalOffset;
            float tensionSlingshotPercent = Math.max(0, Math.min(extraOS, slingshotDist * 2) / slingshotDist);
            float tensionPercent = (float) ((tensionSlingshotPercent / 4)
                    - Math.pow((tensionSlingshotPercent / 4), 2)) * 2f;
            float extraMove = (slingshotDist) * tensionPercent * 2;

            int targetY = mOriginalOffsetTop + (int) ((slingshotDist * dragPercent) + extraMove);
            // where 1.0f is a full circle
            if (mCircleView.getVisibility() != View.VISIBLE) {
                mCircleView.setVisibility(View.VISIBLE);
            }
            if (!mScale) {
                ViewCompat.setScaleX(mCircleView, 1f);
                ViewCompat.setScaleY(mCircleView, 1f);
            }
            if (overscrollTop < mTotalDragDistance) {
                if (mScale) {
                    setAnimationProgress(overscrollTop / mTotalDragDistance);
                }
                if (mProgress.getAlpha() > STARTING_PROGRESS_ALPHA
                        && !isAnimationRunning(mAlphaStartAnimation)) {
                    // Animate the alpha
                    startProgressAlphaStartAnimation();
                }
                float strokeStart = adjustedPercent * .8f;
                mProgress.setStartEndTrim(0f, Math.min(MAX_PROGRESS_ANGLE, strokeStart));
                mProgress.setArrowScale(Math.min(1f, adjustedPercent));
            } else {
                if (mProgress.getAlpha() < MAX_ALPHA && !isAnimationRunning(mAlphaMaxAnimation)) {
                    // Animate the alpha
                    startProgressAlphaMaxAnimation();
                }
            }
            float rotation = (-0.25f + .4f * adjustedPercent + tensionPercent * 2) * .5f;
            mProgress.setProgressRotation(rotation);
            setTargetOffsetTopAndBottom(targetY - mCurrentTargetOffsetTop, true /* requires update */);
        }
        break;
    }
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL: {
        if (mActivePointerId == INVALID_POINTER) {
            if (action == MotionEvent.ACTION_UP) {
                Log.e(LOG_TAG, "Got ACTION_UP event but don't have an active pointer id.");
            }
            return false;
        }
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
        mIsBeingDragged = false;
        if (overscrollTop > mTotalDragDistance) {
            setRefreshing(true, true /* notify */);
        } else {
            // cancel refresh
            mRefreshing = false;
            mProgress.setStartEndTrim(0f, 0f);
            Animation.AnimationListener listener = null;
            if (!mScale) {
                listener = new Animation.AnimationListener() {

                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        if (!mScale) {
                            startScaleDownAnimation(null);
                        }
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }

                };
            }
            animateOffsetToStartPosition(mCurrentTargetOffsetTop, listener);
            mProgress.showArrow(false);
        }
        mActivePointerId = INVALID_POINTER;
        return false;
    }
    }

    return true;
}