wrtapi.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. var menuItemArray = [];
  2. var sensors_js = 0;
  3. function functionName(func)
  4. {
  5. var retval = "";
  6. if(func)
  7. {
  8. var funcNameLong=func.toString();
  9. var funcNameShort = funcNameLong.replace("function", "");
  10. var endIndex = funcNameShort.indexOf("(");
  11. retval = funcNameShort.substr(0, endIndex);
  12. }
  13. return retval;
  14. }
  15. function MenuItem(label, id)
  16. {
  17. this.label = label;
  18. this.id = id;
  19. this.onSelect = 0;
  20. this.append = function(menuItem) {
  21. var handlerFunction = functionName(menuItem.onSelect);
  22. wrt.appendMenuItem(menuItem.label, menuItem.id, this.id, handlerFunction);
  23. menuItemArray.push(menuItem);
  24. };
  25. this.remove = function(menuItem) {
  26. if(menuItem) {
  27. wrt.remove(menuItem.id);
  28. for (var i = 0; i < menuItemArray.length; i++) {
  29. if(menuItemArray[i] && menuItemArray[i].id == id) {
  30. menuItemArray[i] = 0;
  31. break;
  32. }
  33. }
  34. }
  35. };
  36. this.setDimmed = function(flag) {
  37. wrt.setDimmed(this.id, flag);
  38. };
  39. }
  40. function Menu()
  41. {
  42. this.append = function (menuItem)
  43. {
  44. var handlerFunction = functionName(menuItem.onSelect);
  45. wrt.appendMenuItem(menuItem.label, menuItem.id, 0, handlerFunction);
  46. menuItemArray.push(menuItem);
  47. };
  48. this.remove = function (menuItem) {
  49. menuItem.remove(menuItem);
  50. };
  51. this.getMenuItemById = function (id) {
  52. var menuItem;
  53. var retval = 0;
  54. for (var i = 0; i < menuItemArray.length; i++) {
  55. menuItem = menuItemArray[i];
  56. if(menuItem && menuItem.id == id) {
  57. retval = menuItem;
  58. break;
  59. }
  60. }
  61. // if menu item not found, create a dymmy menu item
  62. if(!retval) {
  63. retval = new MenuItem("", -1);
  64. }
  65. return retval;
  66. };
  67. this.getMenuItemByName = function (menuItemLabel) {
  68. var menuItem;
  69. var retval = 0;
  70. for (var i = 0; i < menuItemArray.length; i++) {
  71. menuItem = menuItemArray[i];
  72. if(menuItem && menuItem.label == menuItemLabel) {
  73. retval = menuItem;
  74. break;
  75. }
  76. }
  77. // if menu item not found, create a dymmy menu item
  78. if(!retval) {
  79. retval = new MenuItem("", -1);
  80. }
  81. return retval;
  82. };
  83. this.setLeftSoftkeyLabel = function (label, callbackfunc) {
  84. wrt.setLeftSoftkeyLabel(label, functionName(callbackfunc));
  85. };
  86. this.setRightSoftkeyLabel = function (label, callbackfunc) {
  87. wrt.setRightSoftkeyLabel(label, functionName(callbackfunc));
  88. };
  89. this.showSoftkeys = function () {
  90. wrt.showSoftkeys();
  91. };
  92. this.hideSoftkeys = function () {
  93. wrt.hideSoftkeys();
  94. };
  95. this.clear = function () {
  96. wrt.clear();
  97. };
  98. this.onShow = function () {
  99. };
  100. }
  101. function menuOnShow()
  102. {
  103. menu.onShow();
  104. }
  105. function Widget()
  106. {
  107. this.openURL = function(url) {
  108. wrt.openURL(url);
  109. };
  110. this.setPreferenceForKey = function (preference, key) {
  111. wrt.setPreferenceForKey(preference, key);
  112. };
  113. this.preferenceForKey = function (key) {
  114. return wrt.preferenceForKey(key);
  115. };
  116. this.prepareForTransition = function (transitionMode) {
  117. };
  118. this.performTransition = function () {
  119. };
  120. this.setNavigationEnabled = function (navigationMode) {
  121. wrt. setNavigationEnabled(navigationMode);
  122. };
  123. this.setNavigationType = function (type) {
  124. wrt.setNavigationType(type);
  125. };
  126. this.openApplication = function(uid, param) {
  127. wrt.openApplication(uid, param);
  128. };
  129. this.setDisplayLandscape = function() {
  130. wrt.setDisplayLandscape();
  131. };
  132. this.setDisplayPortrait = function() {
  133. wrt.setDisplayPortrait();
  134. };
  135. var identifier;
  136. var onshow;
  137. var onhide;
  138. var isrotationsupported;
  139. }
  140. function createLocationResult(so)
  141. {
  142. result = new Object();
  143. result.ErrorCode = 0;
  144. result.ErrorMessage = "";
  145. retVal = new Object();
  146. retVal.Longitude = so.longitude();
  147. retVal.Latitude = so.latitude();
  148. retVal.Altitude = so.altitude();
  149. retVal.SatelliteNumView = 0;
  150. retVal.SatelliteNumViewUsed = 0;
  151. retVal.HorizontalSpeed = 0;
  152. retVal.HorizontalSpeedError = 0;
  153. retVal.TrueCourse = 0;
  154. retVal.TrueCourseError = 0;
  155. retVal.MagneticHeading = 0;
  156. retVal.MagneticHeadingError = 0;
  157. retVal.Heading = 0;
  158. retVal.HeadingError = 0;
  159. retVal.MagneticCourse = 0;
  160. retVal.MagneticCourseError = 0;
  161. result.ReturnValue = retVal;
  162. return result;
  163. }
  164. var locationCallbackMethod;
  165. function ILocationImpl()
  166. {
  167. var locationPlugin = wrt.getPlugin("locationapi");
  168. this.GetLocation = function(criteria, callback)
  169. {
  170. //criteria.LocationInformationClass
  171. //criteria.Updateoptions
  172. //criteria.Updateoptions.UpdateInterval
  173. //criteria.Updateoptions.UpdateTimeOut
  174. //criteria.Updateoptions.UpdateMaxAge
  175. //criteria.Updateoptions.PartialUpdates
  176. resultObj = createLocationResult(locationPlugin);
  177. if(callback)
  178. {
  179. locationCallbackMethod = callback;
  180. //locationPlugin.positionUpdated.connect(this.positionUpdated);
  181. callback(1,2,resultObj);
  182. }
  183. return resultObj;
  184. };
  185. this.Trace = function()
  186. {
  187. };
  188. this.Calculate = function()
  189. {
  190. };
  191. this.CancelNotification = function()
  192. {
  193. };
  194. this.positionUpdated = function()
  195. {
  196. resultObj = createLocationResult(locationPlugin);
  197. locationCallbackMethod(1,2,resultObj);
  198. };
  199. }
  200. function ISensorImpl()
  201. {
  202. var sensorsPlugin = wrt.getPlugin("sensorsapi");
  203. var sensorchannelsinfoarray = [];
  204. var transactionIdsAndCallbackFunctions = [];
  205. this.AddChannelInfo = function(ChannelId, ContextType, Quantity, ChannelType, Location, VendorId, DataItemSize, ChannelDataTypeId)
  206. {
  207. var sensorchannelinformation = {
  208. ChannelId: ChannelId,
  209. ContextType: ContextType,
  210. Quantity: Quantity,
  211. ChannelType: ChannelType,
  212. Location: Location,
  213. VendorId: VendorId,
  214. DataItemSize: DataItemSize,
  215. ChannelDataTypeId: ChannelDataTypeId
  216. };
  217. sensorchannelsinfoarray.push(sensorchannelinformation);
  218. };
  219. this.AaccelerometerAxisDataEvent = function(TransId, EventCode, DataType, TimeStamp, XAxisData, YAxisData, ZAxisData)
  220. {
  221. for (var i = 0; i < transactionIdsAndCallbackFunctions.length; i++) {
  222. if(transactionIdsAndCallbackFunctions[i] && transactionIdsAndCallbackFunctions[i].TransactionID == TransId) {
  223. var callbackReturnValue = {
  224. DataType: DataType,
  225. TimeStamp: TimeStamp,
  226. XAxisData: XAxisData,
  227. YAxisData: YAxisData,
  228. ZAxisData: ZAxisData
  229. };
  230. var result = {
  231. ReturnValue: callbackReturnValue,
  232. ErrorCode: 0,
  233. ErrorMessage: 0
  234. };
  235. transactionIdsAndCallbackFunctions[i].CallbackFunction(TransId, EventCode, result);
  236. break;
  237. }
  238. }
  239. };
  240. this.OrientationDataEvent = function(TransId, EventCode, DataType, TimeStamp, DeviceOrientation)
  241. {
  242. for (var i = 0; i < transactionIdsAndCallbackFunctions.length; i++) {
  243. if(transactionIdsAndCallbackFunctions[i] && transactionIdsAndCallbackFunctions[i].TransactionID == TransId) {
  244. var callbackReturnValue = {
  245. DataType: DataType,
  246. TimeStamp: TimeStamp,
  247. DeviceOrientation: DeviceOrientation
  248. };
  249. var result = {
  250. ReturnValue: callbackReturnValue,
  251. ErrorCode: 0,
  252. ErrorMessage: 0
  253. };
  254. transactionIdsAndCallbackFunctions[i].CallbackFunction(TransId, EventCode, result);
  255. break;
  256. }
  257. }
  258. };
  259. this.AccelerometerDoubleTappingDataEvent = function(TransId, EventCode, DataType, TimeStamp, DeviceDirection)
  260. {
  261. for (var i = 0; i < transactionIdsAndCallbackFunctions.length; i++) {
  262. if(transactionIdsAndCallbackFunctions[i] && transactionIdsAndCallbackFunctions[i].TransactionID == TransId) {
  263. var callbackReturnValue = {
  264. DataType: DataType,
  265. TimeStamp: TimeStamp,
  266. DeviceDirection: DeviceDirection
  267. };
  268. var result = {
  269. ReturnValue: callbackReturnValue,
  270. ErrorCode: 0,
  271. ErrorMessage: 0
  272. };
  273. transactionIdsAndCallbackFunctions[i].CallbackFunction(TransId, EventCode, result);
  274. break;
  275. }
  276. }
  277. };
  278. this.FindSensorChannel = function(criteria)
  279. {
  280. while(sensorchannelsinfoarray.length > 0)
  281. {
  282. sensorchannelsinfoarray.pop();
  283. }
  284. sensorsPlugin.findSensorChannel(criteria.SearchCriterion);
  285. var result = {
  286. ReturnValue: sensorchannelsinfoarray,
  287. ErrorCode: 0,
  288. ErrorMessage: 0
  289. };
  290. return result;
  291. };
  292. this.RegisterForNotification = function(criteria, callback)
  293. {
  294. var retval = sensorsPlugin.registerForNotification(criteria.ListeningType, criteria.ChannelInfoMap.ChannelId, criteria.ChannelInfoMap.ContextType, criteria.ChannelInfoMap.Quantity, criteria.ChannelInfoMap.ChannelType, criteria.ChannelInfoMap.Location, criteria.ChannelInfoMap.VendorId, criteria.ChannelInfoMap.DataItemSize, criteria.ChannelInfoMap.ChannelDataTypeId);
  295. var trIdsAndCallbacks = {
  296. TransactionID: retval,
  297. CallbackFunction: callback
  298. };
  299. transactionIdsAndCallbackFunctions.push(trIdsAndCallbacks);
  300. var result = {
  301. TransactionID: retval,
  302. ErrorCode: 0,
  303. ErrorMessage: 0
  304. };
  305. return result;
  306. };
  307. this.Cancel = function(criteria)
  308. {
  309. for (var i = 0; i < transactionIdsAndCallbackFunctions.length; i++) {
  310. if(transactionIdsAndCallbackFunctions[i] && transactionIdsAndCallbackFunctions[i].TransactionID == criteria.TransactionID) {
  311. transactionIdsAndCallbackFunctions[i] = 0;
  312. sensorsPlugin.cancel(criteria.TransactionID);
  313. break;
  314. }
  315. }
  316. var result = {
  317. ErrorCode: 0,
  318. ErrorMessage: 0
  319. };
  320. return result;
  321. };
  322. this.GetChannelProperty = function(criteria)
  323. {
  324. var result = {
  325. PropertyId: "",
  326. PropertyDataType: 0,
  327. ItemIndex: 0,
  328. ReadOnly:false,
  329. PropertyValue: 0,
  330. ErrorCode: 0,
  331. ErrorMessage: ""
  332. };
  333. };
  334. }
  335. function Device()
  336. {
  337. this.getServiceObject = function(provider, interface)
  338. {
  339. if(provider=="Service.Location" && interface=="ILocation")
  340. {
  341. so = new Object();
  342. so.ILocation = new ILocationImpl();
  343. return so;
  344. }
  345. else if(provider=="Service.Sensor" && interface=="ISensor")
  346. {
  347. so = new Object();
  348. so.ISensor = new ISensorImpl();
  349. sensors_js = so.ISensor;
  350. return so;
  351. }
  352. }
  353. }
  354. function addChannelInfo(ChannelId, ContextType, Quantity, ChannelType, Location, VendorId, DataItemSize, ChannelDataTypeId)
  355. {
  356. sensors_js.AddChannelInfo(ChannelId, ContextType, Quantity, ChannelType, Location, VendorId, DataItemSize, ChannelDataTypeId);
  357. }
  358. function accelerometerAxisDataEvent(TransId, EventCode, DataType, TimeStamp, XAxisData, YAxisData, ZAxisData)
  359. {
  360. sensors_js.AaccelerometerAxisDataEvent(TransId, EventCode, DataType, TimeStamp, XAxisData, YAxisData, ZAxisData);
  361. }
  362. function accelerometerDoubleTappingDataEvent(TransId, EventCode, DataType, TimeStamp, DeviceDirection)
  363. {
  364. sensors_js.AccelerometerDoubleTappingDataEvent(TransId, EventCode, DataType, TimeStamp, DeviceDirection);
  365. }
  366. function orientationDataEvent(TransId, EventCode, DataType, TimeStamp, DeviceOrientation)
  367. {
  368. sensors_js.OrientationDataEvent(TransId, EventCode, DataType, TimeStamp, DeviceOrientation);
  369. }
  370. function widgetOnShow()
  371. {
  372. widget.onshow();
  373. }
  374. function widgetOnHide()
  375. {
  376. widget.onhide();
  377. }
  378. var menu = new Menu();
  379. var widget = new Widget();
  380. var device = new Device();
  381. widget.isrotationsupported = wrt.rotationSupported();