cordova.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright 2011 Wolfgang Koller - http://www.gofg.at/
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. Cordova = {
  17. plugins: {},
  18. constructors: {},
  19. callbacks: [],
  20. };
  21. /*
  22. * Execute a callback function & remove it from the Cordova object
  23. */
  24. Cordova.callback = function() {
  25. var scId = arguments[0];
  26. var callbackRef = null;
  27. var parameters = [];
  28. for( var i = 1; i < arguments.length; i++ ) {
  29. //debug.log( "Adding parameter " + arguments[i] );
  30. parameters[i-1] = arguments[i];
  31. }
  32. // Keep reference to callback
  33. callbackRef = Cordova.callbacks[scId];
  34. // Even IDs are success-, odd are error-callbacks - make sure we remove both
  35. if( (scId % 2) !== 0 ) {
  36. scId = scId - 1;
  37. }
  38. // Remove both the success as well as the error callback from the stack
  39. Cordova.callbacks.splice( scId, 2 );
  40. // Finally run the callback
  41. if( typeof callbackRef == "function" ) callbackRef.apply( this, parameters );
  42. };
  43. /*
  44. * Enable a plugin for use within Cordova
  45. */
  46. Cordova.enablePlugin = function( pluginName ) {
  47. // Enable the plugin
  48. Cordova.plugins[pluginName] = true;
  49. // Run constructor for plugin if available
  50. if( typeof Cordova.constructors[pluginName] == "function" ) Cordova.constructors[pluginName]();
  51. }
  52. /*
  53. * Add a plugin-specific constructor function which is called once the plugin is loaded
  54. */
  55. Cordova.addConstructor = function( pluginName, constructor ) {
  56. Cordova.constructors[pluginName] = constructor;
  57. }
  58. /**
  59. * Event interface - http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-Event
  60. */
  61. Cordova.Event = function() {
  62. };
  63. Cordova.Event.CAPTURING_PHASE = 1;
  64. Cordova.Event.AT_TARGET = 2;
  65. Cordova.Event.BUBBLING_PHASE = 3;
  66. Cordova.Event.prototype.type = "unknown";
  67. Cordova.Event.prototype.target = Cordova;
  68. Cordova.Event.prototype.currentTarget = Cordova;
  69. Cordova.Event.prototype.eventPhase = Cordova.Event.AT_TARGET;
  70. Cordova.Event.prototype.bubbles = false;
  71. Cordova.Event.prototype.cancelable = false;
  72. Cordova.Event.prototype.timeStamp = 0;
  73. Cordova.Event.prototype.stopPropagation = function() {};
  74. Cordova.Event.prototype.preventDefault = function() {};
  75. Cordova.Event.prototype.initEvent = function( eventTypeArg, canBubbleArg, cancelableArg ) {
  76. this.type = eventTypeArg;
  77. this.timeStamp = (new Date()).getMilliseconds();
  78. };
  79. /*
  80. * EventHandler interface - handles one type of event
  81. * Not W3C defined, but required in order to handle our custom events
  82. */
  83. Cordova.EventHandler = function( p_type ) {
  84. this.type = p_type
  85. this.listeners = []
  86. }
  87. Cordova.EventHandler.prototype.type = "unknown";
  88. Cordova.EventHandler.prototype.listeners = [];
  89. Cordova.EventHandler.prototype.addEventListener = function( p_listener, p_capture ) {
  90. if( p_capture ) {
  91. this.listeners.unshift( p_listener );
  92. }
  93. else {
  94. this.listeners.push( p_listener );
  95. }
  96. };
  97. Cordova.EventHandler.prototype.removeEventListener = function( p_listener, p_capture ) {
  98. // Try to find the event listener in our list
  99. for( var i = 0; i < this.listeners.length; i++ ) {
  100. if( this.listeners[i] === p_listener ) {
  101. // Remove the listener from our queue
  102. this.listeners.splice( i, 1 );
  103. return;
  104. }
  105. }
  106. };
  107. Cordova.EventHandler.prototype.dispatchEvent = function() {
  108. var event = new Cordova.Event();
  109. event.initEvent( this.type, false, false );
  110. // Notify all listeners about this event
  111. for( var i = 0; i < this.listeners.length; i++ ) {
  112. this.listeners[i].apply(Cordova, arguments);
  113. }
  114. };
  115. /*
  116. * Create the custom Cordova events
  117. */
  118. Cordova.events = {
  119. deviceready: new Cordova.EventHandler( "deviceready" ),
  120. resume: new Cordova.EventHandler( "resume" ),
  121. pause: new Cordova.EventHandler( "pause" ),
  122. online: new Cordova.EventHandler( "online" ),
  123. offline: new Cordova.EventHandler( "offline" ),
  124. backbutton: new Cordova.EventHandler( "backbutton" ),
  125. batterycritical: new Cordova.EventHandler( "batterycritical" ),
  126. batterylow: new Cordova.EventHandler( "batterylow" ),
  127. batterystatus: new Cordova.EventHandler( "batterystatus" ),
  128. menubutton: new Cordova.EventHandler( "menubutton" ),
  129. searchbutton: new Cordova.EventHandler( "searchbutton" ),
  130. startcallbutton: new Cordova.EventHandler( "startcallbutton" ),
  131. endcallbutton: new Cordova.EventHandler( "endcallbutton" ),
  132. volumedownbutton: new Cordova.EventHandler( "volumedownbutton" ),
  133. volumeupbutton: new Cordova.EventHandler( "volumeupbutton" )
  134. };
  135. /*
  136. * EventTarget interface - http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget
  137. */
  138. //Keep references to the original EventTarget implementations
  139. Cordova.doc_addEventListener = document.addEventListener;
  140. Cordova.doc_removeEventListener = document.removeEventListener;
  141. Cordova.doc_dispatchEvent = document.dispatchEvent;
  142. document.addEventListener = function( type, listener, useCapture ) {
  143. if( typeof Cordova.events[type] !== "undefined" ) {
  144. Cordova.events[type].addEventListener( listener, useCapture );
  145. }
  146. else {
  147. Cordova.doc_addEventListener.call(document, type, listener, useCapture);
  148. }
  149. };
  150. document.removeEventListener = function( type, listener, useCapture ) {
  151. if( typeof Cordova.events[type] !== "undefined" ) {
  152. Cordova.events[type].removeEventListener( listener, useCapture );
  153. }
  154. else {
  155. Cordova.doc_removeEventListener.call(document, type, listener, useCapture);
  156. }
  157. };
  158. document.dispatchEvent = function( evt ) {
  159. if( typeof Cordova.events[evt.type] !== "undefined" ) {
  160. Cordova.events[evt.type].dispatchEvent();
  161. }
  162. else {
  163. Cordova.doc_dispatchEvent.call(document, evt);
  164. }
  165. };
  166. /*
  167. * Trigger the global deviceready event - fired from native code
  168. */
  169. Cordova.deviceready = function() {
  170. Cordova.events.deviceready.dispatchEvent();
  171. }
  172. Cordova.resumeOccured = function() {
  173. console.log("Cordova.resumeOccured")
  174. Cordova.events.resume.dispatchEvent();
  175. }
  176. Cordova.pauseOccured = function() {
  177. console.log("Cordova.pauseOccured")
  178. Cordova.events.pause.dispatchEvent();
  179. }
  180. Cordova.onlineOccured = function() {
  181. console.log("Cordova.onlineOccured")
  182. Cordova.events.online.dispatchEvent();
  183. }
  184. Cordova.offlineOccured = function() {
  185. console.log("Cordova.offlineOccured")
  186. Cordova.events.offline.dispatchEvent();
  187. }
  188. Cordova.batteryStatusChanged = function(level, isPlugged, forceStatus) {
  189. console.log("Cordova.batteryStatusChanged: " + level + ", " + isPlugged + ", " + forceStatus)
  190. if (level < 3 && !forceStatus)
  191. Cordova.events.batterycritical.dispatchEvent({level: level, isPlugged: isPlugged})
  192. else if (level < 40 && !forceStatus)
  193. Cordova.events.batterylow.dispatchEvent({level: level, isPlugged: isPlugged})
  194. Cordova.events.batterystatus.dispatchEvent({level: level, isPlugged: isPlugged})
  195. }
  196. Cordova.menuKeyPressed = function() {
  197. console.log("Cordova.menuKeyPressed")
  198. Cordova.events.menubutton.dispatchEvent();
  199. }
  200. Cordova.backKeyPressed = function() {
  201. console.log("Cordova.backKeyPressed")
  202. Cordova.events.backbutton.dispatchEvent();
  203. }
  204. Cordova.searchKeyPressed = function() {
  205. console.log("Cordova.searchKeyPressed")
  206. Cordova.events.searchbutton.dispatchEvent();
  207. }
  208. Cordova.callKeyPressed = function() {
  209. console.log("Cordova.callKeyPressed")
  210. Cordova.events.startcallbutton.dispatchEvent();
  211. }
  212. Cordova.hangupKeyPressed = function() {
  213. console.log("Cordova.hangupKeyPressed")
  214. Cordova.events.endcallbutton.dispatchEvent();
  215. }
  216. Cordova.volumeUpKeyPressed = function() {
  217. console.log("Cordova.volumeUpKeyPressed")
  218. Cordova.events.volumeupbutton.dispatchEvent();
  219. }
  220. Cordova.volumeDownKeyPressed = function() {
  221. console.log("Cordova.volumeDownKeyPressed")
  222. Cordova.events.volumedownbutton.dispatchEvent();
  223. }