This repository was archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathairplane_mode_helper.js
112 lines (103 loc) · 3.21 KB
/
airplane_mode_helper.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
/* exported AirplaneModeHelper */
/*
* AirplaneModeHelper is a helper that makes apps enable / disable
* airplane mode easily. It will expose few methods for you :
*
* 1. AirplaneModeHelper.ready();
* 2. AirplaneModeHelper.setEnabled();
* 3. AirplaneModeHelper.getStatus();
* 4. AirplaneModeHelper.addEventListener('statechange', callback)
* 5. AirplaneModeHelper.removeEventListener('statechange', callback)
*
* If you want to call getStatus(), please make sure to put them inside
* an anonymous function in AirplaneModeHelper.ready() because this is
* an async call.
*
* Like this:
*
* AirplaneModeHelper.ready(function() {
* var status = AirplaneModeHelper.getStatus();
* });
*/
(function(exports) {
'use strict';
// constants
const kEventName = 'statechange';
const kCommunicationKey = 'airplaneMode.enabled';
const kStatusKey = 'airplaneMode.status';
// main
var AirplaneModeHelper = {
_mozSettings: window.navigator.mozSettings,
_callbacks: [],
_cachedStatus: '',
ready: function(cb) {
if (this._cachedStatus === '') {
var self = this;
this.addEventListener(kEventName, function onChangeEvent() {
// make sure _cachedStatus is definitely not ''
if (self._cachedStatus !== '') {
self.removeEventListener(kEventName, onChangeEvent);
cb();
}
});
} else {
cb();
}
},
getStatus: function() {
return this._cachedStatus;
},
addEventListener: function(eventName, callback) {
if (eventName === kEventName) {
this._callbacks.push(callback);
}
},
removeEventListener: function(eventName, callback) {
if (eventName === kEventName) {
var index = this._callbacks.indexOf(callback);
if (index >= 0) {
this._callbacks.splice(index, 1);
}
}
},
setEnabled: function(enabled) {
this.ready(function() {
var status = this.getStatus();
if (status === 'enabling' || status === 'disabling') {
// do nothing when transition
} else {
if (enabled && status === 'enabled' ||
!enabled && status === 'disabled') {
return;
}
var setObj = {};
setObj[kCommunicationKey] = enabled;
this._mozSettings.createLock().set(setObj);
}
}.bind(this));
},
init: function() {
var self = this;
// init _cachedStatus
var lock = window.navigator.mozSettings.createLock();
var req = lock.get(kStatusKey);
req.onsuccess = function() {
self._cachedStatus = req.result[kStatusKey];
self._callbacks.forEach(function(callback) {
callback(self._cachedStatus);
});
};
this._mozSettings.addObserver(kStatusKey, function(evt) {
var currentStatus = evt.settingValue;
self._cachedStatus = currentStatus;
self._callbacks.forEach(function(callback) {
callback(currentStatus);
});
});
}
};
exports.AirplaneModeHelper = AirplaneModeHelper;
// by default, we will add observer immediately for you right after
// you include this helper
AirplaneModeHelper.init();
})(this);