fix: root_automator cannot be force stopped
This commit is contained in:
@@ -14,6 +14,7 @@ import com.stardust.enhancedfloaty.FloatyService;
|
||||
import com.stardust.enhancedfloaty.ResizableExpandableFloatyWindow;
|
||||
import com.stardust.util.UiHandler;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
@@ -67,7 +68,7 @@ public class StardustConsole extends AbstractConsole {
|
||||
private LogListener mLogListener;
|
||||
private UiHandler mUiHandler;
|
||||
private BlockingQueue<String> mInput = new ArrayBlockingQueue<>(1);
|
||||
private ConsoleView mConsoleView;
|
||||
private WeakReference<ConsoleView> mConsoleView;
|
||||
private volatile boolean mShown = false;
|
||||
|
||||
public StardustConsole(UiHandler uiHandler) {
|
||||
@@ -88,7 +89,7 @@ public class StardustConsole extends AbstractConsole {
|
||||
}
|
||||
|
||||
public void setConsoleView(ConsoleView consoleView) {
|
||||
mConsoleView = consoleView;
|
||||
mConsoleView = new WeakReference<>(consoleView);
|
||||
setLogListener(consoleView);
|
||||
synchronized (this) {
|
||||
this.notify();
|
||||
@@ -177,13 +178,13 @@ public class StardustConsole extends AbstractConsole {
|
||||
|
||||
@ScriptInterface
|
||||
public String rawInput() {
|
||||
if (mConsoleView == null) {
|
||||
if (mConsoleView == null || mConsoleView.get() == null) {
|
||||
if (!mShown) {
|
||||
show();
|
||||
}
|
||||
waitForConsoleView();
|
||||
}
|
||||
mConsoleView.showEditText();
|
||||
mConsoleView.get().showEditText();
|
||||
try {
|
||||
return mInput.take();
|
||||
} catch (InterruptedException e) {
|
||||
|
||||
@@ -56,7 +56,7 @@ public class InputEventToAutoFileRecorder extends InputEventRecorder {
|
||||
public void recordInputEvent(@NonNull InputEventObserver.InputEvent event) {
|
||||
try {
|
||||
convertEventOrThrow(event);
|
||||
//Log.d(LOG_TAG, "recordInputEvent: " + event);
|
||||
Log.d(LOG_TAG, "recordInputEvent: " + event);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -3,14 +3,23 @@ package com.stardust.autojs.engine;
|
||||
import android.content.Context;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.util.Log;
|
||||
import android.util.Patterns;
|
||||
|
||||
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.exception.ScriptException;
|
||||
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
|
||||
import com.stardust.autojs.script.AutoFileSource;
|
||||
import com.stardust.pio.PFiles;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/8/1.
|
||||
@@ -22,6 +31,7 @@ public class RootAutomatorEngine extends ScriptEngine.AbstractScriptEngine<AutoF
|
||||
|
||||
private static final String KEY_TOUCH_DEVICE = RootAutomatorEngine.class.getName() + ".touch_device";
|
||||
private static final String LOG_TAG = "RootAutomatorEngine";
|
||||
private static final Pattern PID_PATTERN = Pattern.compile("[0-9]{2,}");
|
||||
|
||||
private static int sTouchDevice = -1;
|
||||
private static final String ROOT_AUTOMATOR_EXECUTABLE_ASSET = "binary/root_automator";
|
||||
@@ -30,6 +40,8 @@ public class RootAutomatorEngine extends ScriptEngine.AbstractScriptEngine<AutoF
|
||||
private String mDeviceNameOrPath;
|
||||
private Thread mThread;
|
||||
private String mExecutablePath;
|
||||
private String mPid;
|
||||
private Process mProcess;
|
||||
|
||||
public RootAutomatorEngine(Context context, String deviceNameOrPath) {
|
||||
mContext = context;
|
||||
@@ -44,11 +56,49 @@ public class RootAutomatorEngine extends ScriptEngine.AbstractScriptEngine<AutoF
|
||||
public void execute(String autoFile) {
|
||||
mExecutablePath = getExecutablePath(mContext);
|
||||
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, "result = " + result);
|
||||
final String[] commands = {
|
||||
"chmod 755 " + mExecutablePath,
|
||||
String.format("\"%s\" \"%s\" -d \"%s\" &", mExecutablePath, autoFile, mDeviceNameOrPath), // to run root_automator
|
||||
"echo $!", // to print the root_automator pid
|
||||
"exit", // to exit su
|
||||
"exit" // to exit shell
|
||||
};
|
||||
try {
|
||||
mProcess = Runtime.getRuntime().exec("su");
|
||||
executeCommands(mProcess, commands);
|
||||
mPid = readPid(mProcess);
|
||||
mProcess.waitFor();
|
||||
} catch (IOException e) {
|
||||
throw new ScriptException(e);
|
||||
} catch (InterruptedException e) {
|
||||
throw new ScriptInterruptedException();
|
||||
} finally {
|
||||
mProcess.destroy();
|
||||
mProcess = null;
|
||||
}
|
||||
}
|
||||
|
||||
private String readPid(Process process) throws IOException {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
Matcher matcher = PID_PATTERN.matcher(line);
|
||||
if (matcher.find()) {
|
||||
return matcher.group();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void executeCommands(Process process, String[] commands) throws IOException {
|
||||
DataOutputStream os = new DataOutputStream(process.getOutputStream());
|
||||
for (String command : commands) {
|
||||
if (command != null) {
|
||||
os.write(command.getBytes());
|
||||
os.writeBytes("\n");
|
||||
}
|
||||
}
|
||||
os.flush();
|
||||
}
|
||||
|
||||
|
||||
@@ -96,7 +146,9 @@ public class RootAutomatorEngine extends ScriptEngine.AbstractScriptEngine<AutoF
|
||||
@Override
|
||||
public void forceStop() {
|
||||
mThread.interrupt();
|
||||
ProcessShell.exec("killall " + mExecutablePath, true);
|
||||
if (mPid != null) {
|
||||
ProcessShell.exec("kill " + mPid, true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -226,7 +226,7 @@ public class ProcessShell extends AbstractShell {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
||||
while ((line = reader.readLine()) != null) {
|
||||
builder.append(line);
|
||||
builder.append(line).append('\n');
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user