Skip to content

Commit

Permalink
Start process if process is no exists, or else send cmd to process Ou…
Browse files Browse the repository at this point in the history
…tputStream to execute.
  • Loading branch information
xianglin1998 committed Sep 8, 2021
1 parent 5aa6624 commit 9ae12ee
Showing 1 changed file with 14 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public abstract class BaseConsoleActivity extends BaseActivity {
protected FillParentWidthDialog mDialogGUI = null;
protected AlertDialog mDialog = null;

protected ProcessBuilder processBuilder = null;
protected Process process = null;

private long mBackTime = 0;
Expand Down Expand Up @@ -262,7 +261,19 @@ public void crlfCompatibility(TextView view, String line) {
* else send char to stdin
* */
protected void start(String exe, String... args) {
if (processBuilder == null || process == null) {
LogUtils.d("Execute cmd: " + exe);
if (isProcessAlive()) {
// The process exists, we can write some text to stdin...
try {
if (!exe.endsWith("\n")) {
exe += "\n";
}
process.getOutputStream().write(exe.getBytes());
process.getOutputStream().flush();
} catch (IOException e) {
e.printStackTrace();
}
} else {
ArrayList<String> cmds = new ArrayList<>();
cmds.add(exe);
if (args != null && args.length > 0) {
Expand All @@ -272,7 +283,7 @@ protected void start(String exe, String... args) {
}
}
}
processBuilder = new ProcessBuilder(cmds)
ProcessBuilder processBuilder = new ProcessBuilder(cmds)
.directory(new File(getDefaultCWD()))
.redirectErrorStream(true);
processBuilder.environment().put("LD_LIBRARY_PATH", FileUtils.getNativePath());
Expand All @@ -282,21 +293,8 @@ protected void start(String exe, String... args) {
} catch (IOException e) {
e.printStackTrace();
showToast(e.getMessage());
processBuilder = null;
process = null;
}
} else {
// The process exists, we can write some text to stdin...
try {
if (!exe.endsWith("\n")) {
exe += "\n";
}
Log.d("???", "发送指令: " + exe);
process.getOutputStream().write(exe.getBytes());
process.getOutputStream().flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Expand Down

0 comments on commit 9ae12ee

Please sign in to comment.