Skip to content

chemerisuk/cordova-support-android-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

74 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 *;
}

About

More convenient base CordovaPlugin class

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages