fix(RootAutomatorEngine): forceStop() not working
This commit is contained in:
@@ -8,6 +8,7 @@ import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.ImageView;
|
||||
import android.workground.WrapContentLinearLayoutManager;
|
||||
|
||||
import com.stardust.scriptdroid.R;
|
||||
@@ -90,9 +91,14 @@ public class ScriptListNavigatorContent implements NavigatorContent {
|
||||
|
||||
private class FileViewHolder extends DirectoryViewHolder {
|
||||
|
||||
private ImageView mIcon;
|
||||
private View mEdit;
|
||||
|
||||
FileViewHolder(final View itemView) {
|
||||
super(itemView);
|
||||
itemView.findViewById(R.id.edit).setOnClickListener(new View.OnClickListener() {
|
||||
mIcon = (ImageView) itemView.findViewById(R.id.icon);
|
||||
mEdit = itemView.findViewById(R.id.edit);
|
||||
mEdit.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
EditActivity.editFile(v.getContext(), getScriptFile());
|
||||
@@ -111,6 +117,14 @@ public class ScriptListNavigatorContent implements NavigatorContent {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bind(ScriptFile file) {
|
||||
super.bind(file);
|
||||
mIcon.setImageResource(file.getType() == ScriptFile.TYPE_AUTO ? R.drawable.record_icon_18
|
||||
: R.drawable.ic_node_js_black);
|
||||
mEdit.setVisibility(file.getType() == ScriptFile.TYPE_JAVA_SCRIPT ? View.VISIBLE : View.INVISIBLE);
|
||||
}
|
||||
|
||||
private ScriptFile getScriptFile() {
|
||||
return mFloatingScriptFileListView.getAdapter().getScriptFileAt(getAdapterPosition());
|
||||
|
||||
|
||||
@@ -109,7 +109,7 @@ public class Scripts {
|
||||
}
|
||||
|
||||
public static ScriptExecution runRepeatedly(ScriptFile scriptFile, int loopTimes, long delay, long interval) {
|
||||
ScriptSource source = new JavaScriptFileSource(scriptFile);
|
||||
ScriptSource source = scriptFile.toSource();
|
||||
String directoryPath = scriptFile.getParent();
|
||||
return AutoJs.getInstance().getScriptEngineService().execute(source, new ExecutionConfig()
|
||||
.requirePath(directoryPath, StorageScriptProvider.DEFAULT_DIRECTORY_PATH)
|
||||
|
||||
@@ -8,18 +8,23 @@ import android.util.AttributeSet;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
import android.workground.WrapContentLinearLayoutManager;
|
||||
|
||||
import com.stardust.autojs.ScriptEngineService;
|
||||
import com.stardust.autojs.engine.JavaScriptEngine;
|
||||
import com.stardust.autojs.engine.ScriptEngineManager;
|
||||
import com.stardust.autojs.execution.ScriptExecution;
|
||||
import com.stardust.autojs.execution.ScriptExecutionListener;
|
||||
import com.stardust.autojs.execution.SimpleScriptExecutionListener;
|
||||
import com.stardust.autojs.engine.ScriptEngine;
|
||||
import com.stardust.autojs.script.AutoFileSource;
|
||||
import com.stardust.autojs.script.JavaScriptSource;
|
||||
import com.stardust.autojs.script.ScriptSource;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.autojs.AutoJs;
|
||||
import com.stardust.scriptdroid.script.ScriptFile;
|
||||
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -139,7 +144,7 @@ public class TaskListRecyclerView extends ThemeColorRecyclerView implements Scri
|
||||
}
|
||||
|
||||
private void onScriptANR(final ScriptEngine engine) {
|
||||
// TODO: 2017/7/19 强制停止
|
||||
// TODO: 2017/7/19 强制停止aq1sws2
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -192,6 +197,7 @@ public class TaskListRecyclerView extends ThemeColorRecyclerView implements Scri
|
||||
|
||||
private class ViewHolder extends RecyclerView.ViewHolder {
|
||||
|
||||
ImageView icon;
|
||||
TextView name, detail;
|
||||
View stop;
|
||||
|
||||
@@ -200,6 +206,7 @@ public class TaskListRecyclerView extends ThemeColorRecyclerView implements Scri
|
||||
itemView.setOnClickListener(mOnItemClickListenerProxy);
|
||||
name = (TextView) itemView.findViewById(R.id.name);
|
||||
detail = (TextView) itemView.findViewById(R.id.detail);
|
||||
icon = (ImageView) itemView.findViewById(R.id.icon);
|
||||
stop = itemView.findViewById(R.id.stop);
|
||||
stop.setOnClickListener(mOnStopClickListener);
|
||||
}
|
||||
@@ -209,6 +216,9 @@ public class TaskListRecyclerView extends ThemeColorRecyclerView implements Scri
|
||||
return;
|
||||
name.setText(source.getName());
|
||||
detail.setText(source.toString());
|
||||
//ignore android studio warning: use equals to compare string
|
||||
icon.setImageResource(source.getEngineName() == AutoFileSource.ENGINE ? R.drawable.record_icon_18
|
||||
: R.drawable.ic_node_js_black);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import android.util.Log;
|
||||
|
||||
import com.stardust.autojs.runtime.api.AbstractShell;
|
||||
import com.stardust.autojs.runtime.api.ProcessShell;
|
||||
import com.stardust.autojs.runtime.api.Shell;
|
||||
import com.stardust.autojs.runtime.record.inputevent.InputDevices;
|
||||
import com.stardust.autojs.script.AutoFileSource;
|
||||
import com.stardust.autojs.script.JavaScriptFileSource;
|
||||
@@ -31,6 +32,7 @@ public class RootAutomatorEngine extends ScriptEngine.AbstractScriptEngine<AutoF
|
||||
private Context mContext;
|
||||
private String mDeviceNameOrPath;
|
||||
private Thread mThread;
|
||||
private String mExecutablePath;
|
||||
|
||||
public RootAutomatorEngine(Context context, String deviceNameOrPath) {
|
||||
mContext = context;
|
||||
@@ -54,10 +56,10 @@ public class RootAutomatorEngine extends ScriptEngine.AbstractScriptEngine<AutoF
|
||||
}
|
||||
|
||||
public AbstractShell.Result execute(String autoFile) {
|
||||
String executablePath = getExecutablePath(mContext);
|
||||
mExecutablePath = getExecutablePath(mContext);
|
||||
AbstractShell.Result r = ProcessShell.execCommand(new String[]{
|
||||
"chmod 777 " + executablePath,
|
||||
executablePath + " " + autoFile + " -d " + mDeviceNameOrPath
|
||||
"chmod 777 " + mExecutablePath,
|
||||
mExecutablePath + " \"" + autoFile + "\" -d " + mDeviceNameOrPath
|
||||
}, true);
|
||||
Log.d(LOG_TAG, "exec: " + autoFile + " result:" + r);
|
||||
return r;
|
||||
@@ -91,6 +93,7 @@ public class RootAutomatorEngine extends ScriptEngine.AbstractScriptEngine<AutoF
|
||||
|
||||
@Override
|
||||
public void forceStop() {
|
||||
ProcessShell.exec("killall " + mExecutablePath, true);
|
||||
mThread.interrupt();
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.stardust.autojs.runtime.api;
|
||||
import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.stardust.autojs.engine.RootAutomatorEngine;
|
||||
import com.stardust.util.ScreenMetrics;
|
||||
|
||||
/**
|
||||
@@ -33,7 +34,7 @@ public abstract class AbstractShell {
|
||||
static final String COMMAND_LINE_END = "\n";
|
||||
|
||||
|
||||
private int mTouchDevice;
|
||||
private int mTouchDevice = -1;
|
||||
private ScreenMetrics mScreenMetrics;
|
||||
|
||||
private boolean mRoot;
|
||||
@@ -50,6 +51,8 @@ public abstract class AbstractShell {
|
||||
public AbstractShell(Context context, boolean root) {
|
||||
mContext = context;
|
||||
mRoot = root;
|
||||
if (context != null)
|
||||
mTouchDevice = RootAutomatorEngine.getTouchDevice(context);
|
||||
init(root ? COMMAND_SU : COMMAND_SH);
|
||||
}
|
||||
|
||||
@@ -64,6 +67,8 @@ public abstract class AbstractShell {
|
||||
public abstract void exit();
|
||||
|
||||
public void SetTouchDevice(int touchDevice) {
|
||||
if (mTouchDevice > 0)
|
||||
return;
|
||||
mTouchDevice = touchDevice;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,12 +3,15 @@ package com.stardust.autojs.runtime.api;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.stardust.autojs.util.ProcessUtils;
|
||||
import com.stardust.pio.UncheckedIOException;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Reader;
|
||||
|
||||
/**
|
||||
@@ -176,13 +179,10 @@ public class ProcessShell extends AbstractShell {
|
||||
|
||||
public static Result execCommand(String[] commands, boolean isRoot) {
|
||||
Result commandResult = new Result();
|
||||
if (commands == null || commands.length == 0) return commandResult;
|
||||
if (commands == null || commands.length == 0)
|
||||
throw new IllegalArgumentException("command is empty");
|
||||
Process process = null;
|
||||
DataOutputStream os = null;
|
||||
BufferedReader successResult = null;
|
||||
BufferedReader errorResult = null;
|
||||
StringBuilder successMsg = null;
|
||||
StringBuilder errorMsg = null;
|
||||
try {
|
||||
process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
|
||||
os = new DataOutputStream(process.getOutputStream());
|
||||
@@ -196,41 +196,38 @@ public class ProcessShell extends AbstractShell {
|
||||
os.writeBytes(COMMAND_EXIT);
|
||||
os.flush();
|
||||
commandResult.code = process.waitFor();
|
||||
successMsg = new StringBuilder();
|
||||
errorMsg = new StringBuilder();
|
||||
successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
|
||||
String s;
|
||||
while ((s = successResult.readLine()) != null) successMsg.append(s);
|
||||
while ((s = errorResult.readLine()) != null) errorMsg.append(s);
|
||||
commandResult.result = successMsg.toString();
|
||||
commandResult.error = errorMsg.toString();
|
||||
Log.i(TAG, commandResult.toString());
|
||||
commandResult.result = readAll(process.getInputStream());
|
||||
commandResult.error = readAll(process.getErrorStream());
|
||||
Log.d(TAG, commandResult.toString());
|
||||
} catch (Exception e) {
|
||||
String errmsg = e.getMessage();
|
||||
if (errmsg != null) {
|
||||
Log.e(TAG, errmsg);
|
||||
} else {
|
||||
e.printStackTrace();
|
||||
}
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (os != null) os.close();
|
||||
if (successResult != null) successResult.close();
|
||||
if (errorResult != null) errorResult.close();
|
||||
} catch (IOException e) {
|
||||
String errMsg = e.getMessage();
|
||||
if (errMsg != null) {
|
||||
Log.e(TAG, errMsg);
|
||||
} else {
|
||||
e.printStackTrace();
|
||||
if (process != null) {
|
||||
process.getInputStream().close();
|
||||
process.getOutputStream().close();
|
||||
}
|
||||
} catch (IOException ignored) {
|
||||
|
||||
}
|
||||
if (process != null) {
|
||||
process.destroy();
|
||||
}
|
||||
if (process != null) process.destroy();
|
||||
}
|
||||
return commandResult;
|
||||
}
|
||||
|
||||
private static String readAll(InputStream inputStream) throws IOException {
|
||||
String line;
|
||||
StringBuilder builder = new StringBuilder();
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
||||
while ((line = reader.readLine()) != null) {
|
||||
builder.append(line);
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public static Result execCommand(String command, boolean isRoot) {
|
||||
String[] commands = command.split("\n");
|
||||
return execCommand(commands, isRoot);
|
||||
|
||||
@@ -25,7 +25,7 @@ import jackpal.androidterm.util.TermSettings;
|
||||
* Created by Stardust on 2017/4/24.
|
||||
*/
|
||||
|
||||
public class Shell extends AbstractShell implements AutoCloseable {
|
||||
public class Shell extends AbstractShell {
|
||||
|
||||
public interface Callback {
|
||||
|
||||
@@ -181,10 +181,6 @@ public class Shell extends AbstractShell implements AutoCloseable {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
exit();
|
||||
}
|
||||
|
||||
private class MyShellTermSession extends ShellTermSession {
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.stardust.autojs.runtime.record.inputevent;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.stardust.autojs.engine.RootAutomatorEngine;
|
||||
@@ -11,6 +12,8 @@ import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/8/2.
|
||||
@@ -23,9 +26,10 @@ public class InputEventToAutoFileConverter extends InputEventConverter {
|
||||
private DataOutputStream mDataOutputStream;
|
||||
private File mTmpFile;
|
||||
|
||||
public InputEventToAutoFileConverter() {
|
||||
public InputEventToAutoFileConverter(Context context) {
|
||||
try {
|
||||
mTmpFile = File.createTempFile("Record" + System.currentTimeMillis(), ".auto");
|
||||
mTmpFile = new File(context.getCacheDir(), SimpleDateFormat.getDateTimeInstance().format(new Date()) + ".auto");
|
||||
mTmpFile.deleteOnExit();
|
||||
mDataOutputStream = new DataOutputStream(new FileOutputStream(mTmpFile));
|
||||
writeFileHeader();
|
||||
} catch (IOException e) {
|
||||
|
||||
@@ -9,7 +9,7 @@ import android.content.Context;
|
||||
public class TouchRecorder extends InputEventRecorder {
|
||||
|
||||
public TouchRecorder(Context context) {
|
||||
super(context, new InputEventToAutoFileConverter());
|
||||
super(context, new InputEventToAutoFileConverter(context));
|
||||
listen();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.stardust.autojs.util;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.stardust.autojs.runtime.api.ProcessShell;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/8/3.
|
||||
*/
|
||||
|
||||
public class ProcessUtils {
|
||||
|
||||
|
||||
private static final String LOG_TAG = "ProcessUtils";
|
||||
|
||||
// FIXME: 2017/8/3
|
||||
public static void killProcessTree(Process process) {
|
||||
int pid = getProcessPid(process);
|
||||
if (pid >= 0)
|
||||
kill(pid);
|
||||
process.destroy();
|
||||
|
||||
}
|
||||
|
||||
private static int getProcessPid(Process process) {
|
||||
try {
|
||||
Field pid = process.getClass().getDeclaredField("pid");
|
||||
pid.setAccessible(true);
|
||||
return (int) pid.get(process);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public static void kill(int pid) {
|
||||
String cmd = "kill -TERM -- -" + pid;
|
||||
Log.d(LOG_TAG, cmd);
|
||||
ProcessShell.exec(cmd, true);
|
||||
}
|
||||
}
|
||||
@@ -12,4 +12,5 @@ public class App extends Application {
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user