app.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * App.js is the heart and soul of Qype S60 WRT widget:
  3. * qype object is the core that loads everything else
  4. *
  5. */
  6. qype = Object.extend(qype, {
  7. /* engine object takes care of UI functionality:
  8. * It shows and hides the views when needed,
  9. * and stores the view history for back button
  10. * To find out more, check Engine.js
  11. */
  12. engine: undefined,
  13. /* screen object is used specifically for
  14. * recognizing screen mode (landscape/portrait)
  15. * and screen size
  16. * To find out more, check Screen.js
  17. */
  18. screen: undefined,
  19. // This is just a dummy for GPS features
  20. soGeolocation: undefined,
  21. // Okay, this is where it all begins after HTML is loaded
  22. onload: function() {
  23. // First of all, we hide softkeys
  24. // to release maximum space for UI
  25. if(window.menu) {
  26. window.menu.hideSoftkeys();
  27. }
  28. // Continue to the actual init
  29. this.init();
  30. },
  31. // onresize() captures the onresize event from browser window
  32. onresize: function() {
  33. // Screen object takes care of the rest (Screen.js)
  34. this.screen.resize();
  35. },
  36. // init is executed immediately when the app starts (through onload)
  37. init: function() {
  38. // Set custom click handler for all buttons on UI
  39. // See wrtpressurize.js for more info
  40. util.pressurizeAll("button");
  41. // Initialize the objects, explained before
  42. this.screen = new Screen();
  43. this.engine = new Engine();
  44. // util.el is a common function to getElementById
  45. // Defined in util.js
  46. this.loadingStatus = util.el("loadingStatus");
  47. this.locationStatus = util.el("locationStatus");
  48. /* Now, it's time to register needed views for the engine.
  49. *
  50. * View is a common class to take care of dynamic content
  51. * in the UI. In short, engine calls view class's builder
  52. * when the view is about to be displayed.
  53. *
  54. * Giving undefined as the value registers the view
  55. * as of type plain View, having no dynamic data (i.e. Loading View)
  56. *
  57. * qype.views.<name> are defined in view-specific js-files
  58. * (see, for example, PlacesView.js)
  59. *
  60. * View class is defined in View.js
  61. */
  62. this.engine.registerViews({
  63. "loadingView": undefined,
  64. "locationView": qype.views.LocationView,
  65. "categoriesView": qype.views.CategoriesView,
  66. "placesView": qype.views.PlacesView,
  67. "placeView": qype.views.PlaceView,
  68. "mapView": qype.views.MapView
  69. })
  70. // Initialize screen size info
  71. this.onresize();
  72. // Activate first view (= Loading/splash)
  73. // and do not add it to the navigation
  74. // stack; we don't want user to be able to
  75. // hit back button in main view to return
  76. // to the loading screen
  77. this.engine.activateView("loadingView", {
  78. excludeFromNavigation: true
  79. });
  80. // To make the user experience a little bit smoother,
  81. // we call next step (update location) through a small
  82. // delay
  83. var self = this;
  84. setTimeout(function() {
  85. self.setInitialLocation();
  86. }, 1000)
  87. },
  88. // Initial location is not being fetched from GPS for
  89. // better user-experience; it may take some time to receive
  90. // fix from satellites, and we don't want this to happen
  91. // when user starts the app.
  92. //
  93. // Instead, the last known location is used until user manually
  94. // refreshes GPS location (button in Places View)
  95. setInitialLocation: function() {
  96. // Util.log is used just for writing progress
  97. // to console.log in Web Inspector (util.js)
  98. util.log("{app} Using fixed location");
  99. // Set status text in Loading View
  100. this.setLoadingStatus("Let's first take you to London...");
  101. var latitude, longitude = undefined;
  102. /* Here we are using widget storage to store as key-value pairs
  103. * the last known GPS location.
  104. * If none exists and/or this is the first time the app is used,
  105. * default values are used instead
  106. */
  107. try {
  108. latitude = window.widget.preferenceForKey("latitude") ;
  109. longitude = window.widget.preferenceForKey("longitude") ;
  110. } catch (e) {
  111. // Do nothing; this is just for safe if widget object
  112. // is not supported by the browser for some reason
  113. }
  114. // If no stored values were available, use default
  115. if(!latitude || !longitude) {
  116. latitude = 51.51091; // London
  117. longitude = -0.13654; // London
  118. }
  119. // Now, because no actual GPS is being used,
  120. // we are calling the location update callback
  121. // directly; coordinates are given as parameters
  122. this.onUpdateLocationSuccess({
  123. coords : {
  124. latitude : latitude,
  125. longitude : longitude,
  126. altitude : 0,
  127. accuracy : 0,
  128. altitudeAccuracy : 0,
  129. heading : 0,
  130. speed : 0
  131. },
  132. timestamp : Date
  133. });
  134. },
  135. /* This is called when user presses 'Refresh GPS' button
  136. * in Places View
  137. */
  138. refreshLocation: function() {
  139. // Reset location status text
  140. this.setLocationStatus("Loading..");
  141. // Set the Location View to become visible
  142. this.engine.activateView("locationView");
  143. // Launch GPS location update, with given
  144. // callback to handle the results
  145. this.updateLocation(
  146. this.onUpdateLocationSuccess.bind(this),
  147. this.onUpdateLocationFailure.bind(this)
  148. );
  149. },
  150. // Writes loading status updates to Loading View
  151. setLoadingStatus: function(txt) {
  152. this.loadingStatus.innerHTML = txt;
  153. },
  154. // Writes location status updates to Loading View
  155. setLocationStatus: function(txt) {
  156. this.locationStatus.innerHTML = txt;
  157. },
  158. /* Really simple function to take care of GPS location update
  159. */
  160. updateLocation: function(onSuccess, onError) {
  161. // Set status text in Location View
  162. this.setLocationStatus("Acquiring location...");
  163. /* ***************************
  164. PLACE HERE CODE SNIPPET 13.1
  165. ****************************/
  166. // Initialize Platform Service object for GPS
  167. this.soGeolocation = this.soGeolocation || nokia.device.load("geolocation");
  168. /* ***************************
  169. PLACE HERE CODE SNIPPET 13.2
  170. ****************************/
  171. // Set parameters for the service object
  172. var opts = {
  173. timeout: 30000,
  174. maximumAge: 20000
  175. };
  176. /* ***************************
  177. PLACE HERE CODE SNIPPET 13.3
  178. ****************************/
  179. // Initialize location getting (and set the callbacks & parameters)
  180. try {
  181. this.soGeolocation.getCurrentPosition(onSuccess, onError, opts);
  182. }
  183. catch (e) {
  184. // In case something goes wrong, write to log & status
  185. util.log("{qype.updateLocation} error fetching the position: " + Object.toJSON(e));
  186. this.setLocationStatus("Error fetching the location! (" + Object.toJSON(e) + ")");
  187. }
  188. },
  189. /* onUpdateLocationSuccess is called when GPS receives a location
  190. * It stores the received location to widget storage (as the last known position)
  191. * and calls engine startup in Engine.js
  192. */
  193. onUpdateLocationSuccess: function(position) {
  194. // Necessary updates to Web Inspector log and to location status
  195. util.log("{app} Got location!");
  196. this.setLocationStatus("Found: lat " + position.coords.latitude + ", lon " + position.coords.longitude);
  197. // Store the coordinates as key-value pairs to widget storage
  198. /*try {
  199. window.widget.setPreferenceForKey(position.coords.latitude.toString(), "latitude");
  200. window.widget.setPreferenceForKey(position.coords.longitude.toString(), "longitude");
  201. } catch (e) {
  202. // Do nothing;
  203. }*/
  204. // Slight delay to shoow the current status message
  205. // before moving on
  206. var self = this;
  207. setTimeout(function() {
  208. self.engine.startup(position);
  209. }, 1200)
  210. },
  211. /* In case the GPS location fetching fails, onUpdateLocationFailure
  212. * is called instead of onUpdateLocationSuccess
  213. */
  214. onUpdateLocationFailure: function(e) {
  215. // Update web inspector log and location status
  216. util.log("{qype.init} error fetching the location");
  217. this.setLocationStatus("Error fetching the location (event handler)! (" + ((e) ? Object.toJSON(e) : '') + ")");
  218. }
  219. });