This is a Step by Step Tutorial for Android PhoneGap Plugin. If you are new to PhoneGap please refer to this documentation. Incase you are new the concept of PhoneGap Plugin, please refer to this documentation.
Plugin Idea
PhoneGap provides an API to read and write file, however it does not provide a api to list files. We will build a PhoneGap Plugin named DirectoryListingPlugin which helps us read contents of say SDCARD on Android. This is shown in the video below.
action contains the action sent by the javascript. This can be used to do more than one action when javascript calls this plugin. e.g for File Plugin, the action could be LIST,MKDIR,DELETE, etc
data is the arguments coming from JavaScript API to the native. This is an JSON Object. e.g for File Plugin are filename,path,etc
callbackId - TBD
When Javascript calls the plugin it is a non blocking call. The javascript is notified back using callbacks
In list() function call PhoneGap.exec(<<successCallback>>,<<failureCallback>>,<<Plugin Name>>,<<Action Name>>,<<Arguments Array>>);
Finally register both DirectoryListing class as an JavaScript Plugin and register Java Class as the native Plugin (invoked from javascript)
/**
*
* @return Object literal singleton instance of DirectoryListing
*/
var DirectoryListing = function() {
};
/**
* @param directory The directory for which we want the listing
* @param successCallback The callback which will be called when directory listing is successful
* @param failureCallback The callback which will be called when directory listing encouters an error
*/
DirectoryListing.prototype.list = function(directory,successCallback, failureCallback) {
return PhoneGap.exec( successCallback, //Success callback from the plugin
failureCallback, //Error callback from the plugin
'DirectoryListPlugin', //Tell PhoneGap to run "DirectoryListingPlugin" Plugin
'list', //Tell plugin, which action we want to perform
[directory]); //Passing list of args to the plugin
};
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin("directoryListing", new DirectoryListing());
});
Note that prior to PhoneGap v1.0.0 the addConstructor function above also needed a line to register the native (java) class with PhoneGap's plugin manager. Hence if you download the sample code you will see this line included in the addConstructor function.
If you see a Failed to run constructor: ReferenceError: PluginManager is not definedwarning its probably because you are on version 1.0.0 or higher and are still trying to use addService in your Javascript constructor.
Write TestCase for the Plugin
Coming Soon... (I am working on a strategy to use Selenium JavaScript to test phonegap)
Test the plugin
In order to test the plugin, you need to create a dummy application which consumes the plugin.
This can be done by creating a standard PhoneGap application for Android and including either the Java source code for your plugin or the JAR as well as the the JavaScript code.
The one thing to do when you are actually including a plugin in an application is that you need to edit the /res/xml/plugins.xml file such that the JavaScript code can get access to the Java plugin code. Add the following XML element as a child of the "plugins" element in the plugins.xml file: