Symbian developer community

 
wiki

Create Auto-update Feature for a WRT Widget

From Symbian Developer Community

Jump to: navigation, search

Automatic updates have become a must-have feature for many applications. Luckily, auto-update for WRT Widgets is extremely easy to implement.

Recipe
Amount of time required: 30 minutes
Required libraries: N/A
Required header files: N/A
PlatSec Capabilities: N/A
Compatibility: S60 3rd Edition FP2, S60 5th Edition v1.0
Comes with Code: Symbian.org WRT Widget

Overview

Widget auto-update

The update process can be done in few steps as follows:

  • Create a web page that contains the latest version number
    • Remember to use fixed location - once widget is deployed, page address should never change
  • Make the latest WGZ file available over the web
    • Upload the latest WGZ that matches version in the page.
    • WGZ address should also be fixed - once widget is deployed, WGZ address should never change
  • In the widget, always maintain current widget version
    • For example, we can have version.js (included from your index.html)
      var widgetversion="1.0.0";
  • Get the latest version number over HTTP. This can be either:
    • A stand-alone page or
    • The latest version number can be embedded in another page. If you choose this option, your can parse the current version from the page content.
  • If the versions don't match, kick off new WGZ download
    • To assist clean installation, after kicking-off the download, you can close the running widget instance

Code

In our example below, we have embedded the version number as a comment in a wiki page. The format is fairly simple - version 1.0.0 would be embedded in the following format:

Current widget version is [1.0.0]
In order to extract the version number we will look for
Current widget version is [
and extract all text until we reach
]

Further, since the version checking involves a HTTP roundtrip, we will use an asynchronous call to retrieve the version. This is why the update check is split into two stages - submitting the HTTP request in checkForUpdates then handling the response in checkForUpdatesStage2.

 
 
// Update global variables
var myversion = "1.0rc9";
var versionWikiPageString = "Current widget version is [";
var versionWikiPageUrl = "http://developer.symbian.org/wiki/index.php/Symbian.org_WRT_Widget";
var downloadUrl = "http://developer.symbian.org/wiki/images/c/c5/Symbian.org.wgz";
 
// Submit the HTTP request to get the latest widget version
function checkForUpdates() {
uiManager.showNotification(-1, "wait", "Checking for updates...", -1);
updatePageAjax = new Ajax();
updatePageAjax.onreadystatechange = checkForUpdatesStage2;
updatePageAjax.open('GET', nocache(versionWikiPageUrl), true);
updatePageAjax.send(null);
}
 
// AJAX callback function
function checkForUpdatesStage2() {
 
if (updatePageAjax.readyState == 4) {
 
// extract version number - the response is HTML
var content = updatePageAjax.responseText;
var ind = content.indexOf(versionWikiPageString);
if ( ind == -1 ) {
// not in expected format
uiManager.showNotification(3000, "warning", "Update failed, check manually.");
return;
}
 
// adjust for the prefix length
ind += versionWikiPageString.length;
 
// look for end index
var ind2 = content.indexOf("]",ind);
if ( ind2 == -1 || (ind2-ind) > 10 ) {
// too long to really be a version string
uiManager.showNotification(3000, "warning", "Update failed, check manually.");
return;
}
 
// extract version string
var version = content.substring(ind,ind2);
 
// compare to this version
if ( version != myversion ) {
 
// New version available!
var answer = confirm("Install new version " + version + "?");
if (answer) {
 
// ok, we have the update, kick off the download
uiManager.hideNotification();
openURL(nocache(downloadUrl));
setTimeout(function () {window.close();}, 1000);
 
} else {
 
// User refused the update
uiManager.showNotification(3000, "info", "Update cancelled.");
 
}
} else {
 
// Versions match - up-to-date
uiManager.showNotification(3000, "info", "Up to date!");
 
}
}
}
 
 
 

Finally, note the call to openURL is formed as follows:

 
openURL(nocache(downloadUrl));
 

The nocache function is used to prevent browser cache from interfering with the download. For example, if you have previously updated using auto-update, the browser may keep the previous widget version in the cache. Attempting to download new version would return cached file - effectively an old version - and the update would fail. More info on the trick used to avoid this problem (including code for the nocache function) can be found on a separate page.

This code comes from the Symbian.org WRT Widget.

Comments

Sign in to comment…