Socket
Book a DemoInstallSign in
Socket

cordova-annotated-plugin-android

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cordova-annotated-plugin-android

Enhanced CordovaPlugin class

latest
Source
npmnpm
Version
1.0.4
Version published
Weekly downloads
689
12.58%
Maintainers
1
Weekly downloads
 
Created
Source

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 firstOption, String secondOption, CallbackContext 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 action, JSONArray args, CallbackContext 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 options, CallbackContext 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 firstOption, String secondOption, CallbackContext 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

Keywords

cordova

FAQs

Package last updated on 04 Jan 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts