Process Management

Introduction

In this article you will learn how to use the Tizen Application and SystemInfo APIs in a sample application called the Task Manager. This article shows how to:

  • display the list of running applications
  • kill a selected application
  • monitor the resources used by the system and other applications
  • determine the battery level

Prerequisites

To use the Task Manager Sample application you need to enable the tizen, systeminfo and application features in the config.xml file, and update jQuery to version 1.8.1 and jQuery Mobile to version 1.2.0. The sample application was tested on Tizen SDK 2.0.0a2.

List of running applications

It can sometimes be useful for a user to know what applications are currently running, so this example shows you how to get a list of all the applications running on the device. To get this list, you need to use the Tizen Application API. This API also provides methods to access other application management functionality such as getting the list of installed applications, handling the application lifecycle, etc. More information about the Application API can be found in the Tizen Documentation. The method used to get the list of running applications is as follows:

void getAppsContext(ApplicationContextArraySuccessCallback successCallback,optional ErrorCallback? errorCallback);

The sample application uses a UL list to display all the running applications. The following method shows how to create a list item:

/** @private Makes list item
 *  @param {String} name Name of the application
 *  @param {String} icon path to the icon of application
 *  @param {number} id ContextId of application
 *  @returns {String} list item
 */
var makeListItem = function(name, icon, id) {
	return "<li class=\"ui-li-has-multiline\" data-app-id=\"" + id + "\">"+ "<img src=\"" + icon + "\"><h3>" + name + "</h3><p>touch to kill an app</p>";
};

It’s a simple method that returns the html for the list item. It uses the application name, the path to the application icon and the application’s contextId. The method that gets the list of all running applications is implemented as follows:

/**
 * Gets list of running applications and put it to container
 */
getRunningAppsList : function() {
	//to be used as closure
	var that = this;
	var hMakeListItem = makeListItem;
	var hContainer = container;
	tizen.application.getAppsContext(function(contexts) {
		//empty the <ul> container
		hContainer.html('');
		var appInfo, item;
		for ( var i = 0; i < contexts.length; ++i) {
			//get information about the application
			appInfo = tizen.application.getAppInfo(contexts[i].appId);
			//if it is application that should be shown (such as in the menus) make the item and append to the list
			if (appInfo.show) {
				item = hMakeListItem(appInfo.name, appInfo.iconPath, contexts[i].id);
				hContainer.append(item);
			}
		}
		hContainer.listview('refresh');
		//kill the selected application when the user touches the item
		hContainer.find('li').unbind().bind({
			click : function(event) {
				var id = this.getAttribute('data-app-id');
				that.killApp(id);
			}
		});
	}, function() {
		console.log("getAppsContext() error");
	});
}
TIP
To pass object functions to be used in event handlers you need to use closures. You can read more about closures in many tutorials, easily found on the Internet.

Kill a selected application

To kill an application with a given context id you can use the following method:

/**
 * When user confirm kills the application with given contextId and if success lunch callback function
 * @param {number} contextId ContextId of application to be killed
 */
killApp : function(contextId) {
	//make closure to refresh the list of running applications
	var that = this;
	if (confirm("Are you sure to exit this app?")) {
		tizen.application.kill(contextId, function() {
			that.getRunningAppsList();
		}, function() {
			console.log("killApp() error");
		});
	}
}

Check the system performance

To check the system performance you can use the Tizen SystemInfo API. It allows you to access information about the system, such as the CPU usage, the memory used by applications and the battery level. This example shows you how to access this information and display it for the user. The method used to get the value of a system property is:

void getPropertyValue(PropertyId property, SystemInfoPropertySuccessCallbacksuccessCallback, optional ErrorCallback? errorCallback);

Before using this method, you first must check that the given property is supported:

boolean isSupported(PropertyId property);

It’s also important to be able to track any changes to the property value, so the SystemInfo API provides the following method to add a property value change listener:

long addPropertyValueChangeListener(PropertyId property,SystemInfoPropertySuccessCallback successCallback, optional ErrorCallback?errorCallback, optional SystemInfoOptions? options);

More information about the SystemInfo API can be found in the Tizen Documentation. This example uses the HTML5 meter tag to display system property values, and the following code shows how to get the handlers to the Processor, Storage and Power meters:

/**
 * Gets handlers for CPU, Storage and Power meters
 */
init : function() {
	processor = document.getElementById('processor');
	storage = document.getElementById('storage');
	power = document.getElementById('power');
}

The following method is used to get the system information values and add the change event listeners:

/**
 * Get CPU, Power and Storage properties and put them into corresponding meters
 */
getSystemInfoValues : function() {
//closures:
	var hProcessor = processor;
	var hStorage = storage;
	var hPower = power;
	var hSetCpuMeter = this.setCpuMeter;
	var hSetStorageMeter = this.setStorageMeter;
	var hSetPowerMeter = this.setPowerMeter;
	if (tizen.systeminfo.isSupported('Cpu')) {
		tizen.systeminfo.getPropertyValue('Cpu', function(property) {
			hSetCpuMeter(property, hProcessor);
		});
		tizen.systeminfo.addPropertyValueChangeListener('Cpu', function(property) {
			hSetCpuMeter(property, hProcessor);
		}, function() {
			console.log('addPropertyValueChangeListener() error');
		});
	}
	if (tizen.systeminfo.isSupported('Storage')) {
		tizen.systeminfo.getPropertyValue('Storage', function(property) {
			hSetStorageMeter(property, hStorage);
		});
		tizen.systeminfo.addPropertyValueChangeListener('Storage', function(property) {
			hSetStorageMeter(property, hStorage);
		}, function() {
			console.log('addPropertyValueChangeListener() error');
		});
	}
	if (tizen.systeminfo.isSupported('Power')) {
		tizen.systeminfo.getPropertyValue('Power', function(property) {
			hSetPowerMeter(property, hPower);
		});
		tizen.systeminfo.addPropertyValueChangeListener('Power', function(property) {
			hSetPowerMeter(property, hPower);
		}, function() {
			console.log('addPropertyValueChangeListener() error');
		});
	}
}

The following methods are used to set the meters with the retrieved property values:

/**
 * Sets the CPU meter to the given value
 * @param {Object} cpuInfo SystemInfoCpu object
 * @param {DOM meter} cpuMeter handler to the CPU meter
 */
setCpuMeter : function(cpuInfo, cpuMeter) {
	tizen.logger.info(cpuInfo.load);
	cpuMeter.value = cpuInfo.load;
},
/**
 * Sets the Storage meter to the value from the first storage device
 * @param {Objec} storageInfo SystemInfoStorage object
 * @param {DOM meter} storageMeter handler to the storage meter
 * @returns
 */
setStorageMeter : function(storageInfo, storageMeter) {
	if (storageInfo.length > 0) {
		var main = storageInfo[0];
		storageMeter.max = main.capacity;
		storageMeter.value = main.capacity - main.availableCapacity;
	}
},
/**
 * Sets the Power meter to the given value
 * @param {Objec} powerInfo SystemInfoPower object
 * @param {DOM meter} powerMeter handler to the power meter
 * @returns
 */
setPowerMeter : function(powerInfo, powerMeter) {
	powerMeter.value = powerInfo.level;
}

All the methods shown are contained within the Task Manger sample application provided with this article.

Summary

Tizen Device APIs provide powerful tools, both to manage the applications’ lifecycles and to check system performance. They can easily be used to write applications such as Task Managers, or any other applications that need to measure system performance or provide application status information.

File attachments: