From 58a3ba639fe745ccba1672262b6b2d569fd1293b Mon Sep 17 00:00:00 2001 From: Arthur Hung Date: Wed, 8 Aug 2018 10:41:55 +0800 Subject: Add support for secondary display with display ID - Add display ID parameter for input shell command. - Do some code refactory to use BaseCommand. Bug: 112338741 Test: adb shell input [-d display_id] command args Change-Id: I7264c913f784a35b41458261e1c3356b30f34035 --- .../src/com/android/commands/input/Input.java | 433 ++++++++++++--------- 1 file changed, 245 insertions(+), 188 deletions(-) diff --git a/cmds/input/src/com/android/commands/input/Input.java b/cmds/input/src/com/android/commands/input/Input.java index 74edffb4738d..b905e94cd96f 100644 --- a/cmds/input/src/com/android/commands/input/Input.java +++ b/cmds/input/src/com/android/commands/input/Input.java @@ -16,15 +16,20 @@ package com.android.commands.input; +import static android.view.Display.DEFAULT_DISPLAY; +import static android.view.Display.INVALID_DISPLAY; + import android.hardware.input.InputManager; import android.os.SystemClock; -import android.util.Log; import android.view.InputDevice; import android.view.KeyCharacterMap; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.ViewConfiguration; +import com.android.internal.os.BaseCommand; + +import java.io.PrintStream; import java.util.HashMap; import java.util.Map; @@ -33,9 +38,11 @@ import java.util.Map; * desired character output. */ -public class Input { +public class Input extends BaseCommand { private static final String TAG = "Input"; private static final String INVALID_ARGUMENTS = "Error: Invalid arguments for command: "; + private static final String INVALID_DISPLAY_ARGUMENTS = + "Error: Invalid arguments for display ID."; private static final Map SOURCES = new HashMap() {{ put("keyboard", InputDevice.SOURCE_KEYBOARD); @@ -50,6 +57,7 @@ public class Input { put("joystick", InputDevice.SOURCE_JOYSTICK); }}; + private static final Map COMMANDS = new HashMap(); /** * Command-line entry point. @@ -60,217 +68,256 @@ public class Input { (new Input()).run(args); } - private void run(String[] args) { - if (args.length < 1) { - showUsage(); - return; - } + Input() { + COMMANDS.put("text", new InputText()); + COMMANDS.put("keyevent", new InputKeyEvent()); + COMMANDS.put("tap", new InputTap()); + COMMANDS.put("swipe", new InputSwipe()); + COMMANDS.put("draganddrop", new InputDragAndDrop()); + COMMANDS.put("press", new InputPress()); + COMMANDS.put("roll", new InputRoll()); + } - int index = 0; - String command = args[index]; + @Override + public void onRun() throws Exception { + String arg = nextArgRequired(); int inputSource = InputDevice.SOURCE_UNKNOWN; - if (SOURCES.containsKey(command)) { - inputSource = SOURCES.get(command); - index++; - command = args[index]; + + // Get source (optional). + if (SOURCES.containsKey(arg)) { + inputSource = SOURCES.get(arg); + arg = nextArgRequired(); } - final int length = args.length - index; - - try { - if (command.equals("text")) { - if (length == 2) { - inputSource = getSource(inputSource, InputDevice.SOURCE_KEYBOARD); - sendText(inputSource, args[index+1]); - return; - } - } else if (command.equals("keyevent")) { - if (length >= 2) { - final boolean longpress = "--longpress".equals(args[index + 1]); - final int start = longpress ? index + 2 : index + 1; - inputSource = getSource(inputSource, InputDevice.SOURCE_KEYBOARD); - if (args.length > start) { - for (int i = start; i < args.length; i++) { - int keyCode = KeyEvent.keyCodeFromString(args[i]); - sendKeyEvent(inputSource, keyCode, longpress); - } - return; - } - } - } else if (command.equals("tap")) { - if (length == 3) { - inputSource = getSource(inputSource, InputDevice.SOURCE_TOUCHSCREEN); - sendTap(inputSource, Float.parseFloat(args[index+1]), - Float.parseFloat(args[index+2])); - return; - } - } else if (command.equals("swipe")) { - int duration = -1; - inputSource = getSource(inputSource, InputDevice.SOURCE_TOUCHSCREEN); - switch (length) { - case 6: - duration = Integer.parseInt(args[index+5]); - case 5: - sendSwipe(inputSource, - Float.parseFloat(args[index+1]), Float.parseFloat(args[index+2]), - Float.parseFloat(args[index+3]), Float.parseFloat(args[index+4]), - duration); - return; - } - } else if (command.equals("draganddrop")) { - int duration = -1; - inputSource = getSource(inputSource, InputDevice.SOURCE_TOUCHSCREEN); - switch (length) { - case 6: - duration = Integer.parseInt(args[index+5]); - case 5: - sendDragAndDrop(inputSource, - Float.parseFloat(args[index+1]), Float.parseFloat(args[index+2]), - Float.parseFloat(args[index+3]), Float.parseFloat(args[index+4]), - duration); - return; - } - } else if (command.equals("press")) { - inputSource = getSource(inputSource, InputDevice.SOURCE_TRACKBALL); - if (length == 1) { - sendTap(inputSource, 0.0f, 0.0f); - return; - } - } else if (command.equals("roll")) { - inputSource = getSource(inputSource, InputDevice.SOURCE_TRACKBALL); - if (length == 3) { - sendMove(inputSource, Float.parseFloat(args[index+1]), - Float.parseFloat(args[index+2])); - return; - } - } else { - System.err.println("Error: Unknown command: " + command); - showUsage(); + + // Get displayId (optional). + int displayId = INVALID_DISPLAY; + if ("-d".equals(arg)) { + displayId = getDisplayId(); + arg = nextArgRequired(); + } + + // Get command and run. + InputCmd cmd = COMMANDS.get(arg); + if (cmd != null) { + try { + cmd.run(inputSource, displayId); return; + } catch (NumberFormatException ex) { + throw new IllegalArgumentException(INVALID_ARGUMENTS + arg); } - } catch (NumberFormatException ex) { } - System.err.println(INVALID_ARGUMENTS + command); - showUsage(); + + throw new IllegalArgumentException("Error: Unknown command: " + arg); } - /** - * Convert the characters of string text into key event's and send to - * device. - * - * @param text is a string of characters you want to input to the device. - */ - private void sendText(int source, String text) { + private int getDisplayId() { + String displayArg = nextArgRequired(); + if ("INVALID_DISPLAY".equalsIgnoreCase(displayArg)) { + return INVALID_DISPLAY; + } else if ("DEFAULT_DISPLAY".equalsIgnoreCase(displayArg)) { + return DEFAULT_DISPLAY; + } else { + try { + final int displayId = Integer.parseInt(displayArg); + if (displayId == INVALID_DISPLAY) { + return INVALID_DISPLAY; + } + return Math.max(displayId, 0); + } catch (NumberFormatException e) { + throw new IllegalArgumentException(INVALID_DISPLAY_ARGUMENTS); + } + } + } + + class InputText implements InputCmd { + @Override + public void run(int inputSource, int displayId) { + inputSource = getSource(inputSource, InputDevice.SOURCE_KEYBOARD); + sendText(inputSource, nextArgRequired(), displayId); + } - StringBuffer buff = new StringBuffer(text); + /** + * Convert the characters of string text into key event's and send to + * device. + * + * @param text is a string of characters you want to input to the device. + */ + private void sendText(int source, final String text, int displayId) { + final StringBuffer buff = new StringBuffer(text); + boolean escapeFlag = false; + for (int i = 0; i < buff.length(); i++) { + if (escapeFlag) { + escapeFlag = false; + if (buff.charAt(i) == 's') { + buff.setCharAt(i, ' '); + buff.deleteCharAt(--i); + } + } + if (buff.charAt(i) == '%') { + escapeFlag = true; + } + } - boolean escapeFlag = false; - for (int i=0; i] [...]"); - System.err.println(); - System.err.println("The sources are: "); + @Override + public void onShowUsage(PrintStream out) { + out.println("Usage: input [] [-d DISPLAY_ID] [...]"); + out.println(); + out.println("The sources are: "); for (String src : SOURCES.keySet()) { - System.err.println(" " + src); + out.println(" " + src); } - System.err.println(); - System.err.println("The commands and default sources are:"); - System.err.println(" text (Default: touchscreen)"); - System.err.println(" keyevent [--longpress] ..." + out.println(); + out.printf("-d: specify the display ID.\n" + + " (Default: %d for key event, %d for motion event if not specified.)", + INVALID_DISPLAY, DEFAULT_DISPLAY); + out.println(); + out.println("The commands and default sources are:"); + out.println(" text (Default: touchscreen)"); + out.println(" keyevent [--longpress] ..." + " (Default: keyboard)"); - System.err.println(" tap (Default: touchscreen)"); - System.err.println(" swipe [duration(ms)]" + out.println(" tap (Default: touchscreen)"); + out.println(" swipe [duration(ms)]" + " (Default: touchscreen)"); - System.err.println(" draganddrop [duration(ms)]" + out.println(" draganddrop [duration(ms)]" + " (Default: touchscreen)"); - System.err.println(" press (Default: trackball)"); - System.err.println(" roll (Default: trackball)"); + out.println(" press (Default: trackball)"); + out.println(" roll (Default: trackball)"); } } -- cgit v1.2.3-59-g8ed1b