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

@@ -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);