Skip to content

Commit

Permalink
refactor(android): MessageHandler formatting and simplifying (#2510)
Browse files Browse the repository at this point in the history
  • Loading branch information
imjacobclark authored Jun 24, 2020
1 parent 7c82ad3 commit befe798
Showing 1 changed file with 27 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,36 @@ public void postMessage(String jsonStr) {

String type = postData.getString("type");

if (type != null && type.equals("cordova")) {
String callbackId = postData.getString("callbackId");
boolean typeIsNotNull = type != null;
boolean isCordovaPlugin = typeIsNotNull && type.equals("cordova");
boolean isJavaScriptError = typeIsNotNull && type.equals("js.error");

String callbackId = postData.getString("callbackId");

if (isCordovaPlugin) {
String service = postData.getString("service");
String action = postData.getString("action");
String actionArgs = postData.getString("actionArgs");
Logger.verbose(Logger.tags("Plugin"), "To native (Cordova plugin): callbackId: " + callbackId + ", service: " + service +
", action: " + action + ", actionArgs: " + actionArgs);

Logger.verbose(Logger.tags("Plugin"), "To native (Cordova plugin): callbackId: " + callbackId + ", service: " + service + ", action: " + action + ", actionArgs: " + actionArgs);

this.callCordovaPluginMethod(callbackId, service, action, actionArgs);
} else if (type != null && type.equals("js.error")) {
} else if (isJavaScriptError) {
Logger.error("JavaScript Error: " + jsonStr);
} else {
String callbackId = postData.getString("callbackId");
String pluginId = postData.getString("pluginId");
String methodName = postData.getString("methodName");
JSObject methodData = postData.getJSObject("options", new JSObject());

Logger.verbose(Logger.tags("Plugin"), "To native (Capacitor plugin): callbackId: " + callbackId + ", pluginId: " + pluginId + ", methodName: " + methodName);

this.callPluginMethod(callbackId, pluginId, methodName, methodData);
}

} catch (Exception ex) {
Logger.error("Post message error:", ex);
}
}

private void callPluginMethod(String callbackId, String pluginId, String methodName, JSObject methodData) {
PluginCall call = new PluginCall(this, pluginId, callbackId, methodName, methodData);

bridge.callPluginMethod(pluginId, methodName, call);
}

private void callCordovaPluginMethod(String callbackId, String service, String action, String actionArgs){
cordovaPluginManager.exec(service, action, callbackId, actionArgs);
}

public void sendResponseMessage(PluginCall call, PluginResult successResult, PluginResult errorResult) {
try {
PluginResult data = new PluginResult();
Expand All @@ -77,7 +73,8 @@ public void sendResponseMessage(PluginCall call, PluginResult successResult, Plu
data.put("pluginId", call.getPluginId());
data.put("methodName", call.getMethodName());

if (errorResult != null) {
boolean pluginResultInError = errorResult != null;
if (pluginResultInError) {
data.put("success", false);
data.put("error", errorResult);
Logger.debug("Sending plugin error: " + data.toString());
Expand All @@ -86,17 +83,12 @@ public void sendResponseMessage(PluginCall call, PluginResult successResult, Plu
data.put("data", successResult);
}

// Only eval the JS code if this is a valid callback id
if (!call.getCallbackId().equals(PluginCall.CALLBACK_ID_DANGLING)) {
boolean isValidCallbackId = !call.getCallbackId().equals(PluginCall.CALLBACK_ID_DANGLING);
if (isValidCallbackId) {
final String runScript = "window.Capacitor.fromNative(" + data.toString() + ")";

final WebView webView = this.webView;
webView.post(new Runnable() {
@Override
public void run() {
webView.evaluateJavascript(runScript, null);
}
});

webView.post(() -> webView.evaluateJavascript(runScript, null));
} else {
bridge.storeDanglingPluginResult(call, data);
}
Expand All @@ -106,4 +98,12 @@ public void run() {
}
}

private void callPluginMethod(String callbackId, String pluginId, String methodName, JSObject methodData) {
PluginCall call = new PluginCall(this, pluginId, callbackId, methodName, methodData);
bridge.callPluginMethod(pluginId, methodName, call);
}

private void callCordovaPluginMethod(String callbackId, String service, String action, String actionArgs){
cordovaPluginManager.exec(service, action, callbackId, actionArgs);
}
}

0 comments on commit befe798

Please sign in to comment.