You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@cordova.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2018/05/01 07:35:00 UTC

[jira] [Commented] (CB-14059) Improve API for plugin action handlers on Android platform

    [ https://issues.apache.org/jira/browse/CB-14059?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16459507#comment-16459507 ] 

ASF GitHub Bot commented on CB-14059:
-------------------------------------

wtrocki closed pull request #439: CB-14059: New interface for Android plugins
URL: https://github.com/apache/cordova-android/pull/439
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/framework/src/org/apache/cordova/ActionBasedCordovaPlugin.java b/framework/src/org/apache/cordova/ActionBasedCordovaPlugin.java
new file mode 100644
index 000000000..5c08a180d
--- /dev/null
+++ b/framework/src/org/apache/cordova/ActionBasedCordovaPlugin.java
@@ -0,0 +1,43 @@
+package org.apache.cordova;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+
+import java.util.Arrays;
+
+import java.util.List;
+
+/**
+ * Plugin implementation that accepts array of actions that implement desired functionality
+ *  and can be executed from JavaScript side.
+ *
+ * Example:
+ * class MyAction implements CordovaNativeAction { ... }
+ *
+ * class MyPlugin extends ActionBasedCordovaPlugin {
+ *     public ActionBasedCordovaPlugin(){
+ *         super(new MyAction())
+ *     }
+ * }
+ *
+ * @see CordovaNativeAction
+ */
+public class ActionBasedCordovaPlugin extends CordovaPlugin {
+
+    private final List<CordovaNativeAction> actions;
+
+    public ActionBasedCordovaPlugin(CordovaNativeAction ...actions) {
+        this.actions = Arrays.asList(actions);
+    }
+
+    @Override
+    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
+        for(CordovaNativeAction actionImpl: this.actions){
+            if(actionImpl.supportsAction(action)){
+                return actionImpl.execute(args, callbackContext);
+            }
+        }
+        return false;
+    }
+}
+
diff --git a/framework/src/org/apache/cordova/CordovaNativeAction.java b/framework/src/org/apache/cordova/CordovaNativeAction.java
new file mode 100644
index 000000000..7554c65b7
--- /dev/null
+++ b/framework/src/org/apache/cordova/CordovaNativeAction.java
@@ -0,0 +1,35 @@
+package org.apache.cordova;
+
+import org.json.JSONArray;
+
+/**
+ * Interface that needs to be implemented in order to add new action that can be executed from
+ * JavaScript
+ */
+public interface CordovaNativeAction {
+
+     /**
+      * Executes the request for specific action.
+      *
+      * This method is called from the WebView thread. To do a non-trivial amount of work, use:
+      *     cordova.getThreadPool().execute(runnable);
+      *
+      * To run on the UI thread, use:
+      *     cordova.getActivity().runOnUiThread(runnable);
+      *
+      * @param args         The exec() arguments in JSON form.
+      * @param callbackContext The callback context used when calling back into JavaScript.
+      * @return                Whether the action was valid.
+      */
+     boolean execute(JSONArray args, CallbackContext callbackContext);
+
+    /**
+     *
+     * Method used to match action to implementation
+     *
+     * @param action - action that needs to be performed
+     * @return true if action matches this implementation
+     */
+     boolean supportsAction(String action);
+}
+


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> Improve API for plugin action handlers on Android platform
> ----------------------------------------------------------
>
>                 Key: CB-14059
>                 URL: https://issues.apache.org/jira/browse/CB-14059
>             Project: Apache Cordova
>          Issue Type: Improvement
>            Reporter: Wojciech Trocki
>            Priority: Minor
>
> On the cordova-dev list one of the community members suggested that Android APi seems to be much complex than IOS. 
> See: http://mail-archives.apache.org/mod_mbox/cordova-dev/201804.mbox/%3Cpony-7bfc042908953daf1c7bd61390106e071c756368-457837495d8f36fb4e01073fadc53d7174bc0cbb%40dev.cordova.apache.org%3E
> ??cordova-ios has a nice method binding for plugins. Unfortunately cordova-android requires at present boilerplate implementation of method execute:??
> {code}
>  public class MyPlugin extends CordovaPlugin {
>  @Override
>  public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
>        if (METHOD_1.equals(action)) { 
>               method1(args, callbackContext);  
>        }
>        else if (METHOD_2.equals(action)) { 
>             method2(args, callbackContext); ... 
>        }
>  }
> }
> {code}
> Creating ticket to suggest improvements to Android Api.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org