Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cordova-support-android-plugin

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cordova-support-android-plugin

More convenient base CordovaPlugin class

  • 2.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

cordova-support-android-plugin

NPM version NPM downloads NPM total downloads Twitter

The plugin introduces new base class for Android Cordova plugins called ReflectiveCordovaPlugin that extends CordovaPlugin and allows to reduce boilerplate code.

Index

Default implementation of execute

This is an example of typical cordova plugin implementation:

// required imports...

public class MyPlugin extends CordovaPlugin {
    private static final String METHOD_1 = "method1";
    private static final String METHOD_2 = "method2";

    @Override
    public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {
        if (METHOD_1.equals(action)) {
            method1(args, callbackContext);
        } else if (METHOD_2.equals(action)) {
            method2(callbackContext);
        // more methods might go here...
        } else {
            return false;
        }
        return true;
    }

    protected void method1(CordovaArgs args, CallbackContext callbackContext) {
        // method1 implementation goes here
    }

    protected void method2(CallbackContext callbackContext) {
        // method2 implementation goes here
    }
}

Below is equal of code using ReflectiveCordovaPlugin:

// required imports...
import by.chemerisuk.cordova.support.ReflectiveCordovaPlugin;

public class MyPlugin extends ReflectiveCordovaPlugin {
    @CordovaMethod
    protected void method1(CordovaArgs args, CallbackContext callbackContext) {
        // method1 implementation goes here
    }

    @CordovaMethod
    protected void method2(CallbackContext callbackContext) {
        // method2 implementation goes here
    }
}

Asynchronous actions

Cordova best practise is to invoke time-consuming logic in a separate thread:

// required imports...

public class MyPlugin extends CordovaPlugin {
    private static final String METHOD_1 = "method1";

    @Override
    public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {
        if (METHOD_1.equals(action)) {
            method1(callbackContext);
        } else {
            return false;
        }
        return true;
    }

    protected void method1(CallbackContext callbackContext) {
        cordova.getThreadPool().execute(new Runnable {
            @Override
            public void run() {
                // method1 implementation goes here
            }
        });
    }
}

@CordovaMethod annotation allows to specify execution thread as enumaration paratemer:

// required imports...
import by.chemerisuk.cordova.support.ReflectiveCordovaPlugin;
import static by.chemerisuk.cordova.support.ExecutionThread.WORKER;

public class MyPlugin extends ReflectiveCordovaPlugin {

    @CordovaMethod(WORKER)
    protected void method1(CallbackContext callbackContext) {
        // method1 implementation goes here
    }

}

ProGuard notes

Only needed for version 1.

If you obfuscate app with ProGuard then proguard-rules.pro usually contains rules:

-keep class org.apache.cordova.* { *; }
-keep class org.apache.cordova.engine.* { *; }
-keep public class * extends org.apache.cordova.CordovaPlugin

ReflectiveCordovaPlugin uses method names to match an appropriate action. Therefore you should keep names for methods with @CordovaMethod annotation:

-keepclassmembers class ** {
    @by.chemerisuk.cordova.support.CordovaMethod *;
}

keep public enum by.chemerisuk.cordova.support.ReflectiveCordovaPlugin$** {
    **[] $VALUES;
    public *;
}

Keywords

FAQs

Package last updated on 11 Aug 2022

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc