1/* 2 * Copyright (C) 2013 DroidDriver committers 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.google.android.droiddriver.util; 18 19import android.os.SystemClock; 20import android.view.InputDevice; 21import android.view.KeyEvent; 22import android.view.MotionEvent; 23 24import com.google.android.droiddriver.InputInjector; 25import com.google.android.droiddriver.exceptions.ActionException; 26 27/** 28 * Helper methods to create InputEvents. 29 */ 30public class Events { 31 /** 32 * @return a touch down event at the specified coordinates 33 */ 34 public static MotionEvent newTouchDownEvent(int x, int y) { 35 long downTime = SystemClock.uptimeMillis(); 36 MotionEvent event = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_DOWN, x, y, 1); 37 event.setSource(InputDevice.SOURCE_TOUCHSCREEN); 38 return event; 39 } 40 41 /** 42 * @return a touch up event at the specified coordinates 43 */ 44 public static MotionEvent newTouchUpEvent(long downTime, int x, int y) { 45 long eventTime = SystemClock.uptimeMillis(); 46 MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, 1); 47 event.setSource(InputDevice.SOURCE_TOUCHSCREEN); 48 return event; 49 } 50 51 /** 52 * @return a touch move event at the specified coordinates 53 */ 54 public static MotionEvent newTouchMoveEvent(long downTime, int x, int y) { 55 long eventTime = SystemClock.uptimeMillis(); 56 MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, x, y, 1); 57 event.setSource(InputDevice.SOURCE_TOUCHSCREEN); 58 return event; 59 } 60 61 public static KeyEvent newKeyEvent(long downTime, int action, int keyCode) { 62 KeyEvent event = new KeyEvent(downTime, downTime, action, keyCode, 0 /* repeat */); 63 event.setSource(InputDevice.SOURCE_KEYBOARD); 64 return event; 65 } 66 67 /** 68 * Injects {@code event}. {@code event} is recycled and should not be used 69 * after. 70 * 71 * @throws ActionException if injection failed 72 */ 73 public static void injectEvent(InputInjector injector, MotionEvent event) { 74 Logs.call(injector, "injectInputEvent", event); 75 try { 76 if (!injector.injectInputEvent(event)) { 77 throw new ActionException("Failed to inject " + event); 78 } 79 } finally { 80 event.recycle(); 81 } 82 } 83 84 public static long touchDown(InputInjector injector, int x, int y) { 85 MotionEvent downEvent = newTouchDownEvent(x, y); 86 long downTime = downEvent.getDownTime(); 87 injectEvent(injector, downEvent); 88 return downTime; 89 } 90 91 public static void touchUp(InputInjector injector, long downTime, int x, int y) { 92 injectEvent(injector, newTouchUpEvent(downTime, x, y)); 93 } 94 95 public static void touchMove(InputInjector injector, long downTime, int x, int y) { 96 injectEvent(injector, newTouchMoveEvent(downTime, x, y)); 97 } 98 99 private Events() {} 100} 101