MotionEvent me = MotionEvent.obtain( downTime, eventTime, action, x, y, metaState ); me.setSource(4098); view.dispatchTouchEvent(me);
MotionEvent event = MotionEvent.obtain(when, when, action, x, y, pressure, DEFAULT_SIZE,DEFAULT_META_STATE, DEFAULT_PRECISION_X, DEFAULT_PRECISION_Y, DEFAULT_DEVICE_ID,DEFAULT_EDGE_FLAGS); event.setSource(inputSource); InputManager.getInstance().injectInputEvent(event,InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
/** * Builds a MotionEvent and injects it into the event stream. * * @param inputSource the InputDevice.SOURCE_* sending the input event * @param action the MotionEvent.ACTION_* for the event * @param when the value of SystemClock.uptimeMillis() at which the event happened * @param x x coordinate of event * @param y y coordinate of event * @param pressure pressure of event */ private void injectMotionEvent(int inputSource, int action, long when, float x, float y, float pressure) { final float DEFAULT_SIZE = 1.0f; final int DEFAULT_META_STATE = 0; final float DEFAULT_PRECISION_X = 1.0f; final float DEFAULT_PRECISION_Y = 1.0f; final int DEFAULT_DEVICE_ID = 0; final int DEFAULT_EDGE_FLAGS = 0; MotionEvent event = MotionEvent.obtain(when, when, action, x, y, pressure, DEFAULT_SIZE, DEFAULT_META_STATE, DEFAULT_PRECISION_X, DEFAULT_PRECISION_Y, DEFAULT_DEVICE_ID, DEFAULT_EDGE_FLAGS); event.setSource(inputSource); Log.i(TAG, "injectMotionEvent: " + event); InputManager.getInstance().injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH); }
private static void injectMotionEvent(InputManager im, Method injectInputEventMethod, int inputSource, int action, long downTime, long eventTime, float x, float y, float pressure) throws InvocationTargetException, IllegalAccessException { MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, pressure, 1.0f, 0, 1.0f, 1.0f, 0, 0); event.setSource(inputSource); injectInputEventMethod.invoke(im, new Object[]{event, Integer.valueOf(0)}); }
private MotionEvent getMotionEvent(long when, int action, float x, float y, float pressure, int inputSource) { MotionEvent event = MotionEvent.obtain(when, when, action, x, y, pressure, Config.DEFAULT_SIZE, Config.DEFAULT_META_STATE, Config.DEFAULT_PRECISION_X, Config.DEFAULT_PRECISION_Y, getInputDeviceId(inputSource), Config.DEFAULT_EDGE_FLAGS); event.setSource(inputSource); return event; }
//========================================================================= //== Utility Methods === //========================================================================= /** * Helper method injects a click event at a point on the active screen via the UiAutomation object. * @param x the x position on the screen to inject the click event * @param y the y position on the screen to inject the click event * @param automation a UiAutomation object rtreived through the current Instrumentation */ static void injectClickEvent(float x, float y, UiAutomation automation){ //A MotionEvent is a type of InputEvent. //The event time must be the current uptime. final long eventTime = SystemClock.uptimeMillis(); //A typical click event triggered by a user click on the touchscreen creates two MotionEvents, //first one with the action KeyEvent.ACTION_DOWN and the 2nd with the action KeyEvent.ACTION_UP MotionEvent motionDown = MotionEvent.obtain(eventTime, eventTime, KeyEvent.ACTION_DOWN, x, y, 0); //We must set the source of the MotionEvent or the click doesn't work. motionDown.setSource(InputDevice.SOURCE_TOUCHSCREEN); automation.injectInputEvent(motionDown, true); MotionEvent motionUp = MotionEvent.obtain(eventTime, eventTime, KeyEvent.ACTION_UP, x, y, 0); motionUp.setSource(InputDevice.SOURCE_TOUCHSCREEN); automation.injectInputEvent(motionUp, true); //Recycle our events back to the system pool. motionUp.recycle(); motionDown.recycle(); }
@SuppressLint("ClickableViewAccessibility") @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: DSListView.this.showListView(); break; case MotionEvent.ACTION_UP: DSListView.this.readyToHide = true; hideListView(); break; } event.offsetLocation(0, selectorTopMargin + topMarginCompensation); event.setSource(MOTION_EVENT_SOURCE); DSListView.this.listView.onTouchEvent(event); return true; } });
final MotionEvent eventDown = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, 0); eventDown.setSource(InputDevice.SOURCE_TOUCHSCREEN); final MotionEvent eventUp = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, 0); eventUp.setSource(InputDevice.SOURCE_TOUCHSCREEN); Thread task = new Thread(new Runnable() {
public void run() { float zoom = view.getScale(); for (MotionEvent event : events) { event.setLocation(zoom * event.getX(), zoom * event.getY()); try { event.setSource(InputDevice.SOURCE_CLASS_POINTER); } catch (NoSuchMethodError e) { throw new WebDriverException("You are using an Android WebDriver APK " + "for ICS SDKs or more recent SDK versions. For more info see " + "http://code.google.com/p/selenium/wiki/AndroidDriver#Supported_Platforms.", e); } view.dispatchTouchEvent(event); synchronized (syncObject) { done = true; syncObject.notify(); } } } });
public boolean injectInputEvent(int action, float x, float y, int metaState) { MotionEvent e = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), action, x, y, metaState); e.setSource(InputDevice.SOURCE_TOUCHSCREEN); boolean b = uiAutomation.injectInputEvent(e, true); e.recycle(); return b; }
mLongClickStarted = -1; if (wasLong) { // setSource is a hack to let us know to ignore that cancel. ev.setSource(InputDevice.SOURCE_ANY); //returning true means target will get ACTION_CANCEL. wasLong = false; return true;