Having your webOS application wake up for background tasks even without an open window


When you think of a Web application, you think of going to an HTML page that then loads scripts and resources to display functionality. Applications in general do not always fit that model. If you launch an “application” like Growl on the Mac, a window doesn’t launch. It merrily kicks off a service in the background.
The same is possible in the world of webOS and browser extension systems. One of the user experiences that I enjoy on webOS is the notification system. Non-modal, and any application can tie into it. There are various levels of notifications:
  • Banners: Thin bottom line one liner that appears quickly
  • Icons: Notifications are grouped together in time
  • Dashboards: Larger areas. A DOM window so you can put controls in there. For example, the media dashboard lets you play, pause, etc.
  • Popups: Even richer larger actionable areas
More on these notifications in another post. If you have an application that wants to tie into the notification system you can do so even if a window isn’t open.
I wanted to find out how to do this, and luckily Mitch Allen has a simple example of this in his RSS News application that he builds in his book on Palm webOS.
I hope to offer a simpler API specialized for this, but it is quite trivial to do thanks to the Power service. The timeout service is located via palm://com.palm.power/timeout and there are various properties that you can pass in to do the real work when you are woken up. What application should be opened? What task should be run when opened? That is all tied in via the application manager service (e.g. see the palm://com.palm.applicationManager/open service below).
The wakeup code in the news application is defined in the setWakeup method in the main application assistant (NOTE: assistants help with the controller part of MVC in Mojo):
// setWakeup - called to setup the wakeup alarm for background feed updates
//   if preferences are not set for a manual update (value of "00:00:00")
AppAssistant.prototype.setWakeup = function() {    
    if (News.feedUpdateInterval !== "00:00:00") {
        this.wakeupRequest = new Mojo.Service.Request("palm://com.palm.power/timeout", {
            method: "set",
            parameters: {
                "key": "com.palm.app.news.update",
                "in": News.feedUpdateInterval,
                "wakeup": News.feedUpdateBackgroundEnable,
                "uri": "palm://com.palm.applicationManager/open",
                "params": {
                    "id": "com.palm.app.news",
                    "params": {"action": "feedUpdate"}
                }
            },
            onSuccess: function(response) {
                Mojo.Log.info("Alarm Set Success", response.returnValue);
                News.wakeupTaskId = Object.toJSON(response.taskId);
            },
            onFailure: function(response) {
                Mojo.Log.info("Alarm Set Failure",
                    response.returnValue, response.errorText);
            }
        });
        Mojo.Log.info("Set Update Timeout");
    }
};
Mojo applications have a certain lifecycle and your application will get callbacks for setting up, shutting down, activation (when you come back to a scene), deactivation, etc.
Thus, you will see that setWakeup is called in the main AppAssistant.prototype.setup and when the application is launched AppAssistant.prototype.handleLaunch.
Anyway, suffice to say, having a lot of fun digging into webOS

0 comments: (+add yours?)

Post a Comment

We whole heartly respect your feedback :)