Activity Manager

The Activity Manager service (com.palm.activitymanager) acts as a traffic cop for Activities (apps, services, tasks, network flows, etc.) running on the device, balancing Activity priorities and system resources to optimize the user's experience. The Activity Manager has the following goals:

In this Section:

See also: Activity Manager Data Types

 


Activity Manager Features

Activity Manager features include:

 


Activity Participants

A running Activity must have a parent. Initially, that would be the service or app that creates it. In addition, Activities can have subscribers, which include the Activity's parent, potential adopters and others wishing to monitor the Activity. Subscribers are services or apps that are sent Activity event notifications (start, stop, etc., see Activity Events for a complete list). Adopters are subscribers who can also take over as an Activity's parent. They register their willingness to do this with the adopt method. Apps or services can subscribe to an Activity through either the create, adopt, or monitor methods.

If an Activity loses its parent, and there are no waiting adopters, it is canceled unless marked as persistent or explicit, and all subscribers are sent cancel events. When this happens, adopters have the opportunity to take over as the parent and keep the Activity alive. However, it is canceled if no app or service adopts the Activity during that time.

An Activity with a callback can briefly run without a parent while the callback is invoked; the Activity Manager waits for a specified time to see if the app or service adopts the Activity. If not adopted after that time, the Activity is canceled. If an Activity is marked as being persistent or explicit, it is restarted.

 


Activity Lifecycle

In most cases, an Activity is created to run in either the foreground or the background. A foreground Activity runs as soon as its specified prerequisites (schedule, requirements, triggers) are met.

When its prerequisites are met, a background Activity is moved into a ready queue, and a limited number are allowed to run depending on system resources. Activities can be scheduled to run at a specific time or when certain conditions (requirements) are met or events (triggers) occur. They can also be scheduled to repeat at a particular interval. The Activity Manager attempts to batch-run Activities repeating at the same interval, allowing the device to suspend/restart less frequently, conserving battery use.

When an Activity finishes, the parent informs the Activity Manager with the complete method. If the Activity has other subscribers, they are sent "complete" events. An Activity is not considered finished until all of its subscribers exit.

An Activity marked explicit (via the Type object) can only be terminated by its parent with a stop, cancel, or complete call. Otherwise, after a timeout, it is restarted via a specified callback. Explicit termination is also required if the Activity is defined with the persist attribute. In this case, the Activity state is stored in db8, continues across reboots, and requires explicit termination.

Activities that need to run to completion should be marked "explicit" or "persist".

There are four methods specifically for ending an Activity:

 

Handling Incoming Service Requests

Any entity with bus access can generate a service request. Mostly they come from apps or the Activity Manager itself as a result of a scheduled or triggered Activity.

If the call comes from an app, the Mojo Framework should have allocated an Activity for the app (or the specific card) and passed it along with the other service call arguments. If the incoming call is the callback generated for a scheduled Activity that has been selected to run, the Activity Manager adds the "$activity" property to the other callback parameters. This property contains information about the Activity, such as its requirements, triggers, and metadata.

Before processing a request, a service should associate itself with the Activity via the adopt or monitor calls. If the request could have a life of its own (a sync, for example), then calling adopt makes sense since the service could take over as the Activity's parent if the current parent exits. Otherwise, your service should call monitor.

A service should wait until the adopt or monitor call returns successfully before beginning Activity work. If either of these calls fail, it indicates the caller has exited and the request should not be processed.

Once adopt or monitor succeeds, the service can proceed to do the work indicated in the original incoming request. If the request was to simply kick-off a long running task, then the service should do this and simply return.

A service should continue to process incoming events on an Activity subscription. If a service receives a "cancel" event and the service did not use adopt to inform the Activity Manager that it was willing to take over as the Activity parent, it should be prepared to stop Activity work and unsubscribe. If it did use adopt and it receives the "orphan" event (indicating that the original parent(s) have unsubscribed and the service is now the Activity parent), then, when it is done, the service should call complete to inform the Activity Manager before unsubscribing.

 

Activity Lifecycle Flowchart

 

Example Lifecycle Flows

Normal inbound app request

Potentially long running inbound app request

Incoming scheduled background activity

 


Activity Checklists

Incoming Requests

Scheduled Activities

Scheduled Interval Activities