This commit is contained in:
hyb1996
2017-05-06 21:42:18 +08:00
parent ed2ebe1599
commit 840bb56528
83 changed files with 110 additions and 442 deletions

View File

@@ -69,7 +69,7 @@ public class ScriptRuntime extends AbstractScriptRuntime {
}
public AbstractShell.Result shell(String cmd, int root) {
return ProcessShell.exec(cmd, root != 0);
return ProcessShell.execCommand(cmd, root != 0);
}
public UiSelector selector(JavaScriptEngine engine) {

View File

@@ -28,8 +28,8 @@ public abstract class AbstractShell {
}
}
private static final String COMMAND_SU = "su";
private static final String COMMAND_SH = "sh";
static final String COMMAND_SU = "su";
static final String COMMAND_SH = "sh";
static final String COMMAND_EXIT = "exit\n";
static final String COMMAND_LINE_END = "\n";

View File

@@ -1,12 +1,15 @@
package com.stardust.autojs.runtime.api;
import android.util.Log;
import com.stardust.pio.UncheckedIOException;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
/**
* Created by Stardust on 2017/1/20.
@@ -109,26 +112,23 @@ public class ProcessShell extends AbstractShell implements AutoCloseable {
}
public ProcessShell readSucceedOutput() {
try {
while (mSucceedReader.ready()) {
String line = mSucceedReader.readLine();
mSucceedOutput.append(line).append("\n");
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
read(mSucceedReader, mSucceedOutput);
return this;
}
public ProcessShell readErrorOutput() {
String line;
private void read(BufferedReader reader, StringBuilder sb) {
try {
while ((line = mErrorReader.readLine()) != null) {
mErrorOutput.append(line).append("\n");
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public ProcessShell readErrorOutput() {
read(mErrorReader, mErrorOutput);
return this;
}
@@ -173,5 +173,68 @@ public class ProcessShell extends AbstractShell implements AutoCloseable {
}
}
public static Result execCommand(String[] commands, boolean isRoot) {
Result commandResult = new Result();
if (commands == null || commands.length == 0) return commandResult;
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());
for (String command : commands) {
if (command != null) {
os.write(command.getBytes());
os.writeBytes(COMMAND_LINE_END);
os.flush();
}
}
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());
} catch (Exception e) {
String errmsg = e.getMessage();
if (errmsg != null) {
Log.e(TAG, errmsg);
} else {
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.destroy();
}
return commandResult;
}
public static Result execCommand(String command, boolean isRoot) {
String[] commands = command.split("\n");
return execCommand(commands, isRoot);
}
}