Search in sources :

Example 16 with MotionEvent

use of android.view.MotionEvent in project android_frameworks_base by ParanoidAndroid.

the class TouchExplorer method sendMotionEvent.

/**
     * Sends an event.
     *
     * @param prototype The prototype from which to create the injected events.
     * @param action The action of the event.
     * @param pointerIdBits The bits of the pointers to send.
     * @param policyFlags The policy flags associated with the event.
     */
private void sendMotionEvent(MotionEvent prototype, int action, int pointerIdBits, int policyFlags) {
    prototype.setAction(action);
    MotionEvent event = null;
    if (pointerIdBits == ALL_POINTER_ID_BITS) {
        event = prototype;
    } else {
        event = prototype.split(pointerIdBits);
    }
    if (action == MotionEvent.ACTION_DOWN) {
        event.setDownTime(event.getEventTime());
    } else {
        event.setDownTime(mInjectedPointerTracker.getLastInjectedDownEventTime());
    }
    // on the wrong item since click and long press behave differently.
    if (mLongPressingPointerId >= 0) {
        final int remappedIndex = event.findPointerIndex(mLongPressingPointerId);
        final int pointerCount = event.getPointerCount();
        PointerProperties[] props = PointerProperties.createArray(pointerCount);
        PointerCoords[] coords = PointerCoords.createArray(pointerCount);
        for (int i = 0; i < pointerCount; i++) {
            event.getPointerProperties(i, props[i]);
            event.getPointerCoords(i, coords[i]);
            if (i == remappedIndex) {
                coords[i].x -= mLongPressingPointerDeltaX;
                coords[i].y -= mLongPressingPointerDeltaY;
            }
        }
        MotionEvent remapped = MotionEvent.obtain(event.getDownTime(), event.getEventTime(), event.getAction(), event.getPointerCount(), props, coords, event.getMetaState(), event.getButtonState(), 1.0f, 1.0f, event.getDeviceId(), event.getEdgeFlags(), event.getSource(), event.getFlags());
        if (event != prototype) {
            event.recycle();
        }
        event = remapped;
    }
    if (DEBUG) {
        Slog.d(LOG_TAG, "Injecting event: " + event + ", policyFlags=0x" + Integer.toHexString(policyFlags));
    }
    // Make sure that the user will see the event.
    policyFlags |= WindowManagerPolicy.FLAG_PASS_TO_USER;
    if (mNext != null) {
        // TODO: For now pass null for the raw event since the touch
        //       explorer is the last event transformation and it does
        //       not care about the raw event.
        mNext.onMotionEvent(event, null, policyFlags);
    }
    mInjectedPointerTracker.onMotionEvent(event);
    if (event != prototype) {
        event.recycle();
    }
}
Also used : PointerCoords(android.view.MotionEvent.PointerCoords) PointerProperties(android.view.MotionEvent.PointerProperties) GesturePoint(android.gesture.GesturePoint) MotionEvent(android.view.MotionEvent)

Example 17 with MotionEvent

use of android.view.MotionEvent in project android_frameworks_base by ParanoidAndroid.

the class AccessibilityInputFilter method onInputEvent.

@Override
public void onInputEvent(InputEvent event, int policyFlags) {
    if (DEBUG) {
        Slog.d(TAG, "Received event: " + event + ", policyFlags=0x" + Integer.toHexString(policyFlags));
    }
    if (event instanceof MotionEvent && event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN)) {
        MotionEvent motionEvent = (MotionEvent) event;
        onMotionEvent(motionEvent, policyFlags);
    } else if (event instanceof KeyEvent && event.isFromSource(InputDevice.SOURCE_KEYBOARD)) {
        KeyEvent keyEvent = (KeyEvent) event;
        onKeyEvent(keyEvent, policyFlags);
    } else {
        super.onInputEvent(event, policyFlags);
    }
}
Also used : KeyEvent(android.view.KeyEvent) MotionEvent(android.view.MotionEvent)

Example 18 with MotionEvent

use of android.view.MotionEvent in project android_frameworks_base by ParanoidAndroid.

the class TouchUtils method drag.

/**
     * Simulate touching a specific location and dragging to a new location.
     * 
     * @param test The test case that is being run
     * @param fromX X coordinate of the initial touch, in screen coordinates
     * @param toX Xcoordinate of the drag destination, in screen coordinates
     * @param fromY X coordinate of the initial touch, in screen coordinates
     * @param toY Y coordinate of the drag destination, in screen coordinates
     * @param stepCount How many move steps to include in the drag
     */
public static void drag(InstrumentationTestCase test, float fromX, float toX, float fromY, float toY, int stepCount) {
    Instrumentation inst = test.getInstrumentation();
    long downTime = SystemClock.uptimeMillis();
    long eventTime = SystemClock.uptimeMillis();
    float y = fromY;
    float x = fromX;
    float yStep = (toY - fromY) / stepCount;
    float xStep = (toX - fromX) / stepCount;
    MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, 0);
    inst.sendPointerSync(event);
    inst.waitForIdleSync();
    for (int i = 0; i < stepCount; ++i) {
        y += yStep;
        x += xStep;
        eventTime = SystemClock.uptimeMillis();
        event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, x, y, 0);
        inst.sendPointerSync(event);
        inst.waitForIdleSync();
    }
    eventTime = SystemClock.uptimeMillis();
    event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, 0);
    inst.sendPointerSync(event);
    inst.waitForIdleSync();
}
Also used : Instrumentation(android.app.Instrumentation) Point(android.graphics.Point) MotionEvent(android.view.MotionEvent)

Example 19 with MotionEvent

use of android.view.MotionEvent in project android_frameworks_base by ParanoidAndroid.

the class TouchUtils method touchAndCancelView.

/**
     * Simulate touching the center of a view and cancelling (so no onClick should
     * fire, etc).
     *
     * @param test The test case that is being run
     * @param v The view that should be clicked
     */
public static void touchAndCancelView(InstrumentationTestCase test, View v) {
    int[] xy = new int[2];
    v.getLocationOnScreen(xy);
    final int viewWidth = v.getWidth();
    final int viewHeight = v.getHeight();
    final float x = xy[0] + (viewWidth / 2.0f);
    float y = xy[1] + (viewHeight / 2.0f);
    Instrumentation inst = test.getInstrumentation();
    long downTime = SystemClock.uptimeMillis();
    long eventTime = SystemClock.uptimeMillis();
    MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, 0);
    inst.sendPointerSync(event);
    inst.waitForIdleSync();
    eventTime = SystemClock.uptimeMillis();
    final int touchSlop = ViewConfiguration.get(v.getContext()).getScaledTouchSlop();
    event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_CANCEL, x + (touchSlop / 2.0f), y + (touchSlop / 2.0f), 0);
    inst.sendPointerSync(event);
    inst.waitForIdleSync();
}
Also used : Instrumentation(android.app.Instrumentation) Point(android.graphics.Point) MotionEvent(android.view.MotionEvent)

Example 20 with MotionEvent

use of android.view.MotionEvent in project android_frameworks_base by ParanoidAndroid.

the class TouchUtils method longClickView.

/**
     * Simulate touching the center of a view, holding until it is a long press, and then releasing.
     * 
     * @param test The test case that is being run
     * @param v The view that should be clicked
     */
public static void longClickView(InstrumentationTestCase test, View v) {
    int[] xy = new int[2];
    v.getLocationOnScreen(xy);
    final int viewWidth = v.getWidth();
    final int viewHeight = v.getHeight();
    final float x = xy[0] + (viewWidth / 2.0f);
    float y = xy[1] + (viewHeight / 2.0f);
    Instrumentation inst = test.getInstrumentation();
    long downTime = SystemClock.uptimeMillis();
    long eventTime = SystemClock.uptimeMillis();
    MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, 0);
    inst.sendPointerSync(event);
    inst.waitForIdleSync();
    eventTime = SystemClock.uptimeMillis();
    final int touchSlop = ViewConfiguration.get(v.getContext()).getScaledTouchSlop();
    event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, x + touchSlop / 2, y + touchSlop / 2, 0);
    inst.sendPointerSync(event);
    inst.waitForIdleSync();
    try {
        Thread.sleep((long) (ViewConfiguration.getLongPressTimeout() * 1.5f));
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    eventTime = SystemClock.uptimeMillis();
    event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, 0);
    inst.sendPointerSync(event);
    inst.waitForIdleSync();
}
Also used : Instrumentation(android.app.Instrumentation) Point(android.graphics.Point) MotionEvent(android.view.MotionEvent)

Aggregations

MotionEvent (android.view.MotionEvent)1088 View (android.view.View)497 TextView (android.widget.TextView)264 ImageView (android.widget.ImageView)190 GestureDetector (android.view.GestureDetector)95 Point (android.graphics.Point)81 AdapterView (android.widget.AdapterView)80 ViewGroup (android.view.ViewGroup)75 Paint (android.graphics.Paint)72 ListView (android.widget.ListView)70 SuppressLint (android.annotation.SuppressLint)69 OnTouchListener (android.view.View.OnTouchListener)69 Intent (android.content.Intent)68 RecyclerView (android.support.v7.widget.RecyclerView)53 Test (org.junit.Test)47 ScrollView (android.widget.ScrollView)46 Handler (android.os.Handler)43 AbsListView (android.widget.AbsListView)42 WindowManager (android.view.WindowManager)41 LinearLayout (android.widget.LinearLayout)40