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.alibaba.akita.widget.TitlePageIndicator.java

public boolean onTouchEvent(android.view.MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }//from   w  w w.  jav a 2 s  .  c om
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction();

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            if (!mViewPager.isFakeDragging()) {
                mViewPager.beginFakeDrag();
            }

            mLastMotionX = x;
            try {
                mViewPager.fakeDragBy(deltaX);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;
            final float leftThird = halfWidth - sixthWidth;
            final float rightThird = halfWidth + sixthWidth;
            final float eventX = ev.getX();

            if (eventX < leftThird) {
                if (mCurrentPage > 0) {
                    mViewPager.setCurrentItem(mCurrentPage - 1);
                    return true;
                }
            } else if (eventX > rightThird) {
                if (mCurrentPage < count - 1) {
                    mViewPager.setCurrentItem(mCurrentPage + 1);
                    return true;
                }
            } else {
                //Middle third
                if (mCenterItemClickListener != null) {
                    mCenterItemClickListener.onCenterItemClick(mCurrentPage);
                }
            }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging()) {
            try {
                mViewPager.endFakeDrag();
            } catch (NullPointerException npe) {
                npe.printStackTrace(); // fix a crash report.
            }
        }
        break;

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

    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    return true;
}

From source file:com.code44.finance.views.StaggeredGridView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    mVelocityTracker.addMovement(ev);/*from  w  w w  . j  a  v  a  2  s . c  o m*/
    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);
                    }

                    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:it.mb.whatshare.Utils.java

/**
 * Returns a string that represents the symbolic name of the specified
 * action such as "ACTION_DOWN", "ACTION_POINTER_DOWN(3)" or an equivalent
 * numeric constant such as "35" if unknown. By Google.
 * //from w  w  w.j a v  a 2  s.  com
 * @param action
 *            The action.
 * @return The symbolic name of the specified action.
 */
public static String actionToString(int action) {
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        return "ACTION_DOWN";
    case MotionEvent.ACTION_UP:
        return "ACTION_UP";
    case MotionEvent.ACTION_CANCEL:
        return "ACTION_CANCEL";
    case MotionEvent.ACTION_OUTSIDE:
        return "ACTION_OUTSIDE";
    case MotionEvent.ACTION_MOVE:
        return "ACTION_MOVE";
    case MotionEvent.ACTION_HOVER_MOVE:
        return "ACTION_HOVER_MOVE";
    case MotionEvent.ACTION_SCROLL:
        return "ACTION_SCROLL";
    case MotionEvent.ACTION_HOVER_ENTER:
        return "ACTION_HOVER_ENTER";
    case MotionEvent.ACTION_HOVER_EXIT:
        return "ACTION_HOVER_EXIT";
    }
    int index = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_POINTER_DOWN:
        return "ACTION_POINTER_DOWN(" + index + ")";
    case MotionEvent.ACTION_POINTER_UP:
        return "ACTION_POINTER_UP(" + index + ")";
    default:
        return Integer.toString(action);
    }
}

From source file:com.borqs.qiupu.fragment.TitlePageIndicator.java

public boolean onTouchEvent(android.view.MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/*ww w.j  a  va 2  s .  c om*/
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction();

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            //TODO
            //                    if (!mViewPager.isFakeDragging()) {
            //                        mViewPager.beginFakeDrag();
            //                    }

            mLastMotionX = x;

            //TODO
            //                    mViewPager.fakeDragBy(deltaX);
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;
            final float leftThird = halfWidth - sixthWidth;
            final float rightThird = halfWidth + sixthWidth;
            final float eventX = ev.getX();

            if (eventX < leftThird) {
                if (mCurrentPage > 0) {
                    mViewPager.setCurrentItem(mCurrentPage - 1);
                    return true;
                }
            } else if (eventX > rightThird) {
                if (mCurrentPage < count - 1) {
                    mViewPager.setCurrentItem(mCurrentPage + 1);
                    return true;
                }
            } else {
                //Middle third
                if (mCenterItemClickListener != null) {
                    mCenterItemClickListener.onCenterItemClick(mCurrentPage);
                }
            }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        //TODO
        //                if (mViewPager.isFakeDragging()) mViewPager.endFakeDrag();
        break;

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

    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    return true;
}

From source file:com.acious.android.paginationseekbar.PaginationSeekBar.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!isEnabled()) {
        return false;
    }/*from   w  w w  .j a  va  2  s. c  o  m*/
    int actionMasked = MotionEventCompat.getActionMasked(event);
    switch (actionMasked) {
    case MotionEvent.ACTION_DOWN:
        mDownX = event.getX();
        startDragging(event, isInScrollingContainer());
        break;
    case MotionEvent.ACTION_MOVE:
        if (isDragging()) {
            updateDragging(event);
        } else {
            final float x = event.getX();
            if (Math.abs(x - mDownX) > mTouchSlop) {
                startDragging(event, false);
            }
        }
        break;
    case MotionEvent.ACTION_UP:
        notifyPageChange(mValue, true);
    case MotionEvent.ACTION_CANCEL:
        stopDragging();
        break;
    }
    return true;
}

From source file:com.dgmltn.ranger.internal.AbsRangeBar.java

/**
 * Some logic ideas came from://from  w  w  w  .  j a v a  2  s .  co  m
 * https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/AbsSeekBar.java#L564
 *
 * @param event
 * @return
 */
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!isEnabled()) {
        return false;
    }

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        //PbLog.e(TAG, "onTouchEvent: ACTION_DOWN");
        if (isInScrollingContainer()) {
            //PbLog.e(TAG, "onTouchEvent: ACTION_DOWN isInScrollingContainer() == true; waiting for slop");
            mTouchDown = new PointF(event.getX(), event.getY());
        } else {
            //PbLog.e(TAG, "onTouchEvent: ACTION_DOWN isInScrollingContainer() == false; start tracking/processing");
            setPressed(true);
            //if (mThumb != null) {
            //    invalidate(mThumb.getBounds()); // This may be within the padding region
            //}
            invalidate();
            onStartTrackingTouch();
            trackTouchEvent(event);
            attemptClaimDrag();
        }
        break;
    case MotionEvent.ACTION_MOVE:
        //PbLog.e(TAG, "onTouchEvent: ACTION_MOVE");
        if (mIsTrackingTouch) {
            //PbLog.e(TAG, "onTouchEvent: ACTION_MOVE mIsTrackingTouch == true; track/process");
            trackTouchEvent(event);
        } else {
            final float x = event.getX();
            if (mTouchDown != null && Math.abs(x - mTouchDown.x) > mScaledTouchSlop) {
                //PbLog.e(TAG, "onTouchEvent: ACTION_MOVE mIsTrackingTouch == false; slop exceeded; start tracking/processing");
                setPressed(true);
                //if (mThumb != null) {
                //    invalidate(mThumb.getBounds()); // This may be within the padding region
                //}
                invalidate();
                onStartTrackingTouch();
                trackTouchEvent(event);
                attemptClaimDrag();
            }
        }
        break;
    case MotionEvent.ACTION_UP:
        //PbLog.e(TAG, "onTouchEvent: ACTION_UP");
        if (mIsTrackingTouch) {
            //PbLog.e(TAG, "onTouchEvent: ACTION_UP mIsTrackingTouch == true; track/process and stop tracking/processing");
            trackTouchEvent(event);
            onStopTrackingTouch();
            setPressed(false);
        } else {
            //PbLog.e(TAG, "onTouchEvent: ACTION_UP mIsTrackingTouch == false; start and stop tracking/processing tap-seek");
            // Touch up when we never crossed the touch slop threshold should
            // be interpreted as a tap-seek to that location.
            onStartTrackingTouch();
            trackTouchEvent(event);
            onStopTrackingTouch();
        }
        mTouchDown = null;
        // ProgressBar doesn't know to repaint the thumb drawable
        // in its inactive state when the touch stops (because the
        // value has not apparently changed)
        invalidate();
        break;
    case MotionEvent.ACTION_CANCEL:
        //PbLog.e(TAG, "onTouchEvent: ACTION_CANCEL");
        if (mIsTrackingTouch) {
            //PbLog.e(TAG, "onTouchEvent: ACTION_CANCEL mIsTrackingTouch == true; stop tracking/processing");
            onStopTrackingTouch();
            setPressed(false);
        }
        mTouchDown = null;
        invalidate(); // see above explanation
        break;
    }
    return true;
}

From source file:com.bulletnoid.android.widget.StaggeredGridView.StaggeredGridView2.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    int action = ev.getAction();
    View v;/*from   ww  w. j  a v a2s.  com*/

    /*if (mPositionScroller != null) {
    mPositionScroller.stop();
    }
            
    if (!isAttachedToWindow()) {
    // 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;
    }
            
    if (mFastScroller != null && mFastScroller.onInterceptTouchEvent(ev)) {
    return true;
    }*/

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        int touchMode = mTouchMode;
        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
            v = getChildAt(motionPosition - mFirstPosition);
            //mMotionViewOriginalTop = v.getTop();
            mLastTouchX = x;
            mLastTouchY = y;
            mMotionPosition = motionPosition;
            mTouchMode = TOUCH_MODE_DOWN;
            clearScrollingCache();
        }
        mTouchRemainderY = 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 = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            if (pointerIndex == -1) {
                pointerIndex = 0;
                mActivePointerId = ev.getPointerId(pointerIndex);
            }
            final int y = (int) MotionEventCompat.getY(ev, pointerIndex);
            initVelocityTrackerIfNotExists();
            mVelocityTracker.addMovement(ev);
            if (startScrollIfNeeded(y)) {
                return true;
            }
            break;
        }
        break;
    }

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

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

    /*mVelocityTracker.addMovement(ev);
    final int action=ev.getAction()&MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mVelocityTracker.clear();
        //mScroller.abortAnimation();
        mLastTouchY=ev.getY();
        mActivePointerId=MotionEventCompat.getPointerId(ev, 0);
        mTouchRemainderY=0;
        if (mTouchMode==TOUCH_MODE_FLINGING) {
            // Catch!
            mTouchMode=TOUCH_MODE_DRAGGING;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
            return true;
        }
        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;
            return true;
        }
    }
            
    case MotionEvent.ACTION_UP: {
        mTouchMode=TOUCH_MODE_REST;
        mVelocityTracker.clear();
        reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
    }
    }*/

    return false;
}

From source file:com.achep.acdisplay.ui.fragments.AcDisplayFragment.java

@Override
public void onForwardedEvent(MotionEvent event, int activePointerId) {
    int action = event.getActionMasked();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        // Track the velocity of movement, so we
        // can do swipe-to-dismiss.
        mVelocityTracker = VelocityTracker.obtain();
        mTouchSticky = false;//from w  w w.  j a v a  2 s .  c  o  m
    case MotionEvent.ACTION_MOVE:
    case MotionEvent.ACTION_UP:
        populateStdMotion(event);

        if (action != MotionEvent.ACTION_UP) {
            return; // Don't fall down.
        }

        boolean dismissing = swipeToDismiss();
        if (!dismissing) {
            if (mTouchSticky) {
                // Disable the default timeout mechanism and let
                // the selected widget to stay for a while.
                onWidgetStick(mSelectedWidget);
            } else if (mPressedIconView == null || !isPinnable()) {
                showHomeWidget();
            } else {
                onWidgetPin(mSelectedWidget);
            }
        }
    case MotionEvent.ACTION_CANCEL:
        mTouchHandler.removeCallbacksAndMessages(null);
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mTouchSticky = false;

        if (action == MotionEvent.ACTION_CANCEL) {
            showHomeWidget();
        }
        break;
    }
}

From source file:com.app.afteryou.ui.staggered.StaggeredGridView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!isEnabled()) {
        // A disabled view that is clickable still consumes the touch
        // events, it just doesn't respond to them.
        return isClickable() || isLongClickable();
    }//w w w  .  j ava2 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) {
            setTouchMode(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:
        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();
        }

        setTouchMode(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
            setTouchMode(TOUCH_MODE_FLINGING);
            mScroller.fling(0, 0, 0, (int) velocity, 0, 0, Integer.MIN_VALUE, Integer.MAX_VALUE);
            mLastTouchY = 0;
            ViewCompat.postInvalidateOnAnimation(this);
        } else {
            setTouchMode(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);
                    }

                    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:ca.mymenuapp.ui.widgets.SlidingUpPanelLayout.java

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

    if (!mCanSlide || !mIsSlidingEnabled || (mIsUnableToDrag && action != MotionEvent.ACTION_DOWN)) {
        mDragHelper.cancel();//from   ww  w .ja  va2s  .  com
        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();
    boolean interceptTap = false;

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        mIsUnableToDrag = false;
        mInitialMotionX = x;
        mInitialMotionY = y;
        if (isDragViewUnder((int) x, (int) y) && !mIsUsingDragViewTouchEvents) {
            interceptTap = true;
        }
        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) {
            if (adx > mScrollTouchSlop && ady < mScrollTouchSlop) {
                return super.onInterceptTouchEvent(ev);
            }
            // Intercept the touch if the drag view has any vertical scroll.
            // onTouchEvent will determine if the view should drag vertically.
            else if (ady > mScrollTouchSlop) {
                interceptTap = isDragViewUnder((int) x, (int) y);
            }
        }

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

    final boolean interceptForDrag = mDragHelper.shouldInterceptTouchEvent(ev);

    return interceptForDrag || interceptTap;
}