Skip to content

Commit

Permalink
Make the terminal way better
Browse files Browse the repository at this point in the history
  • Loading branch information
DerGoogler committed Sep 23, 2023
1 parent d73dce3 commit 3380b15
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions Android/app/src/main/java/com/dergoogler/plugin/TerminalPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

public class TerminalPlugin extends CordovaPlugin {
Expand All @@ -25,27 +29,20 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo
String cmd = data.getString(0);

this.terminalCallbackContext = callbackContext;
String[] commands = {"su", "-c", cmd};

cordova.getThreadPool().execute(() -> {
Shell.cmd(cmd)
.to(new CallbackList<String>() {
@Override
public void onAddElement(String s) {
updateTerminal(s);
}
})
.submit(result -> {
if (result.isSuccess()) {
callbackContext.error(1);
} else {
callbackContext.error(0);
}
});

cordova.getThreadPool().execute(() -> {
try {
run(commands);
callbackContext.error(1);
} catch (IOException e) {
callbackContext.error(0);
e.printStackTrace();
}
});



return true;

case "test":
Expand All @@ -59,6 +56,19 @@ public void onAddElement(String s) {

}

public void run(String... command) throws IOException {
ProcessBuilder pb = new ProcessBuilder(command).redirectErrorStream(true);
Process process = pb.start();
try (BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
while (true) {
String line = in.readLine();
if (line == null)
break;
updateTerminal(line);
}
}
}

private void updateTerminal(String line) {
sendUpdate(line, true);
}
Expand Down

0 comments on commit 3380b15

Please sign in to comment.