fix: root_automator cannot be force stopped

This commit is contained in:
hyb1996 2017-10-27 11:34:41 +08:00
parent ece78e5903
commit 19bbbe9fe1
7 changed files with 83 additions and 59 deletions

View File

@ -80,19 +80,15 @@ public class CircularMenu implements Recorder.OnStateChangedListener {
}
private void setupListeners() {
mWindow.setOnActionViewClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mState == STATE_RECORDING) {
stopRecord();
} else if (mWindow.isExpanded()) {
mWindow.collapse();
} else {
AutoJs.getInstance().getLayoutInspector().captureCurrentWindow();
mWindow.expand();
}
mWindow.setOnActionViewClickListener(v -> {
if (mState == STATE_RECORDING) {
stopRecord();
} else if (mWindow.isExpanded()) {
mWindow.collapse();
} else {
AutoJs.getInstance().getLayoutInspector().captureCurrentWindow();
mWindow.expand();
}
});
}
@ -130,12 +126,7 @@ public class CircularMenu implements Recorder.OnStateChangedListener {
.customView(listView, false)
.positiveText(R.string.cancel)
.build();
listView.setOnItemOperatedListener(new ScriptListView.OnItemOperatedListener() {
@Override
public void OnItemOperated(ScriptFile file) {
dialog.dismiss();
}
});
listView.setOnItemOperatedListener(file -> dialog.dismiss());
DialogUtils.showDialog(dialog);
}

View File

@ -307,7 +307,6 @@ public class ScriptListView extends SwipeRefreshLayout implements SwipeRefreshLa
@Override
public void onBindViewHolder(BindableViewHolder<?> holder, int position) {
int positionOfCategoryFile = positionOfCategoryFile();
Log.d(LOG_TAG, String.format("view holder = %s, pos = %d, posOfCategory = %d, size = %d", holder.getClass().toString(), position, positionOfCategoryFile, mScriptList.count()));
BindableViewHolder bindableViewHolder = (BindableViewHolder) holder;
if (position == positionOfCategoryDir || position == positionOfCategoryFile) {
// FIXME: 2017/10/20 java.lang.ClassCastException: java.lang.Boolean cannot be cast to com.stardust.scriptdroid.model.script.ScriptFile
@ -323,17 +322,14 @@ public class ScriptListView extends SwipeRefreshLayout implements SwipeRefreshLa
@Override
public int getItemViewType(int position) {
int viewType;
int positionOfCategoryFile = positionOfCategoryFile();
if (position == positionOfCategoryDir || position == positionOfCategoryFile) {
viewType = VIEW_TYPE_CATEGORY;
return VIEW_TYPE_CATEGORY;
} else if (position < positionOfCategoryFile) {
viewType = VIEW_TYPE_DIRECTORY;
return VIEW_TYPE_DIRECTORY;
} else {
viewType = VIEW_TYPE_FILE;
return VIEW_TYPE_FILE;
}
Log.d(LOG_TAG, String.format("view type = %d, pos = %d, posOfCategory = %d, size = %d", viewType, position, positionOfCategoryFile, mScriptList.count()));
return viewType;
}
@Override

View File

@ -3,6 +3,8 @@ package com.stardust.scriptdroid;
import org.junit.Test;
import java.util.concurrent.Callable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
@ -20,28 +22,10 @@ public class ExampleUnitTest {
@Test
public void test() {
Observable.fromCallable(new Callable<String>() {
@Override
public String call() throws Exception {
System.out.println(Thread.currentThread());
return "";
}
})
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.newThread())
.doOnComplete(new Action() {
@Override
public void run() throws Exception {
System.out.println(Thread.currentThread());
}
})
.observeOn(Schedulers.newThread())
.subscribe(new Consumer<String>() {
@Override
public void accept(@NonNull String s) throws Exception {
System.out.println(Thread.currentThread());
}
});
Matcher matcher = Pattern.compile("[0-9]+").matcher("2937Finish!");
if (matcher.find()) {
System.out.println(matcher.group());
}
}
@Test

View File

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

View File

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

View File

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

View File

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