org.symbian.tools.wrttools.doc.WebDeveloper/html/GUID-B7297E44-2CCE-4098-807B-551684CF608E.html
author Eugene Ostroukhov <eugeneo@symbian.org>
Fri, 11 Jun 2010 13:33:03 -0700
changeset 372 1e408ee32d8a
parent 229 716254ccbcc0
permissions -rw-r--r--
Added templates for WRT 1.1 platform services


<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="copyright" content="(C) Copyright 2009"/><meta name="DC.rights.owner" content="(C) Copyright 2009"/><meta name="DC.Type" content="mobileconcept"/><meta name="DC.Title" content="Media Management Service API example"/><meta name="DC.Relation" scheme="URI" content="GUID-2FC0C5D3-CE2A-45BE-B7E9-04A62A099E86"/><meta name="DC.Relation" scheme="URI" content="GUID-7C69DDA4-16F1-4A8F-BDB2-4CB0015B4E81"/><meta name="DC.Relation" scheme="URI" content="GUID-65AAF569-D347-462B-B59A-9D7CA184AB9C"/><meta name="DC.Relation" scheme="URI" content="GUID-E5684FF0-28C9-4721-9421-6B9432087086"/><meta name="DC.Format" content="XHTML"/><meta name="DC.Identifier" content="GUID-B7297E44-2CCE-4098-807B-551684CF608E"/><title>Media Management Service API example </title><script type="text/javascript">
      function initPage() {}
    </script><link href="../PRODUCT_PLUGIN/book.css" rel="stylesheet" type="text/css"/><link href="css/s60/style.css" rel="stylesheet" type="text/css" media="all"/></head><body onload="initPage();"><div class="body"><div class="contentLeft prTxt"><h1 class="pageHeading" id="GUID-B7297E44-2CCE-4098-807B-551684CF608E">Media Management Service API example</h1><div>
<p/>
<p>This section presents the full source code of a working sample widget
for the <a href="GUID-E3D26890-581A-427F-8AFF-16081528A8B8.html#GUID-E3D26890-581A-427F-8AFF-16081528A8B8">Media Management
Service</a>. The widget is programmed to retrieve audio file information.
You can download the <code>wgz</code> package for this widget from
section <a href="GUID-775005BC-2FF8-45A9-BBA6-6CED6B5780A2.html#GUID-775005BC-2FF8-45A9-BBA6-6CED6B5780A2">Example widgets</a>.</p>
<p>For general information about creating widgets, see section <a href="GUID-0E3095DB-03FF-4240-83F2-6D876AD2083A.html#GUID-0E3095DB-03FF-4240-83F2-6D876AD2083A">Widget component files</a>.</p>
<p>For widget development and debugging purposes, this example writes its
output to <code>c:\data\jslog_widget.log</code> using <code>console.info</code>.
For instructions on how to enable logging in the Web browser for S60, see
section <a href="GUID-B584CA90-543B-4AED-B134-A3A616259DB9.html#GUID-B584CA90-543B-4AED-B134-A3A616259DB9">JavaScript console</a>.</p>
<div><h3>Info.plist</h3>
<pre class="codeblock" id="GUID-4107599A-19E9-43B1-B549-2CF7ABD9103C">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Nokia//DTD PLIST 1.0//EN" "http://www.nokia.com/NOKIA_COM_1/DTDs/plist-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
&lt;dict&gt;
  &lt;key&gt;DisplayName&lt;/key&gt;
  &lt;string&gt;MediaManagementSample&lt;/string&gt;
  &lt;key&gt;Identifier&lt;/key&gt;
  &lt;string&gt;com.nokia.widget.sapi.MediaManagementSample&lt;/string&gt;
  &lt;key&gt;Version&lt;/key&gt;
  &lt;string&gt;1.0&lt;/string&gt;
  &lt;key&gt;MainHTML&lt;/key&gt;
  &lt;string&gt;mediamanagement-sample.html&lt;/string&gt;
&lt;/dict&gt;
&lt;/plist&gt;</pre>
</div>
<div><h3>mediamanagement-sample.html</h3>
<pre class="codeblock" id="GUID-5E5742F2-1DD9-47AA-8B8F-55E38334B9EE">&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;script type="text/javascript" src="js/common.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript" src="js/mediamanagement-sample.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body id="docBody" bgcolor="#ddeeff" onload="setup()" style=width:100%;height:100%;&gt;
    &lt;form name="frm"&gt;
      &lt;h3&gt;MediaManagement API Sample Widget&lt;/h3&gt;
      &lt;input type="button" onclick="getListAsync('img1')" value="GetListAsync"&gt;&lt;img id="img1" src="pic/blank.png" width="25" height="25" align="center"&gt;
      &lt;br&gt;
      &lt;input type="button" onclick="cancelGetListAsync('img2')" value="CancelGetListAsync"&gt;&lt;img id="img2" src="pic/blank.png" width="25" height="25" align="center"&gt;
      &lt;hr&gt;
      &lt;div class="mediaman" id="mediaman" bgcolor="#ddeeff" style=width:100%;height:100%;overflow:auto&gt;&lt;/div&gt;
    &lt;/form&gt;
  &lt;/body&gt;
&lt;/html&gt;</pre>
</div>
<div><h3>common.js</h3>
<pre class="codeblock" id="GUID-535B332D-25E9-4CE5-827E-AD41E837F7B5">// 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 + "&lt;BR&gt;" + "Failed Error: " + errCode + "&lt;BR&gt;";
    if(resultList.ErrorMessage != undefined)
      msg += "Error Message: " + resultList.ErrorMessage;
    showIMG(imgId,"no");
  } else {
    showIMG(imgId,"yes");
  }

  //print error message
  if(divId != null &amp;&amp; divId != undefined)
    document.getElementById(divId).innerHTML = msg;
  console.info(msg);

  return errCode;
}

// Build the message by reading a iteratorable 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('&lt;showIterableList&gt; ' + 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 + '&lt;BR/&gt;';
    else {
      for(var key in obj) {
        txt +=  key + ":";
        txt += showObject( obj[key] );
        txt += '&lt;BR/&gt;';
      }
      txt += '&lt;BR/&gt;';
    }
  }
  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 &lt; sText.length &amp;&amp; IsNumber == true; i++)
  {
    Char = sText.charAt(i);
    if (ValidChars.indexOf(Char) == -1)
    {
      IsNumber = false;
    }
  }
  return IsNumber; 
}</pre>
</div>
<div><h3>mediamanagement-sample.js</h3>
<pre class="codeblock" id="GUID-4AB2559C-EA8A-42B9-8731-FF808464A037">// mediamanagement-sample.js
//
// In this sample the media objects and meta-data from the S60 Media
// Gallery will be listed; Also, async operation will be canceled.
//
//SAPI Error Codes
// 0    - Success
// 1000 - InvalidServiceArgument
// 1001 - UnknownArgumentName
// 1002 - BadArgumentType
// 1003 - MissingArgument
// 1004 - ServiceNotSupported
// 1005 - ServiceInUse
// 1006 - ServiceNotReady
// 1007 - NoMemory
// 1008 - HardwareNotAvailable
// 1009 - ServerBusy
// 1010 - EntryExists
// 1011 - AccessDenied
// 1012 - NotFound
// 1013 - UnknownFormat
// 1014 - GeneralError
// 1015 - CancelSuccess
// 1016 - ServiceTimedOut
// 1017 - PathNotFound


// Declare the service object
var so;

// imgid for callback1 function
var imgid_callback1;

// id of the div used to display information
const DIV_ID = 'mediaman';


// Called from onload()
function setup()
{
  try {
    so = device.getServiceObject("Service.MediaManagement", "IDataSource");
    console.info("setup: so: %s", so);
  }
  catch(e) {
    alert('&lt;setup&gt; ' +e);
  }
}

// Get List Async
// This method retrieves a list of media objects and meta-data from the S60 Media Gallery.
// In order to test this method make sure that there are media objects in the media Gallery. 
function getListAsync(imgId)
{
  // Setup input params using dot syntax
  var criteria = new Object();
  criteria.Type = 'FileInfo';
  criteria.Filter = new Object();
  criteria.Filter.FileType = 'Sound';
  criteria.Sort = new Object();
  criteria.Sort.Key = 'FileSize';

  // set the image id for callback function
  imgid_callback1 = imgId;

  try {
    // Media Management supports asynchronous call
    var result = so.IDataSource.GetList(criteria, callback);
    if(!checkError("IDataSource::getListAsync",result,DIV_ID,imgId))
      showIMG(imgId,"");
  }
  catch (e) {
    showIMG(imgId,"no");
    alert ("getListAsync: " + e);
  }
}

// This method cancels outstanding asynchronous request.
function cancelGetListAsync(imgId)
{
  // Setup input params using dot syntax
  var criteria = new Object();
  criteria.Type = 'FileInfo';
  criteria.Filter = new Object();
  criteria.Filter.FileType = 'Sound';
  criteria.Sort = new Object();
  criteria.Sort.Key = 'FileSize';

  // set the image id for callback function
  imgid_callback1 = imgId;

  try {
    // Media Management supports asynchronous call
    var result = so.IDataSource.GetList(criteria, callback);
    if(!checkError("IDataSource::cancelGetListAsync",result,DIV_ID,imgId)){
      showIMG(imgId,"");
      var criteria2 = new Object(); 
      criteria2.TransactionID = result.TransactionID;
      var cresult = so.IDataSource.Cancel(criteria2);
      checkError("IDataSource::cancelGetListAsync",cresult,DIV_ID,imgId);
    }
  }
  catch (e) {
    showIMG(imgId,"no");
    alert ("cancelGetListAsync: " + e);
  }
}

// This is the asynchronous callback handler
function callback(transId, eventCode, result)
{
  console.info("callback: transId: %d  eventCode: %d result.ErrorCode: %d", transId, eventCode, result.ErrorCode);
  if(!checkError("IDataSource::getListAsync",result,DIV_ID,imgid_callback1)) {
    document.getElementById(DIV_ID).innerHTML = showIterableList(result.ReturnValue);
  }
}</pre>
</div>
</div></div></div><div class="footer"><hr/><div class="copy">© Nokia 2009.</div></div></body></html>