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:android.support.designox.widget.CoordinatorLayout.java

/**
 * Reset all Behavior-related tracking records either to clean up or in preparation
 * for a new event stream. This should be called when attached or detached from a window,
 * in response to an UP or CANCEL event, when intercept is request-disallowed
 * and similar cases where an event stream in progress will be aborted.
 */// w  w w .j  av  a  2  s  .  co  m
private void resetTouchBehaviors() {
    if (mBehaviorTouchView != null) {
        final Behavior b = ((LayoutParams) mBehaviorTouchView.getLayoutParams()).getBehavior();
        if (b != null) {
            final long now = SystemClock.uptimeMillis();
            final MotionEvent cancelEvent = MotionEvent.obtain(now, now, MotionEvent.ACTION_CANCEL, 0.0f, 0.0f,
                    0);
            b.onTouchEvent(this, mBehaviorTouchView, cancelEvent);
            cancelEvent.recycle();
        }
        mBehaviorTouchView = null;
    }

    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        lp.resetTouchBehaviorTracking();
    }
}

From source file:com.blestep.sportsbracelet.view.TimelineChartView.java

/**
 * {@inheritDoc}//  w  w w  .  j av  a2  s.c  o  m
 */
@Override
public boolean onTouchEvent(final MotionEvent event) {

    final int action = event.getActionMasked();
    final int index = event.getActionIndex();
    final int pointerId = event.getPointerId(index);

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        // Initialize velocity tracker
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        } else {
            mVelocityTracker.clear();
        }
        mVelocityTracker.addMovement(event);
        mScroller.forceFinished(true);
        mState = STATE_INITIALIZE;

        mInitialTouchOffset = mCurrentOffset;
        mInitialTouchX = event.getX();
        mInitialTouchY = event.getY();
        return true;

    case MotionEvent.ACTION_MOVE:
        mVelocityTracker.addMovement(event);
        float diffX = event.getX() - mInitialTouchX;
        float diffY = event.getY() - mInitialTouchY;
        if (Math.abs(diffX) > mTouchSlop || mState >= STATE_MOVING) {
            mCurrentOffset = mInitialTouchOffset + diffX;
            if (mCurrentOffset < 0) {
                onOverScroll();
                mCurrentOffset = 0;
            } else if (mCurrentOffset > mMaxOffset) {
                onOverScroll();
                mCurrentOffset = mMaxOffset;
            }
            mVelocityTracker.computeCurrentVelocity(1000, mMaxFlingVelocity);
            mState = STATE_MOVING;
            ViewCompat.postInvalidateOnAnimation(this);
        } else if (Math.abs(diffY) > mTouchSlop && mState < STATE_MOVING) {
            return false;
        }
        return true;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        if (mState >= STATE_MOVING) {
            final int velocity = (int) VelocityTrackerCompat.getXVelocity(mVelocityTracker, pointerId);
            mScroller.forceFinished(true);
            mState = STATE_FLINGING;
            mScroller.fling((int) mCurrentOffset, 0, velocity, 0, 0, (int) mMaxOffset, 0, 0);
            ViewCompat.postInvalidateOnAnimation(this);
        } else {
            // Reset scrolling state
            mState = STATE_IDLE;
        }
        return true;
    }
    return false;
}

From source file:com.android.launcher3.BubbleTextView.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    // Call the superclass onTouchEvent first, because sometimes it changes the state to
    // isPressed() on an ACTION_UP
    boolean result = super.onTouchEvent(event);

    // Check for a stylus button press, if it occurs cancel any long press checks.
    if (mStylusEventHelper.onMotionEvent(event)) {
        mLongPressHelper.cancelLongPress();
        result = true;/*www  . j a va2 s.c o m*/
    }

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        // So that the pressed outline is visible immediately on setStayPressed(),
        // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
        // to create it)
        if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
            mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
        }

        // If we're in a stylus button press, don't check for long press.
        if (!mStylusEventHelper.inStylusButtonPressed()) {
            mLongPressHelper.postCheckForLongPress();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        // If we've touched down and up on an item, and it's still not "pressed", then
        // destroy the pressed outline
        if (!isPressed()) {
            mPressedBackground = null;
        }

        mLongPressHelper.cancelLongPress();
        break;
    case MotionEvent.ACTION_MOVE:
        if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
            mLongPressHelper.cancelLongPress();
        }
        break;
    }
    return result;
}

From source file:com.albedinsky.android.support.ui.widget.ViewPagerWidget.java

/**
 *///from  ww w . j a va  2s . c o m
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
    if (!hasPrivateFlag(PFLAG_PAGE_SWIPING_ENABLED)) {
        return false;
    }

    if (hasPrivateFlag(PrivateFlags.PFLAG_PULL_ENABLED) && mPullController.processTouchEvent(event)) {
        this.requestParentDisallowInterceptTouchEvent(true);
        return true;
    }

    if (hasPrivateFlag(PFLAG_PAGE_FLING_SWIPING_ENABLED)) {
        this.ensureVelocityTracker();
        mVelocityTracker.addMovement(event);
        switch (event.getActionMasked()) {
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            mVelocityTracker.computeCurrentVelocity(UiConfig.VELOCITY_UNITS);
            final float xVelocity = mVelocityTracker.getXVelocity();
            if (Math.abs(xVelocity) > mPageFlingSwipingSensitivity) {
                super.onTouchEvent(event);
                this.handleFling(xVelocity);
                return true;
            }
        }
    }
    return super.onTouchEvent(event);
}

From source file:com.android.deskclock.alarms.AlarmActivity.java

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (mAlarmHandled) {
        LogUtils.v(LOGTAG, "onTouch ignored: %s", motionEvent);
        return false;
    }//from w w w.j  a  va 2  s .com

    final int[] contentLocation = { 0, 0 };
    mContentView.getLocationOnScreen(contentLocation);

    final float x = motionEvent.getRawX() - contentLocation[0];
    final float y = motionEvent.getRawY() - contentLocation[1];

    final int alarmLeft = mAlarmButton.getLeft() + mAlarmButton.getPaddingLeft();
    final int alarmRight = mAlarmButton.getRight() - mAlarmButton.getPaddingRight();

    final float snoozeFraction, dismissFraction;
    if (mContentView.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
        snoozeFraction = getFraction(alarmRight, mSnoozeButton.getLeft(), x);
        dismissFraction = getFraction(alarmLeft, mDismissButton.getRight(), x);
    } else {
        snoozeFraction = getFraction(alarmLeft, mSnoozeButton.getRight(), x);
        dismissFraction = getFraction(alarmRight, mDismissButton.getLeft(), x);
    }
    setAnimatedFractions(snoozeFraction, dismissFraction);

    switch (motionEvent.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
        LogUtils.v(LOGTAG, "onTouch started: %s", motionEvent);

        // Stop the pulse, allowing the last pulse to finish.
        mPulseAnimator.setRepeatCount(0);
        break;
    case MotionEvent.ACTION_UP:
        LogUtils.v(LOGTAG, "onTouch ended: %s", motionEvent);

        if (snoozeFraction == 1.0f) {
            snooze();
        } else if (dismissFraction == 1.0f) {
            dismiss();
        } else {
            if (snoozeFraction > 0.0f || dismissFraction > 0.0f) {
                // Animate back to the initial state.
                AnimatorUtils.reverse(mAlarmAnimator, mSnoozeAnimator, mDismissAnimator);
            } else if (mAlarmButton.getTop() <= y && y <= mAlarmButton.getBottom()) {
                // User touched the alarm button, hint the dismiss action
                hintDismiss();
            }

            // Restart the pulse.
            mPulseAnimator.setRepeatCount(ValueAnimator.INFINITE);
            if (!mPulseAnimator.isStarted()) {
                mPulseAnimator.start();
            }
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        resetAnimations();
        break;
    default:
        break;
    }

    return true;
}

From source file:com.coco.slidinguppanel.SlidingUpPanel.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (getState() == STATE_OPENED) {
        // disable touch handle when in opened state.
        return false;
    }/*ww w.  j  ava  2s  . c  om*/

    final int action = MotionEventCompat.getActionMasked(ev);

    if (action == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) {
        // Don't handle edge touches immediately -- they may actually belong to one of our descendants.
        return false;
    }

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

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        onTouchDown(ev, false);
        DEBUG_LOG("Down at " + mLastMotionX + "," + mLastMotionY + " mIsBeingDragged=" + mIsBeingDragged
                + " mIsUnableToDrag=" + mIsUnableToDrag);
        break;
    }
    case MotionEvent.ACTION_MOVE: {
        final int activePointerId = mActivePointerId;
        if (activePointerId == INVALID_POINTER || mIsUnableToDrag) {
            // If we don't have a valid id, the touch down wasn't on content.
            break;
        }
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float xDiff = Math.abs(x - mInitialMotionX);
        final float yDiff = Math.abs(y - mInitialMotionY);
        DEBUG_LOG("Moved to " + x + "," + y + " diff=" + xDiff + "," + yDiff);
        onTouchMove(x, y, xDiff, yDiff, false);
        break;
    }
    case MotionEvent.ACTION_UP: {
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            final int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker,
                    mActivePointerId);
            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float y = MotionEventCompat.getY(ev, pointerIndex);
            final int totalDelta = (int) (y - mInitialMotionY);
            boolean toOpen = determineToOpen(initialVelocity, totalDelta);
            startFling(toOpen, initialVelocity);
            endDrag();
        }
        DEBUG_LOG("Touch up!!!");
        break;
    }
    case MotionEvent.ACTION_CANCEL: {
        if (mIsBeingDragged) {
            startFling(isOpen(), 0);
            endDrag();
        }
        DEBUG_LOG("Touch cancel!!!");
        break;
    }
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        mLastMotionX = x;
        mLastMotionY = y;
        mActivePointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        break;
    }
    case MotionEvent.ACTION_POINTER_UP:
        onTouchPointerUp(ev);
        break;
    }

    return true;
}

From source file:com.android.mail.browse.ConversationContainer.java

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

    if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
        mTouchIsDown = false;// w w w  .ja  v  a 2  s  . com
    } else if (!mTouchIsDown
            && (action == MotionEvent.ACTION_MOVE || action == MotionEvent.ACTION_POINTER_DOWN)) {

        forwardFakeMotionEvent(ev, MotionEvent.ACTION_DOWN);
        if (mMissedPointerDown) {
            forwardFakeMotionEvent(ev, MotionEvent.ACTION_POINTER_DOWN);
            mMissedPointerDown = false;
        }

        mTouchIsDown = true;
    }

    final boolean webViewResult = mWebView.onTouchEvent(ev);

    //        LogUtils.v(TAG, "in Container.OnTouch. action=%d x/y=%f/%f pointers=%d",
    //                ev.getActionMasked(), ev.getX(), ev.getY(), ev.getPointerCount());
    return webViewResult;
}

From source file:android.support.design.widget.CoordinatorLayout.java

/**
 * Reset all Behavior-related tracking records either to clean up or in preparation
 * for a new event stream. This should be called when attached or detached from a window,
 * in response to an UP or CANCEL event, when intercept is request-disallowed
 * and similar cases where an event stream in progress will be aborted.
 *///from   w w w .  j a v a  2s.  co m
private void resetTouchBehaviors() {
    if (mBehaviorTouchView != null) {
        final Behavior b = ((LayoutParams) mBehaviorTouchView.getLayoutParams()).getBehavior();
        if (b != null) {
            final long now = SystemClock.uptimeMillis();
            final MotionEvent cancelEvent = MotionEvent.obtain(now, now, MotionEvent.ACTION_CANCEL, 0.0f, 0.0f,
                    0);
            b.onTouchEvent(this, mBehaviorTouchView, cancelEvent);
            cancelEvent.recycle();
        }
        mBehaviorTouchView = null;
    }

    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        lp.resetTouchBehaviorTracking();
    }
    mDisallowInterceptReset = false;
}

From source file:com.bluepixel.android.sgpool.ui.widget.SwipeRefreshLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    ensureTarget();/*from  w w  w.  j  av  a  2s  .com*/

    final int action = MotionEventCompat.getActionMasked(ev);

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

    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:
        mLastMotionY = mInitialMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsBeingDragged = false;
        mCurrPercentage = 0;
        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 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 yDiff = y - mInitialMotionY;
        if (yDiff > mTouchSlop) {
            mLastMotionY = y;
            mIsBeingDragged = true;
        }
        break;

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

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

    return mIsBeingDragged;
}

From source file:com.am.pullview.swiperefresh.SwipeRefreshLayout.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    final int action = event.getAction();
    boolean handled = false;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mCurrPercentage = 0;/*from  w  w w  .j a v a2  s. com*/
        mDownEvent = MotionEvent.obtain(event);
        mPrevY = mDownEvent.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        if (mDownEvent != null && !mReturningToStart) {
            final float eventY = event.getY();
            float yDiff = eventY - mDownEvent.getY();
            if (yDiff > mTouchSlop) {
                // User velocity passed min velocity; trigger a refresh
                if (yDiff > mDistanceToTriggerSync) {
                    // User movement passed distance; trigger a refresh
                    startRefresh();
                    handled = true;
                    break;
                } else {
                    // Just track the user's movement
                    setTriggerPercentage(
                            mAccelerateInterpolator.getInterpolation(yDiff / mDistanceToTriggerSync));
                    float offsetTop = yDiff;
                    if (mPrevY > eventY) {
                        offsetTop = yDiff - mTouchSlop;
                    }
                    updateContentOffsetTop((int) (offsetTop));
                    if (mPrevY > eventY && (mTarget.getTop() < mTouchSlop)) {
                        // If the user puts the view back at the top, we
                        // don't need to. This shouldn't be considered
                        // cancelling the gesture as the user can restart from the top.
                        removeCallbacks(mCancel);
                    } else {
                        updatePositionTimeout();
                    }
                    mPrevY = event.getY();
                    handled = true;
                }
            }
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        if (mDownEvent != null) {
            mDownEvent.recycle();
            mDownEvent = null;
        }
        break;
    }
    return handled;
}