org.symbian.tools.wrttools.previewer/preview/script/lib/sapi/Sensor.js
changeset 337 afe6a53c0ed4
parent 334 915daddff233
child 402 f943a50b6689
equal deleted inserted replaced
336:131c321bf080 337:afe6a53c0ed4
    22  * WRT v1.1
    22  * WRT v1.1
    23  * 
    23  * 
    24  */
    24  */
    25 
    25 
    26 (function(){
    26 (function(){
    27 
    27 	var CHANNEL_ACCEL = 7;
       
    28 	var CHANNEL_ACCELDT = 8;
       
    29 	var CHANNEL_ORIENTATION = 10;
       
    30 	var CHANNEL_ROTATION = 11;
       
    31 
       
    32 	var transactionToCallback = new Object();
       
    33 	var nextTransactionId = 2123148; // Random seed number! (Actual number doesn't matter)
       
    34 
       
    35 	var AccelerometerAxis			= new Array();
       
    36 	var AccelerometerDoubleTapping	= new Array();
       
    37 	var Orientation					= new Array();
       
    38 	var Rotation					= new Array();
       
    39 	var xyOrientation = 0;
       
    40 	var zOrientation = 0;
       
    41 	
    28 	var provider = 'Service.Sensor',
    42 	var provider = 'Service.Sensor',
    29 		Interface = 'ISensor';
    43 		Interface = 'ISensor';
    30 	var transID = new Array();
    44 
    31 	/**
    45 	/**
    32 	 * Sensor service
    46 	 * Sensor service
    33 	 */
    47 	 */
    34 	var SensorService = function(){
    48 	var SensorService = function(){
    35 		this.FindSensorChannel 			= __FindSensorChannel;
    49 		this.FindSensorChannel 			= __FindSensorChannel;
    36 		this.RegisterForNotification	= __RegisterForNotification;
    50 		this.RegisterForNotification	= __RegisterForNotification;
    37 		this.Cancel						= __Cancel;
    51 		this.Cancel						= __Cancel;
    38 		this.GetChannelProperty			= __GetChannelProperty;		
    52 		this.GetChannelProperty			= __GetChannelProperty;
    39 	};
    53 	};
    40 
    54 
    41 	device.implementation.extend(provider, Interface, new SensorService() );
    55 	device.implementation.extend(provider, Interface, new SensorService() );
    42 
    56 
    43 	/******************************************************/	
    57 	/******************************************************/	
    48 		_t = context._t,
    62 		_t = context._t,
    49 		method = '',
    63 		method = '',
    50 		result = false,
    64 		result = false,
    51 		DBase = null;
    65 		DBase = null;
    52 
    66 
    53 
    67 	device.implementation.setOrientation = function(xy, z) {
       
    68 		xyOrientation = xy;
       
    69 		zOrientation = z;
       
    70 		
       
    71 		for (var i = 0; i < Orientation.length; i++) {
       
    72 			notifyOrientationChangeListener(Orientation[i]);
       
    73 		}
       
    74 	};
       
    75 
       
    76 	function notifyOrientationChangeListener(callback) {
       
    77 		var orientation;
       
    78 		if (xyOrientation > -45 && xyOrientation <= 45) {
       
    79 			orientation = "DisplayUp";
       
    80 		} else if (xyOrientation > 45 && xyOrientation <= 135) {
       
    81 			orientation = "DisplayLeftUp";
       
    82 		} else if (xyOrientation > 135 || xyOrientation <= -135) {
       
    83 			orientation = "DisplayDown";
       
    84 		} else if (xyOrientation > -135 && xyOrientation <= -45) {
       
    85 			orientation = "DisplayRightUp";
       
    86 		}
       
    87 		callback(getTransactionId(callback), 9, context.Result( {
       
    88 			DeviceOrientation : orientation
       
    89 		}));
       
    90 	}
       
    91 	
       
    92 	function getTransactionId(callback) {
       
    93 		for ( var tId in transactionToCallback) {
       
    94 			if (transactionToCallback[tId] == callback) {
       
    95 				return tId;
       
    96 			}
       
    97 		}
       
    98 	}
       
    99 	
    54 	/**
   100 	/**
    55 	 * Sensor: FindSensorChannel
   101 	 * Sensor: FindSensorChannel
    56 	 * @param {Object} criteria
   102 	 * @param {Object} criteria
    57 	 */
   103 	 */
    58 	function __FindSensorChannel(criteria){
   104 	function __FindSensorChannel(criteria){
   116 			return error(device.implementation.ERR_BAD_ARGUMENT_TYPE, msg.msgChannelInfoMapInvalid);
   162 			return error(device.implementation.ERR_BAD_ARGUMENT_TYPE, msg.msgChannelInfoMapInvalid);
   117 		
   163 		
   118 		if(!(criteria.ListeningType== "ChannelData" ))
   164 		if(!(criteria.ListeningType== "ChannelData" ))
   119 		 	return error(device.implementation.ERR_INVALID_SERVICE_ARGUMENT, msg.msgOutofRange);
   165 		 	return error(device.implementation.ERR_INVALID_SERVICE_ARGUMENT, msg.msgOutofRange);
   120 
   166 
   121 		if(typeof callback == 'function')
   167 		if (typeof callback == 'function') {
   122 		{
   168 			var channels = criteria.ChannelInfoMap;
   123 			var result = context.callAsync(this, arguments.callee, criteria, callback);
   169 			// for ( var channel in channels) {
   124 			transID.push(result.TransactionID);
   170 			var notify = null;
   125 			return result;
   171 			switch (channels.ChannelId) {
   126 		}
   172 			case CHANNEL_ACCEL:
   127 		if(flag)
   173 				AccelerometerAxis.push(callback);
   128 			transID.shift();
   174 				break;
       
   175 			case CHANNEL_ACCELDT:
       
   176 				AccelerometerDoubleTapping.push(callback);
       
   177 				break;
       
   178 			case CHANNEL_ORIENTATION:
       
   179 				Orientation.push(callback);
       
   180 				notify = notifyOrientationChangeListener;
       
   181 				break;
       
   182 			case CHANNEL_ROTATION:
       
   183 				Rotation.push(callback);
       
   184 				break;
       
   185 			}
       
   186 			// }
       
   187 			var tID = nextTransactionId++;
       
   188 			transactionToCallback[tID] = callback;
       
   189 			setTimeout(notify, 20, callback);
       
   190 //			var result = context.callAsync(this, arguments.callee, criteria, callback);
       
   191 			return context.AsyncResult(tID);
       
   192 		}
   129 				
   193 				
   130 		return context.ErrorResult();
   194 		return context.ErrorResult();
   131 	}
   195 	}
   132 
   196 	
   133 
       
   134 	/**
   197 	/**
   135 	 * Sensor: Cancel
   198 	 * Sensor: Cancel
   136 	 * @param {Object} criteria
   199 	 * @param {Object} criteria
   137 	 */
   200 	 */
   138 	function __Cancel(criteria){
   201 	function __Cancel(criteria){
   145 			return error(device.implementation.ERR_MISSING_ARGUMENT, msg.msgTransIDMissing);
   208 			return error(device.implementation.ERR_MISSING_ARGUMENT, msg.msgTransIDMissing);
   146 
   209 
   147 		if (criteria.TransactionID == Infinity || criteria.TransactionID == -Infinity) 
   210 		if (criteria.TransactionID == Infinity || criteria.TransactionID == -Infinity) 
   148 			return error(device.implementation.ERR_BAD_ARGUMENT_TYPE, msg.msgTransIDMissing);
   211 			return error(device.implementation.ERR_BAD_ARGUMENT_TYPE, msg.msgTransIDMissing);
   149 		
   212 		
   150 		if (typeof criteria.TransactionID != 'number') 
   213 		if (typeof criteria.TransactionID != 'number')
   151 			return error(device.implementation.ERR_BAD_ARGUMENT_TYPE, msg.msgIncorrectTransID);		
   214 			return error(device.implementation.ERR_BAD_ARGUMENT_TYPE, msg.msgIncorrectTransID);
   152 
   215 
   153 		for (var i=0; i<transID.length; i++) {
   216 		var callback = transactionToCallback[criteria.TransactionID];
   154 			if (criteria.TransactionID == transID[i]){
   217 		if (typeof callback == 'function') {
   155 				clearTimeout(criteria.TransactionID);
   218 			removeCallback(callback, AccelerometerAxis);
   156 				return context.ErrorResult();
   219 			removeCallback(callback, AccelerometerDoubleTapping);
       
   220 			removeCallback(callback, Orientation);
       
   221 			removeCallback(callback, Rotation);
       
   222 			return context.ErrorResult();
       
   223 		}
       
   224 		return error(device.implementation.ERR_INVALID_SERVICE_ARGUMENT,
       
   225 				msg.msgInvalidTransID);
       
   226 	}
       
   227 
       
   228 	
       
   229 	function removeCallback(callback, array) {
       
   230 		var i = 0;
       
   231 		for (i = 0; i < array.length; i++) {
       
   232 			var el = array[i];
       
   233 			if (el == callback) {
       
   234 				array.splice(i, 1);
       
   235 				return;
   157 			}
   236 			}
   158 		};
   237 		}
   159 		
   238 	}
   160 		return error(device.implementation.ERR_INVALID_SERVICE_ARGUMENT, msg.msgInvalidTransID);
       
   161 
       
   162 	}
       
   163 
       
   164 
   239 
   165 	/**
   240 	/**
   166 	 * Sensor: GetChannelProperty
   241 	 * Sensor: GetChannelProperty
   167 	 * @param {Object} criteria
   242 	 * @param {Object} criteria
   168 	 */
   243 	 */