Last active
April 29, 2018 15:02
-
-
Save wtrocki/43bfdda18c086a3283bb8ba3bf2d052e to your computer and use it in GitHub Desktop.
Example how Cordova api could be extended to improve end user interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.apache.cordova; | |
| import org.json.JSONArray; | |
| import org.json.JSONException; | |
| import java.util.Arrays; | |
| import java.util.List; | |
| /** | |
| * Default plugin implementation that accepts array of CorodvaNativeAction interfaces | |
| * that implement native functionalities to be executed from WebView. | |
| * | |
| * Example: | |
| * class MyAction implements CorodvaNativeAction { ... } | |
| * | |
| * class MyPlugin extends StandardCordovaPlugin { | |
| * public MyPlugin(){ | |
| * super(new MyAction()) | |
| * } | |
| * } | |
| * | |
| * @see CorodvaNativeAction | |
| */ | |
| public class StandardCordovaPlugin extends CordovaPlugin { | |
| private final List<CorodvaNativeAction> actions; | |
| public StandardCordovaPlugin(CorodvaNativeAction ...actions) { | |
| this.actions = Arrays.asList(actions); | |
| } | |
| @Override | |
| public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { | |
| for(CorodvaNativeAction actionImpl: this.actions){ | |
| if(actionImpl.supportsAction(action)){ | |
| return actionImpl.execute(args, callbackContext); | |
| } | |
| } | |
| return false; | |
| } | |
| } | |
| /** | |
| * Interface native action that can be executed from JavaScript | |
| */ | |
| interface CorodvaNativeAction { | |
| /** | |
| * 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); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment