Skip to content

Commit

Permalink
Catch and log exceptions in JavascriptInterfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn committed Jun 5, 2018
1 parent 8a36e90 commit e7909cb
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void onReceiveValue(String result) {
private SimprintsSupport simprints;
private PhotoGrabber photoGrabber;

//> ACTIVITY LIFECYCLE METHODS
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Expand Down Expand Up @@ -104,15 +105,6 @@ public void onReceiveValue(String result) {
}
}

private void configureUseragent() {
String current = container.getUserAgentString();

if(current.contains(APPLICATION_ID)) return;

container.setUserAgentString(String.format("%s %s/%s",
current, APPLICATION_ID, VERSION_NAME));
}

@Override public boolean onCreateOptionsMenu(Menu menu) {
if(settings.allowsConfiguration()) {
getMenuInflater().inflate(R.menu.unbranded_web_menu, menu);
Expand Down Expand Up @@ -182,6 +174,7 @@ private void configureUseragent() {
}
}

//> PUBLIC API
public void evaluateJavascript(final String js) {
container.post(new Runnable() {
public void run() {
Expand All @@ -200,6 +193,30 @@ public void run() {
});
}

public void errorToJsConsole(String message, Object... extras) {
jsConsole("error", message, extras);
}

public void logToJsConsole(String message, Object... extras) {
jsConsole("log", message, extras);
}

//> PRIVATE HELPERS
private void jsConsole(String type, String message, Object... extras) {
String formatted = String.format(message, extras);
String escaped = formatted.replace("'", "\\'");
evaluateJavascript("console." + type + "('" + escaped + "');");
}

private void configureUseragent() {
String current = container.getUserAgentString();

if(current.contains(APPLICATION_ID)) return;

container.setUserAgentString(String.format("%s %s/%s",
current, APPLICATION_ID, VERSION_NAME));
}

private void openSettings() {
startActivity(new Intent(this,
SettingsDialogActivity.class));
Expand Down Expand Up @@ -244,7 +261,7 @@ private void setUpUiClient(XWalkView container) {
if(photoGrabber.canHandle(acceptType, capture)) {
photoGrabber.chooser(callback, capture);
} else {
evaluateJavascript(String.format("console.log('No file chooser is currently implemented for \"accept\" value: %s');", acceptType));
logToJsConsole("No file chooser is currently implemented for \"accept\" value: %s", acceptType);
warn(this, "openFileChooser() :: No file chooser is currently implemented for \"accept\" value: %s", acceptType);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import android.os.Process;
import android.widget.DatePicker;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand All @@ -21,6 +23,7 @@
import static java.util.Calendar.MONTH;
import static java.util.Calendar.YEAR;
import static java.util.Locale.UK;
import static org.medicmobile.webapp.mobile.MedicLog.log;

public class MedicAndroidJavascript {
private static final String DATE_FORMAT = "yyyy-MM-dd";
Expand All @@ -44,6 +47,7 @@ public void setLocationManager(LocationManager locationManager) {
this.locationManager = locationManager;
}

//> JavascriptInterface METHODS
@org.xwalk.core.JavascriptInterface
@android.webkit.JavascriptInterface
public String getAppVersion() {
Expand All @@ -59,14 +63,18 @@ public String getAppVersion() {
@org.xwalk.core.JavascriptInterface
@android.webkit.JavascriptInterface
public void playAlert() {
if(soundAlert != null) soundAlert.trigger();
try {
if(soundAlert != null) soundAlert.trigger();
} catch(Exception ex) {
logException(ex);
}
}

@org.xwalk.core.JavascriptInterface
@android.webkit.JavascriptInterface
public String getDataUsage() {
int uid = Process.myUid();
try {
int uid = Process.myUid();
return new JSONObject()
.put("system", getDataUsage(
TrafficStats.getTotalRxBytes(),
Expand Down Expand Up @@ -117,7 +125,11 @@ public String getLocation() {
@org.xwalk.core.JavascriptInterface
@android.webkit.JavascriptInterface
public void datePicker(final String targetElement) {
datePicker(targetElement, Calendar.getInstance());
try {
datePicker(targetElement, Calendar.getInstance());
} catch(Exception ex) {
logException(ex);
}
}

@org.xwalk.core.JavascriptInterface
Expand All @@ -130,6 +142,8 @@ public void datePicker(final String targetElement, String initialDate) {
datePicker(targetElement, c);
} catch(ParseException ex) {
datePicker(targetElement);
} catch(Exception ex) {
logException(ex);
}
}

Expand All @@ -139,21 +153,35 @@ public void datePicker(final String targetElement, String initialDate) {
@org.xwalk.core.JavascriptInterface
@android.webkit.JavascriptInterface
public boolean simprints_available() {
return simprints.isAppInstalled();
try {
return simprints.isAppInstalled();
} catch(Exception ex) {
logException(ex);
return false;
}
}

@org.xwalk.core.JavascriptInterface
@android.webkit.JavascriptInterface
public void simprints_ident(int targetInputId) {
simprints.startIdent(targetInputId);
try {
simprints.startIdent(targetInputId);
} catch(Exception ex) {
logException(ex);
}
}

@org.xwalk.core.JavascriptInterface
@android.webkit.JavascriptInterface
public void simprints_reg(int targetInputId) {
simprints.startReg(targetInputId);
try {
simprints.startReg(targetInputId);
} catch(Exception ex) {
logException(ex);
}
}

//> PRIVATE HELPER METHODS
private void datePicker(String targetElement, Calendar initialDate) {
// Remove single-quotes from the `targetElement` CSS selecter, as
// we'll be using these to enclose the entire string in JS. We
Expand Down Expand Up @@ -185,6 +213,20 @@ public void onDateSet(DatePicker view, int year, int month, int day) {
dialog.show();
}

private void logException(Exception ex) {
log(ex, "Execption thrown in JavascriptInterface function.");

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
String stacktrace = sw.toString()
.replace("\n", "; ")
.replace("\t", " ");

parent.errorToJsConsole("Execption thrown in JavascriptInterface function: %s", stacktrace);
}

//> STATIC HELPERS
private static String jsonError(String message, Exception ex) {
return jsonError(message + ex.getClass() + ": " + ex.getMessage());
}
Expand Down

0 comments on commit e7909cb

Please sign in to comment.