Background Applications

We’ve done most of the work to make a background application, and in some ways, News is already running in the background. Background applications could be any of these types:

Minimized application

Has a card stage (or window), but is not in the foreground view.

Dashboard application

Has a dashboard stage (or panel), but no card stage.

Background application

Has neither a card nor a dashboard stage, but wakes periodically through an alarm and can issue notifications and create dashboards or cards when appropriate.

Minimized and dashboard applications should use conventional JavaScript timers, such as setTimeout() or setInterval(), to schedule recurring actions, for instance checking for new articles. In its current form, News uses setTimeout() and runs while minimized. You’ll notice if you close the main card stage while there is a News dashboard panel, the application will run in the background, posting notifications when feeds are updated with new stories. It will not perform these updates once the device goes to sleep.

Background applications can run without a window, and can wake the device from sleep or across boots by using the Alarm service. Since the framework will close any application unless there is an open window, there isn’t another option for this type of application.

We’ll replace the setTimeout() timer with an alarm set in the setWakeup() method in app-assistant.js:

// ------------------------------------------------------------------------
// 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");
        }
};

You might want to refer back to Chapter 9, where the Alarm service is reviewed in detail. In this case, we set up the alarm for the specified News.feedUpdateInterval and requested that the alarm wake the device by setting the wakeup property to true.

Note

The Alarm service will not accept any relative alarm values of less than five minutes.

To field the update, we’ll add another action handler in the application assistant’s handleLaunch method:

switch (launchParams.action) {

       .
       .
       .

        // UPDATE FEEDS
        case "feedUpdate"  :
            // Set next wakeup alarm
            this.setWakeup();

            // Update the feed list
            Mojo.Log.info("Update FeedList");
            this.feeds.updateFeedList();
        break;
        }
    }

This is pretty straightforward, and will work as long as the device is awake when the alarm fires. But when it is sleeping, the application will only have a few seconds before the power management system will force the device back to sleep.

Note

A background application that receives an alarm when the device is asleep will have less than five seconds before being shut down again. If you need more time than this, you should use activityStart() and activityStop() methods to prevent the device from sleeping until your activity has completed. Refer to Chapter 9 for more information on these service methods.

Five seconds isn’t enough time for News to complete a full update of all the feeds during a single alarm wakeup cycle. But the update process is structured to resume with the next feed to be updated, so in these cases, News will do a full update over several cycles.

To allow the user full control of the application’s background behavior, we’ll add some additional preferences features:

Manual updates

In addition to the update intervals from five minutes to one day, we’ll add an option for manual updates only which will disable the background updates.

Wakeup enable/disable

A toggle to turn off the option of waking up the device during background updates.

Notification enable/disable

A toggle to turn notifications on or off. When set to off, the feed updates will be carried out, but without any notifications or dashboard updates.

If you’re interested in seeing the final version of the application assistant, you should review the full code listing for app-assistant.js in Appendix D.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.7.212