Example usage for android.view MotionEvent getActionMasked

List of usage examples for android.view MotionEvent getActionMasked

Introduction

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

Prototype

public final int getActionMasked() 

Source Link

Document

Return the masked action being performed, without pointer index information.

Usage

From source file:org.libreoffice.impressremote.fragment.slides.PointerFragment.java

@Override
public boolean onTouch(View v, MotionEvent event) {

    float x = (event.getX() - xoffset) / displaywidth;
    float y = (event.getY() - yoffset) / displayheight;

    // get masked action
    int aMaskedAction = event.getActionMasked();

    switch (aMaskedAction) {
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_POINTER_DOWN:
        // a pointer start
        mCommunicationService.getCommandsTransmitter().startPointer(x, y);
        break;//from ww w  . j a va2s.  com
    case MotionEvent.ACTION_MOVE:
        // a pointer was moved
        if (nextUpdate <= event.getEventTime()) {
            mCommunicationService.getCommandsTransmitter().movePointer(x, y);
            nextUpdate = event.getEventTime() + REFRESH_MILLIS;
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
    case MotionEvent.ACTION_CANCEL:
        // a pointer was removed
        mCommunicationService.getCommandsTransmitter().stopPointer();
        break;
    }
    return true;
}

From source file:org.wikipedia.page.bottomcontent.BottomContentHandler.java

public BottomContentHandler(PageViewFragmentInternal parentFragment, CommunicationBridge bridge,
        ObservableWebView webview, LinkHandler linkHandler, ViewGroup hidingView, PageTitle pageTitle) {
    this.parentFragment = parentFragment;
    this.bridge = bridge;
    this.webView = webview;
    this.linkHandler = linkHandler;
    this.pageTitle = pageTitle;
    activity = parentFragment.getActivity();
    app = (WikipediaApp) activity.getApplicationContext();
    displayDensity = activity.getResources().getDisplayMetrics().density;

    bottomContentContainer = hidingView;
    webview.addOnScrollChangeListener(this);
    webview.addOnContentHeightChangedListener(this);

    pageLastUpdatedText = (TextView) bottomContentContainer.findViewById(R.id.page_last_updated_text);
    pageLicenseText = (TextView) bottomContentContainer.findViewById(R.id.page_license_text);
    readMoreContainer = bottomContentContainer.findViewById(R.id.read_more_container);
    readMoreList = (ListView) bottomContentContainer.findViewById(R.id.read_more_list);

    // set up pass-through scroll functionality for the ListView
    readMoreList.setOnTouchListener(new View.OnTouchListener() {
        private int touchSlop = ViewConfiguration.get(readMoreList.getContext()).getScaledTouchSlop();
        private boolean slopReached;
        private boolean doingSlopEvent;
        private boolean isPressed = false;
        private float startY;
        private float amountScrolled;

        @Override//w w w  .jav a 2s.c o  m
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getActionMasked() & MotionEvent.ACTION_MASK;
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                isPressed = true;
                startY = event.getY();
                amountScrolled = 0;
                slopReached = false;
                break;
            case MotionEvent.ACTION_MOVE:
                if (isPressed && !doingSlopEvent) {
                    int contentHeight = (int) (webView.getContentHeight() * displayDensity);
                    int maxScroll = contentHeight - webView.getScrollY() - webView.getHeight();
                    int scrollAmount = Math.min((int) (startY - event.getY()), maxScroll);
                    // manually scroll the WebView that's underneath us...
                    webView.scrollBy(0, scrollAmount);
                    amountScrolled += scrollAmount;
                    if (Math.abs(amountScrolled) > touchSlop && !slopReached) {
                        slopReached = true;
                        // send an artificial Move event that scrolls it by an amount
                        // that's greater than the touch slop, so that the currently
                        // highlighted item is unselected.
                        MotionEvent moveEvent = MotionEvent.obtain(event);
                        moveEvent.setLocation(event.getX(), event.getY() + touchSlop * 2);
                        doingSlopEvent = true;
                        readMoreList.dispatchTouchEvent(moveEvent);
                        doingSlopEvent = false;
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                isPressed = false;
                break;
            default:
                break;
            }
            return false;
        }
    });

    funnel = new SuggestedPagesFunnel(app, pageTitle.getSite());

    // preload the display density, since it will be used in a lot of places
    displayDensity = activity.getResources().getDisplayMetrics().density;
    // hide ourselves by default
    hide();
}

From source file:com.phonemetra.turbo.keyboard.emoji.EmojiPalettesView.java

@Override
public boolean onTouch(final View v, final MotionEvent event) {
    if (event.getActionMasked() != MotionEvent.ACTION_DOWN) {
        return false;
    }//from   ww  w .  j  ava2s.  co m
    final Object tag = v.getTag();
    if (!(tag instanceof Integer)) {
        return false;
    }
    final int code = (Integer) tag;
    mKeyboardActionListener.onPressKey(code, 0 /* repeatCount */, true /* isSinglePointer */);
    // It's important to return false here. Otherwise, {@link #onClick} and touch-down visual
    // feedback stop working.
    return false;
}

From source file:com.vinaysshenoy.okulus.OkulusImageView.java

@Override
public boolean onTouchEvent(final MotionEvent event) {

    boolean handled = super.onTouchEvent(event);
    if (mTouchSelectorEnabled) {

        final int action = event.getActionMasked();
        switch (action) {

        case MotionEvent.ACTION_DOWN: {
            //User touched the view
            mAlreadyInside = true;//from  w w w  .j  a  va 2 s  .  c o m
            updateTouchSelectorColor(mTouchSelectorColor);
            handled = true;
            break;
        }

        case MotionEvent.ACTION_MOVE: {

            final int eventX = (int) event.getX();
            final int eventY = (int) event.getY();

            if (!mViewRect.contains(eventX + mViewRect.left, eventY + mViewRect.top)) {
                //User moved outside
                if (mAlreadyInside) {
                    mAlreadyInside = false;
                    updateTouchSelectorColor(Color.TRANSPARENT);
                }
            }
            handled = true;
            break;
        }

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL: {
            //User left the view
            mAlreadyInside = false;
            updateTouchSelectorColor(Color.TRANSPARENT);
            handled = true;
            break;
        }

        }
    }

    return handled;

}

From source file:com.android.inputmethod.keyboard.emoji.EmojiPalettesView.java

/**
 * Called from {@link EmojiPageKeyboardView} through {@link android.view.View.OnTouchListener}
 * interface to handle touch events from View-based elements such as the space bar.
 * Note that this method is used only for observing {@link MotionEvent#ACTION_DOWN} to trigger
 * {@link KeyboardActionListener#onPressKey}. {@link KeyboardActionListener#onReleaseKey} will
 * be covered by {@link #onClick} as long as the event is not canceled.
 *///w  w w . j a  v a  2 s  . co m
@Override
public boolean onTouch(final View v, final MotionEvent event) {
    if (event.getActionMasked() != MotionEvent.ACTION_DOWN) {
        return false;
    }
    final Object tag = v.getTag();
    if (!(tag instanceof Integer)) {
        return false;
    }
    final int code = (Integer) tag;
    mKeyboardActionListener.onPressKey(code, 0 /* repeatCount */, true /* isSinglePointer */);
    // It's important to return false here. Otherwise, {@link #onClick} and touch-down visual
    // feedback stop working.
    return false;
}

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;/*from  w w w .j a  va 2 s .c o  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.commonsware.cwac.crossport.design.widget.HeaderBehavior.java

@Override
public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent ev) {
    if (mTouchSlop < 0) {
        mTouchSlop = ViewConfiguration.get(parent.getContext()).getScaledTouchSlop();
    }/*from  w  w w .  j  av  a2  s . c  om*/

    switch (ev.getActionMasked()) {
    case MotionEvent.ACTION_DOWN: {
        final int x = (int) ev.getX();
        final int y = (int) ev.getY();

        if (parent.isPointInChildBounds(child, x, y) && canDragView(child)) {
            mLastMotionY = y;
            mActivePointerId = ev.getPointerId(0);
            ensureVelocityTracker();
        } else {
            return false;
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = ev.findPointerIndex(mActivePointerId);
        if (activePointerIndex == -1) {
            return false;
        }

        final int y = (int) ev.getY(activePointerIndex);
        int dy = mLastMotionY - y;

        if (!mIsBeingDragged && Math.abs(dy) > mTouchSlop) {
            mIsBeingDragged = true;
            if (dy > 0) {
                dy -= mTouchSlop;
            } else {
                dy += mTouchSlop;
            }
        }

        if (mIsBeingDragged) {
            mLastMotionY = y;
            // We're being dragged so scroll the ABL
            scroll(parent, child, dy, getMaxDragOffset(child), 0);
        }
        break;
    }

    case MotionEvent.ACTION_UP:
        if (mVelocityTracker != null) {
            mVelocityTracker.addMovement(ev);
            mVelocityTracker.computeCurrentVelocity(1000);
            float yvel = mVelocityTracker.getYVelocity(mActivePointerId);
            fling(parent, child, -getScrollRangeForDragFling(child), 0, yvel);
        }
        // $FALLTHROUGH
    case MotionEvent.ACTION_CANCEL: {
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        break;
    }
    }

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

    return true;
}

From source file:com.intel.xdk.multitouch.MultiTouch.java

@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
    super.initialize(cordova, webView);

    //get convenience reference to activity
    activity = cordova.getActivity();/*from w  w w . j a v a  2 s  .  c  o  m*/

    activity.runOnUiThread(new Runnable() {
        public void run() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                try {
                    Method m = WebView.class.getMethod("setWebContentsDebuggingEnabled", boolean.class);
                    m.invoke(WebView.class, true);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                //WebView.setWebContentsDebuggingEnabled(true);
            }
        }
    });

    touchy = new View.OnTouchListener() {

        private SparseArray<Long> pointerId2Identifier = new SparseArray<Long>();

        @Override
        public boolean onTouch(View v, MotionEvent ev) {
            if (isMultitouchEnabled) {
                int pointerCount = ev.getPointerCount();

                //get the correct action
                int maskedAction = ev.getActionMasked();
                int idx = ev.getActionIndex();
                try {
                    String js = null;
                    if (maskedAction == MotionEvent.ACTION_POINTER_DOWN
                            || maskedAction == MotionEvent.ACTION_DOWN) {
                        int x = (int) ev.getX(idx);
                        int y = (int) ev.getY(idx);
                        int h = (int) v.getHeight();
                        int w = (int) v.getWidth();
                        int id = ev.getPointerId(idx);
                        //make a timestamp identifier and store it
                        long identifier = System.currentTimeMillis();
                        pointerId2Identifier.put(id, identifier);
                        js = String.format("{id:%d,x:%d,y:%d,h:%d,w:%d,type:'touchstart'},", identifier, x, y,
                                h, w);
                        queueMultitouchData(js, id, maskedAction);
                    } else if (maskedAction == MotionEvent.ACTION_POINTER_UP
                            || maskedAction == MotionEvent.ACTION_UP) {
                        int x = (int) ev.getX(idx);
                        int y = (int) ev.getY(idx);
                        int h = (int) v.getHeight();
                        int w = (int) v.getWidth();
                        int id = ev.getPointerId(idx);
                        js = String.format("{id:%d,x:%d,y:%d,h:%d,w:%d,type:'touchend'},",
                                pointerId2Identifier.get(id), x, y, h, w);
                        pointerId2Identifier.remove(id);
                        queueMultitouchData(js, id, maskedAction);
                    } else if (maskedAction == MotionEvent.ACTION_MOVE) {
                        //send all events if it is a move
                        for (int i = 0; i < pointerCount; i++) {
                            int x = (int) ev.getX(i);
                            int y = (int) ev.getY(i);
                            int h = (int) v.getHeight();
                            int w = (int) v.getWidth();
                            int id = ev.getPointerId(i);
                            js = String.format("{id:%d,x:%d,y:%d,h:%d,w:%d,type:'touchmove'},",
                                    pointerId2Identifier.get(id), x, y, h, w);
                            queueMultitouchData(js, id, maskedAction);
                        }
                    } else {
                        Log.e("[intel.xdk]", "got a MotionEvent that is not up/down/move:" + ev);
                    }
                    //Log.i("[intel.xdk]", "onTouchEvent:"+js);
                } catch (Exception e) {
                    Log.e("[intel.xdk]", "Got an exception back from WebView: ", e);
                }

                return true;
            } else {
                return false;
            }
        }
    };
    webView.setOnTouchListener(touchy);

    lock = this;
    messages = new ArrayList<String>(0);
}

From source file:de.Maxr1998.xposed.maxlock.ui.LockFragment.java

@SuppressWarnings("deprecation")
private void setupKnockCodeLayout() {
    final View container = rootView.findViewById(R.id.container);
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) container.getLayoutParams();
    params.setMargins(0, 0, 0, 0);/*from  www.  j a v a2  s.c o m*/
    container.setLayoutParams(params);
    container.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent e) {
            if (e.getActionMasked() == MotionEvent.ACTION_DOWN) {
                mInputText.append("\u2022");

                // Center values
                int[] loc = new int[2];
                container.getLocationOnScreen(loc);
                int viewCenterX = loc[0] + container.getWidth() / 2;
                int viewCenterY = loc[1] + container.getHeight() / 2;

                // Track touch positions
                knockCodeX.add(e.getRawX());
                knockCodeY.add(e.getRawY());
                if (knockCodeX.size() != knockCodeY.size()) {
                    throw new RuntimeException("The amount of the X and Y coordinates doesn't match!");
                }

                // Calculate center
                float centerX;
                float differenceX = Collections.max(knockCodeX) - Collections.min(knockCodeX);
                if (differenceX > 50) {
                    centerX = Collections.min(knockCodeX) + differenceX / 2;
                } else
                    centerX = viewCenterX;

                float centerY;
                float differenceY = Collections.max(knockCodeY) - Collections.min(knockCodeY);
                if (differenceY > 50) {
                    centerY = Collections.min(knockCodeY) + differenceY / 2;
                } else
                    centerY = viewCenterY;

                // Calculate key
                key.setLength(0);
                for (int i = 0; i < knockCodeX.size(); i++) {
                    float x = knockCodeX.get(i), y = knockCodeY.get(i);
                    if (x < centerX && y < centerY)
                        key.append("1");
                    else if (x > centerX && y < centerY)
                        key.append("2");
                    else if (x < centerX && y > centerY)
                        key.append("3");
                    else if (x > centerX && y > centerY)
                        key.append("4");
                }
                checkInput();
                return true;
            }
            return false;
        }
    });
    divider = new View(getActivity());
    divider.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            Math.round(getResources().getDisplayMetrics().density)));
    divider.setBackgroundColor(getResources().getColor(R.color.light_white));
    ((ViewGroup) container).addView(divider);
    if (prefs.getBoolean(Common.INVERT_COLOR, false) && prefs.getBoolean(Common.KC_SHOW_DIVIDERS, true)) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
            divider.setBackground(getResources().getDrawable(android.R.color.black));
        else
            divider.setBackgroundDrawable(getResources().getDrawable(android.R.color.black));
    } else if (!prefs.getBoolean(Common.KC_SHOW_DIVIDERS, true) || screenWidth > screenHeight) {
        divider.setVisibility(View.GONE);
    }
}

From source file:com.personal.taskmanager2.utilities.RecyclerViewTouchListener.java

@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
    boolean mPaused = mRecyclerView.getScrollState() != RecyclerView.SCROLL_STATE_IDLE;

    mViewWidth = mRecyclerView.getWidth();

    switch (e.getActionMasked()) {
    case MotionEvent.ACTION_CANCEL: {
        if (mVelocityTracker == null) {
            break;
        }/* www. ja  va2  s . c om*/
        Log.d(TAG, "Cancel Intercept");

        if (mChildView != null) {
            // cancel
            mChildView.animate().translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mDownX = 0;
        mDeltaX = 0;
        mChildView = null;
        mChildPosition = RecyclerView.NO_POSITION;
        mSwiping = false;

        break;
    }

    case MotionEvent.ACTION_DOWN: {
        if (mPaused) {
            return false;
        }
        Log.d(TAG, "Down Intercept");

        mChildView = mRecyclerView.findChildViewUnder(e.getRawX(), e.getY());
        if (mChildView == null) {
            Log.d(TAG, "child view is null in action down");
            break;
        }

        mChildPosition = mRecyclerView.getChildPosition(mChildView);
        mDownX = e.getRawX();
        if (mCallbacks.canDismiss(mChildPosition)) {
            mVelocityTracker = VelocityTracker.obtain();
            mVelocityTracker.addMovement(e);
        } else {
            mChildView = null;
        }
        //rv.onTouchEvent(e);
        return false;
    }

    case MotionEvent.ACTION_UP: {
        if (mVelocityTracker == null) {
            break;
        }

        Log.d(TAG, "Up Intercept");

        float deltaX = e.getRawX() - mDownX;
        float absDeltaX = Math.abs(deltaX);
        mVelocityTracker.addMovement(e);
        mVelocityTracker.computeCurrentVelocity(1000);
        float velocityX = mVelocityTracker.getXVelocity();
        float absVelocityX = Math.abs(velocityX);
        float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
        boolean dismiss = false;
        boolean dismissRight = false;
        if (absDeltaX > mViewWidth / 2) {
            dismiss = true;
            dismissRight = deltaX > 0;
        } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity
                && absVelocityY < absVelocityX) {
            // dismiss only if flinging in the same direction as dragging
            dismiss = (velocityX < 0) == (deltaX < 0);
            dismissRight = mVelocityTracker.getXVelocity() > 0;
        }

        if (dismiss) {
            dismiss(mChildView, mChildPosition, dismissRight);
        } else {
            mChildView.animate().alpha(1).translationX(0).setDuration(mAnimationTime).setListener(null);
        }

        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mDownX = 0;
        mDeltaX = 0;
        mChildView = null;
        mChildPosition = RecyclerView.NO_POSITION;
        mSwiping = false;
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (mVelocityTracker == null || mPaused) {
            break;
        }

        Log.d(TAG, "Move Intercept");

        mVelocityTracker.addMovement(e);
        float deltaX = e.getRawX() - mDownX;
        float absDeltaX = Math.abs(deltaX);
        if (absDeltaX > mSlop) {
            mDeltaX = deltaX;
            return true;
        }
        break;
    }
    }
    return false;
}