Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions androidjs/src/main/java/com/android/js/api/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down