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

Add a PhoneGap plug-in to the BlackBerry Hybrid Web Container

Prerequisites
Set up the BlackBerry Eclipse development IDE. See  http://us.blackberry.com/developers/javaappdev/javaplugin.jsp
Task

This example procedure shows the steps to create and use a custom plug-in to get battery information for the device.

  1. In Eclipse, import the HybridWebContainer project.
  2. Open the plugins.xml file, which is located in res/xml, and add this tag:
    < plugin name="Battery1" value="com.sybase.hwc.Battery1"/> 
  3. Add a new Java source file called Battery1.java to the src folder, and paste in this code:
    package com.sybase.hwc;
    import  org.apache.cordova.api.Plugin;
    import  org.apache.cordova.api.PluginResult;
    import org.apache.cordova.json4j.JSONArray;
    
    public class Battery1 extends Plugin {
        public static final String GET_LEVEL = "getLevel";
    
        /**
         * Executes the requested action and returns a PluginResult.
         *
         * @param action     The action to execute.
         * @param callbackId The callback ID to be invoked upon action completion.
         * @param args       JSONArry of arguments for the action.
         * @return           A PluginResult object with a status and message.
         */
        public PluginResult execute(String action, JSONArray args, String callbackId) {
            PluginResult result = null;
            if (GET_LEVEL.equals(action)) {
                // retrieve the device battery level
                int level = net.rim.device.api.system.DeviceInfo.getBatteryLevel();
                result = new PluginResult(PluginResult.Status.OK, level);
            }
            else {
                result = new PluginResult(PluginResult.Status.INVALID_ACTION,
                    "Battery: Invalid action: " + action);
            }
            return result;
        }
    
        /**
         * Called when Plugin is paused.
         */
        public void onPause() {
        }
    
        /**
         * Called when Plugin is resumed.
         */
        public void onResume() {
        }
    
        /**
         * Called when Plugin is destroyed.
         */
        public void onDestroy() {
        }
    } 
  4. 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. The rest of this example explains how to test and use the PhoneGap plug-in.
  5. Create a new Hybrid App.
    1. Select File > New > Mobile Application Project.
    2. In Project name, enter PhonegapTest.
    3. Click Finish.
  6. Right-click the PhonegapTest project folder and select New > Hybrid App Designer.
  7. Click Next.
  8. Select Can be started, on demand, from the client and click Finish.
  9. Add an HtmlView control to the start screen of the Hybrid App.
  10. Run the Hybrid App Package Generation wizard to create the Generated Hybrid App directory structure Generated Hybrid App\PhonegapTest\ html\js.
  11. Open the Custom.js file and add this code:
    var Battery1 = {
    	    level: function(successCallback, errorCallback) {
    	        PhoneGap.exec(successCallback, errorCallback, 'Battery1', 'getLevel',[]);
    	    }
    	};
    
    function getBatteryLevel() {
        Battery1.level(function(level) {
                alert('Battery level is ' + level);
            }, 
            function(error) {
                alert('Error retrieving battery level:' + error);
            }); 
    }
  12. Find the customAfterHybridAppLoad() function, and add this code:
    function customAfterHybridAppLoad() {
    document.addEventListener("deviceready", getBatteryLevel, false );   
    } 
    This is the code that makes use of the plug-in.
  13. Generate the Hybrid App package again.
  14. Assign the Hybrid App to a device that has the modified Hybrid Web Container installed.
  15. On the device, run the Hybrid App.
    You see the alert message with the battery level information.