AppManager Service API example

This section presents the full source code of a working sample widget for the AppManager Service. You can download the wgz package for this widget from section Example widgets.

For general information about creating widgets, see section Widget component files.

For widget development and debugging purposes, this example writes its output to c:\data\jslog_widget.log using console.info. For instructions on how to enable logging in the Web browser for S60, see section JavaScript console.

Info.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Nokia//DTD PLIST 1.0//EN" "http://www.nokia.com/NOKIA_COM_1/DTDs/plist-1.0.dtd">
<plist version="1.0">
<dict>
  <key>DisplayName</key>
  <string>AppManagerSample</string>
  <key>Identifier</key>
  <string>com.nokia.widget.sapi.appmanager.sample</string>
  <key>Version</key>
  <string>1.0</string>
  <key>MainHTML</key>
  <string>appmanager-sample.html</string>
</dict>
</plist>

AppManager-sample.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <script type="text/javascript" src="js/common.js"></script>
    <script type="text/javascript" src="js/appmanager-sample.js"></script>
  </head>
  <body id='docBody' bgcolor="#ddeeff" onload="setup()" style=width:100%;height:100%;>

    <form name="frm">
    	<h3>AppManager API Sample Widget</h3>
      <input type="button" onclick="getList('img1')" value="GetList"><img id="img1" src="pic/blank.png" width="25" height="25" align="center"><br>
      <input type="button" onclick="getFilteredList('img2')" value="GetFilteredList"><img id="img2" src="pic/blank.png" width="25" height="25" align="center"><br>
      <input type="button" onclick="launchDoc('img3')" value="LaunchDoc"><img id="img3" src="pic/blank.png" width="25" height="25" align="center"><br>
      <input type="button" onclick="launchApp('img4')" value="LaunchApp"><img id="img4" src="pic/blank.png" width="25" height="25" align="center"><br>
      <input type="button" onclick="launchDocAsync('img5')" value="LaunchDocAsync"><img id="img5" src="pic/blank.png" width="25" height="25" align="center"><br>
      <input type="button" onclick="launchAppAsync('img6')" value="LaunchAppAsync"><img id="img6" src="pic/blank.png" width="25" height="25" align="center"><br>
      <input type="button" onclick="cancelLaunchDocAsync('img7')" value="CancelLaunchDocAsync"><img id="img7" src="pic/blank.png" width="25" height="25" align="center"><br>
      <input type="button" onclick="cancelLaunchAppAsync('img8')" value="CancelLaunchAppAsync"><img id="img8" src="pic/blank.png" width="25" height="25" align="center"><hr>
      <div class='appman' id='appman' bgcolor="#ddeeff" style=width:100%;height:100%;overflow:auto>
      </div>
    </form>
  </body>
</html>

common.js

// common.js
//
// This file contains some utility functions

// Check the error code and show the information to users
function checkError(message, resultList, divId, imgId)
{
  var errCode = resultList.ErrorCode;
  var msg = "";
  
  if (errCode) {
    msg = message + "QBRZ" + "Failed Error: " + errCode + "QBRZ";
    if(resultList.ErrorMessage != undefined)
      msg += "Error Message: " + resultList.ErrorMessage;
    showIMG(imgId,"no"); 
  } else {
    showIMG(imgId,"yes"); 
  }

  //Print error message
  if(divId != null && divId != undefined)
    document.getElementById(divId).innerHTML = msg;
  console.info(msg);
  
  return errCode;
}

// Build the message by reading an iterable list in a recursive manner
function showIterableList(iterator)
{
  var msg = "";
  try
  {
    iterator.reset();
    var item;
    while (( item = iterator.getNext()) != undefined ){
       msg += showObject( item );
    }
  }
  catch(e)
  {
    alert('QshowIterableListZ ' + e);
  }
  return msg;
}

// Build the message by reading a JS object in a recursive manner
function showObject( obj )
{
  var txt = "";
  try { 
    if ( typeof obj != 'object' )
      return "" + obj + 'QBR/Z';
    else {
      for(var key in obj) {
        txt +=  key + ":";
        txt += showObject( obj[key] );
        txt += 'QBR/Z';
      }
      txt += 'QBR/Z';
    }
  } 
  catch (e) 
  {
    alert("showObject: " + e);
  }
  return txt;
}

// Show the image to indicate the test result
function showIMG(imgId, isOK) 
{
  if(imgId == null || imgId == undefined)
    return;
  
  if(isOK == "yes")
    document.getElementById(imgId).src = "pic/yes.png";
  else if(isOK == "no")
    document.getElementById(imgId).src = "pic/no.png";
  else
    document.getElementById(imgId).src = "pic/blank.png";
}

// Show elements in object by using 'alert'
function testObject(obj)
{
  var msg = "";
  for(var key in obj) {
    msg = msg + ":" + key + "=" + obj[key];
  }
  alert(msg);
}

// Test whether the input is numeric
function IsNumeric(sText)
{
  var ValidChars = "0123456789.";
  var IsNumber=true;
  var Char;
  
  for (i = 0; i Q sText.length && IsNumber == true; i++) 
  { 
    Char = sText.charAt(i); 
    if (ValidChars.indexOf(Char) == -1) 
    {
       IsNumber = false;
    }
  }
  return IsNumber; 
}

AppManager-sample.js

// common.js
//
// This file contains some utility functions

// Check the error code and show the information to users
function checkError(message, resultList, divId, imgId)
{
  var errCode = resultList.ErrorCode;
  var msg = "";
  
  if (errCode) {
    msg = message + "QBRZ" + "Failed Error: " + errCode + "QBRZ";
    if(resultList.ErrorMessage != undefined)
      msg += "Error Message: " + resultList.ErrorMessage;
    showIMG(imgId,"no"); 
  } else {
    showIMG(imgId,"yes"); 
  }

  //Print error message
  if(divId != null && divId != undefined)
    document.getElementById(divId).innerHTML = msg;
  console.info(msg);
  
  return errCode;
}

// Build the message by reading an iterable list in a recursive manner
function showIterableList(iterator)
{
  var msg = "";
  try
  {
    iterator.reset();
    var item;
    while (( item = iterator.getNext()) != undefined ){
       msg += showObject( item );
    }
  }
  catch(e)
  {
    alert('QshowIterableListZ ' + e);
  }
  return msg;
}

// Build the message by reading a JS object in a recursive manner
function showObject( obj )
{
  var txt = "";
  try { 
    if ( typeof obj != 'object' )
      return "" + obj + 'QBR/Z';
    else {
      for(var key in obj) {
        txt +=  key + ":";
        txt += showObject( obj[key] );
        txt += 'QBR/Z';
      }
      txt += 'QBR/Z';
    }
  } 
  catch (e) 
  {
    alert("showObject: " + e);
  }
  return txt;
}

// Show the image to indicate the test result
function showIMG(imgId, isOK) 
{
  if(imgId == null || imgId == undefined)
    return;
  
  if(isOK == "yes")
    document.getElementById(imgId).src = "pic/yes.png";
  else if(isOK == "no")
    document.getElementById(imgId).src = "pic/no.png";
  else
    document.getElementById(imgId).src = "pic/blank.png";
}

// Show elements in object by using 'alert'
function testObject(obj)
{
  var msg = "";
  for(var key in obj) {
    msg = msg + ":" + key + "=" + obj[key];
  }
  alert(msg);
}

// Test whether the input is numeric
function IsNumeric(sText)
{
  var ValidChars = "0123456789.";
  var IsNumber=true;
  var Char;
  
  for (i = 0; i Q sText.length && IsNumber == true; i++) 
  { 
    Char = sText.charAt(i); 
    if (ValidChars.indexOf(Char) == -1) 
    {
       IsNumber = false;
    }
  }
  return IsNumber; 
}