This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
airplane_mode.js
384 lines (337 loc) · 11.3 KB
/
airplane_mode.js
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* global SettingsListener, AirplaneMode */
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
'use strict';
(function(exports) {
var AirplaneModeServiceHelper = {
_settings: {},
_initSetting: function(settingID) {
var self = this;
var settingEnabledID = settingID + '.enabled';
var settingSuspendedID = settingID + '.suspended';
// forget the mozSetting states when user toggle 'xyz' on manually,
// e.g. set 'xyz'.suspend = false when 'xyz'.enabled === true
window.navigator.mozSettings.addObserver(
settingEnabledID,
function(e) {
if (e.settingValue) {
self._unsuspend(settingSuspendedID);
}
});
// init and observe the corresponding mozSettings
// for Data connection, Bluetooth, Wifi, GPS, and NFC
SettingsListener.observe(settingEnabledID, false,
function(value) {
self._settings[settingEnabledID] = value;
});
// remember the mozSetting states before the airplane mode disables them
SettingsListener.observe(settingSuspendedID, false,
function(value) {
self._settings[settingSuspendedID] = value;
});
},
// turn off the mozSetting corresponding to `key'
// and remember its initial state by storing it in another setting
_suspend: function(key) {
var enabled = this._settings[key + '.enabled'];
var suspended = this._settings[key + '.suspended'];
if (suspended) {
return;
}
// remember the state before switching it to false
var sset = {};
sset[key + '.suspended'] = enabled;
SettingsListener.getSettingsLock().set(sset);
// switch the state to false if necessary
if (enabled) {
var eset = {};
eset[key + '.enabled'] = false;
SettingsListener.getSettingsLock().set(eset);
}
},
// turn on the mozSetting corresponding to `key'
// if it has been suspended by the airplane mode
_restore: function(key) {
var suspended = this._settings[key + '.suspended'];
// clear the 'suspended' state
var sset = {};
sset[key + '.suspended'] = false;
SettingsListener.getSettingsLock().set(sset);
// switch the state to true if it was suspended
if (suspended) {
var rset = {};
rset[key + '.enabled'] = true;
SettingsListener.getSettingsLock().set(rset);
}
},
_unsuspend: function(settingSuspendedID) {
// clear the 'suspended' state
var sset = {};
sset[settingSuspendedID] = false;
SettingsListener.getSettingsLock().set(sset);
},
isEnabled: function(key) {
return this._settings[key + '.enabled'];
},
isSuspended: function(key) {
return this._settings[key + '.suspended'];
},
init: function() {
['ril.data', 'bluetooth', 'wifi', 'geolocation', 'nfc'].forEach(
this._initSetting.bind(this)
);
},
updateStatus: function(value) {
// FM Radio will be turned off in Gecko, more detailed about why we do
// this in Gecko instead, please check bug 997064.
var bluetooth = window.navigator.mozBluetooth;
var wifiManager = window.navigator.mozWifiManager;
var nfc = window.navigator.mozNfc;
// Radio is a special service (might not exist e.g. tablet)
// if value is true,
// it means Radio.enabled is false
if (window.Radio) {
window.Radio.enabled = !value;
}
if (value) {
// Turn off mobile data:
// we toggle the mozSettings value here just for the sake of UI,
// platform RIL disconnects mobile data when
// 'ril.radio.disabled' is true.
this._suspend('ril.data');
// Turn off Bluetooth.
if (bluetooth) {
this._suspend('bluetooth');
}
// Turn off Wifi and Wifi tethering.
if (wifiManager) {
this._suspend('wifi');
SettingsListener.getSettingsLock().set({
'tethering.wifi.enabled': false
});
}
// Turn off Geolocation.
this._suspend('geolocation');
// Turn off NFC
if (nfc) {
this._suspend('nfc');
}
} else {
// Note that we don't restore Wifi tethering when leaving airplane mode
// because Wifi tethering can't be switched on before data connection is
// established.
// Don't attempt to turn on mobile data if it's already on
if (!this._settings['ril.data.enabled']) {
this._restore('ril.data');
}
// Don't attempt to turn on Bluetooth if it's already on
if (bluetooth && !bluetooth.enabled) {
this._restore('bluetooth');
}
// Don't attempt to turn on Wifi if it's already on
if (wifiManager && !wifiManager.enabled) {
this._restore('wifi');
}
// Don't attempt to turn on Geolocation if it's already on
if (!this._settings['geolocation.enabled']) {
this._restore('geolocation');
}
// Don't attempt to turn on NFC if it's already on
if (nfc && !this._settings['nfc.enabled']) {
this._restore('nfc');
}
}
}
};
// main
var AirplaneMode = {
/*
* We will cache the helper as our internal value
*/
_serviceHelper: AirplaneModeServiceHelper,
/*
* This is an internal key to store current state of AirplaneMode
*/
_enabled: null,
/*
* This is an event mapping table that will help us wait for
* specific event from its manager to make sure we are now
* in airplane mode or not.
*/
_checkedActionsMap: {
wifi: {
enabled: 'wifi-enabled',
disabled: 'wifi-disabled'
},
bluetooth: {
enabled: 'bluetooth-adapter-added',
disabled: 'bluetooth-disabled'
},
radio: {
enabled: 'radio-enabled',
disabled: 'radio-disabled'
}
},
/*
* When turning on / off airplane mode, we will start watching
* needed events to make sure we are in airplane mode or not.
*
* @param {boolean} value
* @param {Object} checkedActions
*/
watchEvents: function(value, checkedActions) {
var self = this;
for (var serviceName in this._checkedActionsMap) {
// if we are waiting for specific service
if (serviceName in checkedActions) {
var action = value ? 'disabled' : 'enabled';
var eventName = this._checkedActionsMap[serviceName][action];
// then we will start watch events coming from its manager
window.addEventListener(eventName,
(function(eventName, serviceName) {
return function toUpdateAirplaneMode() {
window.removeEventListener(eventName, toUpdateAirplaneMode);
checkedActions[serviceName] = true;
self._updateAirplaneModeStatus(checkedActions);
};
}(eventName, serviceName)));
}
}
},
/*
* This is a ES5 feature that can help the others easily get/set
* AirplaneMode.
*
* @param {boolean} value
*/
set enabled(value) {
if (value !== this._enabled) {
// start watching events
this.watchEvents(value, this._getCheckedActions(value));
this._enabled = value;
// tell services to do their own operations
this._serviceHelper.updateStatus(value);
}
},
/*
* This is a ES5 feature that can help the others easily get AirplaneMode
* states.
*
* @return {boolean}
*/
get enabled() {
return this._enabled;
},
/*
* In order to make sure all needed managers work successfully. We have to
* use this method to update airplaneMode related keys to tell
* AirplaneModeHelper our current states and is finised or not.
*/
_updateAirplaneModeStatus: function(checkActions) {
var self = this;
var areAllActionsDone;
areAllActionsDone = this._areCheckedActionsAllDone(checkActions);
if (areAllActionsDone) {
SettingsListener.getSettingsLock().set({
'airplaneMode.enabled': self._enabled,
'airplaneMode.status': self._enabled ? 'enabled' : 'disabled',
// NOTE
// this is for backward compatibility,
// because we will update this value only when airplane mode
// is on / off, it will not affect apps using this value
'ril.radio.disabled': self._enabled
});
} else {
// keep updating the status to reflect current status
SettingsListener.getSettingsLock().set({
'airplaneMode.status': self._enabled ? 'enabling' : 'disabling'
});
}
},
/*
* By default, these three API takes longer time and with success / error
* callback. we just have to wait for these three items.
*
* @param {boolean} value
* @return {Object} checkedActions
*/
_getCheckedActions: function(value) {
// we have to re-init all need-to-check managers
var checkedActions = {};
if (value === true) {
// check connection
if (window.Radio) {
checkedActions.radio = false;
}
// check bluetooth
if (this._serviceHelper.isEnabled('bluetooth')) {
checkedActions.bluetooth = false;
}
// check wifi
if (this._serviceHelper.isEnabled('wifi')) {
checkedActions.wifi = false;
}
} else {
// check connection
if (window.Radio) {
checkedActions.radio = false;
}
// check bluetooth
if (this._serviceHelper.isSuspended('bluetooth')) {
checkedActions.bluetooth = false;
}
// check wifi
if (this._serviceHelper.isSuspended('wifi')) {
checkedActions.wifi = false;
}
}
return checkedActions;
},
/*
* We have to use this method to check whether all actions
* are done or not.
*
* @return {boolean}
*/
_areCheckedActionsAllDone: function(checkedActions) {
for (var key in checkedActions) {
if (checkedActions[key] === false) {
return false;
}
}
return true;
},
/*
* Entry point
*/
init: function apm_init() {
var self = this;
this._serviceHelper.init();
// monitor airplaneMode communication key change
SettingsListener.observe('airplaneMode.enabled', false, function(value) {
self.enabled = value;
});
/*
* If we are in airplane mode and the user just dial out an
* emergency call, we have to exit airplane mode.
*/
if (window.Radio) {
window.Radio.addEventListener('radiostatechange', function(state) {
if (state === 'enabled' && this._enabled === true) {
this.enabled = false;
}
}.bind(this));
}
}
};
exports.AirplaneMode = AirplaneMode;
})(window);
if (document && (document.readyState === 'complete' ||
document.readyState === 'interactive')) {
setTimeout(AirplaneMode.init.bind(AirplaneMode));
} else {
window.addEventListener('load', function onload() {
window.removeEventListener('load', onload);
AirplaneMode.init();
});
}