org.symbian.tools.wrttools.doc.WebDeveloper/html/GUID-7C9CCFF3-A6E0-4593-8315-1A0EAAFD05AF.html
changeset 229 716254ccbcc0
equal deleted inserted replaced
228:913c9751c067 229:716254ccbcc0
       
     1 
       
     2 <!DOCTYPE html
       
     3   PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
       
     4 <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="SystemInfo Service API example"/><meta name="DC.Relation" scheme="URI" content="GUID-1D78998F-2A45-4100-AAD0-FCCAC300DBC1"/><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-AEB26A58-1DE2-46CB-81EC-6DB3A477B7A3"/><meta name="DC.Format" content="XHTML"/><meta name="DC.Identifier" content="GUID-7C9CCFF3-A6E0-4593-8315-1A0EAAFD05AF"/><title>SystemInfo Service API example </title><script type="text/javascript">
       
     5       function initPage() {}
       
     6     </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-7C9CCFF3-A6E0-4593-8315-1A0EAAFD05AF">SystemInfo Service API example</h1><div>
       
     7 <p/>
       
     8 <p>This section presents the full source code of a working sample widget
       
     9 for the <a href="GUID-E0ED671A-BBDA-4C07-828F-BD35782D9B36.html#GUID-E0ED671A-BBDA-4C07-828F-BD35782D9B36">SystemInfo
       
    10 Service</a>. You can download the <code>wgz</code> package for
       
    11 this widget from section <a href="GUID-775005BC-2FF8-45A9-BBA6-6CED6B5780A2.html#GUID-775005BC-2FF8-45A9-BBA6-6CED6B5780A2">Example widgets</a>.</p>
       
    12 <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>
       
    13 <p>For widget development and debugging purposes, this example writes its
       
    14 output to <code>c:\data\jslog_widget.log</code> using <code>console.info</code>.
       
    15 For instructions on how to enable logging in the Web browser for S60, see
       
    16 section <a href="GUID-B584CA90-543B-4AED-B134-A3A616259DB9.html#GUID-B584CA90-543B-4AED-B134-A3A616259DB9">JavaScript console</a>.</p>
       
    17 <div><h3>Info.plist</h3>
       
    18 <pre class="codeblock" id="GUID-7B7E64B5-53C1-441B-AC88-E22FCCA9257D">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
       
    19 &lt;!DOCTYPE plist PUBLIC "-//Nokia//DTD PLIST 1.0//EN" "http://www.nokia.com/NOKIA_COM_1/DTDs/plist-1.0.dtd"&gt;
       
    20 &lt;plist version="1.0"&gt;
       
    21 &lt;dict&gt;
       
    22   &lt;key&gt;DisplayName&lt;/key&gt;
       
    23   &lt;string&gt;SysInfoSample&lt;/string&gt;
       
    24   &lt;key&gt;Identifier&lt;/key&gt;
       
    25   &lt;string&gt;com.nokia.widget.sapi.sysinfo.sample&lt;/string&gt;
       
    26   &lt;key&gt;Version&lt;/key&gt;
       
    27   &lt;string&gt;1.0&lt;/string&gt;
       
    28   &lt;key&gt;MainHTML&lt;/key&gt;
       
    29   &lt;string&gt;sysinfo-sample.html&lt;/string&gt;
       
    30 &lt;/dict&gt;
       
    31 &lt;/plist&gt;</pre>
       
    32 </div>
       
    33 <div><h3>sysInfo-sample.html</h3>
       
    34 <pre class="codeblock" id="GUID-A3011DD3-5720-4704-8330-12BC1DE02DBD">&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
       
    35 &lt;html&gt;
       
    36   &lt;head&gt;
       
    37     &lt;script type="text/javascript" src="js/common.js"&gt;&lt;/script&gt;
       
    38     &lt;script type="text/javascript" src="js/sysinfo-sample.js"&gt;&lt;/script&gt;
       
    39   &lt;/head&gt;
       
    40   &lt;body id='docBody' bgcolor="#ddeeff" onload="setup()" style=width:100%;height:100%;&gt;
       
    41     &lt;form name="frm"&gt;
       
    42       &lt;h3&gt;Sysinfo API Sample Widget&lt;/h3&gt;
       
    43       &lt;input type="button" onclick="getSysInfo('img1')" value="GetSysInfo"&gt;&lt;img id="img1" src="pic/blank.png" width="25" height="25" align="center"&gt;&lt;br&gt;
       
    44       &lt;input type="button" onclick="setSysInfo('img2')" value="SetSysInfo"&gt;&lt;img id="img2" src="pic/blank.png" width="25" height="25" align="center"&gt;&lt;br&gt;
       
    45       &lt;input type="button" onclick="getSysInfoAsync('img3')" value="GetSysInfoAsync"&gt;&lt;img id="img3" src="pic/blank.png" width="25" height="25" align="center"&gt;&lt;br&gt;
       
    46       &lt;input type="button" onclick="getNotification('img4')" value="GetNotification"&gt;&lt;img id="img4" src="pic/blank.png" width="25" height="25" align="center"&gt;&lt;br&gt;
       
    47       &lt;input type="button" onclick="cancelGetSysInfoAsync('img5')" value="CancelGetSysInfoAsync"&gt;&lt;img id="img5" src="pic/blank.png" width="25" height="25" align="center"&gt;&lt;hr&gt;
       
    48       &lt;div class='sysinfo' id='sysinfo' bgcolor="#ddeeff" style=width:100%;height:100%;overflow:auto&gt;&lt;/div&gt;
       
    49     &lt;/form&gt;
       
    50   &lt;/body&gt;
       
    51 &lt;/html&gt;</pre>
       
    52 </div>
       
    53 <div><h3>common.js</h3>
       
    54 <pre class="codeblock" id="GUID-88C7ECB0-6656-4C16-9B9C-846E61F1AFF7">// common.js
       
    55 //
       
    56 // This file contains some utility functions
       
    57 
       
    58 // Check the error code and show the information to users
       
    59 function checkError(message, resultList, divId, imgId)
       
    60 {
       
    61   var errCode = resultList.ErrorCode;
       
    62   var msg = "";
       
    63 
       
    64   if (errCode) {
       
    65     msg = message + "&lt;BR&gt;" + "Failed Error: " + errCode + "&lt;BR&gt;";
       
    66     if(resultList.ErrorMessage != undefined)
       
    67       msg += "Error Message: " + resultList.ErrorMessage;
       
    68     showIMG(imgId,"no");
       
    69   } else {
       
    70     showIMG(imgId,"yes");
       
    71   }
       
    72 
       
    73   //print error message
       
    74   if(divId != null &amp;&amp; divId != undefined)
       
    75     document.getElementById(divId).innerHTML = msg;
       
    76   console.info(msg);
       
    77 
       
    78   return errCode;
       
    79 }
       
    80 
       
    81 // Build the message by reading a iteratorable list in a recursive manner
       
    82 function showIterableList(iterator)
       
    83 {
       
    84   var msg = "";
       
    85   try
       
    86   {
       
    87     iterator.reset();
       
    88     var item;
       
    89     while (( item = iterator.getNext()) != undefined ){
       
    90       msg += showObject( item );
       
    91     }
       
    92   }
       
    93   catch(e)
       
    94   {
       
    95     alert('&lt;showIterableList&gt; ' + e);
       
    96   }
       
    97   return msg;
       
    98 }
       
    99 
       
   100 // Build the message by reading a JS object in a recursive manner
       
   101 function showObject( obj )
       
   102 {
       
   103   var txt = "";
       
   104   try {
       
   105     if ( typeof obj != 'object' )
       
   106       return "" + obj + '&lt;BR/&gt;';
       
   107     else {
       
   108       for(var key in obj) {
       
   109         txt +=  key + ":";
       
   110         txt += showObject( obj[key] );
       
   111         txt += '&lt;BR/&gt;';
       
   112       }
       
   113       txt += '&lt;BR/&gt;';
       
   114     }
       
   115   }
       
   116   catch (e)
       
   117   {
       
   118     alert("showObject: " + e);
       
   119   }
       
   120   return txt;
       
   121 }
       
   122 
       
   123 // Show the image to indicate the test result
       
   124 function showIMG(imgId, isOK)
       
   125 {
       
   126   if(imgId == null || imgId == undefined)
       
   127     return;
       
   128 
       
   129   if(isOK == "yes")
       
   130     document.getElementById(imgId).src = "pic/yes.png";
       
   131   else if(isOK == "no")
       
   132     document.getElementById(imgId).src = "pic/no.png";
       
   133   else
       
   134     document.getElementById(imgId).src = "pic/blank.png";
       
   135 }
       
   136 
       
   137 // Show elements in object by using 'alert'
       
   138 function testObject(obj)
       
   139 {
       
   140   var msg = "";
       
   141   for(var key in obj) {
       
   142     msg = msg + ":" + key + "=" + obj[key];
       
   143   }
       
   144   alert(msg);
       
   145 }
       
   146 
       
   147 // Test whether the input is numeric
       
   148 function IsNumeric(sText)
       
   149 {
       
   150   var ValidChars = "0123456789.";
       
   151   var IsNumber=true;
       
   152   var Char;
       
   153 
       
   154   for (i = 0; i &lt; sText.length &amp;&amp; IsNumber == true; i++)
       
   155   {
       
   156     Char = sText.charAt(i);
       
   157     if (ValidChars.indexOf(Char) == -1)
       
   158     {
       
   159       IsNumber = false;
       
   160     }
       
   161   }
       
   162   return IsNumber;
       
   163 }</pre>
       
   164 </div>
       
   165 <div><h3>sysInfo-sample.js</h3>
       
   166 <pre class="codeblock" id="GUID-1F051C8C-63E5-46EC-98F3-7138EF3C0A84">// sysinfo-sample.js
       
   167 //
       
   168 // In this sample system information will be listed and changed; The
       
   169 // notification will be send when the system info changes; Also,
       
   170 // async operation will be canceled.
       
   171 //
       
   172 
       
   173 //SAPI Error Codes
       
   174 // 0    - Success
       
   175 // 1000 - InvalidServiceArgument
       
   176 // 1001 - UnknownArgumentName
       
   177 // 1002 - BadArgumentType
       
   178 // 1003 - MissingArgument
       
   179 // 1004 - ServiceNotSupported
       
   180 // 1005 - ServiceInUse
       
   181 // 1006 - ServiceNotReady
       
   182 // 1007 - NoMemory
       
   183 // 1008 - HardwareNotAvailable
       
   184 // 1009 - ServerBusy
       
   185 // 1010 - EntryExists
       
   186 // 1011 - AccessDenied
       
   187 // 1012 - NotFound
       
   188 // 1013 - UnknownFormat
       
   189 // 1014 - GeneralError
       
   190 // 1015 - CancelSuccess
       
   191 // 1016 - ServiceTimedOut
       
   192 // 1017 - PathNotFound
       
   193 
       
   194 // Declare the service object
       
   195 var so;
       
   196 
       
   197 // imgid for callback1 function
       
   198 var imgid_callback1;
       
   199 
       
   200 // imgid for callback2 function
       
   201 var imgid_callback2;
       
   202 
       
   203 // imgid for callback3 function
       
   204 var imgid_callback3;
       
   205 
       
   206 // id of the div used to display information
       
   207 const DIV_ID = 'sysinfo';
       
   208 
       
   209 // Called from onload()
       
   210 function setup()
       
   211 {
       
   212   try {
       
   213     so = device.getServiceObject("Service.SysInfo", "ISysInfo");
       
   214     console.info("setup: so: %s", so);
       
   215   }
       
   216   catch (e) {
       
   217     alert('&lt;setup&gt; ' +e);
       
   218   }
       
   219 }
       
   220 
       
   221 //Get SysInfo
       
   222 function getSysInfo(imgId)
       
   223 {
       
   224   // get the entity
       
   225   var entity = prompt("Please enter the Entry", "General");
       
   226   var key = prompt("Please enter the Key", "VibraActive");
       
   227   var criteria = new Object();
       
   228   criteria.Entity = entity;
       
   229   criteria.Key = key;
       
   230 
       
   231   try {
       
   232     var result = so.ISysInfo.GetInfo(criteria);
       
   233     if(!checkError("ISysInfo::getSysInfo",result,DIV_ID,imgId)) {
       
   234       document.getElementById(DIV_ID).innerHTML = showObject(result.ReturnValue);
       
   235     }
       
   236   }
       
   237   catch (e) {
       
   238     showIMG(imgId,"no");
       
   239     alert ("getSysInfo: " + e);
       
   240   }
       
   241 }
       
   242 
       
   243 //Set SysInfo
       
   244 function setSysInfo(imgId)
       
   245 {
       
   246   // get the entity
       
   247   var entity = prompt("Please enter the Entry", "General");
       
   248   var key = prompt("Please enter the Key", "VibraActive");
       
   249   var vals = prompt("Please enter the new Value", 0);
       
   250 
       
   251   var systemData = new Object();
       
   252   if(IsNumeric(vals)) {
       
   253     systemData.Status = parseInt(vals);
       
   254   } else {
       
   255     systemData.StringData = vals;
       
   256   }
       
   257 
       
   258   var criteria = new Object();
       
   259   criteria.Entity = entity;
       
   260   criteria.Key = key;
       
   261   criteria.SystemData = systemData;
       
   262 
       
   263   try {
       
   264     var result = so.ISysInfo.SetInfo(criteria);
       
   265     checkError("ISysInfo::setSysInfo",result,DIV_ID,imgId);
       
   266   }
       
   267   catch (e) {
       
   268     showIMG(imgId,"no");
       
   269     alert ("setSysInfo: " + e);
       
   270   }
       
   271 }
       
   272 
       
   273 //Get SysInfo
       
   274 function getSysInfoAsync(imgId)
       
   275 {
       
   276   // get the entity
       
   277   var entity = prompt("Please enter the Entry", "Network");
       
   278   var key = prompt("Please enter the Key", "HomeNetwork");
       
   279 
       
   280   var criteria = new Object();
       
   281   criteria.Entity = entity;
       
   282   criteria.Key = key;
       
   283 
       
   284   try {
       
   285     imgid_callback1 = imgId;
       
   286     var result = so.ISysInfo.GetInfo(criteria, callback1);
       
   287     if(!checkError("ISysInfo::getSysInfoAsync",result,DIV_ID,imgId))
       
   288       showIMG(imgId, "");
       
   289   }
       
   290   catch (e) {
       
   291     showIMG(imgId,"no");
       
   292     alert ("getSysInfoAsync: " + e);
       
   293   }
       
   294 }
       
   295 
       
   296 // set callback function for notification
       
   297 function getNotification(imgId)
       
   298 {
       
   299   // get the entity
       
   300   var entity = prompt("Please enter the Entry", "General");
       
   301   var key = prompt("Please enter the Key", "VibraActive");
       
   302 
       
   303   var criteria = new Object();
       
   304   criteria.Entity = entity;
       
   305   criteria.Key = key;
       
   306 
       
   307   try {
       
   308     imgid_callback2 = imgId;
       
   309     var result = so.ISysInfo.GetNotification(criteria,callback2);
       
   310     if(!checkError("ISysInfo::getNotification",result,DIV_ID,imgId))
       
   311       showIMG(imgId, "");
       
   312   }
       
   313   catch (e) {
       
   314     showIMG(imgId,"no");
       
   315     alert ("setSysInfo: " + e);
       
   316   }
       
   317 }
       
   318 
       
   319 //Ccancel SysInfo
       
   320 function cancelGetSysInfoAsync(imgId)
       
   321 {
       
   322   // get the entity
       
   323   var entity = prompt("Please enter the Entry", "Network");
       
   324   var key = prompt("Please enter the Key", "HomeNetwork");
       
   325 
       
   326   var criteria = new Object();
       
   327   criteria.Entity = entity;
       
   328   criteria.Key = key;
       
   329 
       
   330   try {
       
   331     imgid_callback3 = imgId;
       
   332     var result = so.ISysInfo.GetInfo(criteria, callback3);
       
   333     if(!checkError("ISysInfo::cancelGetSysInfoAsync",result,DIV_ID,imgId)){
       
   334       var criteria2 = new Object();
       
   335       criteria2.TransactionID = result.TransactionID;
       
   336       var cresult = so.ISysInfo.Cancel(criteria2);
       
   337       checkError("ISysInfo::cancelGetSysInfoAsync",cresult,DIV_ID,imgId);
       
   338     }
       
   339   }
       
   340   catch (e) {
       
   341     showIMG(imgId,"no");
       
   342     alert ("cancelGetSysInfoAsync: " + e);
       
   343   }
       
   344 }
       
   345 
       
   346 // This is the asynchronous callback handler
       
   347 function callback1(transId, eventCode, result)
       
   348 {
       
   349   console.info("callback1: transId: %d  eventCode: %d result.ErrorCode: %d", transId, eventCode, result.ErrorCode);
       
   350   if(!checkError("callback1::getSysInfoAsync",result,DIV_ID,imgid_callback1)) {
       
   351     document.getElementById(DIV_ID).innerHTML = showObject(result.ReturnValue);
       
   352   }
       
   353 }
       
   354 
       
   355 // This is the asynchronous callback handler
       
   356 function callback2(transId, eventCode, result)
       
   357 {
       
   358   console.info("callback2: transId: %d  eventCode: %d result.ErrorCode: %d", transId, eventCode, result.ErrorCode);
       
   359   if(!checkError("callback2::getNotification",result,DIV_ID,imgid_callback2)) {
       
   360     document.getElementById(DIV_ID).innerHTML = "Got Notification!Following settings changed!\n" + showObject(result.ReturnValue);
       
   361   }
       
   362 }
       
   363 
       
   364 // This is the asynchronous callback handler
       
   365 function callback3(transId, eventCode, result)
       
   366 {
       
   367   console.info("callback3: transId: %d  eventCode: %d result.ErrorCode: %d", transId, eventCode, result.ErrorCode);
       
   368   if(!checkError("callback3::getNotification",result,DIV_ID,imgid_callback3)) {
       
   369     document.getElementById(DIV_ID).innerHTML = showObject(result.ReturnValue);
       
   370   }
       
   371 }</pre>
       
   372 </div>
       
   373 </div></div></div><div class="footer"><hr/><div class="copy">© Nokia 2009.</div></div></body></html>