cordova-support-android-plugin
Advanced tools
Comparing version 0.3.2 to 0.3.3
{ | ||
"name": "cordova-support-android-plugin", | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"description": "More convenient base CordovaPlugin class", | ||
@@ -5,0 +5,0 @@ "cordova": { |
151
README.md
@@ -1,145 +0,9 @@ | ||
# cordova-support-android-plugin | ||
More convenient base CordovaPlugin class | ||
# cordova-support-android-plugin<br>[![NPM version][npm-version]][npm-url] [![NPM downloads][npm-downloads]][npm-url] | ||
The plugin introduces new base class for Android Cordova plugins called `ReflectiveCordovaPlugin` that extends `CordovaPlugin` and allows to reduce boilerplate code. Please read links below to understand all new capabilities: | ||
* [Default implementation of `execute`](https://github.com/chemerisuk/cordova-support-android-plugin/wiki/Default-implementation-of-execute) | ||
* [Argument binding](https://github.com/chemerisuk/cordova-support-android-plugin/wiki/Argument-binding) | ||
* [Asynchronous execution](https://github.com/chemerisuk/cordova-support-android-plugin/wiki/Asynchronous-execution) | ||
## Default implementation of `execute` | ||
Let's look at a simple plugin | ||
```java | ||
import org.apache.cordova.CordovaPlugin; | ||
... | ||
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(callbackContext); | ||
} else if (METHOD_2.equals(action)) { | ||
method2(callbackContext); | ||
/// more methods... | ||
} else { | ||
return false; | ||
} | ||
return true; | ||
} | ||
protected void method1(CallbackContext callbackContext) { | ||
// method1 implementation | ||
} | ||
protected void method2(CallbackContext callbackContext) { | ||
// method2 implementation | ||
} | ||
... | ||
} | ||
``` | ||
In order to use the plugin extend your plugin class from `ReflectiveCordovaPlugin`. Then mark action methods with annotation `@CordovaMethod`. Make sure that name of such method matches parameter `action` passed to `execute` | ||
```java | ||
import by.chemerisuk.cordova.support.ReflectiveCordovaPlugin; | ||
... | ||
public class MyPlugin extends ReflectiveCordovaPlugin { | ||
@CordovaMethod | ||
protected void method1(CallbackContext callbackContext) { | ||
// method1 implementation | ||
} | ||
@CordovaMethod | ||
protected void method2(CallbackContext callbackContext) { | ||
// method2 implementation | ||
} | ||
... | ||
} | ||
``` | ||
## Argument binding | ||
Often you need to send parameters from JavaScript into Java | ||
```java | ||
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.getBoolean(0), callbackContext); | ||
} else if (METHOD_2.equals(action)) { | ||
method2(args.getString(0), args.getBoolean(1), callbackContext); | ||
} else { | ||
return false; | ||
} | ||
return true; | ||
} | ||
protected void method1(boolean flag, CallbackContext callbackContext) { | ||
// method1 implementation | ||
} | ||
protected void method2(String id, boolean flag, CallbackContext callbackContext) { | ||
// method2 implementation | ||
} | ||
... | ||
} | ||
``` | ||
`ReflectiveCordovaPlugin` does argument parsing for you, no extra code is required: | ||
```java | ||
public class MyPlugin extends ReflectiveCordovaPlugin { | ||
@CordovaMethod | ||
protected void method1(boolean flag, CallbackContext callbackContext) { | ||
// method1 implementation | ||
} | ||
@CordovaMethod | ||
protected void method2(String id, boolean flag, CallbackContext callbackContext) { | ||
// method2 implementation | ||
} | ||
... | ||
} | ||
``` | ||
Reference and value types are supported, as well as `JSONObject` and `JSONArray`. If parameter type is incompatable with value `JSONException` is fired as expected. | ||
## Asynchronous execution | ||
Cordova best practise is to invoke time-consuming logic in a separate thread: | ||
```java | ||
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(args.getBoolean(0), callbackContext);s | ||
} else { | ||
return false; | ||
} | ||
return true; | ||
} | ||
protected void method1(boolean flag, CallbackContext callbackContext) { | ||
cordova.getThreadPool().execute(new Runnable { | ||
@Override | ||
public void run() { | ||
// time-consuming method1 implementation | ||
} | ||
}); | ||
} | ||
} | ||
``` | ||
`@CordovaMethod` has special `async` flag that allows to make the same: | ||
```java | ||
public class MyPlugin extends ReflectiveCordovaPlugin { | ||
@CordovaMethod(async = true) | ||
protected void method1(boolean flag, CallbackContext callbackContext) { | ||
// time-consuming method1 implementation | ||
} | ||
} | ||
``` | ||
## ProGuard notes | ||
If you obfuscate your app with ProGuard then `proguard-rules.pro` usually contains rules: | ||
If you obfuscate app with ProGuard then `proguard-rules.pro` usually contains rules: | ||
@@ -160,1 +24,4 @@ ``` | ||
[npm-url]: https://www.npmjs.com/package/cordova-support-android-plugin | ||
[npm-version]: https://img.shields.io/npm/v/cordova-support-android-plugin.svg | ||
[npm-downloads]: https://img.shields.io/npm/dm/cordova-support-android-plugin.svg |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
8590
99
27