Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
New Developer
UserNeo
Posts: 74
Registered: ‎10-23-2008
My Device: 9000

"Battery Status" Extension?

 

Hi,

I am not a Java developer, but I need a small "battery status" Javascript Extension for my widget. Can someone help me there?

Maybe someone has a "battery status" JavaScript Extension or the source code for this? The Extension will only return the current value of the battery, just like in "otpions -> status".

 

thx & cu

UserNeo

Retired
astanley
Posts: 1,382
Registered: ‎07-02-2009
My Device: BlackBerry Bold 9900
My Carrier: Bell

Re: "Battery Status" Extension?

The Java code to get this info is simply the following :

 

 

DeviceInfo.getBatteryLevel()

 

 

Are you already comfortable building your own JavaScript Extensions?  If you are just getting started, I'd recommend completing the "Building a BlackBerry WebWorks Extension" tutorial and substitute any "Alert" logic for the getBatteryLevel() logic above.

 

Let the community know if you have any further questions - I am noticing a lot more people are diving into extension development.

 

Sincerely,

Adam

Follow me on Twitter: @n_adam_stanley
-------------------------------------------------------------------------------------------------------------------------
Your app doesn't work? Use BlackBerry remote web inspector to find out why.
Developer
jtyberg
Posts: 124
Registered: ‎09-06-2010
My Device: Not Specified

Re: "Battery Status" Extension?

Assuming you add a feature called 'system'...this is untested, and simplistic, but you'll get the gist:


 

in WidgetExtension class:

public void loadFeature(String feature, String version, Document doc,
ScriptEngine scriptEngine) throws Exception {
script = scriptEngine;
if (feature.equals("system")) {
scriptEngine.addExtension("system.battery", new Battery());
}
}


Battery Scriptable element:

public final class Battery extends Scriptable {
   public static final String FIELD_LEVEL = "level";

    /**
* The following fields are supported from the script environment:
*
* <code>system.battery.level</code> - retrieves battery level
*/
    public Object getField(String name) throws Exception {
        if (FIELD_LEVEL.equals(name)) {
            return new ScriptableFunction() {
                public Object invoke(Object obj, Object[] oargs) throws Exception {
                    int level = DeviceInfo.getBatteryLevel();
                    return "{" + level + "}"; // JSON
                }
            };
        }
        return super.getField(name);
    }
}

 


 

New Developer
UserNeo
Posts: 74
Registered: ‎10-23-2008
My Device: 9000

Re: "Battery Status" Extension?

Thank you for your answers. I tried to create an extension, but the widget crashes with Java Error. Where is my fault?

 

BatteryExtension.java

 

package samplecode;

import org.w3c.dom.Document;

import net.rim.device.api.browser.field2.BrowserField;
import net.rim.device.api.script.ScriptEngine;
import net.rim.device.api.web.WidgetConfig;
import net.rim.device.api.web.WidgetExtension;

public final class BatteryExtension implements WidgetExtension {
	public String[] getFeatureList() {
		return null;
	}

	public void loadFeature(String feature, String version, Document doc,
		    ScriptEngine scriptEngine) throws Exception {
   
	    if (feature.equals("system")) {
	        scriptEngine.addExtension("system.battery", new BatteryFunction());
	    }
	}

	public void register(WidgetConfig widgetConfig, BrowserField browserField) {
	}

	public void unloadFeatures(Document doc) {
	}
}

 

 

BatteryFunction.java

 

package samplecode;

import net.rim.device.api.script.ScriptableFunction;
import net.rim.device.api.system.DeviceInfo;


public class BatteryFunction extends ScriptableFunction {
	public static final String FIELD_LEVEL = "level";

    /**
     * The following fields are supported from the script environment:
     * 
     *  <code>system.battery.level</code> - retrieves battery level
     */    
    public Object getField(String name) throws Exception {
        if (FIELD_LEVEL.equals(name)) {
            return new ScriptableFunction() {
                public Object invoke(Object obj, Object[] oargs) throws Exception {
                    int level = DeviceInfo.getBatteryLevel();
                    return "{" + level + "}";  // JSON
                }
            };
        }
        return super.getField(name);
    }
}

 

library.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<library>
    <extension>
        <entryClass>samplecode.BatteryExtension</entryClass>
    </extension>
    <features>
        <feature id="system.battery" version="1.0.0" ></feature>
    </features>
</library>

 

 

 

index.htm

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
	<head>
	    <meta name="viewport" id="viewport" content="height=device-height,width=device-width,user-scalable=no" />
		<title>Untitled Page</title>

        <script type="text/javascript">

            alert("battery:" + system.battery.level);
        </script>
	</head>
	<body>
	</body>
</html>

 

config.xml

 

<?xml version="1.0" encoding="utf-8"?>
<widget xmlns:rim="http://www.blackberry.com/ns/widgets" version="1.0.0" rim:header="RIM-webworks:rim/webworks" xmlns="http://www.w3.org/ns/widgets">
  <name>My WebWorks Application</name>
  <description />
  <author href="" rim:copyright="" email="">Me</author>
  <content src="index.htm" />
  <feature id="system.battery" required="true" version="1.0.0.0" />
  <license href="" />
  <rim:cache disableAllCache="false" aggressiveCacheAge="2592000" maxCacheSizeTotal="1024" maxCacheSizeItem="128" />
</widget>

 

 

 cu

UserNeo

Developer
jtyberg
Posts: 124
Registered: ‎09-06-2010
My Device: Not Specified

Re: "Battery Status" Extension?

[ Edited ]

You need to understand how WebWorks loads the extension features.  It goes through the list of features you provide, then calls loadFeature for each one.  You're not providing the feature list.  So in your extension class, try this:

 

 

public String[] getFeatureList() {
String[] result = new String[1];
result[0] = "system";
return result;
}

 

Then change your library.xml and config.xml to use the 'system' feature (not system.battery):

 

 <feature id="system" version="1.0.0" ></feature> 

Your app should then load the 'system' feature.  In loadFeature, you add the 'system.battery' function to the script engine, which is implemented using BatteryFunction class. 

 

Invoking 'system.battery.level' will invoke BatteryFunction.getField.  In this case, the Java code implements an anonymous class to retrieve the battery level.  This is a very compact example.  If you were to implement additional system or battery features, a full Java class would be more suitable.  The key is, the Java class that is returned by getField must implement the 'invoke' method.

 

 

New Contributor
unimedisys
Posts: 8
Registered: ‎07-19-2010
My Device: Bold 9000

Re: "Battery Status" Extension?

I have applied the corrections however still no luck with it. Can you post the working project ?