Opera Platform
Introduction
The Opera Platform SDK is a framework for building web based applications on mobile devices. Currently it only supports Symbian devices running Series 60, but according to Opera the SDK will also be available for Windows Mobile devices. It uses HTML and Stylesheets to present the user interface, and Javascript for functionality. It also supports developing AJAX applications using XMLHTTPRequest. This makes developing for the Opera Platform SDK very similar to developing web applications. Additionally the SDK provides access to phone functionality such as messaging or receiving events from the hardware buttons. The XMLHTTPRequest object can also read files on the device filesystem. All the example applications can be found in this archive.
A simple AJAX tutorial
In this chapter we will make a simple AJAX web application which converts between different currencies. The example uses a freely available web service [IV]. I will later show how to modify this application to make it an Opera Platform Web Application.
AJAX is asynchronous, because you don’t wait for data to be returned from a server, but rather hava a function be called when data is returned. This function is supplied by the XMLHttpRequest objet. Also there is no need to reload the entire web page, as it is possible to modify the DOM tree[VI] using JavaScript code.
Making the HTML document
The HTML document will consist of two lists of available currencies, an input for amount to convert, a button to do the conversion and a paragraph for displaying the result. In this example I have only included a few currencies, but the complete list is used in the example.
Currency Converter
| From: | |
| To: | |
| Amount: | |
Adding the script
This gives us our user interface, but still nothing happens when we click the ‘Convert’ button. The first step is to create the XMLHTTPRequest object. The XMLHttpRequest object is supported on most modern browsers, but in MS Internet Explorer it is an ActiveX component, so this needs to be concidered. We will now add code to make this object, the following script should be placed in the head tag.
Asynchronous communication with server
Now we will need to send a request to the web service. Insert the following code into the script above.
var convert = function() { // Retrieve data from the input fields. var f = document.getElementById('from').value; var t = document.getElementById('to').value; // If from and to are the same, no need to send request to server. if(f==t){ var amount = document.getElementById('amount').value; document.getElementById('result').innerHTML = amount + ' ' + f + ' = ' + amount + ' ' + t; return; } // Make the GET Request URL http.open('GET', 'http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency='+ f +'&ToCurrency=' + t); // Specify a function to handle the result http.onreadystatechange = handleResponse; // Send request http.send(null); }
The last three statements are where AJAX comes into the picture. First we create the request using http.open. As mentioned AJAX is asynchronous. Therefore our convert method will not wait for an response from the server, rather we let XMLHttpRequest take care of that. When the response is ready the http.onreadystatechange method is called. In the example we set handleResponse as our onreadystatechange method. The last line sends the request to the server. Often this function call will work without the null parameter. However the IE ActiveX component require it, and other implementations will ignore it, so it’s best not to leave it out. The last step is to create the handleResponse method which will be called when the response from the server is ready.
var handleResponse = function() { // Check if response is correct if(http.readyState==4 && http.status==200){ var f = document.getElementById('from').value; var t = document.getElementById('to').value; var amount = document.getElementById('amount').value; // The response is XML var xml = http.responseXML; // The rate is in a 'double' tag var rate = xml.getElementsByTagName('double')[0].firstChild.nodeValue; // Do the conversion var result = rate*amount; // Display the result document.getElementById('result').innerHTML = amount + ' ' + f + ' = ' + result + ' ' + t; } }
First we check if the response is correct. Then we parse the resulting XML document. The format of the result is specified by the web service. Using XML formated data is common, but not required. Any text based format will work. Then we update the DOM tree to display the result.
The last thing we need to do is to attach functionality to the ‘Convert’ button, so we’ll switch the button code with the following
This will cause our convert method to be called when button is clicked.
Installing the SDK
The SDK is available for Series 60, and for a desktop environement. All installation files can be downloaded from the Opera Platform Developer forum[I]. The desktop environment can be used for development and testing without having to copy the application to the phone. However the desktop environment doesn’t support phone functions such as messaging and making calls, so using this functionality will cause your application to fail silently on the desktop.
Installing the desktop environment
To develop web applications in a desktop environment you need to download the Opera Platform enabled version of the Opera Desktop Browser. After signing up to at the Opera Platform website[I] download and install the Windows/Mac OSX/Linux Opera Platform browser. This is a custom version of the standard Opera web browser, with relaxed security. Therefore it is not recommended for ordinary browsing, but can be installed in a separate directory, and used alongside the standard Opera web browser. Then you will need the Opera Platform SDK Zip archive for desktop. Unzip this to any location. This archive contains the Opera Platform and some demo web applications. To start the Opera Platform go to the oxygen-sdk-1.0-beta1-rev1\ directory and open the home.html file. This will launch the Opera Platform in the Opera browser.
Installing the Series 60 SDK
To run your web applications on a Series 60 device you will need to install the Opera Platform Application Player for Series 60. This has the same function as the Opera Platfrom Desktop browser. The application player can be downloaded from the same location as the desktop environment. If communication between the phone and computer is set up correctly you can just run the SIS file to install the application player. It is recomended to install to a memory card rather than on the phone memory if possible. Also download and install the Opera Platform SDK SIS installer for Symbian Series 60. This is exactly the same as the Opera Platfor Zip archive, so you could also just copy the desktop installation, but the SIS file is more convenient. The Platform SDK will be installed in System\Apps\op\data\ directory, which will contain the same as the oxygen-sdk-1.0-beta1-rev1\ directory for your desktop environment. From now on I will refer to this location as the root directory of the SDK installation, both for desktop and device. The SIS file will install an icon called Web apps in your Series 60 application menu, which will open the Opera Platform.
Other useful tools
- P3NFS - On Linux it’s possible to use P3NFS, which lets you mount the Symbian filesystem on your local filesystem. Thus enabling you to develop directly on the target device.
- FExplorer - FExplorer is a file system browser that give you access to the complete file system. It is usefull for copying web applications from the visible parts of the file system to the target location.
- MakeSIS - Used to make SIS files, that can install your application without having to manually move it from the visible part of the file system.
Making Opera Platform Web Applications
In this part we will make the currency conversion application into an Opera Plataform web application. To achieve this we need to include the Opera Platform framework script, and the Opera Platform persistent stylesheet. Also we can specify an application class. The following code includes the Opera Platform framework script and the Opera Platform persistent stylesheet:
It is also possible to include your own stylesheets to further style the application. The application class is defined as any other class, but it automatically inherits properties from from the Opera Platform application prototype. This enables your application to implement Opera Platform functionality, such as recieving messages from other applications, keypress events and making menus.
The next step in making the application an Opera Platform web application is making an application definition file. This should be in the same directory as the application, and named appdef.xml. The application definition file is an XML file specifying configuration data for the application. For our currency conversion application we specify the appdef.xml file like this
Currency Converter dwgsdbrtvahwrwut3556 0.1 Convert Banarne ../chrome/icons/appgridIcon1.png 710
Most properties are self explanatory, but I will explain a few.
- guid - the guid property must be unique for each application.
- javaScriptNS - this specifies the application class, which will be instansiated when the application is loaded.
- generatedEvents - comma seperated list which specifies which messages this application is capable of generating.
- supportedEvents - comma seperated list which specifies which messages this application is capable of handling
For a more thorough description of appdef.xml and the available message events see [II].
The final step is to add the application to the registeredApps.xml file. This file can be found in the root SDK directory. It contains one url tag for each application. To add the currency converter to the list of web applications add the following line to the registeredApps.xml file
file
Where file is the HTML file that contains the application. It’s location is specified relative to the root SDK directory. Your application should be in a directory under the root SDK directory, and each directory should only contain one application. If the currency converter is saved as index.html under a directory named CurrencyConverter the registeredApps.xml entry for this application would be
CurrencyConverter/index.html
The serviceid must be unique for each web application.
Implementing Yahoo! Maps on the Opera Platform SDK
Yahoo! Maps[V] provide an AJAX API. This makes it simple to use maps in Opera Platform web applications. To do this we will need a div container to contain the map, and a javascript to control it. First we need to include the Yahoo! Maps AJAX API library like this.
The appid parameter is your application ID, this can be obtained from the Yahoo! Maps web site. The next step is to add the map container.
Then we need to create the script. In the script we create YMap object, that will control the map view.
We give the map container as a parameter to the YMap constructor. Then we call the drawZoomAndCenter method to draw a map of the location specified by myPoint with zoom level 3. Further we make the appdef.xml file as usual, but this time we specify loadMap as the application class.
loadMap
This example isn’t really programmed in an object oriented fashion, so the only effect specifying loadMap as the application class will have is that it will be called when the application starts.
Adding controls
Now we will add some controls to the map. The YMap object provides functions for panning and zooming, so all we need to do is make a user interface that trigger theese functions. We add some links
[N] [S] [W] [E] [+] [-]
Then we add the panMap, zoomIn and zoomOut functions to our javascript.
function panMap(direction){ // Retrieve current position var myPoint = myMap.getCenterLatLon(); // Move 20px var crd = myMap.convertLatLonXY(myPoint); switch (direction){ case 'N': crd.y=crd.y-20; break; case 'S': crd.y=crd.y+20; break; case 'E': crd.x=crd.x+20; break; case 'W': crd.x=crd.x-20; break; } // Write to lat/lon object myPoint = myMap.convertXYLatLon(crd); // Pan to new location myMap.panToLatLon(myPoint); } function zoomIn(){ var zoom = myMap.getZoomLevel(); if(zoom<=1) return; myMap.setZoomLevel(zoom-1); } function zoomOut(){ var zoom = myMap.getZoomLevel(); if(zoom>=16) return; myMap.setZoomLevel(zoom+1); }
Using Opera Platform DOM Interface - Sending SMS
The Opera Platform SDK offers developers access to some platform specific functionality, such as battery indicators and messaging, through JavaScript. All of the DOM Interface functionality[III] is available thorugh the device object. In this example we will send an SMS message, but sending other messages (MMS, E-Mail etc.) is very similar.
draft = device.createMessage(); contact = device.createContactByAddress('sms',phone_nr); draft.to.add(contact); draft.content = message; sent = draft.send('sms');
First we create a DraftMessage object using device.createMessage. Then we create a contact using device.createContactByAddress, specifying that it’s an SMS contact so the address is a phone number. Contacts can also be fetched from the phone contact list. The contact list can be accessed through the deviece.contacts object, which is of type ContactList. The next step is to add contacts that will recieve the message to the to object in line 3. The DraftMessage object has a list of attachments, this is an AttachmentList object named attachmens. This is where we add the contents of the message. The content object however is a shorthand for accessing the first text/plain attachment’s text attribute, so for SMS text messages it is sufficient to use the content object. This is set in line 4. The last thing to do is to send the message through the send method. This is done on the last line, with the argument ’sms’ specifying it is an SMS message to send. The send method returns a CompleteMessage object.
A complete example of and SMS sender application can be found below.
SMS Sender
| Send to: |
| Message |
|
0/160
|
Making Menus and Dialogs
The Opera Platform Dom Interface provides methods for making a menu and dialogs in web applications. First to enable menus call the method this.createMenu(). This must be done in the ‘Init’ event, and will make an object named o2_menu. Now we can add menu items using o2_menu.main.addChild(String label, String ID, new O2_Command(function)). The ID can be used to further process the menu item, eg. to add a submenu use
o2_menu.main.addChild(”Submenu”, “sub”, new O2_Command()); o2_menu.main.sub.addChild(”SubmenuItem”, “item”, new O2_Command(function(){alert(’submenu’)}));
A convenient way to call methods in the main application class is to store a reference to it in the o2_menu object.
this.createMenu(); o2_menu.app = this; o2_menu.main.addChild(”Call foo”, “foo”, new O2_Command(function(){o2_menu.app.foo();}));
To enable dialogs one must first call this.createDialogController();. This will create the o2_dialogs object. This code will create a question dialog
o2_dialogs.addDialog(’foo_or_bar’, ‘Wanna foo or bar?’, ‘question’, ‘Foo’, ‘Bar’, new O2_Command(function(){alert(’bar’);}), new O2_Command(function(){alert(’foo’);}));
The first argument is the id of the dialog. Next is the text, type, alternatives, and functions to be called. To show the dialog use o2_dialogs.foo_or_bar.show();
Launching Applications
It is possible to launch other applications from Opera Platform applications. The DOM Interface provides methods for launching common applications such as the camera, contact list etc. It’s also possible to launch applications based on GUID or application name. The following example shows how to launch the camera or calendar application. It’s also possible to launch an URL in the default browser using launchURL. This example also shows how to make menus and dialogs.
Camera
This application can start the camera or calendar for you, or open google in default browser. the last option does’nt seem to work though. Press the left button to start.
Conclusions
The Opera Platform SDK is a powerful tool for easily creating web applications on mobile phones. If one is familliar with web development, previous skills can immediately be put into use. It has it’s limitations though, which may not make it suitable for certain applications. Overall it’s best suited for web based applications, where the data source is one or more web services.
Strenghts
- Based on web standards: Because http is used for data transfer it can be used with all networks and with any wap access point. Also because it’s based on HTML, CSS and JavaScript exitsting web applications can easily be ported to Opera Platform and vice versa. And also web teqnichues are well documented, and any documentation found is also valid for Opera Platform Web Applications.
- Portability: Because the Opera Platform serves as a layer between Web Applications and the underlying operating system developers don’t have to worry about portability. For now the Opera Platform is only available for Series 60 devices though.
- Access to phone functionality: Developers have easy access to phone functionality such as setting up a call, sending and recieving messages, the contact list and calendar.
Limitations
- Controlling applications: It’s possible to launch applications, such as the camera, but no possibility to control the application. It would e.g. be nice to be able to access a picture taken with the camera, for display in the application or upload to a server.
- No Bluetooth support: It’s not possible to recieve data from Bluetooth devices, such as GPS devices or other phones.
Functionality not working as expected
- Controlling applications: The Application interface provides the methods focus and blur. Theese doesn’t seem to have any effect though.
- Menu navigation: It seems that when navigating through menus key events are passed on, having the effect that you also navigate the html document. Also when setting the menu labels with the o2_menu.setPriority1Text and the o2_menu.setPrioriy2Text methods the labels doesn’t seem to change before after the first time the menu is opened. Theese labels are used to describe the two softkeys function.
- Floating point numbers: Floating point numbers are rounded when added to or converted to a string. This only happens on target devices, not in the desktop environment. This makes e.g. the currency converter application inaccurate. An example is the following code:
var twopointfive = 2.5; alert(”2.5 = ” + twopointfive); var five = twopointfive * 2; alert(”5 = ” + five);
The first alert shows “2.5 = 3″, showing that the number is rounded. The second alert shows “5 = 5″, so the number is rounded only when adding it to the string it seems.