1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| public void test() {
injectMultiTouch([[PointerCoords,...],[PointerCoords,...]]); }
private PointerCoords createPoint(int x, int y) { PointerCoords pointerCoords = new PointerCoords(); pointerCoords.x = x; pointerCoords.y = y; pointerCoords.pressure = 1; pointerCoords.size = 1; return pointerCoords; }
private boolean injectMultiTouch(PointerCoords[]... touches) { boolean ret = true; if (touches.length < 2) { throw new IllegalArgumentException("Must provide coordinates for at least 2 pointers"); }
int maxSteps = 0; for (int x = 0; x < touches.length; x++) maxSteps = (maxSteps < touches[x].length) ? touches[x].length : maxSteps;
PointerProperties[] properties = new PointerProperties[touches.length]; PointerCoords[] pointerCoords = new PointerCoords[touches.length]; for (int x = 0; x < touches.length; x++) { MotionEvent.PointerProperties prop = new MotionEvent.PointerProperties(); prop.id = x; prop.toolType = MotionEvent.TOOL_TYPE_FINGER; properties[x] = prop;
pointerCoords[x] = touches[x][0]; }
long downTime = SystemClock.uptimeMillis(); MotionEvent event; event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 1, properties, pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0); ret &= mUiAutomation.injectInputEvent(event, true);
for (int x = 1; x < touches.length; x++) { event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), getPointerAction(MotionEvent.ACTION_POINTER_DOWN, x), x + 1, properties, pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0); ret &= mUiAutomation.injectInputEvent(event, true); }
for (int i = 1; i < maxSteps - 1; i++) { for (int x = 0; x < touches.length; x++) { if (touches[x].length > i) pointerCoords[x] = touches[x][i]; else pointerCoords[x] = touches[x][touches[x].length - 1]; }
event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE, touches.length, properties, pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0);
ret &= mUiAutomation.injectInputEvent(event, true); SystemClock.sleep(5); }
for (int x = 0; x < touches.length; x++) pointerCoords[x] = touches[x][touches[x].length - 1];
for (int x = 1; x < touches.length; x++) { event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), getPointerAction(MotionEvent.ACTION_POINTER_UP, x), x + 1, properties, pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0); ret &= mUiAutomation.injectInputEvent(event, true); }
event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 1, properties, pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0); ret &= mUiAutomation.injectInputEvent(event, true);
return ret; }
|