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..e59b716 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,14 @@ import android.app.Activity; import android.os.Environment; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +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; import static android.os.Environment.DIRECTORY_DOWNLOADS; @@ -55,4 +63,32 @@ public String getPath(String name) { return "-1"; } } + + public String exec(String[] cmdarray) throws JSONException { + JSONObject result = new JSONObject(); + try { + 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())); + result.put("stderr", readStream(process.getErrorStream())); + } catch (Exception e) { + result.put("error", e.getMessage()); + } + return result.toString(); + } + + private String readStream(InputStream stream) throws IOException { + BufferedReader reader = new BufferedReader( + new InputStreamReader(stream)); + StringBuilder content = new StringBuilder(); + int cnt = 0; + String s; + while((s = reader.readLine()) != null) { + content.append(s + "\n"); + cnt++; + } + return cnt == 1 ? content.toString().trim() : content.toString(); + } } 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..1ff612c 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 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..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 @@ -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 String exec(String[] cmdarray) throws JSONException { + return app.exec(cmdarray); + } + @Override public String getName() { return "Call";