Adding a Custom Plug-in to the Android Hybrid Web Container

Add a PhoneGap (now called Cordova) plug-in to the Android Hybrid Web Container.

  1. In Eclipse, open the HybridWebContainer project.
  2. Open the plugins.xml file, which is located in res/xml.
  3. Add your custom plug-in, for example:
    <plugin name="DirectoryListPlugin" value="com.sybase.hwc.DirectoryListPlugin" /> 
  4. Add plug-in images to the HybridWebContainer project.
    The plug-in used in this example does not include images, but they are allowed in plug-ins. Images for plug-ins are usually stored in res\drawable.
  5. Add the Java source file that implements the custom plug-in, for example, DirectoryListplugin.java.
    This example PhoneGap plug-in lists all files on the SDCard of the device.
    /**
     * 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 org.apache.cordova.api.Plugin;
    import org.apache.cordova.api.PluginResult;
    import org.apache.cordova.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;
    	}
    }
  6. Save the file.
    These are all the changes needed for the Hybrid Web Container; you can now build it and install it on the device. What the plug-in actually does is implemented in the Java file in the execute function.