Sample Blackberry Browser Plugin with XML Scripting
The Blackberry API is powerful but also incompletely documented; I've had to uncover many parts of it by searching multiple sources and through trial and error.
The sample code in this post illustrates several features of the Blackberry API used in the Berry411 application. The BrowserContentProvider interface allows an application like Berry411 to register itself as a handler for a custom mime-type "text/xml-berry411". Berry411 uses the SAX parser interface to parse and interpret scripting commands in the document returned from the server-- in effect this allows Berry411 to define its own custom scripting language allowing web pages to interact with the address book and other features of the Blackberry.
The BrowserContentProvider also allows an application to define its own rendering for pages that take advantage of all of the Blackberry UI widgets, rather than relying upon the browser for rendering.
Finally, the sample code illustrates a "launchOrRaiseApp" method which will ensure that the application with the given module name (e.g. "net_rim_bb_addressbook_app") is running and in the foreground. If the application is already running, it will be foregrounded, otherwise it will be launched.
-
import net.rim.device.api.xml.parsers.*;
-
import org.xml.sax.*;
-
import org.xml.sax.helpers.*;
-
import net.rim.blackberry.api.browser.*;
-
import net.rim.device.api.browser.field.*;
-
import net.rim.device.api.browser.plugin.*;
-
import net.rim.blackberry.api.invoke.*;
-
public final class BrowserPlugin extends BrowserContentProvider
-
implements BrowserPageContext
-
{
-
/**
-
* Retrieves list of mime types this provider can accept given a set of rendering options.
-
* @param context Rendering options in place this provider should consider.
-
* @return Array of mime types this provider will accept, given the provided rendering options.
-
*/
-
// Return subset of getSupportedMimeTypes() if accept depends in rendering options.
-
// For example HTML can be disabled in the rendering options, and HTMLConverter would remove
-
// html MIME types.
-
return ACCEPT;
-
}
-
/**
-
* Retrieves all the mime content types supported by this provider.
-
* @return Mime types this converter supports.
-
*/
-
return ACCEPT;
-
}
-
class BerryHandler extends DefaultHandler
-
{
-
private Contact _currentContact = null;
-
private BrowserContentBaseImpl _browserContent;
-
public BerryHandler(BrowserContentBaseImpl impl)
-
{
-
_browserContent = impl;
-
}
-
{
-
// SAX Parser implementation omitted
-
}
-
{
-
return _actionSummary;
-
}
-
}
-
/**
-
* Retrieves a browser content capable of rendering the mime content this provider can handle.
-
* @param context Provider context object provided by rendering session.
-
* @return Browser content to render specialized content.
-
*/
-
public BrowserContent getBrowserContent( BrowserContentProviderContext context)
-
throws RenderingException
-
{
-
if (context == null) {
-
throw new RenderingException("No Context is passed into Provider");
-
}
-
BrowserContentBaseImpl browserContentBaseImpl = new BrowserContentBaseImpl(context.getHttpConnection().getURL(),
-
null, context.getRenderingApplication(),
-
context.getRenderingSession().getRenderingOptions(), context.getFlags());
-
VerticalFieldManager vfm = new VerticalFieldManager(Manager.VERTICAL_SCROLL);
-
vfm.add(new LabelField("Processed Example action"));
-
try {
-
HttpConnection conn = context.getHttpConnection();
-
SAXParserFactory factory = SAXParserFactory.newInstance();
-
SAXParser parser = factory.newSAXParser();
-
parser.setAllowUndefinedNamespaces(true);
-
BerryHandler handler = new BerryHandler(browserContentBaseImpl);
-
parser.parse(in, handler);
-
vfm.add(new LabelField(handler.getActionSummary()));
-
}
-
e.printStackTrace();
-
}
-
vfm.add(new EditField("Error:", e.toString()));
-
}
-
browserContentBaseImpl.setContent(vfm);
-
browserContentBaseImpl.setTitle("Example");
-
// Set browser page context. This tells the browser how to display this field.
-
browserContentBaseImpl.setBrowserPageContext(this);
-
return browserContentBaseImpl;
-
}
-
/**
-
* Retrieves value of specified property as a boolean value.
-
* @param id ID of property to query.
-
* @param defaultValue Expected default value of property.
-
* @return Current value of property.
-
*/
-
public boolean getPropertyWithBooleanValue(int id, boolean defaultValue) {
-
return false;
-
}
-
/**
-
* Retrieves value of specified property as an int.
-
* @param id ID of property to query.
-
* @param defaultValue Expected default value of property.
-
* @return Current value of property.
-
*/
-
public int getPropertyWithIntValue(int id, int defaultValue) {
-
if (id == BrowserPageContext.DISPLAY_STYLE) {
-
// Disable the scroll bar.
-
return BrowserPageContext.STYLE_NO_VERTICAL_SCROLLBAR;
-
}
-
return 0;
-
}
-
/**
-
* Retrieves value of specified property as an object.
-
* @param id ID of property to query.
-
* @param defaultValue Expected default value of property.
-
* @return Current value of property.
-
*/
-
return null;
-
}
-
/* Retrieves value of specified property as a String value.
-
* @param id - ID of property to query.
-
* @param defaultValue - Expected default value of property.
-
* @return Current value of property.
-
*/
-
return null;
-
}
-
// -----------------------------------------------------
-
// static helper functions for use from BrowserPlugin
-
{
-
ApplicationManager appMgr = ApplicationManager.getApplicationManager();
-
ApplicationDescriptor[] apps = appMgr.getVisibleApplications();
-
for (int i=0; i <apps.length; i++) {
-
ApplicationDescriptor app = apps[i];
-
if (app.getModuleName().equals(desiredModule)) {
-
try {
-
appMgr.runApplication(app, true);
-
return true;
-
}
-
catch(ApplicationManagerException e) {
-
}
-
}
-
}
-
return false;
-
}
-
{
-
if (!raiseApp(desiredModule)) {
-
int handle = CodeModuleManager.getModuleHandle(desiredModule);
-
if (handle <= 0 ) {
-
return false;
-
} else {
-
ApplicationDescriptor[] appDescriptors = CodeModuleManager.getApplicationDescriptors(handle);
-
if (appDescriptors == null ) {
-
return false;
-
}
-
else {
-
if ( appDescriptors.length <=0 ) {
-
return false;
-
} else {
-
ApplicationDescriptor descriptor = new ApplicationDescriptor(
-
appDescriptors[0],
-
desiredModule, null,
-
null, -1, null, -1,
-
0
-
);
-
try {
-
ApplicationManager.getApplicationManager().runApplication(descriptor);
-
}
-
catch(ApplicationManagerException e) {
-
return false;
-
}
-
}
-
}
-
}
-
}
-
return true;
-
}
-
}