Digging into BlackBerry Hybrid development

This is the second article on the BlackBerry Hybrid development topic. In this second part we’ll look a little deeper into the Hybrid development showing common uses and samples of code. If you need an introduction on the topic, please read the first article here.

Loading a Web Page
Display an HTML page is now a very simple task:

BrowserField myBrowserField = new BrowserField();
myBrowserField.requestContent("http://www.blackberry.com");

Take a look at the BrowserFieldDemo available in any JDE installation, and discover how big is the differences with the past.

Configuring the BrowserField
The new API introduces a simple way to configure the browser field with many properties. Just invoke BrowserFieldConfig.setProperty(key, value) to override default value of a property in the BrowserField. The first parameter is the name of the property, and the second parameter is the value for that property.

For example, the following code sample set the NAVIGATION_MODE,  GEAR and COOKIES properties:


BrowserFieldConfig myBrowserFieldConfig = new BrowserFieldConfig();
myBrowserFieldConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE,
                                 BrowserFieldConfig.NAVIGATION_MODE_POINTER);
myBrowserFieldConfig.setProperty(BrowserFieldConfig.ENABLE_GEARS,
                                 Boolean.TRUE);
myBrowserFieldConfig.setProperty(BrowserFieldConfig.ENABLE_COOKIES,
                                 Boolean.TRUE);
BrowserField myBrowserField = new BrowserField(myBrowserFieldConfig);
myBrowserField.requestContent("http://www.blackberry.com");

Configuring the Connection Type
You can specify what type of connection layer should be used by your BrowserField in order to retrieve the web content. In normal circumstances is not required to set the connection factory, since this field reads the connection types from device default settings.

BrowserFieldConfig myBrowserFieldConfig = new BrowserFieldConfig();
ConnectionFactory connectionFactory = new ConnectionFactory();
 //Array of connection methods
int[] trasportTypes = {	TransportInfo.TRANSPORT_BIS_B,
                        TransportInfo.TRANSPORT_TCP_CELLULAR,
                        TransportInfo.TRANSPORT_TCP_WIFI};

//Set the Factory connection preferences
connectionFactory.setPreferredTransportTypes(trasportTypes); 

//Set the Factory in the configuration of the BrowserField
myBrowserFieldConfig.setProperty(BrowserFieldConfig.CONNECTION_FACTORY,
                                 connectionFactory);

BrowserField myBrowserField = new BrowserField(myBrowserFieldConfig);
myBrowserField.requestContent("http://www.blackberry.com");

warning
The creation of the ConnectionFactory class is a time consuming task, so you should put it in a separate thread when used in a real application.

Using listener
You can define a requester handler that is called just before, during and after the connection is established. For example, you can implement your own progress bar in your application using this listener. This also provide the ability to centralize the process of URL request, such as editing header fields, or any relevant debug clocking.

Any connection handler must implement the BrowserFiledListener Interface.

BrowserFieldListener listener = new BrowserFieldListener() {
 public void documentLoaded(BrowserField browserField,
                            Document document) throws Exception {
    super.documentLoaded(browserField, document);
    //Base URI of downloaded resource
    String url = document.getBaseURI();
 }

 public void downloadProgress(BrowserField browserField,
                              ContentReadEvent event)
  throws Exception {
    super.downloadProgress(browserField, event);
    //Calculate the progress bar amount
    int progress = event.getItemsRead() / event.getItemsToRead();
 }
};

Using the script engine
The Scriptable classes can be used to extend the functionalities of your java code to JavaScript. It is the base class for java objects intended to be accessed from scripts.  JavaScript code is executed in the script engine, and you can use your java code from JavaScript. This can give to you a very high control on your application functionality.

ScriptableFunction _functionVibrate = new ScriptableFunction() {
  public Object invoke(Object thiz, Object[] args)
    throws Exception {
    if(args.length > 0 ) {
      int duration = Integer.parseInt(args[0].toString());
      Alert.startVibrate(duration);
    }
    return UNDEFINED;
  }
};

// Export the function in the Script Engine
myBrowserField.extendScriptEngine("samplescript.vibrate",
                                                _functionVibrate);

And in your HTML code you can invoke the exported function using JavaScript:

function myFunction(duration) {
  samplescript.vibrate(duration);
}

NOTE
The Object values passed in and returned from the various methods on this interface should be either language-dependent primitive values, or other Scriptable objects. For ECMA-262 engines, such as, the primitive types consist of String, Integer, Double, Boolean, and Date. These will get converted to ECMA strings, numbers, boolean, and Date values. Java objects that are neither a primitive type or a Scriptable may not be correctly mapped to an object in the script environment.

6 thoughts on “Digging into BlackBerry Hybrid development

  1. Pingback: 2010 in review | Danilo Ercoli

  2. Matin Shin

    Thank you!

    I want to use pushscreen in ScriptableFunction.
    but don’t excute pushscreen in ScriptableFunction.
    Do you know?

    this source :

    ScriptableFunction nFilterInit = new ScriptableFunction()
    {
    public Object invoke(Object thiz, Object[] args) throws Exception
    {
    if(args.length > 0)
    {
    System.out.println( “=====excute=====”);
    //don’t excute
    UiApplication.getUiApplication().pushScreen( new TestScreen() );

    System.out.println( “=====excute=====”);
    }
    return Boolean.TRUE;
    }
    };
    browserField.extendScriptEngine(“blackberry.init”, nFilterInit);

    Reply
      1. Robert Laird

        UiApplication.getUiApplication().invokeLater(new Runnable()
        {
        public void run()
        {
        UiApplication.getUiApplication().pushScreen( new TestScreen() );

        }
        });

        Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s