forked from mozilla-b2g/gaia
/
airplane_mode_service_helper.js
185 lines (167 loc) · 5.52 KB
/
airplane_mode_service_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
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
/* global BaseModule */
'use strict';
(function() {
var AirplaneModeServiceHelper = function() {};
/**
* AirplaneModeServiceHelper should be deprecated in the future.
* We should let each API handler to observe airplane mode
* status change to turn on/off each API on their own.
*/
AirplaneModeServiceHelper.SETTINGS = [
'ril.data.enabled',
'ril.data.suspended',
'bluetooth.enabled',
'bluetooth.suspended',
'wifi.enabled',
'wifi.suspended',
'geolocation.enabled',
'geolocation.suspended',
'nfc.enabled',
'nfc.suspended'
];
BaseModule.create(AirplaneModeServiceHelper, {
name: 'AirplaneModeServiceHelper',
'_observe_ril.data.enabled': function(value) {
if (value) {
this._unsuspend('ril.data.suspended');
}
},
'_observe_bluetooth.enabled': function(value) {
if (value) {
this._unsuspend('bluetooth.suspended');
}
},
'_observe_geolocation.enabled': function(value) {
if (value) {
this._unsuspend('geolocation.suspended');
}
},
'_observe_wifi.enabled': function(value) {
if (value) {
this._unsuspend('wifi.suspended');
}
},
'_observe_nfc.enabled': function(value) {
if (value) {
this._unsuspend('nfc.suspended');
}
},
// turn off the mozSetting corresponding to `key'
// and remember its initial state by storing it in another setting
_suspend: function(key) {
this.debug('suspending: ' + key);
var enabled = this._settings[key + '.enabled'];
var suspended = this._settings[key + '.suspended'];
if (suspended) {
this.debug('already suspended.');
return;
}
// remember the state before switching it to false
var sset = {};
sset[key + '.suspended'] = enabled;
this.writeSetting(sset);
// switch the state to false if necessary
if (enabled) {
// make sure both BT API and settings key are handled
if ('bluetooth' === key) {
window.dispatchEvent(new CustomEvent('request-disable-bluetooth'));
} else {
var eset = {};
eset[key + '.enabled'] = false;
this.writeSetting(eset);
}
}
},
// turn on the mozSetting corresponding to `key'
// if it has been suspended by the airplane mode
_restore: function(key) {
this.debug('restoring: ' + key);
var suspended = this._settings[key + '.suspended'];
// clear the 'suspended' state
var sset = {};
sset[key + '.suspended'] = false;
this.writeSetting(sset);
// switch the state to true if it was suspended
if (suspended) {
// make sure both BT API and settings key are handled
if ('bluetooth' === key) {
window.dispatchEvent(new CustomEvent('request-enable-bluetooth'));
} else {
var rset = {};
rset[key + '.enabled'] = true;
this.writeSetting(rset);
}
}
},
_unsuspend: function(settingSuspendedID) {
this.debug('unsuspending: ' + settingSuspendedID);
// clear the 'suspended' state
var sset = {};
sset[settingSuspendedID] = false;
this.writeSetting(sset);
},
isEnabled: function(key) {
return this._settings[key + '.enabled'];
},
isSuspended: function(key) {
return this._settings[key + '.suspended'];
},
updateStatus: function(value) {
this.debug('updating status.');
// 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;
this.publish(value ? 'airplanemode-enabled' : 'airplanemode-disabled');
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');
this.writeSetting({
'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 && !this._settings['bluetooth.enabled']) {
this._restore('bluetooth');
}
// Don't attempt to turn on Wifi if it's already on
if (wifiManager && !this._settings['wifi.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');
}
}
}
});
}());