Example usage for android.view MotionEvent getDownTime

List of usage examples for android.view MotionEvent getDownTime

Introduction

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

Prototype

public final long getDownTime() 

Source Link

Document

Returns the time (in ms) when the user originally pressed down to start a stream of position events.

Usage

From source file:com.anysoftkeyboard.keyboards.views.AnyKeyboardView.java

@Override
public boolean onTouchEvent(@NonNull MotionEvent me) {
    if (getKeyboard() == null)//I mean, if there isn't any keyboard I'm handling, what's the point?
        return false;

    if (areTouchesDisabled(me)) {
        return super.onTouchEvent(me);
    }/*from  w ww.j  a  v  a2 s.co  m*/

    final int action = MotionEventCompat.getActionMasked(me);

    // Gesture detector must be enabled only when mini-keyboard is not
    // on the screen.
    if (!mMiniKeyboardPopup.isShowing() && mGestureDetector != null && mGestureDetector.onTouchEvent(me)) {
        Logger.d(TAG, "Gesture detected!");
        mKeyPressTimingHandler.cancelAllMessages();
        dismissAllKeyPreviews();
        return true;
    }

    if (action == MotionEvent.ACTION_DOWN) {
        mFirstTouchPoint.x = (int) me.getX();
        mFirstTouchPoint.y = (int) me.getY();
        mIsFirstDownEventInsideSpaceBar = mSpaceBarKey != null
                && mSpaceBarKey.isInside(mFirstTouchPoint.x, mFirstTouchPoint.y);
    }
    // If the motion event is above the keyboard and it's a MOVE event
    // coming even before the first MOVE event into the extension area
    if (!mIsFirstDownEventInsideSpaceBar && me.getY() < mExtensionKeyboardYActivationPoint
            && !mMiniKeyboardPopup.isShowing() && !mExtensionVisible && action == MotionEvent.ACTION_MOVE) {
        if (mExtensionKeyboardAreaEntranceTime <= 0)
            mExtensionKeyboardAreaEntranceTime = SystemClock.uptimeMillis();

        if (SystemClock.uptimeMillis()
                - mExtensionKeyboardAreaEntranceTime > DELAY_BEFORE_POPPING_UP_EXTENSION_KBD) {
            KeyboardExtension extKbd = ((ExternalAnyKeyboard) getKeyboard()).getExtensionLayout();
            if (extKbd == null || extKbd.getKeyboardResId() == AddOn.INVALID_RES_ID) {
                Logger.i(TAG, "No extension keyboard");
                return super.onTouchEvent(me);
            } else {
                // telling the main keyboard that the last touch was
                // canceled
                MotionEvent cancel = MotionEvent.obtain(me.getDownTime(), me.getEventTime(),
                        MotionEvent.ACTION_CANCEL, me.getX(), me.getY(), 0);
                super.onTouchEvent(cancel);
                cancel.recycle();

                mExtensionVisible = true;
                dismissAllKeyPreviews();
                if (mExtensionKey == null) {
                    mExtensionKey = new AnyKey(new Row(getKeyboard()), getThemedKeyboardDimens());
                    mExtensionKey.edgeFlags = 0;
                    mExtensionKey.height = 1;
                    mExtensionKey.width = 1;
                    mExtensionKey.popupResId = extKbd.getKeyboardResId();
                    mExtensionKey.externalResourcePopupLayout = mExtensionKey.popupResId != 0;
                    mExtensionKey.x = getWidth() / 2;
                    mExtensionKey.y = mExtensionKeyboardPopupOffset;
                }
                // so the popup will be right above your finger.
                mExtensionKey.x = (int) me.getX();

                onLongPress(extKbd, mExtensionKey, AnyApplication.getConfig().isStickyExtensionKeyboard(),
                        getPointerTracker(me));
                // it is an extension..
                getMiniKeyboard().setPreviewEnabled(true);
                return true;
            }
        } else {
            return super.onTouchEvent(me);
        }
    } else if (mExtensionVisible && me.getY() > mExtensionKeyboardYDismissPoint) {
        // closing the popup
        dismissPopupKeyboard();
        return true;
    } else {
        return super.onTouchEvent(me);
    }
}

From source file:com.google.blockly.android.ui.Dragger.java

/**
 * Let the Dragger know that a block was touched. This will be called when the block in the
 * workspace has been touched, but a drag has not yet been started.
 *
 * This method handles both regular touch events and intercepted touch events, with the latter
 * identified with the {@code interceptMode} parameter.  The only difference is that intercepted
 * events only return true (indicating they are handled) when a drag has been initiated. This
 * allows any underlying View, such as a field to handle the MotionEvent normally.
 *
 * @param dragMode The mode (immediate or sloppy) for handling this touch event.
 * @param dragHandler The {@link DragHandler} attached to this view.
 * @param touchedView The {@link BlockView} that detected a touch event.
 * @param event The touch event./*from   w  w w  .  j a  v a2s. c  om*/
 * @param interceptMode When true forces all {@link MotionEvent#ACTION_MOVE} events
 *                                   that match {@link #mPendingDrag} to return true / handled.
 *                                   Otherwise, it only returns true if a drag is started.
 *
 * @return True if the event was handled by this touch implementation.
 */
@VisibleForTesting
boolean onTouchBlockImpl(@DragMode int dragMode, DragHandler dragHandler, BlockView touchedView,
        MotionEvent event, boolean interceptMode) {
    if (mWithinOnTouchBlockImpl) {
        throw new IllegalStateException("onTouchBlockImpl() called recursively. Make sure OnDragHandler."
                + "maybeGetDragGroupCreator() is not manipulating the View hierarchy.");
    }
    mWithinOnTouchBlockImpl = true;

    final int action = MotionEventCompat.getActionMasked(event);

    boolean matchesPending = false;
    if (mPendingDrag != null) {
        matchesPending = mPendingDrag.isMatchAndProcessed(event, touchedView);
        if (!matchesPending && !mPendingDrag.isAlive()) {
            mPendingDrag = null; // Was a part of previous gesture. Delete.
        }
    }

    if (LOG_TOUCH_EVENTS) {
        Log.d(TAG,
                "onTouchBlockImpl: " + (dragMode == DRAG_MODE_IMMEDIATE ? "IMMEDIATE" : "SLOPPY")
                        + (interceptMode ? " intercept" : " direct") + "\n\t" + event + "\n\tMatches pending? "
                        + matchesPending);

        mMainHandler.removeCallbacks(mLogPending); // Only call once per event 'tick'
        mMainHandler.post(mLogPending);
    }

    final boolean result;
    if (action == MotionEvent.ACTION_DOWN) {
        if (mPendingDrag == null) {
            mPendingDrag = new PendingDrag(mController, touchedView, event);
            if (interceptMode) {
                // Do not handle intercepted down events. Allow child views (particularly
                // fields) to handle the touch normally.
                result = false;
            } else {
                // The user touched the block directly.
                if (dragMode == DRAG_MODE_IMMEDIATE) {
                    result = maybeStartDrag(dragHandler);
                } else {
                    result = true;
                }
            }
        } else if (matchesPending && !interceptMode) {
            // The Pending Drag was created during intercept, but the child did not handle it
            // and the event has bubbled down to here.
            if (dragMode == DRAG_MODE_IMMEDIATE) {
                result = maybeStartDrag(dragHandler);
            } else {
                result = true;
            }
        } else {
            result = false; // Pending drag already started with a different view / pointer id.
        }
    } else if (matchesPending) {
        // This touch is part of the current PendingDrag.
        if (action == MotionEvent.ACTION_MOVE) {
            if (mPendingDrag.isDragging()) {
                result = false; // We've already cancelled or started dragging.
            } else {
                // Mark all direct move events as handled, but only intercepted events if they
                // initiate a new drag.
                boolean isDragGesture = (!interceptMode && dragMode == DRAG_MODE_IMMEDIATE
                        && event.getDownTime() > TAP_TIMEOUT) || isBeyondSlopThreshold(event);
                boolean isNewDrag = isDragGesture && maybeStartDrag(dragHandler);
                result = isNewDrag || !interceptMode;
            }
        }
        // Handle the case when the user releases before moving far enough to start a drag.
        else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
            if (!mPendingDrag.isDragging()) {
                if (!interceptMode && mPendingDrag.isClick()) {
                    dragHandler.onBlockClicked(mPendingDrag);
                }
                finishDragging(FINISH_BEHAVIOR_REVERT);
            }
            result = !interceptMode;
        } else {
            result = false; // Unrecognized event action
        }
    } else {
        result = false; // Doesn't match existing drag.
    }

    if (LOG_TOUCH_EVENTS)
        Log.d(TAG, "\treturn " + result);
    mWithinOnTouchBlockImpl = false;
    return result;
}