Add a PhoneGap plug-in to the Hybrid Web Container.
<plugin name="DirectoryListPlugin" value="com.sybase.hwc.DirectoryListPlugin" />
/**
* Example of Android PhoneGap Plugin
*/
package com.sybase.hwc;
import java.io.File;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import com.phonegap.api.PluginResult.Status;
/**
* PhoneGap plugin which can be involved in following manner from javascript
* <p>
* result example - {"filename":"/sdcard","isdir":true,"children":[{"filename":"a.txt","isdir":false},{..}]}
* </p>
* <pre>
* {@code
* successCallback = function(result){
* //result is a json
*
* }
* failureCallback = function(error){
* //error is error message
* }
*
* window.plugins.DirectoryListing.list("/sdcard",
* successCallback
* failureCallback);
*
* }
* </pre>
* @author Rohit Ghatol
*
*/
public class DirectoryListPlugin extends Plugin {
/** List Action */
public static final String ACTION="list";
/*
* (non-Javadoc)
*
* @see com.phonegap.api.Plugin#execute(java.lang.String,
* org.json.JSONArray, java.lang.String)
*/
@Override
public PluginResult execute(String action, JSONArray data, String callbackId) {
Log.d("DirectoryListPlugin", "Plugin Called");
PluginResult result = null;
if (ACTION.equals(action)) {
try {
String fileName = data.getString(0);
JSONObject fileInfo = getDirectoryListing(new File(fileName));
Log
.d("DirectoryListPlugin", "Returning "
+ fileInfo.toString());
result = new PluginResult(Status.OK, fileInfo);
} catch (JSONException jsonEx) {
Log.d("DirectoryListPlugin", "Got JSON Exception "
+ jsonEx.getMessage());
result = new PluginResult(Status.JSON_EXCEPTION);
}
} else {
result = new PluginResult(Status.INVALID_ACTION);
Log.d("DirectoryListPlugin", "Invalid action : "+action+" passed");
}
return result;
}
/**
* Gets the Directory listing for file, in JSON format
* @param file The file for which we want to do directory listing
* @return JSONObject representation of directory list. e.g {"filename":"/sdcard","isdir":true,"children":[{"filename":"a.txt","isdir":false},{..}]}
* @throws JSONException
*/
private JSONObject getDirectoryListing(File file)
throws JSONException {
JSONObject fileInfo = new JSONObject();
fileInfo.put("filename", file.getName());
fileInfo.put("isdir", file.isDirectory());
if (file.isDirectory()) {
JSONArray children = new JSONArray();
fileInfo.put("children", children);
if (null != file.listFiles()) {
for (File child : file.listFiles()) {
children.put(getDirectoryListing(child));
}
}
}
return fileInfo;
}
}
/**
*
* @return 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, //Callback which will be called when directory listing is successful
failureCallback, //Callback which will be called when directory listing encounters an error
'DirectoryListPlugin', //Telling PhoneGap that we want to run "DirectoryListing" Plugin
'list', //Telling the plugin, which action we want to perform
[directory]); //Passing a list of arguments to the plugin, in this case this is the directory path
};
/**
* <ul>
* <li>Register the Directory Listing Javascript plugin.</li>
* <li>Also register native call which will be called when this plugin runs</li>
* </ul>
*/
PhoneGap.addConstructor(function() {
//Register the javascript plugin with PhoneGap
PhoneGap.addPlugin('directorylisting', new DirectoryListing());
});
var directoryListingFunction = function()
{
var dl = new DirectoryListing();
function onSuccess(r)
{
var replace = document.getElementById('key2');
if(replace)
{
var theHtml = "<html><head><title>A Title</title></head><body>Top level contents of sd card:<br/>";
if(r.children)
{
var index = 0;
for(index = 0; index <= r.children.length;index++)
{
if(r.children[index]){
theHtml += r.children[index].filename + "<br/>";
}
}
}
else
{
alert("No r.children!!");
}
theHtml +="</body></html>";
replace.innerHTML = theHtml;
}
}
function onError(e)
{
alert( "Error: " + e );
}
var result = dl.list( "/sdcard", onSuccess, onError );
}
directoryListingFunction();