This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
eu_roaming_manager.js
364 lines (326 loc) · 10.8 KB
/
eu_roaming_manager.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
/* globals SIMSlotManager, Notification, MozActivity, Promise */
'use strict';
(function(exports) {
var EU_ROAMING_FILE_PATH = '/resources/eu-roaming.json';
/**
* EuRoamingManager observes the mobile codes of the current connected
* operator and display a notification when necessary.
*
* @class EuRoamingManager
*/
function EuRoamingManager() {
this._started = false;
this._connections = Array.slice(navigator.mozMobileConnections || []);
this._simMobileCodes = [];
this._curNetworkMobileCodes = [];
this._homeOperatorList = null;
this._foreignOperatorList = null;
}
EuRoamingManager.prototype = {
TAG_PREFIX: 'euRoamingNotificaton',
EU_ROAMING_ENABLED_KEY: 'eu-roaming.enabled',
EU_ROAMING_NOTIFICATION_STATE_KEY: 'eu-roaming.notification.state',
NOTIFICATION_STATES: {
UNAVAILABLE: 'unavailable',
DISPLAYED: 'displayed',
OPENED: 'opened'
},
/**
* Starts the module.
*
* @memberof EuRoamingManager.prototype
*/
start: function() {
if (this._started || !this._connections.length) {
return;
}
this._started = true;
// Clear existing notifications.
this._clearNotifications();
this._init();
},
/**
* Load the EU roaming file and register related listeners when necessary.
*
* @memberof EuRoamingManager.prototype
* @returns {Promise}
*/
_init: function() {
var that = this;
return this._loadJSON(EU_ROAMING_FILE_PATH)
.then(function(result) {
if (!result) {
return;
}
that._homeOperatorList = result.home;
that._foreignOperatorList = result.foreign;
if (that._homeOperatorList &&
that._foreignOperatorList &&
Object.keys(that._homeOperatorList).length > 0 &&
Object.keys(that._foreignOperatorList).length > 0) {
that._initValues();
that._addSimChangeListener();
that._addDataChangeListener();
// Check the current state ar first.
that._connections.forEach(
that._tryShowEuRoamingNotification.bind(that));
}
});
},
/**
* Initialzes values.
*
* @memberof EuRoamingManager.prototype
*/
_initValues: function() {
// Initialize the default values.
SIMSlotManager.getSlots().forEach(function(simSlot, index) {
this._curNetworkMobileCodes.push(null);
var iccInfo = simSlot && simSlot.simCard && simSlot.simCard.iccInfo;
if (iccInfo) {
this._simMobileCodes.push({ mcc: iccInfo.mcc, mnc: iccInfo.mnc });
} else {
this._simMobileCodes.push(null);
}
}, this);
},
/**
* Check if the mobile codes is a valid EU romaing home operator.
*
* @memberof EuRoamingManager.prototype
* @param {String} mcc
* @param {String} mnc
* @returns {Boolean}
*/
_isEURoamingHomeOperator: function(mcc, mnc) {
if (!this._homeOperatorList || !mcc || !mnc) {
return false;
}
return !!this._homeOperatorList[mcc][mnc];
},
/**
* Check if the mobile codes is a valid EU romaing foreign operator.
*
* @memberof EuRoamingManager.prototype
* @param {String} mcc
* @param {String} mnc
* @returns {Boolean}
*/
_isEURoamingForeignOperator: function(mcc, mnc) {
if (!this._foreignOperatorList || !mcc || !mnc) {
return false;
}
return !!this._foreignOperatorList[mcc][mnc];
},
/**
* Add listeners on events related to the change of the mobile codes stored
* on the icc card.
*
* @memberof EuRoamingManager.prototype
*/
_addSimChangeListener: function() {
['updated', 'iccinfochange', 'cardstatechange'].forEach(
function(eventName) {
window.addEventListener('simslot-' + eventName,
this._onSimChanged.bind(this));
}, this);
},
/**
* Add listeners on events related to the change of the mobile codes of the
* currently connected data network.
*
* @memberof EuRoamingManager.prototype
*/
_addDataChangeListener: function() {
this._connections.forEach(function(connection, index) {
connection.addEventListener('datachange', function() {
this._tryShowEuRoamingNotification(connection, index);
}.bind(this));
}, this);
},
/**
* The function stores the mobile codes of the currently used sim card and
* try to display the notification.
*
* @memberof EuRoamingManager.prototype
*/
_onSimChanged: function(event) {
var simSlot = event.detail;
var iccInfo = simSlot && simSlot.simCard && simSlot.simCard.iccInfo;
if (!iccInfo) {
return;
}
this._simMobileCodes[simSlot.index] =
{ mcc: iccInfo.mcc, mnc: iccInfo.mnc };
this._tryShowEuRoamingNotification(simSlot.conn, simSlot.index);
},
/**
* The function displays the EU roaming notification when:
* - Roaming on the newly connected data network.
* - The mobile codes of the operators of the sim card are listed in the
* "home" section of the EU roaming file.
* - The mobile codes of the operators of the connected data network are
* listed in the "foreign" section of the EU roaming file.
*
* @memberof EuRoamingManager.prototype
* @param {MobileConnection} connection
* @param {Number} index
* @returns {Promise}
*/
_tryShowEuRoamingNotification: function(connection, index) {
var data = connection.data;
var network = data && data.network;
if (!network) {
return;
}
var simMobileCode = this._simMobileCodes[index];
var simMcc = simMobileCode && simMobileCode.mcc;
var simMnc = simMobileCode && simMobileCode.mnc;
var curNetworkMobileCode = this._curNetworkMobileCodes[index];
var curNetworkMcc = curNetworkMobileCode && curNetworkMobileCode.mcc;
var curNetworkMnc = curNetworkMobileCode && curNetworkMobileCode.mnc;
if (network.mcc !== curNetworkMcc || network.mnc !== curNetworkMnc) {
// a new operator detected.
this._curNetworkMobileCodes[index] =
{ mcc: network.mcc, mnc: network.mnc };
if (data.roaming &&
// check if the operator of the sim is in the EU regulation list.
this._isEURoamingHomeOperator(simMcc, simMnc) &&
// check if the new operator is a supported foreign operator or not.
this._isEURoamingForeignOperator(network.mcc, network.mnc)) {
return this._getState(this.EU_ROAMING_NOTIFICATION_STATE_KEY + index)
.then(function(notificationState) {
this._setState(this.EU_ROAMING_ENABLED_KEY + index, true);
// display the notification if it has never been opened before.
if (notificationState !== this.NOTIFICATION_STATES.OPENED) {
this._showNotification(index);
return this._setState(
this.EU_ROAMING_NOTIFICATION_STATE_KEY + index,
this.NOTIFICATION_STATES.DISPLAYED);
}
}.bind(this)).catch(function(err) {
console.error(err);
});
} else {
return Promise.all([
this._setState(this.EU_ROAMING_ENABLED_KEY + index, false),
this._setState(this.EU_ROAMING_NOTIFICATION_STATE_KEY + index,
this.NOTIFICATION_STATES.UNAVAILABLE)
]).catch(function(err) {
console.error(err);
});
}
}
},
/**
* Clear all notifications of EU roaming.
*
* @memberof EuRoamingManager.prototype
* @returns {Promise}
*/
_clearNotifications: function() {
var that = this;
return Notification.get().then(function(notifications) {
notifications.forEach(function(notification) {
var tag = notification && notification.tag;
if (!tag || !tag.startsWith(that.TAG_PREFIX)) {
return;
}
notification.close();
});
});
},
/**
* Shows the EU roaming notification of a sim card.
*
* @memberof EuRoamingManager.prototype
* @param {Number} serviceId
*/
_showNotification: function(serviceId) {
var _ = navigator.mozL10n.get;
var iconUrl = window.location.origin + '/style/eu_roaming_manager/' +
'eu_roaming.png';
var options = {
body: _('euRoamingNotificationMsg'),
icon: iconUrl,
tag: this.TAG_PREFIX + serviceId
};
var notification =
new Notification(_('euRoamingNotificationTitle'), options);
notification.onclick = function() {
this._triggerSettingsActivity(serviceId);
notification.close();
}.bind(this);
notification.onclose = function() {
this._setState(this.EU_ROAMING_NOTIFICATION_STATE_KEY + serviceId,
this.NOTIFICATION_STATES.OPENED);
}.bind(this);
},
/**
* Invokes an activity for setting the EU roaming apn.
*
* @memberof EuRoamingManager.prototype
* @param {Number} serviceId
*/
_triggerSettingsActivity: function(serviceId) {
var params = {
name: 'configure',
data: {
target: 'device',
section: 'apn-list',
options: {
role: 'activity',
type: 'default',
serviceId: serviceId
}
}
};
var activity = new MozActivity(params);
activity.onsuccess = function() {};
},
_getState: function(key) {
return new Promise(function(resolve, reject) {
var req = navigator.mozSettings.createLock().get(key);
req.onsuccess = function() {
resolve(req.result[key]);
};
req.onerror = function() {
reject('get state ' + key + ' error');
};
});
},
_setState: function(key, state) {
return new Promise(function(resolve, reject) {
var obj = {};
obj[key] = state;
var req = navigator.mozSettings.createLock().set(obj);
req.onsuccess = resolve;
req.onerror = function() {
reject('set state ' + key + ' error');
};
});
},
_loadJSON: function(path) {
return new Promise(function(resolve) {
var xhr = new XMLHttpRequest();
xhr.open('GET', path, true);
xhr.responseType = 'json';
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200 || xhr.status === 0) {
resolve(xhr.response);
} else {
resolve(null);
}
}
};
try {
xhr.send();
} catch (e) {
resolve(null);
}
});
}
};
exports.EuRoamingManager = EuRoamingManager;
}(window));