背景:
经常使用的input命令如下:
正常情况下一般要模拟一个触摸点击事件一般用如下命令
input tap 200 300
执行完成后手机画面一般就会响应一个click事件,这里发现一般都是一个手指,一直寻找看看input命令是否可以支持多个手指通过命令同时按下操作的情况,发现并没有这种多指的触摸的命令。难道确实是不支持吗?
input只支持单指源码分析
源码路径
frameworks/base/cmds/input/src/com/android/commands/input/Input.java
public static void main(String[] args) {
(new Input()).run(args);
}
private void run(String[] args) {
if (args.length < 1) {
showUsage();
return;
}
int index = 0;
String command = args[index];
int inputSource = InputDevice.SOURCE_UNKNOWN;
if (SOURCES.containsKey(command)) {
inputSource = SOURCES.get(command);
index++;
command = args[index];
}
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 (length > start) {
for (int i = start; i < length; i++) {
int keyCode = KeyEvent.keyCodeFromString(args[i]);
if (keyCode == KeyEvent.KEYCODE_UNKNOWN) {
keyCode = KeyEvent.keyCodeFromString("KEYCODE_" + args[i]);
}
sendKeyEvent(inputSource, keyCode, longpress);
}
return;
}
}
} else if (command.equals("tap")) {
//可以看到这里input 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();
return;
}
} catch (NumberFormatException ex) {
}
System.err.println(INVALID_ARGUMENTS + command);
showUsage();
}
上面代码可以看到最后input tap这个命令最后走入是sendTap方法:
看看这里sendTap
//注意这里传递的参数只有坐标x,y,和一个inputsource,根本没有看到有多个手指的id传入,基本就可以确定这里其实是不支持多指的
private void sendTap(int inputSource, float x, float y) {
long now = SystemClock.uptimeMillis();
injectMotionEvent(inputSource, MotionEvent.ACTION_DOWN, now, x, y, 1.0f);
injectMotionEvent(inputSource, MotionEvent.ACTION_UP, now, x, y, 0.0f);
}
明显可以看到其实tap就是注入了2个触摸事件,一个是ACTION_DOWN一个是ACTION_UP,按下抬起就是tap也就是常说的click事件。
在看看injectMotionEvent方法:
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_EDGE_FLAGS = 0;
//构建出对应的MotionEvent对象
MotionEvent event = MotionEvent.obtain(when, when, action, x, y, pressure, DEFAULT_SIZE,
DEFAULT_META_STATE, DEFAULT_PRECISION_X, DEFAULT_PRECISION_Y,
getInputDeviceId(inputSource), DEFAULT_EDGE_FLAGS);
event.setSource(inputSource);
Log.i(TAG, "injectMotionEvent: " + event);
//然后在通过inputmanager的相关方法注入这个motionevent
InputManager.getInstance().injectInputEvent(event,
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
总结:
input tap命令只是进行传递了x,y坐标值,并没有传递多指相关的任何id,所以是不支持的多指的
input扩展成多指需求设计
目前只支持单指,那么是否可以考虑让input实现多指呢?
其实是完全可以的,因为MotionEvent构造时候完全可以构造成多个手指的这种MotionEvent,然后进行注入,下面就先进行input命令扩展成支持多指的一个命令设计:
考虑设置一个可以多指点击命令项,暂时命名叫做multiple-tap
所以输入命令如下:
input multiple-tap pointId0 x y pointId1 x y …(省略)
命令解释:
multiple-tap --代表多个手指按下点击情况
pointId1 x y --pointId0代表具体手指索引,可以区分不一样手指,一般0开始,x,y代表是触摸事件的坐标位置
本文章对应视频手把手教你学framework:
七件套专题:
点击这里 https://mp.weixin.qq.com/s/Qv8zjgQ0CkalKmvi8tMGaw
hal+perfetto+surfaceflinger
https://mp.weixin.qq.com/s/LbVLnu1udqExHVKxd74ILg
私聊作者+v(androidframework007)
视频:https://www.bilibili.com/video/BV1wc41117L4/