Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): add interface for messagehandler #7709

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.getcapacitor;

public interface IMessageHandler {
/**
* Posts a message to a JavaScript context. It is expected to be a stringified JSON representation
*
* @param message The stringified json to be posted.
*/
void postMessage(String message);

/**
* Sends a response message.
*
* @param call the plugin call, which is attached to the communication
* @param successResult the result of the native execution
* @param errorResult the error, that might have occured
*
*/
void sendResponseMessage(PluginCall call, PluginResult successResult, PluginResult errorResult);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* MessageHandler handles messages from the WebView, dispatching them
* to plugins.
*/
public class MessageHandler {
public class MessageHandler implements IMessageHandler {

private Bridge bridge;
private WebView webView;
Expand Down Expand Up @@ -49,6 +49,7 @@ public MessageHandler(Bridge bridge, WebView webView, PluginManager cordovaPlugi
*/
@JavascriptInterface
@SuppressWarnings("unused")
@Override
public void postMessage(String jsonStr) {
try {
JSObject postData = new JSObject(jsonStr);
Expand Down Expand Up @@ -98,6 +99,7 @@ public void postMessage(String jsonStr) {
}
}

@Override
public void sendResponseMessage(PluginCall call, PluginResult successResult, PluginResult errorResult) {
try {
PluginResult data = new PluginResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class PluginCall {
*/
public static final String CALLBACK_ID_DANGLING = "-1";

private final MessageHandler msgHandler;
private final IMessageHandler msgHandler;
private final String pluginId;
private final String callbackId;
private final String methodName;
Expand All @@ -33,7 +33,7 @@ public class PluginCall {
@Deprecated
private boolean isReleased = false;

public PluginCall(MessageHandler msgHandler, String pluginId, String callbackId, String methodName, JSObject data) {
public PluginCall(IMessageHandler msgHandler, String pluginId, String callbackId, String methodName, JSObject data) {
this.msgHandler = msgHandler;
this.pluginId = pluginId;
this.callbackId = callbackId;
Expand Down