Example usage for android.view MotionEvent getEventTime

List of usage examples for android.view MotionEvent getEventTime

Introduction

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

Prototype

@Override
public final long getEventTime() 

Source Link

Document

Retrieve the time this event occurred, in the android.os.SystemClock#uptimeMillis time base.

Usage

From source file:ac.robinson.mediaphone.MediaPhoneActivity.java

@Override
public boolean dispatchTouchEvent(MotionEvent e) {
    if (e.getEventTime() < mResumeTime) {
        if (MediaPhone.DEBUG) {
            Log.d(DebugUtilities.getLogTag(this),
                    "Discarded touch event with start time earlier than onResume()");
        }//  w  ww . jav a  2  s  .  c  o m
        return true;
    }

    if (mGestureDetector != null) {
        if (mGestureDetector.onTouchEvent(e)) {
            e.setAction(MotionEvent.ACTION_CANCEL); // swipe detected - don't do the normal event
        }
    }

    try {
        return super.dispatchTouchEvent(e);
    } catch (NullPointerException ex) {
        if (MediaPhone.DEBUG) {
            Log.d(DebugUtilities.getLogTag(this),
                    "Catching touch event Null Pointer Exception; ignoring touch");
        }
        return true; // reported on Play Store - see: http://stackoverflow.com/a/13031529/1993220
    }
}

From source file:refresh.renyi.io.supperrefreshview.customview.VerticalViewPager.java

private void sendCancelEvent() {
    System.out.println("sendCancelEvent");
    MotionEvent last = mLastMoveEvent;
    MotionEvent e = MotionEvent.obtain(last.getDownTime(), last.getEventTime(), MotionEvent.ACTION_CANCEL,
            last.getX(), last.getY(), last.getMetaState());
    dispatchTouchEventSupper(e);//from  w  w  w. j a  v a  2 s . c o  m
}

From source file:refresh.renyi.io.supperrefreshview.customview.VerticalViewPager.java

private void sendDownEvent() {
    System.out.println("sendDownEvent");
    final MotionEvent last = mLastMoveEvent;
    if (last == null)
        return;/*  w  w w .  ja v  a2s.  c o  m*/
    MotionEvent e = MotionEvent.obtain(last.getDownTime(), last.getEventTime(), MotionEvent.ACTION_DOWN,
            last.getX(), last.getY(), last.getMetaState());
    dispatchTouchEventSupper(e);
}

From source file:com.yek.keyboard.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  w w . jav  a2  s  .com

    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 Keyboard.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.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   ww  w  .j  av 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:org.telegram.ui.Components.NumberPicker.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!isEnabled()) {
        return false;
    }//from   w  ww.j  a  va 2s  .c  o m
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    int action = event.getActionMasked();
    switch (action) {
    case MotionEvent.ACTION_MOVE: {
        if (mIngonreMoveEvents) {
            break;
        }
        float currentMoveY = event.getY();
        if (mScrollState != OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
            int deltaDownY = (int) Math.abs(currentMoveY - mLastDownEventY);
            if (deltaDownY > mTouchSlop) {
                removeAllCallbacks();
                onScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
            }
        } else {
            int deltaMoveY = (int) ((currentMoveY - mLastDownOrMoveEventY));
            scrollBy(0, deltaMoveY);
            invalidate();
        }
        mLastDownOrMoveEventY = currentMoveY;
    }
        break;
    case MotionEvent.ACTION_UP: {
        removeChangeCurrentByOneFromLongPress();
        mPressedStateHelper.cancel();
        VelocityTracker velocityTracker = mVelocityTracker;
        velocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity);
        int initialVelocity = (int) velocityTracker.getYVelocity();
        if (Math.abs(initialVelocity) > mMinimumFlingVelocity) {
            fling(initialVelocity);
            onScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);
        } else {
            int eventY = (int) event.getY();
            int deltaMoveY = (int) Math.abs(eventY - mLastDownEventY);
            long deltaTime = event.getEventTime() - mLastDownEventTime;
            if (deltaMoveY <= mTouchSlop && deltaTime < ViewConfiguration.getTapTimeout()) {
                int selectorIndexOffset = (eventY / mSelectorElementHeight) - SELECTOR_MIDDLE_ITEM_INDEX;
                if (selectorIndexOffset > 0) {
                    changeValueByOne(true);
                    mPressedStateHelper.buttonTapped(PressedStateHelper.BUTTON_INCREMENT);
                } else if (selectorIndexOffset < 0) {
                    changeValueByOne(false);
                    mPressedStateHelper.buttonTapped(PressedStateHelper.BUTTON_DECREMENT);
                }
            } else {
                ensureScrollWheelAdjusted();
            }
            onScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
    }
        break;
    }
    return true;
}

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

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

    final int action = MotionEventCompat.getActionMasked(nativeMotionEvent);
    final int pointerCount = MotionEventCompat.getPointerCount(nativeMotionEvent);
    if (pointerCount > 1)
        mLastTimeHadTwoFingers = SystemClock.elapsedRealtime();//marking the time. Read isAtTwoFingersState()

    if (mTouchesAreDisabledTillLastFingerIsUp) {
        if (!areTouchesDisabled(nativeMotionEvent)/*this means it was just reset*/) {
            mTouchesAreDisabledTillLastFingerIsUp = false;
            //continue with onTouchEvent flow.
            if (action != MotionEvent.ACTION_DOWN) {
                //swallowing the event.
                //in case this is a DOWN event, we do want to pass it
                return true;
            }/*  ww  w .  ja v a 2  s.  c  om*/
        } else {
            //swallowing touch event until we reset mTouchesAreDisabledTillLastFingerIsUp
            return true;
        }
    }

    final long eventTime = nativeMotionEvent.getEventTime();
    final int index = MotionEventCompat.getActionIndex(nativeMotionEvent);
    final int id = nativeMotionEvent.getPointerId(index);
    final int x = (int) nativeMotionEvent.getX(index);
    final int y = (int) nativeMotionEvent.getY(index);

    if (mKeyPressTimingHandler.isInKeyRepeat()) {
        // It will keep being in the key repeating mode while the key is
        // being pressed.
        if (action == MotionEvent.ACTION_MOVE) {
            return true;
        }
        final PointerTracker tracker = getPointerTracker(id);
        // Key repeating timer will be canceled if 2 or more keys are in
        // action, and current
        // event (UP or DOWN) is non-modifier key.
        if (pointerCount > 1 && !tracker.isModifier()) {
            mKeyPressTimingHandler.cancelKeyRepeatTimer();
        }
        // Up event will pass through.
    }

    if (action == MotionEvent.ACTION_MOVE) {
        for (int i = 0; i < pointerCount; i++) {
            PointerTracker tracker = getPointerTracker(nativeMotionEvent.getPointerId(i));
            tracker.onMoveEvent((int) nativeMotionEvent.getX(i), (int) nativeMotionEvent.getY(i));
        }
    } else {
        PointerTracker tracker = getPointerTracker(id);
        sendOnXEvent(action, eventTime, x, y, tracker);
    }

    return true;
}

From source file:am.widget.tabstrip.TabStripHelper.java

boolean onTouchEvent(MotionEvent event) {
    if (mClickable) {
        final float x = event.getX();
        final float y = event.getY();
        final int action = event.getAction();
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            mClickPosition = PagerAdapter.POSITION_NONE;
            mDownX = x;//from   w  ww.  j a v  a 2 s  .  c o m
            mDownY = y;
            break;
        case MotionEvent.ACTION_UP:
            mClickPosition = PagerAdapter.POSITION_NONE;
            if (mPager != null && getPageCount() > 0) {
                if (mView instanceof TabStripView)
                    mClickPosition = ((TabStripView) mView).getClickedPosition(mDownX, mDownY, x, y);
                else if (mView instanceof TabStripViewGroup)
                    mClickPosition = ((TabStripViewGroup) mView).getClickedPosition(mDownX, mDownY, x, y);
            }
            break;
        case MotionEvent.ACTION_CANCEL:
            mClickPosition = PagerAdapter.POSITION_NONE;
            break;
        }
    }
    if (mDoubleClickable) {
        final float x = event.getX();
        final float y = event.getY();
        final int action = event.getAction();
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            mFirstDownX = mSecondDownX;
            mFirstDownY = mSecondDownY;
            mSecondDownX = x;
            mSecondDownY = y;
            break;
        case MotionEvent.ACTION_UP:
            final long downTime = event.getDownTime();
            if (mLastUpTime != 0 && downTime - mLastUpTime < DOUBLE_TAP_TIMEOUT) {
                mDoubleClicked = true;
                if (mView instanceof TabStripView)
                    mFirstPosition = ((TabStripView) mView).getClickedPosition(mFirstDownX, mFirstDownY,
                            mFirstUpX, mFirstUpY);
                else if (mView instanceof TabStripViewGroup)
                    mFirstPosition = ((TabStripViewGroup) mView).getClickedPosition(mFirstDownX, mFirstDownY,
                            mFirstUpX, mFirstUpY);
                if (mView instanceof TabStripView)
                    mSecondPosition = ((TabStripView) mView).getClickedPosition(mSecondDownX, mSecondDownY, x,
                            y);
                else if (mView instanceof TabStripViewGroup)
                    mSecondPosition = ((TabStripViewGroup) mView).getClickedPosition(mSecondDownX, mSecondDownY,
                            x, y);
                mLastUpTime = 0;
            } else {
                mDoubleClicked = false;
                mFirstPosition = PagerAdapter.POSITION_NONE;
                mSecondPosition = PagerAdapter.POSITION_NONE;
                mFirstUpX = x;
                mFirstUpY = y;
                mLastUpTime = event.getEventTime();
            }
            break;
        case MotionEvent.ACTION_CANCEL:

            break;
        }
    }

    return false;
}

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

@Override
public boolean onTouchEvent(MotionEvent nativeMotionEvent) {
    if (mKeyboard == null)//I mean, if there isn't any keyboard I'm handling, what's the point?
        return false;
    final int action = MotionEventCompat.getActionMasked(nativeMotionEvent);
    final int pointerCount = MotionEventCompat.getPointerCount(nativeMotionEvent);
    final int oldPointerCount = mOldPointerCount;
    mOldPointerCount = pointerCount;//from w  ww.j  a  v  a  2s .co m
    if (pointerCount > 1)
        mLastTimeHadTwoFingers = SystemClock.elapsedRealtime();//marking the time. Read isAtTwoFingersState()

    if (mTouchesAreDisabledTillLastFingerIsUp) {
        if (mOldPointerCount == 1 && (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP)) {
            mTouchesAreDisabledTillLastFingerIsUp = false;
        }

        return true;
    }
    // TODO: cleanup this code into a multi-touch to single-touch event
    // converter class?
    // If the device does not have distinct multi-touch support panel,
    // ignore all multi-touch
    // events except a transition from/to single-touch.
    if (!mHasDistinctMultitouch && pointerCount > 1 && oldPointerCount > 1) {
        return true;
    }

    // Gesture detector must be enabled only when mini-keyboard is not
    // on the screen.
    if (!mMiniKeyboardVisible && mGestureDetector != null
            && (mGestureDetector.onTouchEvent(nativeMotionEvent))) {
        Log.d(TAG, "Gesture detected!");
        mHandler.cancelKeyTimers();
        dismissKeyPreview();
        return true;
    }

    final long eventTime = nativeMotionEvent.getEventTime();
    final int index = MotionEventCompat.getActionIndex(nativeMotionEvent);
    final int id = nativeMotionEvent.getPointerId(index);
    final int x = (int) nativeMotionEvent.getX(index);
    final int y = (int) nativeMotionEvent.getY(index);

    // Needs to be called after the gesture detector gets a turn, as it
    // may have
    // displayed the mini keyboard
    if (mMiniKeyboard != null && mMiniKeyboardVisible) {
        final int miniKeyboardPointerIndex = nativeMotionEvent.findPointerIndex(mMiniKeyboardTrackerId);
        if (miniKeyboardPointerIndex >= 0 && miniKeyboardPointerIndex < pointerCount) {
            final int miniKeyboardX = (int) nativeMotionEvent.getX(miniKeyboardPointerIndex);
            final int miniKeyboardY = (int) nativeMotionEvent.getY(miniKeyboardPointerIndex);
            MotionEvent translated = generateMiniKeyboardMotionEvent(action, miniKeyboardX, miniKeyboardY,
                    eventTime);
            mMiniKeyboard.onTouchEvent(translated);
            translated.recycle();
        }
        return true;
    }

    if (mHandler.isInKeyRepeat()) {
        // It will keep being in the key repeating mode while the key is
        // being pressed.
        if (action == MotionEvent.ACTION_MOVE) {
            return true;
        }
        final PointerTracker tracker = getPointerTracker(id);
        // Key repeating timer will be canceled if 2 or more keys are in
        // action, and current
        // event (UP or DOWN) is non-modifier key.
        if (pointerCount > 1 && !tracker.isModifier()) {
            mHandler.cancelKeyRepeatTimer();
        }
        // Up event will pass through.
    }

    // TODO: cleanup this code into a multi-touch to single-touch event
    // converter class?
    // Translate mutli-touch event to single-touch events on the device
    // that has no distinct
    // multi-touch panel.
    if (!mHasDistinctMultitouch) {
        // Use only main (id=0) pointer tracker.
        PointerTracker tracker = getPointerTracker(0);
        if (pointerCount == 1 && oldPointerCount == 2) {
            // Multi-touch to single touch transition.
            // Send a down event for the latest pointer.
            tracker.onDownEvent(x, y, eventTime);
        } else if (pointerCount == 2 && oldPointerCount == 1) {
            // Single-touch to multi-touch transition.
            // Send an up event for the last pointer.
            tracker.onUpEvent(tracker.getLastX(), tracker.getLastY(), eventTime);
        } else if (pointerCount == 1 && oldPointerCount == 1) {
            tracker.onTouchEvent(action, x, y, eventTime);
        } else {
            Log.w(TAG, "Unknown touch panel behavior: pointer count is " + pointerCount + " (old "
                    + oldPointerCount + ")");
        }
        return true;
    }

    if (action == MotionEvent.ACTION_MOVE) {
        for (int i = 0; i < pointerCount; i++) {
            PointerTracker tracker = getPointerTracker(nativeMotionEvent.getPointerId(i));
            tracker.onMoveEvent((int) nativeMotionEvent.getX(i), (int) nativeMotionEvent.getY(i), eventTime);
        }
    } else {
        PointerTracker tracker = getPointerTracker(id);
        sendOnXEvent(action, eventTime, x, y, tracker);
    }

    return true;
}

From source file:jmri.enginedriver.throttle.java

public void gestureMove(MotionEvent event) {
    // Log.d("Engine_Driver", "gestureMove action " + event.getAction());
    if (gestureInProgress) {
        // stop the gesture timeout timer
        mainapp.throttle_msg_handler.removeCallbacks(gestureStopped);

        mVelocityTracker.addMovement(event);
        if ((event.getEventTime() - gestureLastCheckTime) > gestureCheckRate) {
            // monitor velocity and fail gesture if it is too low
            gestureLastCheckTime = event.getEventTime();
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000);
            int velocityX = (int) velocityTracker.getXVelocity();
            int velocityY = (int) velocityTracker.getYVelocity();
            // Log.d("Engine_Driver", "gestureVelocity vel " + velocityX);
            if ((Math.abs(velocityX) < threaded_application.min_fling_velocity)
                    && (Math.abs(velocityY) < threaded_application.min_fling_velocity)) {
                gestureFailed(event);/*w w  w .  j av a  2s  .  c  om*/
            }
        }
        if (gestureInProgress) {
            // restart the gesture timeout timer
            mainapp.throttle_msg_handler.postDelayed(gestureStopped, gestureCheckRate);
        }
    }
}