Example usage for android.view MotionEvent obtain

List of usage examples for android.view MotionEvent obtain

Introduction

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

Prototype

static public MotionEvent obtain(long downTime, long eventTime, int action, float x, float y, int metaState) 

Source Link

Document

Create a new MotionEvent, filling in a subset of the basic motion values.

Usage

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

@Override
public boolean onTouch(View v, MotionEvent event) {
    final boolean wasForwarding = mForwarding;
    final boolean forwarding;
    if (wasForwarding) {
        forwarding = onTouchForwarded(event) || !onForwardingStopped();
    } else {/*  w w w  .j a va2 s  .co  m*/
        forwarding = onTouchObserved(event) && onForwardingStarted();

        if (forwarding) {
            // Make sure we cancel any ongoing source event stream.
            final long now = SystemClock.uptimeMillis();
            final MotionEvent e = MotionEvent.obtain(now, now, MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0);
            mSrc.onTouchEvent(e);
            e.recycle();
        }
    }

    mForwarding = forwarding;
    return forwarding || wasForwarding;
}

From source file:com.jakewharton.behavior.drawer.BehaviorDelegate.java

private void cancelChildViewTouch() {
    // Cancel child touches
    if (!childrenCanceledTouch) {
        final long now = SystemClock.uptimeMillis();
        final MotionEvent cancelEvent = MotionEvent.obtain(now, now, MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0);
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            parent.getChildAt(i).dispatchTouchEvent(cancelEvent);
        }//from   www.  ja  v a 2 s. c o  m
        cancelEvent.recycle();
        childrenCanceledTouch = true;
    }
}

From source file:com.ternup.caddisfly.fragment.FormFragment.java

public void showKeyboard() {

    if (mPlaceEditText.getEditText().getText().toString().isEmpty()) {

        (new Handler()).postDelayed(new Runnable() {

            public void run() {
                mPlaceEditText.getEditText().dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),
                        SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0));
                mPlaceEditText.getEditText().dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),
                        SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 0, 0, 0));

            }/*from www. j a v a 2s . c  o m*/
        }, 300);
    }

    /*
            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
        Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(mPlaceEditText.getInputControl(), InputMethodManager.SHOW_FORCED);
    */
}

From source file:org.openqa.selendroid.server.model.AndroidNativeElement.java

private void clickOnScreen(float x, float y) {
    final ServerInstrumentation inst = ServerInstrumentation.getInstance();
    long downTime = SystemClock.uptimeMillis();
    long eventTime = SystemClock.uptimeMillis();
    final MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, 0);
    final MotionEvent event2 = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, 0);

    try {//from   ww w.ja v a 2 s .  co  m
        inst.sendPointerSync(event);
        inst.sendPointerSync(event2);
        try {
            Thread.sleep(300);
        } catch (InterruptedException ignored) {
        }
    } catch (SecurityException e) {
        SelendroidLogger.log("error while clicking element", e);
    }
}

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

private void onLongPress() {
    clearCallbacks();/* w  ww.j a v  a 2  s . co  m*/

    final View src = mSrc;
    if (!src.isEnabled() || src.isLongClickable()) {
        // Ignore long-press if the view is disabled or has its own
        // handler.
        return;
    }

    if (!onForwardingStarted()) {
        return;
    }

    // Don't let the parent intercept our events.
    src.getParent().requestDisallowInterceptTouchEvent(true);

    // Make sure we cancel any ongoing source event stream.
    final long now = SystemClock.uptimeMillis();
    final MotionEvent e = MotionEvent.obtain(now, now, MotionEvent.ACTION_CANCEL, 0, 0, 0);
    src.onTouchEvent(e);
    e.recycle();

    mForwarding = true;
}

From source file:edu.ptu.navpattern.tooltip.Tooltip.java

private View getContentView(final Builder builder) {
    GradientDrawable drawable = new GradientDrawable();
    drawable.setColor(builder.mBackgroundColor);
    drawable.setCornerRadius(builder.mCornerRadius);
    LinearLayout vgContent = new LinearLayout(builder.mContext);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        vgContent.setBackground(drawable);
    } else {//from   w w  w .j a  v  a 2 s  . com
        //noinspection deprecation
        vgContent.setBackgroundDrawable(drawable);
    }
    int padding = (int) builder.mPadding;
    vgContent.setPadding(padding, padding, padding, padding);
    vgContent.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0);
    textViewParams.gravity = Gravity.CENTER;
    textViewParams.topMargin = 1;
    vgContent.setLayoutParams(textViewParams);
    vgContent.setDividerDrawable(vgContent.getResources().getDrawable(R.drawable.divider_line));
    vgContent.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);

    if (builder.itemText != null && builder.itemText.length > 0) {
        for (int i = 0; i < builder.itemText.length; i++) {

            TextView textView = new TextView(builder.mContext);
            textView.setText(builder.itemText[i]);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 44);
            textView.setTextColor(0xffffffff);
            textView.setGravity(Gravity.CENTER_VERTICAL);
            if (builder.itemLogo != null && builder.itemLogo.length > i) {
                Drawable drawableLeft = builder.mContext.getResources().getDrawable(builder.itemLogo[i]);
                /// ??,??.
                //                    drawableLeft.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                //                    textView.setCompoundDrawables(drawableLeft, null, null, null);
                //                    textView.setCompoundDrawablePadding(4);
                //                    textView.setBackgroundDrawable(drawableLeft);
                LinearLayout linearLayout = new LinearLayout(builder.mContext);
                linearLayout.setMinimumHeight((int) dpToPx(44f));
                linearLayout.setOrientation(LinearLayout.HORIZONTAL);
                linearLayout.setGravity(Gravity.CENTER_VERTICAL);
                ImageView icon = new ImageView(builder.mContext);
                icon.setImageDrawable(drawableLeft);
                linearLayout.addView(icon);
                linearLayout.addView(textView);
                vgContent.addView(linearLayout);
                final int position = i;
                linearLayout.setClickable(false);
                linearLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (builder.mOnItemClickListener != null) {
                            builder.mOnItemClickListener.onClick(position);
                        }
                        mTouchListener.onTouch(v,
                                MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
                                        MotionEvent.ACTION_UP, v.getLeft() + 5, v.getTop() + 5, 0));
                    }
                });
            } else {
                vgContent.addView(textView);
                final int position = i;
                textView.setClickable(false);
                textView.setMinimumHeight((int) dpToPx(44f));
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (builder.mOnItemClickListener != null) {
                            builder.mOnItemClickListener.onClick(position);
                        }
                        mTouchListener.onTouch(v, null);
                    }
                });
            }
        }

    }

    mArrowView = new ImageView(builder.mContext);
    mArrowView.setImageDrawable(builder.mArrowDrawable);

    LinearLayout.LayoutParams arrowLayoutParams;
    if (mGravity == Gravity.TOP || mGravity == Gravity.BOTTOM) {
        arrowLayoutParams = new LinearLayout.LayoutParams((int) builder.mArrowWidth, (int) builder.mArrowHeight,
                0);
    } else {
        arrowLayoutParams = new LinearLayout.LayoutParams((int) builder.mArrowHeight, (int) builder.mArrowWidth,
                0);
    }
    arrowLayoutParams.gravity = Gravity.CENTER;
    mArrowView.setLayoutParams(arrowLayoutParams);

    mContentView = new LinearLayout(builder.mContext);
    mContentView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    mContentView.setOrientation(mGravity == Gravity.START || mGravity == Gravity.END ? LinearLayout.HORIZONTAL
            : LinearLayout.VERTICAL);

    padding = (int) dpToPx(5);

    switch (mGravity) {
    case Gravity.START:
        mContentView.setPadding(0, 0, padding, 0);
        break;
    case Gravity.TOP:
    case Gravity.BOTTOM:
        mContentView.setPadding(padding, 0, padding, 0);
        break;
    case Gravity.END:
        mContentView.setPadding(padding, 0, 0, 0);
        break;
    }

    if (mGravity == Gravity.TOP || mGravity == Gravity.START) {
        mContentView.addView(vgContent);
        mContentView.addView(mArrowView);
    } else {
        mContentView.addView(mArrowView);
        mContentView.addView(vgContent);
    }

    if (builder.isCancelable || builder.isDismissOnClick) {
        mContentView.setOnTouchListener(mTouchListener);
    }
    return mContentView;
}

From source file:org.openqa.selendroid.server.model.AndroidWebElement.java

@Override
public void click() {
    String tagName = getTagName();
    if (tagName != null && "OPTION".equals(tagName.toUpperCase())) {
        driver.resetPageIsLoading();/*from  w w  w  . ja  v  a2s  .c o m*/
        driver.executeAtom(AndroidAtoms.CLICK, this);
        driver.waitForPageToLoad();
    }

    Point center = getCenterCoordinates();
    long downTime = SystemClock.uptimeMillis();
    final List<MotionEvent> events = new ArrayList<MotionEvent>();

    MotionEvent downEvent = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN,
            center.x, center.y, 0);
    events.add(downEvent);
    MotionEvent upEvent = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP,
            center.x, center.y, 0);

    events.add(upEvent);

    driver.resetPageIsLoading();
    Activity current = ServerInstrumentation.getInstance().getCurrentActivity();
    EventSender.sendMotion(events, webview, current);

    // If the page started loading we should wait
    // until the page is done loading.
    driver.waitForPageToLoad();
}

From source file:com.yyl.inputmethod.accessibility.AccessibilityEntityProvider.java

/**
 * Simulates a key press by injecting touch events into the keyboard view.
 * This avoids the complexity of trackers and listeners within the keyboard.
 *
 * @param key The key to press./*from w w w.ja v a 2 s . co  m*/
 */
void simulateKeyPress(final Key key) {
    final int x = key.mHitBox.centerX();
    final int y = key.mHitBox.centerY();
    final long downTime = SystemClock.uptimeMillis();
    final MotionEvent downEvent = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_DOWN, x, y, 0);
    final MotionEvent upEvent = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP,
            x, y, 0);

    mKeyboardView.onTouchEvent(downEvent);
    mKeyboardView.onTouchEvent(upEvent);
    downEvent.recycle();
    upEvent.recycle();
}

From source file:com.android.hareime.accessibility.AccessibilityEntityProvider.java

/**
 * Simulates a key press by injecting touch events into the keyboard view.
 * This avoids the complexity of trackers and listeners within the keyboard.
 *
 * @param key The key to press./*from w w  w.  java 2s . c o  m*/
 */
void simulateKeyPress(Key key) {
    final int x = key.mHitBox.centerX();
    final int y = key.mHitBox.centerY();
    final long downTime = SystemClock.uptimeMillis();
    final MotionEvent downEvent = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_DOWN, x, y, 0);
    final MotionEvent upEvent = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP,
            x, y, 0);

    mKeyboardView.onTouchEvent(downEvent);
    mKeyboardView.onTouchEvent(upEvent);
}

From source file:com.android.inputmethod.accessibility.AccessibilityEntityProvider.java

/**
 * Simulates a key press by injecting touch events into the keyboard view.
 * This avoids the complexity of trackers and listeners within the keyboard.
 *
 * @param key The key to press./*from  w w w. jav a2  s .  c  om*/
 */
void simulateKeyPress(Key key) {
    final int x = key.mHitBox.centerX();
    final int y = key.mHitBox.centerY();
    final long downTime = SystemClock.uptimeMillis();
    final MotionEvent downEvent = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_DOWN, x, y, 0);
    final MotionEvent upEvent = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP,
            x, y, 0);

    mKeyboardView.onTouchEvent(downEvent);
    mKeyboardView.onTouchEvent(upEvent);

    downEvent.recycle();
    upEvent.recycle();
}