Example usage for android.app Activity dispatchTouchEvent

List of usage examples for android.app Activity dispatchTouchEvent

Introduction

In this page you can find the example usage for android.app Activity dispatchTouchEvent.

Prototype

public boolean dispatchTouchEvent(MotionEvent ev) 

Source Link

Document

Called to process touch screen events.

Usage

From source file:org.catrobat.catroid.uitest.util.UiTestUtils.java

public static void longClickAndDrag(final Solo solo, final float xFrom, final float yFrom, final float xTo,
        final float yTo, final int steps) {
    final Activity activity = solo.getCurrentActivity();
    Handler handler = new Handler(activity.getMainLooper());

    handler.post(new Runnable() {

        public void run() {
            MotionEvent downEvent = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
                    MotionEvent.ACTION_DOWN, xFrom, yFrom, 0);
            activity.dispatchTouchEvent(downEvent);
            downEvent.recycle();/*  ww w. jav  a 2s. c om*/
        }
    });

    solo.sleep(ViewConfiguration.getLongPressTimeout() + 200);

    handler.post(new Runnable() {
        public void run() {
            double offsetX = xTo - xFrom;
            offsetX /= steps;
            double offsetY = yTo - yFrom;
            offsetY /= steps;
            for (int i = 0; i <= steps; i++) {
                float x = xFrom + (float) (offsetX * i);
                float y = yFrom + (float) (offsetY * i);
                MotionEvent moveEvent = MotionEvent.obtain(SystemClock.uptimeMillis(),
                        SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE, x, y, 0);
                activity.dispatchTouchEvent(moveEvent);

                solo.sleep(20);
                moveEvent.recycle();
            }
        }
    });

    solo.sleep(steps * 20 + 200);

    handler.post(new Runnable() {

        public void run() {
            MotionEvent upEvent = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
                    MotionEvent.ACTION_UP, xTo, yTo, 0);
            activity.dispatchTouchEvent(upEvent);
            upEvent.recycle();
            Log.d(TAG, "longClickAndDrag finished: " + (int) yTo);
        }
    });

    solo.sleep(1000);
}