From 7fdce10a2c602fcd638e402c0cc57171e3b89b64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Suwi=C5=84ski?= Date: Mon, 7 Dec 2020 14:48:41 +0100 Subject: [PATCH 1/6] app.exec() implementation --- .../src/main/java/com/android/js/api/App.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/androidjs/src/main/java/com/android/js/api/App.java b/androidjs/src/main/java/com/android/js/api/App.java index dd26974..22b555b 100644 --- a/androidjs/src/main/java/com/android/js/api/App.java +++ b/androidjs/src/main/java/com/android/js/api/App.java @@ -3,6 +3,13 @@ import android.app.Activity; import android.os.Environment; +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; +import java.util.HashMap; +import java.io.InputStreamReader; +import java.io.BufferedReader; + import static android.os.Environment.DIRECTORY_ALARMS; import static android.os.Environment.DIRECTORY_DCIM; import static android.os.Environment.DIRECTORY_DOWNLOADS; @@ -13,6 +20,8 @@ import static android.os.Environment.DIRECTORY_PODCASTS; import static android.os.Environment.DIRECTORY_RINGTONES; +import static com.android.js.other.Utils.exec; + public class App { private Activity activity; @@ -55,4 +64,29 @@ public String getPath(String name) { return "-1"; } } + + public static Map exec(String[] cmdarray) { + Map result = new HashMap(); + try { + Process process = Runtime.getRuntime().exec(cmdarray); + process.waitFor(); + result.put("status", String.valueOf(process.exitValue())); + result.put("stdout", readStream(process.getInputStream())); + result.put("stderr", readStream(process.getErrorStream())); + } catch (Exception e) { + result.put("error", e.getMessage()); + } + return result; + } + + private static String readStream(InputStream stream) throws IOException { + BufferedReader reader = new BufferedReader( + new InputStreamReader(stream)); + StringBuilder content = new StringBuilder(); + String s; + while((s = reader.readLine()) != null) { + content.append(s + "\n"); + } + return content.toString(); + } } From 63a1913365427cef2e0e578f549d672ebc0674b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Suwi=C5=84ski?= Date: Tue, 8 Dec 2020 09:38:53 +0100 Subject: [PATCH 2/6] app.exec() - JSONObject usage in place of Map, some refactoring --- .../src/main/java/com/android/js/api/App.java | 16 +++++++--------- .../com/android/js/common/JavaWebviewBridge.java | 5 +++++ .../com/android/js/react_native/api/App.java | 6 ++++++ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/androidjs/src/main/java/com/android/js/api/App.java b/androidjs/src/main/java/com/android/js/api/App.java index 22b555b..48585b7 100644 --- a/androidjs/src/main/java/com/android/js/api/App.java +++ b/androidjs/src/main/java/com/android/js/api/App.java @@ -5,10 +5,10 @@ import java.io.IOException; import java.io.InputStream; -import java.util.Map; -import java.util.HashMap; import java.io.InputStreamReader; import java.io.BufferedReader; +import org.json.JSONObject; +import org.json.JSONException; import static android.os.Environment.DIRECTORY_ALARMS; import static android.os.Environment.DIRECTORY_DCIM; @@ -20,8 +20,6 @@ import static android.os.Environment.DIRECTORY_PODCASTS; import static android.os.Environment.DIRECTORY_RINGTONES; -import static com.android.js.other.Utils.exec; - public class App { private Activity activity; @@ -65,21 +63,21 @@ public String getPath(String name) { } } - public static Map exec(String[] cmdarray) { - Map result = new HashMap(); + public String exec(String[] cmdarray) throws JSONException { + JSONObject result = new JSONObject(); try { Process process = Runtime.getRuntime().exec(cmdarray); process.waitFor(); - result.put("status", String.valueOf(process.exitValue())); + result.put("status", process.exitValue()); result.put("stdout", readStream(process.getInputStream())); result.put("stderr", readStream(process.getErrorStream())); } catch (Exception e) { result.put("error", e.getMessage()); } - return result; + return result.toString(); } - private static String readStream(InputStream stream) throws IOException { + private String readStream(InputStream stream) throws IOException { BufferedReader reader = new BufferedReader( new InputStreamReader(stream)); StringBuilder content = new StringBuilder(); diff --git a/androidjs/src/main/java/com/android/js/common/JavaWebviewBridge.java b/androidjs/src/main/java/com/android/js/common/JavaWebviewBridge.java index 6aeada8..cd8a31e 100644 --- a/androidjs/src/main/java/com/android/js/common/JavaWebviewBridge.java +++ b/androidjs/src/main/java/com/android/js/common/JavaWebviewBridge.java @@ -65,6 +65,11 @@ public String getPath(String name) { return app.getPath(name); } + @JavascriptInterface + public static String exec(String[] cmdarray) throws JSONException { + return app.exec(cmdarray); + } + @JavascriptInterface public void initNotification(String title, String msg){ notification.initNotification(title, msg); diff --git a/androidjs/src/main/java/com/android/js/react_native/api/App.java b/androidjs/src/main/java/com/android/js/react_native/api/App.java index 9bbb0a4..b8b63ca 100644 --- a/androidjs/src/main/java/com/android/js/react_native/api/App.java +++ b/androidjs/src/main/java/com/android/js/react_native/api/App.java @@ -3,6 +3,7 @@ import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; +import org.json.JSONException; public class App extends ReactContextBaseJavaModule { private ReactApplicationContext reactContext; @@ -19,6 +20,11 @@ public String getPath(String name) { return app.getPath(name); } + @ReactMethod(isBlockingSynchronousMethod = true) + public static String exec(String[] cmdarray) throws JSONException { + return app.exec(cmdarray); + } + @Override public String getName() { return "Call"; From 92531abca8170a6ab0e83d45b55fcd6e518a5ab9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Suwi=C5=84ski?= Date: Tue, 8 Dec 2020 18:10:21 +0100 Subject: [PATCH 3/6] exec() - back to non-static access --- .../src/main/java/com/android/js/common/JavaWebviewBridge.java | 2 +- .../src/main/java/com/android/js/react_native/api/App.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/androidjs/src/main/java/com/android/js/common/JavaWebviewBridge.java b/androidjs/src/main/java/com/android/js/common/JavaWebviewBridge.java index cd8a31e..1ff612c 100644 --- a/androidjs/src/main/java/com/android/js/common/JavaWebviewBridge.java +++ b/androidjs/src/main/java/com/android/js/common/JavaWebviewBridge.java @@ -66,7 +66,7 @@ public String getPath(String name) { } @JavascriptInterface - public static String exec(String[] cmdarray) throws JSONException { + public String exec(String[] cmdarray) throws JSONException { return app.exec(cmdarray); } diff --git a/androidjs/src/main/java/com/android/js/react_native/api/App.java b/androidjs/src/main/java/com/android/js/react_native/api/App.java index b8b63ca..d426647 100644 --- a/androidjs/src/main/java/com/android/js/react_native/api/App.java +++ b/androidjs/src/main/java/com/android/js/react_native/api/App.java @@ -21,7 +21,7 @@ public String getPath(String name) { } @ReactMethod(isBlockingSynchronousMethod = true) - public static String exec(String[] cmdarray) throws JSONException { + public String exec(String[] cmdarray) throws JSONException { return app.exec(cmdarray); } From 7611d49e77ecbaf5894ba1a3d8edf37dfb39db3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Suwi=C5=84ski?= Date: Thu, 10 Dec 2020 08:42:47 +0100 Subject: [PATCH 4/6] app.exec() - set appData as working directory --- androidjs/src/main/java/com/android/js/api/App.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/androidjs/src/main/java/com/android/js/api/App.java b/androidjs/src/main/java/com/android/js/api/App.java index 48585b7..574bfbd 100644 --- a/androidjs/src/main/java/com/android/js/api/App.java +++ b/androidjs/src/main/java/com/android/js/api/App.java @@ -3,6 +3,7 @@ import android.app.Activity; import android.os.Environment; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -66,7 +67,8 @@ public String getPath(String name) { public String exec(String[] cmdarray) throws JSONException { JSONObject result = new JSONObject(); try { - Process process = Runtime.getRuntime().exec(cmdarray); + Process process = Runtime.getRuntime().exec(cmdarray, null, + new File(this.activity.getFilesDir().getPath())); process.waitFor(); result.put("status", process.exitValue()); result.put("stdout", readStream(process.getInputStream())); From 707af30c7ccc8a976e8994d73849c7107c5dd6fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Suwi=C5=84ski?= Date: Thu, 10 Dec 2020 13:27:55 +0100 Subject: [PATCH 5/6] app.exec() - trim outputs --- androidjs/src/main/java/com/android/js/api/App.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/androidjs/src/main/java/com/android/js/api/App.java b/androidjs/src/main/java/com/android/js/api/App.java index 574bfbd..9f268b4 100644 --- a/androidjs/src/main/java/com/android/js/api/App.java +++ b/androidjs/src/main/java/com/android/js/api/App.java @@ -87,6 +87,6 @@ private String readStream(InputStream stream) throws IOException { while((s = reader.readLine()) != null) { content.append(s + "\n"); } - return content.toString(); + return content.toString().trim(); } } From 5364e8c00e4983af8899bdac778092aa24467ce8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Suwi=C5=84ski?= Date: Thu, 10 Dec 2020 15:10:28 +0100 Subject: [PATCH 6/6] app.exec() - trim only one line output, fix trailing spaces --- androidjs/src/main/java/com/android/js/api/App.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/androidjs/src/main/java/com/android/js/api/App.java b/androidjs/src/main/java/com/android/js/api/App.java index 9f268b4..e59b716 100644 --- a/androidjs/src/main/java/com/android/js/api/App.java +++ b/androidjs/src/main/java/com/android/js/api/App.java @@ -64,9 +64,9 @@ public String getPath(String name) { } } - public String exec(String[] cmdarray) throws JSONException { + public String exec(String[] cmdarray) throws JSONException { JSONObject result = new JSONObject(); - try { + try { Process process = Runtime.getRuntime().exec(cmdarray, null, new File(this.activity.getFilesDir().getPath())); process.waitFor(); @@ -82,11 +82,13 @@ public String exec(String[] cmdarray) throws JSONException { private String readStream(InputStream stream) throws IOException { BufferedReader reader = new BufferedReader( new InputStreamReader(stream)); - StringBuilder content = new StringBuilder(); + StringBuilder content = new StringBuilder(); + int cnt = 0; String s; while((s = reader.readLine()) != null) { content.append(s + "\n"); + cnt++; } - return content.toString().trim(); + return cnt == 1 ? content.toString().trim() : content.toString(); } }