fix: RootAutomatorEngine.forceStop not working

This commit is contained in:
hyb1996
2017-08-14 20:10:33 +08:00
parent 7b4629aa1d
commit 8034dc7b6b
17 changed files with 125 additions and 51 deletions

View File

@@ -2,7 +2,7 @@ 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"];
"swipe", "press", "longPress", "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__);

View File

@@ -5,18 +5,19 @@ module.exports = function(__runtime__, scope){
var engines = {};
engines.execScript = function(name, script, config){
config = fillConfig(config);
return rtEngines.execScript(name, script, config);
return rtEngines.execScript(name, script, fillConfig(config));
}
engines.execScriptFile = function(path, config){
config = fillConfig(config);
return rtEngines.execScriptFile(path, config);
return rtEngines.execScriptFile(path, fillConfig(config));
}
engines.execAutoFile = function(path, config){
config = fillConfig(config);
return rtEngines.execAutoFile(path, config);
return rtEngines.execAutoFile(path, fillConfig(config));
}
engines.myEngine = function(){
return scope.__engine__;
}
engines.stopAll = rtEngines.stopAll.bind(rtEngines);

View File

@@ -45,17 +45,17 @@ module.exports = function(__runtime__, scope){
return __runtime__.info.getLatestActivity();
}
scope.waitForActivity = function(activity, delay){
delay = delay || 200;
scope.waitForActivity = function(activity, period){
period = period || 200;
while(scope.currentActivity() != activity){
sleep(delay);
sleep(period);
}
}
scope.waitForPackage = function(packageName, delay){
delay = delay || 200;
scope.waitForPackage = function(packageName, period){
period = period || 200;
while(scope.currentPackage() != packageName){
sleep(delay);
sleep(period);
}
}

View File

@@ -245,6 +245,7 @@ public class SimpleActionAutomator {
mAccessibilityBridge.ensureServiceEnabled();
}
@SuppressWarnings("unchecked")
private boolean performAction(SimpleAction simpleAction) {
ensureAccessibilityServiceEnabled();

View File

@@ -28,6 +28,9 @@ public abstract class JavaScriptEngine extends ScriptEngine.AbstractScriptEngine
}
public void setRuntime(ScriptRuntime runtime) {
if (mRuntime != null) {
throw new IllegalStateException("a runtime has been set");
}
mRuntime = runtime;
put("__runtime__", runtime);
}

View File

@@ -7,7 +7,10 @@ import android.util.Log;
import com.stardust.autojs.runtime.api.AbstractShell;
import com.stardust.autojs.runtime.api.ProcessShell;
import com.stardust.autojs.core.inputevent.InputDevices;
import com.stardust.autojs.runtime.api.Shell;
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
import com.stardust.autojs.script.AutoFileSource;
import com.stardust.concurrent.VolatileBox;
import com.stardust.pio.PFile;
import java.io.File;
@@ -34,7 +37,6 @@ public class RootAutomatorEngine extends ScriptEngine.AbstractScriptEngine<AutoF
public RootAutomatorEngine(Context context, String deviceNameOrPath) {
mContext = context;
mDeviceNameOrPath = getDeviceNameOrPath(context, deviceNameOrPath);
}
@@ -42,14 +44,14 @@ public class RootAutomatorEngine extends ScriptEngine.AbstractScriptEngine<AutoF
this(context, InputDevices.getTouchDeviceName());
}
public AbstractShell.Result execute(String autoFile) {
public void execute(String autoFile) {
mExecutablePath = getExecutablePath(mContext);
AbstractShell.Result r = ProcessShell.execCommand(new String[]{
Log.d(LOG_TAG, "exec: " + autoFile);
AbstractShell.Result result = ProcessShell.execCommand(new String[]{
"chmod 777 " + mExecutablePath,
mExecutablePath + " \"" + autoFile + "\" -d " + mDeviceNameOrPath
}, true);
Log.d(LOG_TAG, "exec: " + autoFile + " result:" + r);
return r;
Log.d(LOG_TAG, "result = " + result);
}
@@ -90,17 +92,24 @@ public class RootAutomatorEngine extends ScriptEngine.AbstractScriptEngine<AutoF
@Override
public Object execute(AutoFileSource source) {
return execute(source.getFile().getAbsolutePath());
execute(source.getFile().getAbsolutePath());
return null;
}
@Override
public void forceStop() {
ProcessShell.exec("killall " + mExecutablePath, true);
mThread.interrupt();
ProcessShell.exec("killall " + mExecutablePath, true);
}
@Override
public void init() {
mThread = Thread.currentThread();
}
@Override
public synchronized void destroy() {
super.destroy();
Log.d(LOG_TAG, "Shell exit");
}
}

View File

@@ -68,6 +68,7 @@ public class ProcessShell extends AbstractShell {
@Override
public void exit() {
if (mProcess != null) {
Log.d(TAG, "exit: pid = " + ProcessUtils.getProcessPid(mProcess));
mProcess.destroy();
mProcess = null;
}
@@ -195,11 +196,14 @@ public class ProcessShell extends AbstractShell {
}
os.writeBytes(COMMAND_EXIT);
os.flush();
Log.d(TAG, "pid = " + ProcessUtils.getProcessPid(process));
commandResult.code = process.waitFor();
commandResult.result = readAll(process.getInputStream());
commandResult.error = readAll(process.getErrorStream());
Log.d(TAG, commandResult.toString());
} catch (Exception e) {
commandResult.code = -1;
commandResult.error = e.getMessage();
e.printStackTrace();
} finally {
try {

View File

@@ -3,22 +3,26 @@ package com.stardust.autojs.runtime.api;
import android.content.Context;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.InputDevice;
import android.view.ViewConfiguration;
import com.stardust.autojs.core.inputevent.InputDevices;
import com.stardust.autojs.engine.RootAutomatorEngine;
import com.stardust.autojs.runtime.ScriptRuntime;
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
import com.stardust.pio.UncheckedIOException;
import com.stardust.util.ScreenMetrics;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import static com.stardust.autojs.core.inputevent.InputEventCodes.*;
import static com.stardust.autojs.core.inputevent.InputEventCodes.ABS_MT_POSITION_X;
import static com.stardust.autojs.core.inputevent.InputEventCodes.ABS_MT_POSITION_Y;
import static com.stardust.autojs.core.inputevent.InputEventCodes.ABS_MT_TOUCH_MAJOR;
import static com.stardust.autojs.core.inputevent.InputEventCodes.ABS_MT_TRACKING_ID;
import static com.stardust.autojs.core.inputevent.InputEventCodes.BTN_TOOL_FINGER;
import static com.stardust.autojs.core.inputevent.InputEventCodes.BTN_TOUCH;
import static com.stardust.autojs.core.inputevent.InputEventCodes.EV_ABS;
import static com.stardust.autojs.core.inputevent.InputEventCodes.EV_KEY;
import static com.stardust.autojs.core.inputevent.InputEventCodes.EV_SYN;
import static com.stardust.autojs.core.inputevent.InputEventCodes.SYN_MT_REPORT;
import static com.stardust.autojs.core.inputevent.InputEventCodes.SYN_REPORT;
/**
* Created by Stardust on 2017/7/16.
@@ -124,6 +128,24 @@ public class RootAutomator {
swipe(x1, y1, x2, y2, 300, mDefaultId);
}
public void press(int x, int y, int duration, int id) throws IOException {
touchDown(x, y, id);
sleep(duration);
touchUp(id);
}
public void press(int x, int y, int duration) throws IOException {
press(x, y, duration, getDefaultId());
}
public void longPress(int x, int y, int id) throws IOException {
press(x, y, ViewConfiguration.getLongPressTimeout() + 200, id);
}
public void longPress(int x, int y) throws IOException {
press(x, y, ViewConfiguration.getLongPressTimeout() + 200, getDefaultId());
}
public void touchDown(int x, int y, int id) throws IOException {
sendEvent(EV_ABS, ABS_MT_TRACKING_ID, id);
sendEvent(EV_KEY, BTN_TOUCH, 0x00000001);
@@ -168,10 +190,11 @@ public class RootAutomator {
mDefaultId = defaultId;
}
private void sleep(long duration) {
private void sleep(long duration) throws IOException {
try {
Thread.sleep(duration);
} catch (InterruptedException e) {
exit();
throw new ScriptInterruptedException();
}
}

View File

@@ -69,6 +69,7 @@ public class Shell extends AbstractShell {
private volatile RuntimeException mInitException;
private volatile boolean mInitialized = false;
private volatile boolean mWaitingExit = false;
private final boolean mShouldReadOutput;
private Callback mCallback;
public Shell(Context context) {
@@ -76,7 +77,12 @@ public class Shell extends AbstractShell {
}
public Shell(Context context, boolean root) {
this(context, root, true);
}
public Shell(Context context, boolean root, boolean shouldReadOutput) {
super(context, root);
mShouldReadOutput = shouldReadOutput;
}
public Shell() {
@@ -96,7 +102,7 @@ public class Shell extends AbstractShell {
TermSettings settings = new TermSettings(mContext.getResources(), PreferenceManager.getDefaultSharedPreferences(mContext));
try {
mTermSession = new MyShellTermSession(settings, initialCommand);
mTermSession.initializeEmulator(40, 40);
mTermSession.initializeEmulator(1024, 40);
} catch (IOException e) {
mInitException = new UncheckedIOException(e);
}
@@ -181,6 +187,9 @@ public class Shell extends AbstractShell {
}
}
public TermSession getTermSession() {
return mTermSession;
}
private class MyShellTermSession extends ShellTermSession {
@@ -193,7 +202,9 @@ public class Shell extends AbstractShell {
PipedInputStream pipedInputStream = new PipedInputStream(8192);
mBufferedReader = new BufferedReader(new InputStreamReader(pipedInputStream));
mOutputStream = new PipedOutputStream(pipedInputStream);
startReadingThread();
if (mShouldReadOutput) {
startReadingThread();
}
}
private void startReadingThread() {
@@ -215,7 +226,7 @@ public class Shell extends AbstractShell {
}
private void onNewLine(String line) {
Log.d(TAG, line);
//Log.d(TAG, line);
if (!mInitialized) {
if (isRoot() && line.endsWith(" $ su")) {
notifyInitialized();
@@ -277,6 +288,8 @@ public class Shell extends AbstractShell {
@Override
public void finish() {
super.finish();
if (!mShouldReadOutput)
return;
mReadingThread.interrupt();
try {
mBufferedReader.close();

View File

@@ -24,7 +24,7 @@ public class ProcessUtils {
}
private static int getProcessPid(Process process) {
public static int getProcessPid(Process process) {
try {
Field pid = process.getClass().getDeclaredField("pid");
pid.setAccessible(true);