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.pursuer.reader.easyrss.VerticalSingleItemView.java

@SuppressLint({ "NewApi", "SimpleDateFormat" })
public VerticalSingleItemView(final DataMgr dataMgr, final Context context, final String uid, final View menu,
        final VerticalItemViewCtrl itemViewCtrl) {
    this.dataMgr = dataMgr;
    this.context = context;
    this.item = dataMgr.getItemByUid(uid, ITEM_PROJECTION);
    this.fontSize = new SettingFontSize(dataMgr).getData();
    this.theme = new SettingTheme(dataMgr).getData();
    this.showTop = false;
    this.showBottom = false;
    this.menu = menu;
    this.itemViewCtrl = itemViewCtrl;
    this.imageClickTime = 0;

    final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.view = inflater.inflate(R.layout.single_item_view, null);
    // Disable hardware acceleration on Android 3.0-4.1 devices.
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR1
            && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
        view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }/* w ww.j  a va  2s .  c  o m*/

    lastItemView = view.findViewById(R.id.LastItemView);
    nextItemView = view.findViewById(R.id.NextItemView);
    itemScrollView = (OverScrollView) view.findViewById(R.id.ItemScrollView);
    itemScrollView.setTopScrollView(lastItemView);
    itemScrollView.setBottomScrollView(nextItemView);
    itemScrollView.setOnScrollChangeListener(this);
    itemScrollView.setOnTouchListener(this);

    final View btnShowOriginal = view.findViewById(R.id.BtnShowOriginal);
    btnShowOriginal.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(final View view, final MotionEvent event) {
            boolean ret = false;
            final TextView txt = (TextView) view.findViewById(R.id.BtnText);
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                txt.setTextColor(0xFF787878);
                ret = true;
                break;
            case MotionEvent.ACTION_MOVE:
                ret = true;
                break;
            case MotionEvent.ACTION_CANCEL:
                txt.setTextColor(0xBB787878);
                ret = false;
                break;
            case MotionEvent.ACTION_UP:
                txt.setTextColor(0xBB787878);
                final SettingBrowserChoice setting = new SettingBrowserChoice(dataMgr);
                if (setting.getData() == SettingBrowserChoice.BROWSER_CHOICE_EXTERNAL) {
                    final Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.setData(Uri.parse(item.getHref()));
                    context.startActivity(intent);
                } else if (VerticalSingleItemView.this.listener != null) {
                    VerticalSingleItemView.this.listener.showWebsitePage(item.getUid(), false);
                }
                break;
            default:
            }
            return ret;
        }
    });

    final View btnShowMobilized = view.findViewById(R.id.BtnShowMobilized);
    btnShowMobilized.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(final View view, final MotionEvent event) {
            boolean ret = false;
            final TextView txt = (TextView) view.findViewById(R.id.BtnText);
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                txt.setTextColor(0xFF787878);
                ret = true;
                break;
            case MotionEvent.ACTION_MOVE:
                ret = true;
                break;
            case MotionEvent.ACTION_CANCEL:
                txt.setTextColor(0xBB787878);
                ret = false;
                break;
            case MotionEvent.ACTION_UP:
                txt.setTextColor(0xBB787878);
                if (VerticalSingleItemView.this.listener != null) {
                    VerticalSingleItemView.this.listener.showWebsitePage(item.getUid(), true);
                }
                ret = true;
                break;
            default:
            }
            return ret;
        }
    });

    {
        final ListItemItem lastItem = itemViewCtrl.getLastItem(item.getUid());
        final TextView txt = (TextView) lastItemView.findViewById(R.id.LastItemTitle);
        txt.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize);
        if (lastItem == null) {
            lastUid = null;
            menu.findViewById(R.id.BtnPrevious).setEnabled(false);
            final ImageView img = (ImageView) lastItemView.findViewById(R.id.LastItemArrow);
            img.setImageResource(R.drawable.no_more_circle);
            txt.setText(R.string.TxtNoPreviousItem);
        } else {
            lastUid = lastItem.getId();
            final View btnLast = menu.findViewById(R.id.BtnPrevious);
            btnLast.setEnabled(true);
            btnLast.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(final View view) {
                    if (listener != null) {
                        listener.showLastItem();
                    }
                }
            });
            txt.setText(lastItem.getTitle());
        }
    }

    {
        final ListItemItem nextItem = itemViewCtrl.getNextItem(item.getUid());
        final TextView txt = (TextView) nextItemView.findViewById(R.id.NextItemTitle);
        txt.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize);
        if (nextItem == null) {
            nextUid = null;
            menu.findViewById(R.id.BtnNext).setEnabled(false);
            final ImageView img = (ImageView) nextItemView.findViewById(R.id.NextItemArrow);
            img.setImageResource(R.drawable.no_more_circle);
            txt.setText(R.string.TxtNoNextItem);
        } else {
            nextUid = nextItem.getId();
            final View btnNext = menu.findViewById(R.id.BtnNext);
            btnNext.setEnabled(true);
            btnNext.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(final View view) {
                    if (listener != null) {
                        listener.showNextItem();
                    }
                }
            });
            txt.setText(nextItem.getTitle());
        }
    }

    final TextView title = (TextView) view.findViewById(R.id.ItemTitle);
    title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize * 4 / 3);
    title.setText(item.getTitle());
    final TextView info = (TextView) view.findViewById(R.id.ItemInfo);
    info.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize * 4 / 5);
    final StringBuilder infoText = new StringBuilder();
    final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    sdf.setTimeZone(TimeZone.getDefault());
    infoText.append(sdf.format(Utils.timestampToDate(item.getTimestamp())));
    if (item.getAuthor() != null && item.getAuthor().length() > 0) {
        infoText.append(" | By ");
        infoText.append(item.getAuthor());
    }
    if (item.getSourceTitle() != null && item.getSourceTitle().length() > 0) {
        infoText.append(" (");
        infoText.append(item.getSourceTitle());
        infoText.append(")");
    }
    info.setText(infoText);

    webView = (WebView) view.findViewById(R.id.webView);
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.setBackgroundColor(context.getResources()
            .getColor(theme == SettingTheme.THEME_NORMAL ? R.color.NormalBackground : R.color.DarkBackground));
    webView.setFocusable(false);
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
            if (VerticalSingleItemView.this.imageClickTime > System.currentTimeMillis() - 1000) {
                return true;
            }
            final Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setData(Uri.parse(url));
            context.startActivity(intent);
            return true;
        }
    });
    webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public boolean onJsAlert(final WebView view, final String url, final String message,
                final JsResult result) {
            if (VerticalSingleItemView.this.listener != null) {
                if (message.endsWith(".erss")) {
                    VerticalSingleItemView.this.listener
                            .onImageViewRequired(item.getStoragePath() + "/" + message);
                } else {
                    VerticalSingleItemView.this.listener.onImageViewRequired(message);
                }
            }
            VerticalSingleItemView.this.imageClickTime = System.currentTimeMillis();
            result.cancel();
            return true;
        }
    });

    updateButtonStar();
    updateButtonSharing();
    updateButtonOpenLink();

    if (!item.getState().isRead()) {
        dataMgr.markItemAsReadWithTransactionByUid(uid);
        NetworkMgr.getInstance().startImmediateItemStateSyncing();
    }
}

From source file:com.emmasuzuki.quickreturnlistview.view.QuickReturnListView.java

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    // If items are all visible or first item is visible, do nothing for quick return view
    if (mAllItemsVisible || !mQuickReturnEnabled) {
        return super.dispatchTouchEvent(ev);
    }/*from w  w  w .j  a v  a2s  . c o m*/

    mGestureListener.onTouchEvent(ev);

    if (mAbsListView.getFirstVisiblePosition() == 0) {
        mState = STATE_ONSCREEN;
        animateQuickReturnViewToDest(0);

    } else {
        switch (ev.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
            mSettleEnabled = false;
            mPrevY = (int) ev.getY();

            break;

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            // Settle quick return view with animation
            mSettleEnabled = true;
            mScrollSettleHandler.onScroll();

            break;

        case MotionEvent.ACTION_MOVE:
            // Move quick return view according to touch move
            mDeltaY = (int) ev.getY() - mPrevY;

            handleMove();

            mPrevY = (int) ev.getY();

            break;
        }
    }

    return super.dispatchTouchEvent(ev);
}

From source file:com.andview.refreshview.swipe.SwipeMenuLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    boolean isIntercepted = super.onInterceptTouchEvent(ev);
    int action = ev.getAction();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mDownX = mLastX = (int) ev.getX();
        mDownY = (int) ev.getY();
        isIntercepted = false;/*w  w  w.  j  a  va2  s .com*/
        break;
    case MotionEvent.ACTION_MOVE:
        int disX = (int) (ev.getX() - mDownX);
        int disY = (int) (ev.getY() - mDownY);
        isIntercepted = Math.abs(disX) > mScaledTouchSlop && Math.abs(disX) > Math.abs(disY);
        break;
    case MotionEvent.ACTION_UP:
        isIntercepted = false;
        if (isMenuOpen() && mSwipeCurrentHorizontal.isClickOnContentView(getWidth(), ev.getX())) {
            smoothCloseMenu();
            isIntercepted = true;
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        isIntercepted = false;
        if (!mScroller.isFinished())
            mScroller.abortAnimation();
        break;
    }
    return isIntercepted;
}

From source file:co.dift.ui.SwipeToAction.java

/** Private methods **/
private void init() {
    gestureDetector = new GestureDetectorCompat(recyclerView.getContext(),
            new GestureDetector.SimpleOnGestureListener() {
                @Override/*from w  ww  .  j  a v a 2 s  . c  om*/
                public void onLongPress(MotionEvent e) {
                    logger.d("onLongPress called!");
                    if (touchedViewHolder != null) {
                        swipeListener.onLongClick(touchedViewHolder.getItemData());
                    }
                }

                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    logger.d("onSingleTapUp called!");
                    if (touchedViewHolder != null) {
                        swipeListener.onClick(touchedViewHolder.getItemData());
                        return true;
                    }
                    return super.onSingleTapUp(e);
                }
            });

    recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {

        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent ev) {
            gestureDetector.onTouchEvent(ev);

            switch (ev.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN: {
                // starting point
                downX = ev.getX();
                downY = ev.getY();

                downTime = new Date().getTime();

                // which item are we touching
                resolveItem(downX, downY);

                break;
            }

            case MotionEvent.ACTION_UP: {
                upX = ev.getX();
                upY = ev.getY();
                upTime = new Date().getTime();

                resolveState();
                break;
            }

            case MotionEvent.ACTION_MOVE: {
                final float x = ev.getX();
                final float dx = x - downX;

                if (!shouldMove(dx))
                    break;

                // current position. moving only over x-axis
                frontViewLastX = frontViewX + dx + (dx > 0 ? -getRevealThreshold() : getRevealThreshold());
                //                        logger.d("dx: %f, frontViewLastX: %f", dx, frontViewLastX);
                if (maxSwipeXPosition != null) {
                    if (frontViewLastX > 0 && frontViewLastX > maxSwipeXPosition) {
                        frontViewLastX = maxSwipeXPosition;
                    } else if (frontViewLastX <= -maxSwipeXPosition) {
                        frontViewLastX = -maxSwipeXPosition;
                    }
                }
                frontView.setX(frontViewLastX);

                if (frontViewLastX > 0) {
                    revealRight();
                } else {
                    revealLeft();
                }

                break;
            }

            case MotionEvent.ACTION_CANCEL: {
                resolveState();

                break;
            }
            }

            return false;
        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {
            logger.d("onTouchEvent called");
        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
            logger.d("onRequestDisallowInterceptTouchEvent called");
        }
    });
}

From source file:CustomSwipeDismissBehavior.java

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    switch (MotionEventCompat.getActionMasked(event)) {
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        // Reset the ignore flag
        if (mIgnoreEvents) {
            mIgnoreEvents = false;/*from w  w w  .  ja  v a  2  s  .co  m*/
            return false;
        }
        break;
    default:
        mIgnoreEvents = !parent.isPointInChildBounds(child, (int) event.getX(), (int) event.getY());
        break;
    }

    if (mIgnoreEvents) {
        return false;
    }

    ensureViewDragHelper(parent);
    return mViewDragHelper.shouldInterceptTouchEvent(event);
}

From source file:android.support.v7.widget.ForwardingListener.java

/**
 * Observes motion events and determines when to start forwarding.
 *
 * @param srcEvent motion event in source view coordinates
 * @return true to start forwarding motion events, false otherwise
 *//*from   ww w  .jav  a  2 s.c  om*/
private boolean onTouchObserved(MotionEvent srcEvent) {
    final View src = mSrc;
    if (!src.isEnabled()) {
        return false;
    }

    final int actionMasked = MotionEventCompat.getActionMasked(srcEvent);
    switch (actionMasked) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = srcEvent.getPointerId(0);

        if (mDisallowIntercept == null) {
            mDisallowIntercept = new DisallowIntercept();
        }
        src.postDelayed(mDisallowIntercept, mTapTimeout);

        if (mTriggerLongPress == null) {
            mTriggerLongPress = new TriggerLongPress();
        }
        src.postDelayed(mTriggerLongPress, mLongPressTimeout);
        break;
    case MotionEvent.ACTION_MOVE:
        final int activePointerIndex = srcEvent.findPointerIndex(mActivePointerId);
        if (activePointerIndex >= 0) {
            final float x = srcEvent.getX(activePointerIndex);
            final float y = srcEvent.getY(activePointerIndex);

            // Has the pointer moved outside of the view?
            if (!pointInView(src, x, y, mScaledTouchSlop)) {
                clearCallbacks();

                // Don't let the parent intercept our events.
                src.getParent().requestDisallowInterceptTouchEvent(true);
                return true;
            }
        }
        break;
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        clearCallbacks();
        break;
    }

    return false;
}

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

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    boolean dispatchEventToHelper = mInterceptingEvents;

    switch (MotionEventCompat.getActionMasked(event)) {
    case MotionEvent.ACTION_DOWN:
        mInterceptingEvents = parent.isPointInChildBounds(child, (int) event.getX(), (int) event.getY());
        dispatchEventToHelper = mInterceptingEvents;
        break;/*from  w  w  w.jav  a  2s  . co m*/
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        // Reset the ignore flag for next time
        mInterceptingEvents = false;
        break;
    }

    if (dispatchEventToHelper) {
        ensureViewDragHelper(parent);
        return mViewDragHelper.shouldInterceptTouchEvent(event);
    }
    return false;
}

From source file:com.arthurpitman.common.SlidingPanel.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    float y = event.getY();
    int action = event.getActionMasked();
    int pointerId = event.getPointerId(event.getActionIndex());

    switch (action) {
    case MotionEvent.ACTION_POINTER_DOWN:
    case MotionEvent.ACTION_DOWN:
        startTouchY = y;/*from w  w  w.  ja v  a  2 s.c  o m*/
        startTouchScrollY = getScrollY();

        // if touch didn't occur on the actual control, ignore it
        float touchBoundary = viewHeight - collapsedHeight - startTouchScrollY;
        if (y < touchBoundary) {
            return false;
        }

        // start tracking velocity
        if (velocityTracker == null) {
            velocityTracker = VelocityTracker.obtain();
        } else {
            velocityTracker.clear();
        }
        velocityTracker.addMovement(event);
        break;

    case MotionEvent.ACTION_MOVE:
        // determine if a valid touch has started
        if (Math.abs(y - startTouchY) > touchSlop) {
            touchState = TOUCH_STARTED;
        }
        if (velocityTracker != null) {
            velocityTracker.addMovement(event);
        }

        // scroll as appropriate
        if (touchState == TOUCH_STARTED) {
            final int scrollDelta = (int) (startTouchY - y);
            scrollTo(0, Math.max(0, Math.min(viewHeight, startTouchScrollY + scrollDelta)));
        }
        break;

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_POINTER_UP:
    case MotionEvent.ACTION_UP:
        if (touchState == TOUCH_STARTED) {
            final int currentScrollY = getScrollY();

            // get velocity
            float velocity = 0;
            if (velocityTracker != null) {
                velocityTracker.computeCurrentVelocity(1000);
                velocity = VelocityTrackerCompat.getYVelocity(velocityTracker, pointerId);
                velocityTracker.recycle();
                velocityTracker = null;
            }

            // snap to final scroll position
            int target = startTouchScrollY;
            if ((Math.abs(velocity) > minimumflingVelocity) || (Math.abs(y - startTouchY) > (viewHeight / 2))) {
                if (velocity < 0) {
                    target = viewHeight;
                } else {
                    target = 0;
                }
            }
            scroller.startScroll(0, currentScrollY, 0, target - currentScrollY);
            invalidate();
            touchState = TOUCH_NONE;
        }
        break;
    }
    return true;
}

From source file:com.commonsware.cwac.crossport.design.widget.SwipeDismissBehavior.java

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    boolean dispatchEventToHelper = mInterceptingEvents;

    switch (event.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
        mInterceptingEvents = parent.isPointInChildBounds(child, (int) event.getX(), (int) event.getY());
        dispatchEventToHelper = mInterceptingEvents;
        break;/* www .j  a  v  a2s.  co m*/
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        // Reset the ignore flag for next time
        mInterceptingEvents = false;
        break;
    }

    if (dispatchEventToHelper) {
        ensureViewDragHelper(parent);
        return mViewDragHelper.shouldInterceptTouchEvent(event);
    }
    return false;
}

From source file:com.dat.towerofhanoi.draggablerecyclerview.BoardView.java

private boolean handleTouchEvent(MotionEvent event) {
    if (mLists.size() == 0) {
        return false;
    }//  www.ja va2  s  .co  m

    mTouchX = event.getX();
    mTouchY = event.getY();
    if (isDragging()) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_MOVE:
            if (!mAutoScroller.isAutoScrolling()) {
                updateScrollPosition();
            }
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            mAutoScroller.stopAutoScroll();
            mCurrentRecyclerView.onDragEnded();
            if (snapToColumnWhenScrolling()) {
                scrollToColumn(getColumnOfList(mCurrentRecyclerView), true);
            }
            invalidate();
            break;
        }
        return true;
    } else {
        if (snapToColumnWhenScrolling() && mGestureDetector.onTouchEvent(event)) {
            // A page fling occurred, consume event
            return true;
        }
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (!mScroller.isFinished()) {
                // View was grabbed during animation
                mScroller.forceFinished(true);
            }
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            if (snapToColumnWhenScrolling()) {
                scrollToColumn(getClosestColumn(), true);
            }
            break;
        }
        return false;
    }
}