Wikipedia/preview/script/lib/sapi/SysInfo.js
changeset 20 918767a9c8d3
equal deleted inserted replaced
19:f3521a11d878 20:918767a9c8d3
       
     1 /**
       
     2  * SysInfo.js
       
     3  * 
       
     4  * Nokia Web Runtime Service API emulation 
       
     5  * WRT v1.1
       
     6  * 
       
     7  * Copyright 2009 Nokia Corporation. All rights reserved.
       
     8 */
       
     9 
       
    10  
       
    11 (function(){
       
    12 	
       
    13 	var provider = 'Service.SysInfo' ,
       
    14 		Interface = 'ISysInfo';
       
    15 
       
    16 	var supportedEntitiesAndKeys = {
       
    17 		"battery":{
       
    18 			"batterystrength":{"GetInfo":true,"SetInfo":false,"GetNotification":true,"GetInfoModeSync":false},
       
    19 			"chargingstatus":{"GetInfo":true,"SetInfo":false,"GetNotification":true,"GetInfoModeSync":true}
       
    20 		},
       
    21 		"connectivity":{
       
    22 			"bluetooth":{"GetInfo":true,"SetInfo":false,"GetNotification":true,"GetInfoModeSync":true},
       
    23 			"infrared":{"GetInfo":true,"SetInfo":false,"GetNotification":true,"GetInfoModeSync":true},
       
    24 			"activeconnections":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":false},
       
    25 			"connectionstatus":{"GetInfo":false,"SetInfo":false,"GetNotification":true,"GetInfoModeSync":false},
       
    26 			"wlanmacaddress":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true}
       
    27 		},
       
    28       	"device":{
       
    29 			"firmwareversion":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    30 			"platformversion":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    31 			"producttype":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    32 			"manufacturer":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    33 			"machineid":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    34 			"phonemodel":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    35 			"imei":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true}
       
    36 		},
       
    37       	"display":{
       
    38 			"brightness":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    39 			"screensavertimeout":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    40 			"userinactivity":{"GetInfo":false,"SetInfo":false,"GetNotification":true,"GetInfoModeSync":false},
       
    41 			"keyguardtime":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    42 			"autolocktime":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    43 			"autolockstatus":{"GetInfo":true,"SetInfo":false,"GetNotification":true,"GetInfoModeSync":true},
       
    44 			"wallpaper":{"GetInfo":false,"SetInfo":true,"GetNotification":false,"GetInfoModeSync":false,"InputDataType":"string"},
       
    45 			"lighttimeout":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    46 			"displayresolution":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    47 			"displayorientation":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true}
       
    48 		},
       
    49       	"features":{
       
    50 			"bluetooth":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    51 			"infrared":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    52 			"camera":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    53 			"memorycard":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    54 			"fmradio":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    55 			"qwerty":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    56 			"wlan":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    57 			"usb":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    58 			"pen":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    59 			"led":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    60 			"coverui":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    61 			"sidevolumekeys":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    62 			"vibra":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true}
       
    63 		},
       
    64       	"general":{
       
    65 			"accessorystatus":{"GetInfo":false,"SetInfo":false,"GetNotification":true,"GetInfoModeSync":false},
       
    66 			"connectedaccessories":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    67 			"inputlanguage":{"GetInfo":true,"SetInfo":true,"GetNotification":true,"GetInfoModeSync":true},
       
    68 			"supportedlanguages":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    69 			"predictivetext":{"GetInfo":true,"SetInfo":true,"GetNotification":true,"GetInfoModeSync":true},
       
    70 			"vibraactive":{"GetInfo":true,"SetInfo":true,"GetNotification":true,"GetInfoModeSync":true},
       
    71 			"availableusbmodes":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    72 			"activeusbmode":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    73 			"flipstatus":{"GetInfo":true,"SetInfo":false,"GetNotification":true,"GetInfoModeSync":true},
       
    74 			"gripstatus":{"GetInfo":true,"SetInfo":false,"GetNotification":true,"GetInfoModeSync":true},
       
    75 			
       
    76 		},
       
    77       	"memory":{
       
    78 			"driveinfo":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    79 			"listdrives":{"GetInfo":true,"SetInfo":false,"GetNotification":false,"GetInfoModeSync":true},
       
    80 			"criticalmemory":{"GetInfo":false,"SetInfo":false,"GetNotification":true,"GetInfoModeSync":false},
       
    81 			"memorycard":{"GetInfo":true,"SetInfo":false,"GetNotification":true,"GetInfoModeSync":true}
       
    82 		},
       
    83       	"network":{
       
    84 			"signalstrength":{"GetInfo":true,"SetInfo":false,"GetNotification":true,"GetInfoModeSync":false},
       
    85 			"registrationstatus":{"GetInfo":true,"SetInfo":false,"GetNotification":true,"GetInfoModeSync":true},
       
    86 			"networkmode":{"GetInfo":true,"SetInfo":false,"GetNotification":true,"GetInfoModeSync":true},
       
    87 			"currentnetwork":{"GetInfo":true,"SetInfo":false,"GetNotification":true,"GetInfoModeSync":false},
       
    88 			"locationarea":{"GetInfo":false,"SetInfo":false,"GetNotification":true,"GetInfoModeSync":false},
       
    89 			"cellid":{"GetInfo":false,"SetInfo":false,"GetNotification":true,"GetInfoModeSync":false}
       
    90 		}
       
    91 	  };
       
    92 	  
       
    93 	/**
       
    94 	 * SysInfo service
       
    95 	 */
       
    96 	var SysInfoService = function(){
       
    97 		this.GetInfo 			= __GetInfo;
       
    98 		this.SetInfo 			= __SetInfo;
       
    99 		this.GetNotification 	= __GetNotification;
       
   100 		this.Cancel 			= __Cancel;
       
   101 	}
       
   102 
       
   103 	device.implementation.extend(provider, Interface, new SysInfoService() );
       
   104 
       
   105 	var	context = device.implementation.context,
       
   106 		_t = context._t,
       
   107 		method = '',
       
   108 		result = false,
       
   109 		DBase = null,
       
   110 		default_device = 'default';
       
   111 		
       
   112 	/**
       
   113 	 * SysInfo: GetInfo
       
   114 	 * @param {Object} criteria
       
   115 	 * @param {function} callback function for async call (optional)
       
   116 	 */
       
   117 	function __GetInfo(criteria, callback){
       
   118 		var retVal = ValidateArguments("GetInfo",criteria,callback);
       
   119 		if(retVal.ErrorCode != 0)
       
   120 		{
       
   121 			return retVal;
       
   122 		}
       
   123 		//	Async call
       
   124 		if (typeof callback == 'function') {
       
   125 			return context.callAsync(this, arguments.callee, criteria, callback);
       
   126 		}
       
   127 
       
   128 		DBase = context.getData(provider);
       
   129 		var returnValue = DBase;
       
   130 		returnValue = returnValue[criteria.Entity.toLowerCase()][criteria.Key.toLowerCase()];
       
   131 		returnValue.Key = criteria.Key;
       
   132 		returnValue.Entity = criteria.Entity;
       
   133 
       
   134 		if(/^Connectivity$/i.test(criteria.Entity)&& /^ActiveConnections$/i.test(criteria.Key))
       
   135 		{
       
   136 			var temp = returnValue.ConnectionList;
       
   137 			returnValue.ConnectionList = context.Iterator(temp);
       
   138 		}
       
   139 		else if(/^General$/i.test(criteria.Entity)&& /^ConnectedAccessories$/i.test(criteria.Key))
       
   140 		{
       
   141 			var temp = returnValue.AccessoryList;
       
   142 			returnValue.AccessoryList = context.Iterator(temp);			
       
   143 		}
       
   144 		else if(/^Memory$/i.test(criteria.Entity)&& /^DriveInfo$/i.test(criteria.Key))
       
   145 		{
       
   146 			try {
       
   147 				var temp = criteria.SystemData.Drive;
       
   148 				if(temp.length > 3)
       
   149 				{
       
   150 					temp = temp.substring(0,3);
       
   151 				}
       
   152 				temp= returnValue.Drive[temp.toUpperCase()];
       
   153 				if (!temp) {
       
   154 					return error(device.implementation.ERR_BAD_ARGUMENT_TYPE, msg.no_msg);
       
   155 				}
       
   156 				var driveInfo = new Object();
       
   157 				driveInfo.Key = returnValue.Key;
       
   158 				driveInfo.Entity = returnValue.Entity;
       
   159 				driveInfo.DriveInfo = temp;
       
   160 				return context.Result(driveInfo);
       
   161 			}
       
   162 			catch(err)
       
   163 			{
       
   164 				return error(device.implementation.ERR_BAD_ARGUMENT_TYPE, msg.no_msg);
       
   165 			}
       
   166 			
       
   167 		}
       
   168 		return context.Result(returnValue);
       
   169 	}
       
   170 			
       
   171 
       
   172 	/**
       
   173 	 * SysInfo: SetInfo
       
   174 	 * @param {Object} criteria
       
   175 	 * @param {function} callback function for async call (optional)
       
   176 	 */
       
   177 	function __SetInfo(criteria, callback){
       
   178 		var retVal = ValidateArguments("SetInfo",criteria,callback);
       
   179 		if(retVal.ErrorCode != 0)
       
   180 		{
       
   181 			return retVal;
       
   182 		}
       
   183 
       
   184 		//	Current implementation support only for 'General' -> 'InputLanguage' || 'PredictiveText'
       
   185 		if (/^General$/i.test(criteria.Entity) && ( /^InputLanguage$/i.test(criteria.Key) || /^PredictiveText/i.test(criteria.Key) || /^VibraActive/i.test(criteria.Key))) {
       
   186 			//	get the DBase data
       
   187 			DBase = context.getData(provider);
       
   188 			var returnValue = DBase;
       
   189 			
       
   190 			//	Check the 'SupportedLanguages' entries for Setting the new Status
       
   191 			if (/^InputLanguage$/i.test(criteria.Key)) {
       
   192 				var found = false;
       
   193 				var languageList = returnValue['general']['supportedlanguages']['LanguageList'];
       
   194 				for (var key in languageList) {
       
   195 					if (languageList[key] == criteria.SystemData.Status) {
       
   196 						found = true;
       
   197 					}
       
   198 				}
       
   199 				//	Update the 'InputLanguage' if the value value found in the ['SupportedLanguages']
       
   200 				if (found) 
       
   201 					returnValue[criteria.Entity.toLowerCase()][criteria.Key.toLowerCase()]['Status'] = criteria.SystemData.Status;
       
   202 				else 
       
   203 					return error(device.implementation.ERR_SERVICE_NOT_SUPPORTED, msg.not_supported);
       
   204 			}
       
   205 			else if (/^PredictiveText$/i.test(criteria.Key)) {
       
   206 				if (criteria.SystemData.Status == 0 || criteria.SystemData.Status == 1) 
       
   207 					returnValue[criteria.Entity.toLowerCase()][criteria.Key.toLowerCase()]['Status'] = criteria.SystemData.Status;
       
   208 				else 
       
   209 					return error(device.implementation.ERR_NOT_FOUND, msg.badType);
       
   210 			}
       
   211 			else if (/^VibraActive$/i.test(criteria.Key)) {
       
   212 				if (criteria.SystemData.Status == 0 || criteria.SystemData.Status == 1) 
       
   213 					returnValue[criteria.Entity.toLowerCase()][criteria.Key.toLowerCase()]['Status'] = criteria.SystemData.Status;
       
   214 			}
       
   215 			else {
       
   216 				return error(device.implementation.ERR_SERVICE_NOT_SUPPORTED, msg.not_supported);
       
   217 			}
       
   218 		}
       
   219 		else if(/^Display/i.test(criteria.Entity) && /^Wallpaper/i.test(criteria.Key)) {
       
   220 			if(typeof criteria.SystemData.StringData == 'undefined')
       
   221 				return error(device.implementation.ERR_MISSING_ARGUMENT, msg.missingArgs);
       
   222 			returnValue[criteria.Entity.toLowerCase()][criteria.Key.toLowerCase()]['StringData'] = criteria.SystemData.StringData;
       
   223 			return context.ErrorResult(device.implementation.ERR_SUCCESS);
       
   224 		}
       
   225 		return context.Result(result);
       
   226 	}
       
   227 			
       
   228 
       
   229 	/**
       
   230 	 * SysInfo: GetNotification
       
   231 	 * @param {Object} criteria
       
   232 	 * @param {function} callback function for async call
       
   233 	 */
       
   234 	function __GetNotification(criteria, callback){
       
   235 		var retVal = ValidateArguments("GetNotification",criteria,callback);
       
   236 		if(retVal.ErrorCode != 0)
       
   237 		{
       
   238 			return retVal;
       
   239 		}
       
   240 		
       
   241 		// unsupported
       
   242 		if (!/^(Battery|Memory)$/i.test(criteria.Entity) ||
       
   243 			!/^(BatteryStrength|ChargingStatus|MemoryCard)$/i.test(criteria.Key)) {
       
   244 			context.notify(_t('%s:: GetNotification : entity %s, key %s not implemented in preview.').arg(provider, criteria.Entity, criteria.Key));
       
   245 			// register callback anyway so Cancel works. 
       
   246 		}
       
   247 
       
   248 		// evenType = 'entity.key'
       
   249 		var eventType = criteria.Entity+'.'+criteria.Key;
       
   250 						
       
   251 		// process notify
       
   252 		return context.addListener(provider, eventType, criteria, callback, notifyHandler);
       
   253 	}
       
   254 			
       
   255 	function notifyHandler(transactionID, criteria, callback, data){
       
   256 		
       
   257 		var result,
       
   258 			eventCode = {completed:2, error:4, progress:9},
       
   259 			code = eventCode.progress;
       
   260 		try{
       
   261 			DBase = context.getData(provider);
       
   262 			var entity = criteria.Entity.toLowerCase(),
       
   263 				key = criteria.Key.toLowerCase();
       
   264 						
       
   265 			// make a copy of return value
       
   266 			var returnValue = context.extend({},DBase[entity][key]);
       
   267 			
       
   268 			// augment with data
       
   269 			context.extend(returnValue, data);
       
   270 
       
   271 			result = context.Result(returnValue);
       
   272 		} 
       
   273 		catch(e){
       
   274 			code = eventCode.error;
       
   275 		}
       
   276 		callback(transactionID, code, result);
       
   277 	}
       
   278 		
       
   279 			
       
   280 
       
   281 	/**
       
   282 	 * SysInfo: Cancel
       
   283 	 * @param {Object} criteria
       
   284 	 */
       
   285 	function __Cancel(criteria){
       
   286 		method = 'Cancel';
       
   287 
       
   288 		if (!criteria || !criteria.TransactionID)
       
   289 			return error(device.implementation.ERR_MISSING_ARGUMENT, msg.missingTID);
       
   290 
       
   291 		var found = context.removeListener(provider, null, criteria.TransactionID);
       
   292 		if (!found)
       
   293 			return error(device.implementation.ERR_NOT_FOUND);
       
   294 		else
       
   295 			return context.ErrorResult(device.implementation.ERR_SUCCESS);
       
   296 	}
       
   297 
       
   298 
       
   299 	
       
   300 	/*******************************
       
   301 	 * helper functions
       
   302 	 *******************************/
       
   303 	
       
   304 	function error(code, msg /*, args...*/){
       
   305 
       
   306 		var args = ['SysInfo',method].concat([].slice.call(arguments,2));
       
   307 		msg = msg ? _t().arg.apply(msg,args) : undefined;
       
   308 		return context.ErrorResult(code, msg);
       
   309 	}
       
   310 
       
   311 	function cancelRegisteredNotify(result)
       
   312 	{
       
   313 		__Cancel({'TransactionID':result.TransactionID});
       
   314 		method = 'GetNotification';
       
   315 	}
       
   316 
       
   317 	/** 
       
   318 	 * error messages
       
   319 	 * order of %s args: Service name, method name, parameter name 
       
   320 	 */
       
   321 	var msg = {
       
   322 		badType				: '%s: %s: Type is invalid',
       
   323 		badSysInfo			: '%s: %s: SysInfoName is invalid',
       
   324 		missingSysInfo		: '%s: %s: SysInfoName is missing',
       
   325 		missingTID 			: '%s: %s: Transaction ID missing',
       
   326 		not_supported 		: '%s: %s: Not Supported',
       
   327 		is_invalid			: '%s: %s: Is invalid',
       
   328 		missingArgs			: '%s: %s: SystemData Argument Missing',
       
   329 		inSufficientArgs	: '%s: %s: Insufficient Arguments to process',
       
   330 		missingInputParam	: '%s: %s: Input Parameter Missing',
       
   331 		not_supportedSyncVer: '%s: %s: Sync Version Not Supported',
       
   332 		noEntity			: '%s: %s: Entity: Input Parameter Missing',
       
   333 		noKey				: '%s: %s: Key: Input Parameter Missing',
       
   334 		IncorrectSytemData	: '%s: %s: Incorrect SystemData Type, SystemData Must be a Map',
       
   335 		callbackNotFound	: '%s: %s: Callback object not found',
       
   336 		commandNotFound		: '%s: %s: Command Not Supported',
       
   337 		unsupInterface		: 'SysInfo:Requested interface not supported by the provider',
       
   338 		no_msg				: '%s: %s: '
       
   339 	};
       
   340 
       
   341 	/**
       
   342 	 * Common validator for all functions
       
   343 	 * 	 
       
   344 	 *  
       
   345 	 * @param {arguments} function name and arguments of calling function
       
   346 	 * @return {Result} Error object
       
   347 	 */		
       
   348 	  function ValidateArguments(funName,criteria,callback)
       
   349 	  {
       
   350 	  	method = funName;
       
   351 
       
   352 		try {
       
   353 			if (/^GetNotification$/i.test(funName) && typeof callback != "function") {
       
   354 				return error(device.implementation.ERR_BAD_ARGUMENT_TYPE, msg.not_supportedSyncVer);
       
   355 			}
       
   356 			
       
   357 			if (typeof criteria == "undefined" || typeof criteria.Entity == "undefined" || typeof criteria.Key == "undefined") {
       
   358 				return error(device.implementation.ERR_MISSING_ARGUMENT, msg.inSufficientArgs);
       
   359 			}
       
   360 			
       
   361 			if (criteria.Entity == "") {
       
   362 				return error(device.implementation.ERR_MISSING_ARGUMENT, msg.noEntity);
       
   363 			}
       
   364 			
       
   365 			if (criteria.Key == "") {
       
   366 				return error(device.implementation.ERR_MISSING_ARGUMENT, msg.noKey);
       
   367 			}
       
   368 			
       
   369 			if (/^SetInfo$/i.test(funName)) {
       
   370 				if (typeof criteria.SystemData == "undefined" || criteria.SystemData == '') {
       
   371 					return error(device.implementation.ERR_MISSING_ARGUMENT, msg.missingArgs);
       
   372 				}
       
   373 				if (typeof criteria.SystemData.StringData == "undefined" && typeof criteria.SystemData.Status == "undefined") {
       
   374 					return error(device.implementation.ERR_MISSING_ARGUMENT, msg.missingArgs);
       
   375 				}
       
   376 			}
       
   377 			
       
   378 			if (typeof supportedEntitiesAndKeys[criteria.Entity.toLowerCase()] == 'undefined' || typeof supportedEntitiesAndKeys[criteria.Entity.toLowerCase()][criteria.Key.toLowerCase()] == 'undefined') {
       
   379 				return error(device.implementation.ERR_NOT_FOUND, msg.no_msg);
       
   380 			}
       
   381 			
       
   382 			if (!supportedEntitiesAndKeys[criteria.Entity.toLowerCase()][criteria.Key.toLowerCase()][funName]) {
       
   383 				return error(device.implementation.ERR_NOT_FOUND, msg.no_msg);
       
   384 			}
       
   385 			
       
   386 			if (funName == "GetInfo" && !supportedEntitiesAndKeys[criteria.Entity.toLowerCase()][criteria.Key.toLowerCase()]["GetInfoModeSync"] && typeof callback != "function") {
       
   387 				return error(device.implementation.ERR_MISSING_ARGUMENT, msg.not_supportedSyncVer);
       
   388 			}
       
   389 			
       
   390 			return context.ErrorResult(device.implementation.ERR_SUCCESS, "");
       
   391 		}
       
   392 		catch(err){
       
   393 			return error(device.implementation.ERR_BAD_ARGUMENT_TYPE, msg.badType);
       
   394 		}
       
   395 		
       
   396 	  }
       
   397 
       
   398 }) ()