cordova-annotated-plugin-android

1.0.4 • Public • Published

cordova-annotated-plugin-android
NPM version NPM downloads

With this plugin, a cordova plugin can be implemented in this way:

public class MyPlugin extends AnnotatedCordovaPlugin {
    @PluginAction
    private void pluginAction1(int firstOptionString secondOptionCallbackContext callbackContext) {
        ...
    }
}

AnnotatedCordovaPlugin extends original CordovaPlugin, so all methods are still accessible.

This plugin helps developers of cordova plugins to forget of the embarrassing and complicated way to develop a cordova plugin. Usually the developer had to implement a plugin like this (see Android Plugin Development Guide):

public class MyPlugin extends CordovaPlugin {
    @Override
    public boolean execute(String actionJSONArray argsCallbackContext callbackContext) throws JSONException {
        if ("pluginAction1".equals(action)) {
            JSONObject options = args.optJSONObject(0);
            pluginAction1(options, callbackContext);
 
        } else if ("pluginAction1".equals(action)) {
            JSONObject options = args.optJSONObject(0);
            pluginAction2(options, callbackContext);
 
        } else if ("moreActions".equals(action)) {
            ...
        } else {
            LOG.d("PLUGIN_TAG", String.format("Unknown action: %s", action));
            return false;
        }
 
        return true;
    }
 
    private void pluginAction1(JSONObject optionsCallbackContext callbackContext) {
        if (options == null) {
            return new callbackContext.error("options is null, please specify options");
        }
        callbackContext.success();
    }
}

PluginAction annotation

It has 3 parameters, all of them optional:

  • thread (ExecutionThread): enum, can be MAIN, UI, WORKER (defaults to MAIN)
  • actionName (String): the name of the method as it will be called from Javascript (defaults to java annotated method name)
  • isAutofinish (boolean): if callbackContext.success() has not been called and isAutofinish is set to true, when method finishes, callbackContext.success() will be called (defaults to true)
public class MyPlugin extends AnnotatedCordovaPlugin {
    @PluginAction(thread=ExecutionThread.UI, actionName="anotherName", isAutofinish=false)
    private void pluginAction1(int firstOptionString secondOptionCallbackContext callbackContext) {
        ...
        if (iWantSuccess) {
            callbackContext.success();
        } else {
            callbackContext.error();
        }
    }
}

Then from javascript:

myPlugin.anotherName = function (options, successCallback, failureCallback) {
    cordova.exec(successCallback, failureCallback, 'MyPlugin', 'anotherName', options);
};
 
myPlugin.anotherName([1, 'second']);

Agreements

Why this rewritting of chemerisuk plugin?

  • Implements proguard defaults
  • Implements auto finish (automatically calls callbackContext.success() if it has not been called yet)
  • Documentation has been rewritten
  • Methods have been heavily refactored and simplified to improve maintenance

Package Sidebar

Install

npm i cordova-annotated-plugin-android

Weekly Downloads

1,807

Version

1.0.4

License

MIT

Unpacked Size

13.7 kB

Total Files

11

Last publish

Collaborators

  • appfeel