add: module RootAutomator

docs: RootAutomator
This commit is contained in:
hyb1996
2017-08-13 13:11:19 +08:00
parent 9c5f876a8f
commit 8c50bfdcaf
5 changed files with 95 additions and 49 deletions

View File

@@ -0,0 +1,64 @@
# RootAutomator
RootAutomator是一个使用Root权限来模拟触摸的对象用它可以完成触摸与多点触摸并且这些动作的执行没有延迟。
构造RootAutomator需要一个context参数。
```
var ra = RootAutomator(context);
```
### RootAutomator.tap(x, y\[, id\])
* x \<Number\> 横坐标
* y \<Number\> 纵坐标
* id \<Number\> 多点触摸id。默认值为1可以通过setDefaultId指定。
点击位置(x, y)。其中id是一个整数值用于区分多点触摸不同的id表示不同的"手指",例如:
```
var ra = RootAutomator(context);
//让"手指1"点击位置(100, 100)
ra.tap(100, 100, 1);
//让"手指2"点击位置(200, 200);
ra.tap(200, 200, 2);
ra.exit();
```
如果不需要多点触摸则不需要id这个参数。
多点触摸通常用于手势或游戏操作,例如模拟双指捏合、双指上滑等。
### RootAutomator.swipe(x1, x2, y1, y2\[, duration, id\])
* x1 \<Number\> 滑动起点横坐标
* y1 \<Number\> 滑动起点纵坐标
* x2 \<Number\> 滑动终点横坐标
* y2 \<Number\> 滑动终点纵坐标
* duration \<Number\> 滑动时长单位毫秒默认值为300
* id \<Number\> 多点触摸id
模拟一次从(x1, y1)到(x2, y2)的时间为duration毫秒的滑动。
### RootAutomator.press(x, y, duration[\, id\])
* x \<Number\> 横坐标
* y \<Number\> 纵坐标
* duration \<Number\> 按下时长
* id \<Number\> 多点触摸id
模拟按下位置(x, y)时长为duration毫秒。
以上为简单模拟触摸操作的函数。如果要模拟一些复杂的手势,需要更底层的函数。
### RootAutomator.touchDown(x, y[\, id\])
* x \<Number\> 横坐标
* y \<Number\> 纵坐标
* id \<Number\> 多点触摸id
模拟手指按下位置(x, y)。
### RootAutomator.touchMove(x, y[\, id\])
* x \<Number\> 横坐标
* y \<Number\> 纵坐标
* id \<Number\> 多点触摸id
模拟移动手指到位置(x, y)。
### RootAutomator.touchUp([\id\])
* id \<Number\> 多点触摸id
模拟手指弹起。

View File

@@ -37,7 +37,8 @@ require("__general__")(__runtime__, this);
(function(scope){
var modules = ['app', 'automator', 'console', 'dialogs', 'io', 'selector', 'shell', 'web', 'ui', "images", "timers", "events", "engines"];
var modules = ['app', 'automator', 'console', 'dialogs', 'io', 'selector', 'shell', 'web', 'ui',
"images", "timers", "events", "engines", "RootAutomator"];
var len = modules.length;
for(var i = 0; i < len; i++) {
var m = modules[i];
@@ -47,4 +48,3 @@ require("__general__")(__runtime__, this);
__importClass__(android.view.KeyEvent);
__importClass__(com.stardust.autojs.runtime.api.Shell);
__importClass__(com.stardust.autojs.runtime.api.RootAutomator);

View File

@@ -0,0 +1,13 @@
module.exports = function(__runtime__, scope){
function RootAutomator(){
this.__ra__ = Object.create(new com.stardust.autojs.runtime.api.RootAutomator(scope.context));
var methods = ["sendEvent", "touch", "setScreenMetrics", "touchX", "touchY", "sendSync", "sendMtSync", "tap",
"swipe","touchDown", "touchUp", "touchMove", "getDefaultId", "setDefaultId", "exit"];
for(var i = 0; i < methods.length; i++){
var method = methods[i];
this[method] = this.__ra__[method].bind(this.__ra__);
}
}
var p = RootAutomator.prototype;
return RootAutomator;
}

View File

@@ -1,8 +1,12 @@
package com.stardust.autojs.core.record.inputevent;
import android.support.annotation.NonNull;
import android.util.Log;
import com.stardust.autojs.core.inputevent.InputEventCodes;
import com.stardust.autojs.core.inputevent.InputEventObserver;
import com.stardust.autojs.engine.RootAutomatorEngine;
import com.stardust.autojs.runtime.api.RootAutomator;
import static com.stardust.util.ScreenMetrics.getDeviceScreenHeight;
import static com.stardust.util.ScreenMetrics.getDeviceScreenWidth;
@@ -17,16 +21,13 @@ public class InputEventToRootAutomatorRecorder extends InputEventRecorder {
private double mLastEventTime;
private StringBuilder mCode = new StringBuilder();
private int mTouchDevice = -1;
private int mLastTouchX = -1;
private int mLastTouchY = -1;
public InputEventToRootAutomatorRecorder() {
mCode.append("var ra = new RootAutomator(context);\n")
mCode.append("var ra = new RootAutomator();\n")
.append("ra.setScreenMetrics(").append(getDeviceScreenWidth()).append(", ")
.append(getDeviceScreenHeight()).append(");\n");
}
@Override
public void recordInputEvent(@NonNull InputEventObserver.InputEvent event) {
if (mLastEventTime == 0) {
@@ -39,21 +40,18 @@ public class InputEventToRootAutomatorRecorder extends InputEventRecorder {
int type = (int) Long.parseLong(event.type, 16);
int code = (int) Long.parseLong(event.code, 16);
int value = (int) Long.parseLong(event.value, 16);
if (type == 3) {
if (code == 53) {
onTouchX(device, value);
return;
}
if (code == 54) {
onTouchY(device, value);
if (type == InputEventCodes.EV_ABS) {
if (code == InputEventCodes.ABS_MT_POSITION_X || code == InputEventCodes.ABS_MT_POSITION_Y) {
mTouchDevice = device;
RootAutomatorEngine.setTouchDevice(device);
onTouch(code, value);
return;
}
}
checkLastTouch();
if (device != mTouchDevice) {
return;
}
if (type == 0 && code == 0 && value == 0) {
if (type == InputEventCodes.EV_SYN && code == InputEventCodes.SYN_REPORT && value == 0) {
mCode.append("ra.sendSync();\n");
return;
}
@@ -63,43 +61,14 @@ public class InputEventToRootAutomatorRecorder extends InputEventRecorder {
.append(value).append(");\n");
}
private void checkLastTouch() {
if (mLastTouchX >= 0) {
mCode.append("ra.touchX(").append(mLastTouchX).append(");\n");
mLastTouchX = -1;
}
if (mLastTouchY >= 0) {
mCode.append("ra.touchY(").append(mLastTouchY).append(");\n");
mLastTouchY = -1;
}
}
private void onTouchX(int device, int value) {
if (mTouchDevice == -1) {
setTouchDevice(device);
}
mLastTouchX = value;
}
private void onTouchY(int device, int value) {
if (mTouchDevice == -1) {
setTouchDevice(device);
}
if (mLastTouchX >= 0) {
mCode.append("ra.touch(")
.append(mLastTouchX).append(", ")
.append(value).append(");\n");
mLastTouchX = -1;
private void onTouch(int code, int value) {
if (code == InputEventCodes.ABS_MT_POSITION_X) {
mCode.append("ra.touchX(").append(value).append(");\n");
} else {
mLastTouchY = value;
mCode.append("ra.touchY(").append(value).append(");\n");
}
}
private void setTouchDevice(int i) {
//mCode.append("ra.setInputDevice(").append(i).append(");\n");
mTouchDevice = i;
}
public String getCode() {
return mCode.toString();
}

View File

@@ -37,7 +37,7 @@ public class RootAutomator {
@Nullable
private ScreenMetrics mScreenMetrics;
private AbstractShell mShell;
private int mDefaultId = -1;
private int mDefaultId = 1;
public RootAutomator(Context context) {
mShell = new ProcessShell(true);