org.symbian.tools.wrttools.previewer/preview/script/lib/sapi/Sensor.js
changeset 402 f943a50b6689
parent 337 afe6a53c0ed4
child 406 f928c6777132
equal deleted inserted replaced
400:91acf7e95f86 402:f943a50b6689
    34 
    34 
    35 	var AccelerometerAxis			= new Array();
    35 	var AccelerometerAxis			= new Array();
    36 	var AccelerometerDoubleTapping	= new Array();
    36 	var AccelerometerDoubleTapping	= new Array();
    37 	var Orientation					= new Array();
    37 	var Orientation					= new Array();
    38 	var Rotation					= new Array();
    38 	var Rotation					= new Array();
    39 	var xyOrientation = 0;
    39 	var orientation;
    40 	var zOrientation = 0;
    40 	var xaxis = 0, yaxis = 0, zaxis = 0;
    41 	
    41 	
    42 	var provider = 'Service.Sensor',
    42 	var provider = 'Service.Sensor',
    43 		Interface = 'ISensor';
    43 		Interface = 'ISensor';
    44 
    44 
    45 	/**
    45 	/**
    62 		_t = context._t,
    62 		_t = context._t,
    63 		method = '',
    63 		method = '',
    64 		result = false,
    64 		result = false,
    65 		DBase = null;
    65 		DBase = null;
    66 
    66 
    67 	device.implementation.setOrientation = function(xy, z) {
    67 	function notifyAcceleration(x, y, z, o) {
    68 		xyOrientation = xy;
    68 		xaxis = Math.round(x);
    69 		zOrientation = z;
    69 		yaxis = Math.round(y);
    70 		
    70 		zaxis = Math.round(z);
    71 		for (var i = 0; i < Orientation.length; i++) {
    71 
    72 			notifyOrientationChangeListener(Orientation[i]);
    72 		var res = createAccelerationResult();
    73 		}
    73 
    74 	};
    74 		window.setTimeout(function() {
    75 
    75 			for ( var i = 0; i < AccelerometerAxis.length; i++) {
    76 	function notifyOrientationChangeListener(callback) {
    76 				var callback = AccelerometerAxis[i];
    77 		var orientation;
    77 				callback(getTransactionId(callback), 9, res);
    78 		if (xyOrientation > -45 && xyOrientation <= 45) {
    78 			}
    79 			orientation = "DisplayUp";
    79 		}, 5);
    80 		} else if (xyOrientation > 45 && xyOrientation <= 135) {
    80 		
    81 			orientation = "DisplayLeftUp";
    81 		if (orientation != o) {
    82 		} else if (xyOrientation > 135 || xyOrientation <= -135) {
    82 			orientation = o;
    83 			orientation = "DisplayDown";
    83 			var orRes = createOrientationResult();
    84 		} else if (xyOrientation > -135 && xyOrientation <= -45) {
    84 			window.setTimeout(function() {
    85 			orientation = "DisplayRightUp";
    85 				for ( var i = 0; i < Orientation.length; i++) {
    86 		}
    86 					var callback = Orientation[i];
    87 		callback(getTransactionId(callback), 9, context.Result( {
    87 					callback(getTransactionId(callback), 9, orRes);
       
    88 				}
       
    89 			}, 5);
       
    90 		}
       
    91 	}
       
    92 
       
    93 	function createAccelerationResult() {
       
    94 		return context.Result( {
       
    95 			DataType : "AxisData",
       
    96 			TimeStamp : new Date().getTime(),
       
    97 			XAxisData : xaxis,
       
    98 			YAxisData : yaxis,
       
    99 			ZAxisData : zaxis
       
   100 		});
       
   101 	}
       
   102 
       
   103 	function createOrientationResult() {
       
   104 		return context.Result( {
       
   105 			DataType : "AxisData",
       
   106 			TimeStamp : new Date().getTime(),
    88 			DeviceOrientation : orientation
   107 			DeviceOrientation : orientation
    89 		}));
   108 		});
    90 	}
   109 	}
    91 	
   110 		
    92 	function getTransactionId(callback) {
   111 	function getTransactionId(callback) {
    93 		for ( var tId in transactionToCallback) {
   112 		for ( var tId in transactionToCallback) {
    94 			if (transactionToCallback[tId] == callback) {
   113 			if (transactionToCallback[tId] == callback) {
    95 				return tId;
   114 				return tId;
    96 			}
   115 			}
   135 	 * @param {Object} criteria, callback
   154 	 * @param {Object} criteria, callback
   136 	 */
   155 	 */
   137 	function __RegisterForNotification(criteria, callback, flag){
   156 	function __RegisterForNotification(criteria, callback, flag){
   138 		flag = flag || false;
   157 		flag = flag || false;
   139 		method = 'RegisterForNotification';
   158 		method = 'RegisterForNotification';
   140 		context.notify(_t('%s:: RegisterForNotification not implemented in preview').arg(provider));
       
   141 		
   159 		
   142 		if(arguments.length >2 && (typeof flag != "undefined" && typeof flag != "boolean"))
   160 		if(arguments.length >2 && (typeof flag != "undefined" && typeof flag != "boolean"))
   143 			return error(device.implementation.ERR_INVALID_SERVICE_ARGUMENT, msg.msgCriteriaMissing);
   161 			return error(device.implementation.ERR_INVALID_SERVICE_ARGUMENT, msg.msgCriteriaMissing);
   144 
   162 
   145 		if(typeof callback != 'function')
   163 		if(typeof callback != 'function')
   165 		 	return error(device.implementation.ERR_INVALID_SERVICE_ARGUMENT, msg.msgOutofRange);
   183 		 	return error(device.implementation.ERR_INVALID_SERVICE_ARGUMENT, msg.msgOutofRange);
   166 
   184 
   167 		if (typeof callback == 'function') {
   185 		if (typeof callback == 'function') {
   168 			var channels = criteria.ChannelInfoMap;
   186 			var channels = criteria.ChannelInfoMap;
   169 			// for ( var channel in channels) {
   187 			// for ( var channel in channels) {
   170 			var notify = null;
   188 			var result = false;
   171 			switch (channels.ChannelId) {
   189 			switch (channels.ChannelId) {
   172 			case CHANNEL_ACCEL:
   190 			case CHANNEL_ACCEL:
   173 				AccelerometerAxis.push(callback);
   191 				AccelerometerAxis.push(callback);
       
   192 				result = createAccelerationResult();
   174 				break;
   193 				break;
   175 			case CHANNEL_ACCELDT:
   194 			case CHANNEL_ACCELDT:
   176 				AccelerometerDoubleTapping.push(callback);
   195 				AccelerometerDoubleTapping.push(callback);
   177 				break;
   196 				break;
   178 			case CHANNEL_ORIENTATION:
   197 			case CHANNEL_ORIENTATION:
   179 				Orientation.push(callback);
   198 				Orientation.push(callback);
   180 				notify = notifyOrientationChangeListener;
   199 				result = createOrientationResult();
   181 				break;
   200 				break;
   182 			case CHANNEL_ROTATION:
   201 			case CHANNEL_ROTATION:
   183 				Rotation.push(callback);
   202 				Rotation.push(callback);
   184 				break;
   203 				break;
   185 			}
   204 			}
   186 			// }
   205 			// }
   187 			var tID = nextTransactionId++;
   206 			var tID = nextTransactionId++;
   188 			transactionToCallback[tID] = callback;
   207 			transactionToCallback[tID] = callback;
   189 			setTimeout(notify, 20, callback);
   208 			if (result) {
       
   209 				setTimeout(function() {callback(tID, 9, result);}, 20, callback);
       
   210 			}
   190 //			var result = context.callAsync(this, arguments.callee, criteria, callback);
   211 //			var result = context.callAsync(this, arguments.callee, criteria, callback);
   191 			return context.AsyncResult(tID);
   212 			return context.AsyncResult(tID);
   192 		}
   213 		}
   193 				
   214 				
   194 		return context.ErrorResult();
   215 		return context.ErrorResult();
   335 		msgPropertyIDMissing		: '%s:%s:Property id missing',
   356 		msgPropertyIDMissing		: '%s:%s:Property id missing',
   336 		msgInvalidPropertyID		: '%s:%s:Property id is invalid',
   357 		msgInvalidPropertyID		: '%s:%s:Property id is invalid',
   337 		msgChannelNotSupported		: '%s:%s:Channel property not supported',
   358 		msgChannelNotSupported		: '%s:%s:Channel property not supported',
   338 		msgChannelInfoMapInvalid	: '%s:%s:ChannelInfoMap Type Invalid'
   359 		msgChannelInfoMapInvalid	: '%s:%s:ChannelInfoMap Type Invalid'
   339 	};
   360 	};
   340 
   361 	
       
   362 	_BRIDGE_REF.nokia.emulator.setAccelerationCallback(notifyAcceleration);
   341 }) ();
   363 }) ();