Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/apps/system/js/quick_settings.js
blob: 328bb35639774be5e4bbe301f99bfd38fcc1699a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */

'use strict';

var QuickSettings = {
  // Indicate setting status of geolocation.enabled
  geolocationEnabled: false,
  WIFI_STATUSCHANGE_TIMEOUT: 2000,

  init: function qs_init() {
    var settings = window.navigator.mozSettings;
    var conn = window.navigator.mozMobileConnection;
    if (!settings || !conn)
      return;

    this.getAllElements();

    this.overlay.addEventListener('click', this);
    window.addEventListener('utilitytrayshow', this);

    var self = this;

    /*
     * Monitor data network icon
     */
    conn.addEventListener('datachange', function qs_onDataChange() {
      var label = {
        'lte': '4G', // 4G LTE
        'ehrpd': '4G', // 4G CDMA
        'hspa+': 'H+', // 3.5G HSPA+
        'hsdpa': 'H', 'hsupa': 'H', 'hspa': 'H', // 3.5G HSDPA
        'evdo0': '3G', 'evdoa': '3G', 'evdob': '3G', '1xrtt': '3G', // 3G CDMA
        'umts': '3G', // 3G
        'edge': 'E', // EDGE
        'is95a': '2G', 'is95b': '2G', // 2G CDMA
        'gprs': '2G'
      };
      self.data.dataset.network = label[conn.data.type];
    });

    /* monitor data setting
     * TODO prevent quickly tapping on it
     */
    SettingsListener.observe('ril.data.enabled', true, function(value) {
      if (value) {
        self.data.dataset.enabled = 'true';
      } else {
        delete self.data.dataset.enabled;
      }
    });

    /* monitor bluetooth setting and initialization/disable ready event
     * - when settings changed, update UI and lock toogle to prevent quickly
     *   tapping on it.
     * - when got bluetooth initialization/disable ready, active toogle, so
     *   return the control to user.
     */
    var btFirstSet = true;
    SettingsListener.observe('bluetooth.enabled', true, function(value) {
      // check self.bluetooth.dataset.enabled and value are identical
      if ((self.bluetooth.dataset.enabled && value) ||
          (self.bluetooth.dataset.enabled === undefined && !value))
        return;

      if (value) {
        self.bluetooth.dataset.enabled = 'true';
      } else {
        delete self.bluetooth.dataset.enabled;
      }

      // Set to the initializing state to block user interaction until the
      // operation completes. (unless we are being called for the first time,
      // where Bluetooth is already initialize
      if (!btFirstSet)
        self.bluetooth.dataset.initializing = 'true';
      btFirstSet = false;
    });
    window.addEventListener('bluetooth-adapter-added', this);
    window.addEventListener('bluetooth-disabled', this);


    /* monitor wifi setting and initialization/disable ready event
     * - when settings changed, update UI and lock toogle to prevent quickly
     *   tapping on it.
     * - when got bluetooth initialization/disable ready, active toogle, so
     *   return the control to user.
     */
    var wifiFirstSet = true;
    SettingsListener.observe('wifi.enabled', true, function(value) {
      // check self.wifi.dataset.enabled and value are identical
      if ((self.wifi.dataset.enabled && value) ||
          (self.wifi.dataset.enabled === undefined && !value))
        return;

      if (value) {
        self.wifi.dataset.enabled = 'true';
      } else {
        delete self.wifi.dataset.enabled;
      }
      // Set to the initializing state to block user interaction until the
      // operation completes. (unless we are being called for the first time,
      // where Wifi is already initialize
      if (!wifiFirstSet)
        self.wifi.dataset.initializing = 'true';
      wifiFirstSet = false;
    });
    window.addEventListener('wifi-enabled', this);
    window.addEventListener('wifi-disabled', this);
    window.addEventListener('wifi-statuschange', this);

    /* monitor geolocation setting
     * TODO prevent quickly tapping on it
     */
    SettingsListener.observe('geolocation.enabled', true, function(value) {
      self.geolocationEnabled = value;
    });

    // monitor airplane mode
    SettingsListener.observe('ril.radio.disabled', false, function(value) {
      self.data.dataset.airplaneMode = value;
      if (value) {
        self.data.classList.add('quick-settings-airplane-mode');
        self.airplaneMode.dataset.enabled = 'true';
      } else {
        self.data.classList.remove('quick-settings-airplane-mode');
        delete self.airplaneMode.dataset.enabled;
      }
    });
  },

  handleEvent: function qs_handleEvent(evt) {
    evt.preventDefault();
    switch (evt.type) {
      case 'click':
        switch (evt.target) {
          case this.wifi:
            // do nothing if wifi isn't ready
            if (this.wifi.dataset.initializing)
              return;
            var enabled = !!this.wifi.dataset.enabled;
            SettingsListener.getSettingsLock().set({
              'wifi.enabled': !enabled
            });
            if (!enabled)
              this.toggleAutoConfigWifi = true;
            break;

          case this.data:
            if (this.data.dataset.airplaneMode !== 'true') {
              // TODO should ignore the action if data initialization isn't done
              var enabled = !!this.data.dataset.enabled;

              SettingsListener.getSettingsLock().set({
                'ril.data.enabled': !enabled
              });
            }

            break;

          case this.bluetooth:
            // do nothing if bluetooth isn't ready
            if (this.bluetooth.dataset.initializing)
              return;

            var enabled = !!this.bluetooth.dataset.enabled;
            SettingsListener.getSettingsLock().set({
              'bluetooth.enabled': !enabled
            });
            break;

          case this.airplaneMode:
            var enabled = !!this.airplaneMode.dataset.enabled;
            SettingsListener.getSettingsLock().set({
              'ril.radio.disabled': !enabled
            });
            break;

          case this.fullApp:
            // XXX: This should be replaced probably by Web Activities
            var host = document.location.host;
            var domain = host.replace(/(^[\w\d]+\.)?([\w\d]+\.[a-z]+)/, '$2');
            var protocol = document.location.protocol + '//';
            Applications.getByManifestURL(protocol + 'settings.' +
                                          domain + '/manifest.webapp').launch();

            UtilityTray.hide();
            break;
        }
        break;

      case 'utilitytrayshow':
        break;

      // unlock bluetooth toggle
      case 'bluetooth-adapter-added':
      case 'bluetooth-disabled':
        delete this.bluetooth.dataset.initializing;
        break;
      // unlock wifi toggle
      case 'wifi-enabled':
        delete this.wifi.dataset.initializing;
        if (this.toggleAutoConfigWifi) {
          // Check whether it found a wifi to connect after a timeout.
          this.wifiStatusTimer = setTimeout(this.autoConfigWifi.bind(this),
            this.WIFI_STATUSCHANGE_TIMEOUT);
        }
        break;
      case 'wifi-disabled':
        delete this.wifi.dataset.initializing;
        if (this.toggleAutoConfigWifi) {
          clearTimeout(this.wifiStatusTimer);
          this.wifiStatusTimer = null;
          this.toggleAutoConfigWifi = false;
        }
        break;

      case 'wifi-statuschange':
        if (this.toggleAutoConfigWifi && !this.wifi.dataset.initializing)
          this.autoConfigWifi();
        break;
    }
  },

  getAllElements: function qs_getAllElements() {
    // ID of elements to create references
    var elements = ['wifi', 'data', 'bluetooth', 'airplane-mode', 'full-app'];

    var toCamelCase = function toCamelCase(str) {
      return str.replace(/\-(.)/g, function replacer(str, p1) {
        return p1.toUpperCase();
      });
    };

    elements.forEach(function createElementRef(name) {
      this[toCamelCase(name)] =
        document.getElementById('quick-settings-' + name);
    }, this);

    this.overlay = document.getElementById('quick-settings');
  },

  // XXX Break down obj keys in a for each loop because mozSettings
  // does not currently supports multiple keys in one set()
  // https://bugzilla.mozilla.org/show_bug.cgi?id=779381
  setMozSettings: function qs_setter(keypairs) {
    var setlock = SettingsListener.getSettingsLock();
    for (var key in keypairs) {
      var obj = {};
      obj[key] = keypairs[key];
      setlock.set(obj);
    }
  },

  /* Auto-config wifi if user enabled wifi from quick settings bar.
   * If there are no known networks around, wifi settings page
   * will be opened. Otherwise nothing will be done.
   */
  autoConfigWifi: function qs_autoConfigWifi() {
    clearTimeout(this.wifiStatusTimer);
    this.wifiStatusTimer = null;
    this.toggleAutoConfigWifi = false;

    var wifiManager = window.navigator.mozWifiManager;
    var status = wifiManager.connection.status;

    if (status == 'disconnected') {
      var activity = new MozActivity({
        name: 'configure',
        data: {
          target: 'device',
          section: 'wifi'
        }
      });
    }
  }
};

QuickSettings.init();