Source: panels/hotspot/hotspot_settings.js

  1. /**
  2. * Hotspot Settings:
  3. * - Update Hotspot Settings
  4. * @module HotspotSettings
  5. */
  6. define(function(require) {
  7. 'use strict';
  8. var SettingsListener = require('shared/settings_listener');
  9. var SettingsCache = require('modules/settings_cache');
  10. var Observable = require('modules/mvvm/observable');
  11. /**
  12. * @alias module:hotspot/hotspot_settings
  13. * @requires module:modules/mvvm/observable
  14. * @requires module:modules/settings_cache
  15. * @returns {hotspotSettingsPrototype}
  16. */
  17. var hotspotSettingsPrototype = {
  18. /**
  19. * Hotspot SSID.
  20. *
  21. * @access private
  22. * @memberOf hotspotSettingsPrototype
  23. * @type {String}
  24. */
  25. hotspotSSID: '',
  26. /**
  27. * Hotspot security type
  28. *
  29. * @access private
  30. * @memberOf hotspotSettingsPrototype
  31. * @type {String}
  32. */
  33. hotspotSecurity: '',
  34. /**
  35. * Hotspot Password
  36. *
  37. * @access private
  38. * @memberOf hotspotSettingsPrototype
  39. * @type {String}
  40. */
  41. hotspotPassword: '',
  42. /**
  43. * Hotspot SSID setting key
  44. *
  45. * @access public
  46. * @memberOf hotspotSettingsPrototype
  47. * @type {String}
  48. */
  49. tetheringSSIDKey: 'tethering.wifi.ssid',
  50. /**
  51. * Hotspot security type setting key
  52. *
  53. * @access public
  54. * @memberOf hotspotSettingsPrototype
  55. * @type {String}
  56. */
  57. tetheringSecurityKey: 'tethering.wifi.security.type',
  58. /**
  59. * Hotspot password setting key
  60. *
  61. * @access public
  62. * @memberOf hotspotSettingsPrototype
  63. * @type {String}
  64. */
  65. tetheringPasswordKey: 'tethering.wifi.security.password',
  66. /**
  67. * Init module.
  68. *
  69. * @access private
  70. * @memberOf hotspotSettingsPrototype
  71. */
  72. _init: function hs_init() {
  73. this._settings = navigator.mozSettings;
  74. this._bindEvents();
  75. this._updatePasswordIfNeeded();
  76. },
  77. /**
  78. * We will generate a random password for the hotspot
  79. *
  80. * @access private
  81. * @memberOf hotspotSettingsPrototype
  82. */
  83. _generateHotspotPassword: function hs_generateHotspotPassword() {
  84. var words = ['amsterdam', 'ankara', 'auckland',
  85. 'belfast', 'berlin', 'boston',
  86. 'calgary', 'caracas', 'chicago',
  87. 'dakar', 'delhi', 'dubai',
  88. 'dublin', 'houston', 'jakarta',
  89. 'lagos', 'lima', 'madrid',
  90. 'newyork', 'osaka', 'oslo',
  91. 'porto', 'santiago', 'saopaulo',
  92. 'seattle', 'stockholm', 'sydney',
  93. 'taipei', 'tokyo', 'toronto'];
  94. var password = words[Math.floor(Math.random() * words.length)];
  95. for (var i = 0; i < 4; i++) {
  96. password += Math.floor(Math.random() * 10);
  97. }
  98. return password;
  99. },
  100. /**
  101. * We will update hotspot password if needed
  102. *
  103. * @access private
  104. * @memberOf hotspotSettingsPrototype
  105. */
  106. _updatePasswordIfNeeded: function hs_updatePasswordIfNeeded() {
  107. var self = this;
  108. SettingsCache.getSettings(function(results) {
  109. if (!results[self.tetheringPasswordKey]) {
  110. var pwd = self._generateHotspotPassword();
  111. self.setHotspotPassword(pwd);
  112. }
  113. });
  114. },
  115. /**
  116. * Sets the value to the tethering SSID setting
  117. *
  118. * @access public
  119. * @memberOf hotspotSettingsPrototype
  120. * @param {String} value
  121. */
  122. setHotspotSSID: function hs_setHotspotSSID(value) {
  123. var cset = {};
  124. cset[this.tetheringSSIDKey] = value;
  125. this._settings.createLock().set(cset);
  126. },
  127. /**
  128. * Sets the value to the tethering security type setting
  129. *
  130. * @access public
  131. * @memberOf hotspotSettingsPrototype
  132. * @param {String} value
  133. */
  134. setHotspotSecurity: function hs_setHotspotSecurity(value) {
  135. var cset = {};
  136. cset[this.tetheringSecurityKey] = value;
  137. this._settings.createLock().set(cset);
  138. },
  139. /**
  140. * Sets the value to the tethering password setting
  141. *
  142. * @access private
  143. * @memberOf hotspotSettingsPrototype
  144. * @param {String} value
  145. */
  146. setHotspotPassword: function hs_setHotspotPassword(value) {
  147. var cset = {};
  148. cset[this.tetheringPasswordKey] = value;
  149. this._settings.createLock().set(cset);
  150. },
  151. /**
  152. * Updates the current value of hotspot SSID
  153. *
  154. * @access private
  155. * @memberOf hotspotSettingsPrototype
  156. * @param {String} value
  157. */
  158. _onSSIDChange: function hs_onSSIDChange(value) {
  159. this.hotspotSSID = value;
  160. },
  161. /**
  162. * Updates the current value of hotspot security type
  163. *
  164. * @access private
  165. * @memberOf hotspotSettingsPrototype
  166. * @param {String} value
  167. */
  168. _onSecurityChange: function hs_onSecurityChange(value) {
  169. this.hotspotSecurity = value;
  170. },
  171. /**
  172. * Updates the current value of hotspot password
  173. *
  174. * @access private
  175. * @memberOf hotspotSettingsPrototype
  176. * @param {String} value
  177. */
  178. _onPasswordChange: function hs_onPasswordChange(value) {
  179. this.hotspotPassword = value;
  180. },
  181. /**
  182. * Listen to hotspot settings changes
  183. *
  184. * @access private
  185. * @memberOf hotspotSettingsPrototype
  186. */
  187. _bindEvents: function hs_bindEvents() {
  188. SettingsListener.observe(this.tetheringSSIDKey,
  189. '', this._onSSIDChange.bind(this));
  190. SettingsListener.observe(this.tetheringSecurityKey,
  191. 'wpa-psk', this._onSecurityChange.bind(this));
  192. SettingsListener.observe(this.tetheringPasswordKey,
  193. '', this._onPasswordChange.bind(this));
  194. }
  195. };
  196. return function ctor_hotspotSettings() {
  197. // Create the observable object using the prototype.
  198. var hotspotSettings = Observable(hotspotSettingsPrototype);
  199. hotspotSettings._init();
  200. return hotspotSettings;
  201. };
  202. });