From 3a567f17d3082ac59d4ca3816e97dc2a4b076e4d Mon Sep 17 00:00:00 2001 From: chaviw Date: Fri, 4 Jan 2019 13:36:36 -0800 Subject: [PATCH] Added shell command for some general input events Added shell command for events DOWN, UP, and MOVE. This can be invoked by calling 'adb shell input event ' This was needed to test transferTouchFocus, but can be useful for other testing as well. Test: 'adb shell input event ' Change-Id: If3e77b04c7172505e7fe8998b5b3c496044870bb --- .../src/com/android/commands/input/Input.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/cmds/input/src/com/android/commands/input/Input.java b/cmds/input/src/com/android/commands/input/Input.java index b905e94cd96f6..0c861cfd3e638 100644 --- a/cmds/input/src/com/android/commands/input/Input.java +++ b/cmds/input/src/com/android/commands/input/Input.java @@ -44,6 +44,9 @@ public class Input extends BaseCommand { private static final String INVALID_DISPLAY_ARGUMENTS = "Error: Invalid arguments for display ID."; + private static final float DEFAULT_PRESSURE = 1.0f; + private static final float NO_PRESSURE = 0.0f; + private static final Map SOURCES = new HashMap() {{ put("keyboard", InputDevice.SOURCE_KEYBOARD); put("dpad", InputDevice.SOURCE_DPAD); @@ -76,6 +79,7 @@ public class Input extends BaseCommand { COMMANDS.put("draganddrop", new InputDragAndDrop()); COMMANDS.put("press", new InputPress()); COMMANDS.put("roll", new InputRoll()); + COMMANDS.put("motionevent", new InputMotionEvent()); } @Override @@ -304,6 +308,41 @@ public class Input extends BaseCommand { } } + class InputMotionEvent implements InputCmd { + @Override + public void run(int inputSource, int displayId) { + inputSource = getSource(inputSource, InputDevice.SOURCE_TOUCHSCREEN); + sendMotionEvent(inputSource, nextArgRequired(), Float.parseFloat(nextArgRequired()), + Float.parseFloat(nextArgRequired()), displayId); + } + + private void sendMotionEvent(int inputSource, String motionEventType, float x, float y, + int displayId) { + final int action; + final float pressure; + + switch (motionEventType.toUpperCase()) { + case "DOWN": + action = MotionEvent.ACTION_DOWN; + pressure = DEFAULT_PRESSURE; + break; + case "UP": + action = MotionEvent.ACTION_UP; + pressure = NO_PRESSURE; + break; + case "MOVE": + action = MotionEvent.ACTION_MOVE; + pressure = DEFAULT_PRESSURE; + break; + default: + throw new IllegalArgumentException("Unknown motionevent " + motionEventType); + } + + final long now = SystemClock.uptimeMillis(); + injectMotionEvent(inputSource, action, now, now, x, y, pressure, displayId); + } + } + /** * Abstract class for command * use nextArgRequired or nextArg to check next argument if necessary. @@ -391,5 +430,6 @@ public class Input extends BaseCommand { + " (Default: touchscreen)"); out.println(" press (Default: trackball)"); out.println(" roll (Default: trackball)"); + out.println(" event (Default: touchscreen)"); } }