This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
icc.js
521 lines (469 loc) · 16 KB
/
icc.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/* -*- Mode: js; js-indent-level: 2; indent-tabs-mode: nil -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
'use strict';
var icc = {
_displayTextTimeout: 40000,
_defaultURL: null,
_inputTimeout: 40000,
_toneDefaultTimeout: 5000,
init: function icc_init() {
this._iccManager = window.navigator.mozIccManager;
var self = this;
this.clearMenuCache(function() {
window.navigator.mozSetMessageHandler('icc-stkcommand',
function callHandleSTKCommand(message) {
if (self._iccManager && self._iccManager.getIccById) {
self.handleSTKCommand(message);
}
});
});
this.hideViews();
this.protectForms();
this.getIccInfo();
var self = this;
// Update displayTextTimeout with settings parameter
var reqDisplayTimeout = window.navigator.mozSettings.createLock().get(
'icc.displayTextTimeout');
reqDisplayTimeout.onsuccess = function icc_getDisplayTimeout() {
self._displayTextTimeout =
reqDisplayTimeout.result['icc.displayTextTimeout'];
};
window.navigator.mozSettings.addObserver('icc.displayTextTimeout',
function(e) {
self._displayTextTimeout = e.settingValue;
}
);
// Update inputTimeout with settings parameter
var reqInputTimeout = window.navigator.mozSettings.createLock().get(
'icc.inputTextTimeout');
reqInputTimeout.onsuccess = function icc_getInputTimeout() {
self._inputTimeout = reqInputTimeout.result['icc.inputTextTimeout'];
};
window.navigator.mozSettings.addObserver('icc.inputTextTimeout',
function(e) {
self._inputTimeout = e.settingValue;
}
);
// Update toneDefaultTimeout with settings parameter
var reqToneDefaultTimeout = window.navigator.mozSettings.createLock().get(
'icc.toneDefaultTimeout');
reqToneDefaultTimeout.onsuccess = function icc_getToneDefaultTimeout() {
self._toneDefaultTimeout =
reqToneDefaultTimeout.result['icc.toneDefaultTimeout'];
};
window.navigator.mozSettings.addObserver('icc.toneDefaultTimeout',
function(e) {
self._toneDefaultTimeout = e.settingValue;
}
);
},
getIccInfo: function icc_getIccInfo() {
var self = this;
var xhr = new XMLHttpRequest();
xhr.onerror = function() {
DUMP('Failed to fetch file: ' + href, xhr.statusText);
};
xhr.onload = function() {
self._defaultURL = xhr.response.defaultURL;
DUMP('ICC default URL: ', self._defaultURL);
};
xhr.open('GET', '/resources/icc.json', true);
xhr.responseType = 'json';
xhr.send();
},
getIcc: function icc_getIcc(iccId) {
DUMP('ICC Getting ICC for ' + iccId);
return this._iccManager.getIccById(iccId);
},
getConnection: function icc_getConnection(iccId) {
DUMP('ICC Getting Connection for ' + iccId);
for (var i = 0; i < window.navigator.mozMobileConnections.length; i++) {
if (window.navigator.mozMobileConnections[i].iccId === iccId) {
DUMP('ICC Connection ' + i + ' found for ' + iccId);
return window.navigator.mozMobileConnections[i];
}
}
return null;
},
getSIMNumber: function icc_getSIMNumber(iccId) {
DUMP('ICC Getting SIM Number for ' + iccId);
for (var i = 0; i < window.navigator.mozMobileConnections.length; i++) {
if (window.navigator.mozMobileConnections[i].iccId === iccId) {
return i + 1;
}
}
return '';
},
clearMenuCache: function icc_clearMenuCache(callback) {
if (typeof callback != 'function') {
callback = function() {};
}
// Remove previous menu
var resetApplications = window.navigator.mozSettings.createLock().set({
'icc.applications': '{}'
});
resetApplications.onsuccess = function icc_resetApplications() {
DUMP('STK Cache Reseted');
callback();
};
},
handleSTKCommand: function icc_handleSTKCommand(message) {
DUMP('STK Proactive Command for SIM ' + message.iccId + ': ',
message.command);
if (FtuLauncher.isFtuRunning()) {
// Delay the stk command until FTU is done
var self = this;
window.addEventListener('ftudone', function ftudone() {
DUMP('FTU is done!... processing STK message:', message);
self.handleSTKCommand(message);
});
return DUMP('FTU is running, delaying STK...');
}
var cmdId = '0x' + message.command.typeOfCommand.toString(16);
if (icc_worker[cmdId]) {
return icc_worker[cmdId](message);
}
DUMP('STK Command not recognized ! - ', message);
},
/**
* Response ICC Command
*/
responseSTKCommand: function icc_responseSTKCommand(message, response) {
DUMP('STK sendStkResponse -- # response = ', response);
var _icc = icc.getIcc(message.iccId);
_icc && _icc.sendStkResponse(message.command, response);
},
/**
* Common responses
*/
terminateResponse: function(message) {
DUMP('STK Sending STK_RESULT_UICC_SESSION_TERM_BY_USER to card ' +
message.iccId);
this.responseSTKCommand(message, {
resultCode: this._iccManager.STK_RESULT_UICC_SESSION_TERM_BY_USER
});
},
backResponse: function(message) {
DUMP('STK Sending STK_RESULT_BACKWARD_MOVE_BY_USER to card ' +
message.iccId);
this.responseSTKCommand(message, {
resultCode: this._iccManager.STK_RESULT_BACKWARD_MOVE_BY_USER
});
},
/**
* Protect forms from reloading system app
*/
protectForms: function() {
var protect = function(event) {
if (!event) {
return;
}
event.preventDefault();
};
// Prevents from reloading the system app when
// the user taps on the Enter key
var iccView = document.getElementById('icc-view');
if (!iccView) {
return;
}
var forms = iccView.getElementsByTagName('form');
if (!forms) {
return;
}
for (var i = 0; i < forms.length; i++) {
var form = forms[i];
form.onsubmit = protect;
}
},
/******************************************
* ICC Helper methods
******************************************/
calculateDurationInMS: function icc_calculateDurationInMS(timeUnit,
timeInterval) {
var timeout = timeInterval;
switch (timeUnit) {
case this._iccManager.STK_TIME_UNIT_MINUTE:
timeout *= 3600000;
break;
case this._iccManager.STK_TIME_UNIT_SECOND:
timeout *= 1000;
break;
case this._iccManager.STK_TIME_UNIT_TENTH_SECOND:
timeout *= 100;
break;
}
return timeout;
},
hideViews: function icc_hideViews() {
if (!this.icc_view) {
this.icc_view = document.getElementById('icc-view');
}
this.icc_view.classList.remove('visible');
var icc_view_boxes = this.icc_view.children;
for (var i = 0; i < icc_view_boxes.length; i++) {
icc_view_boxes[i].classList.remove('visible');
}
},
alert: function icc_alert(stkMessage, message) {
var _ = navigator.mozL10n.get;
if (!this.icc_alert) {
this.icc_alert = document.getElementById('icc-alert');
this.icc_alert_title = document.getElementById('icc-alert-title');
this.icc_alert_msg = document.getElementById('icc-alert-msg');
this.icc_alert_btn = document.getElementById('icc-alert-btn');
}
this.icc_alert_title.textContent = _('icc-message-title', {
'id': this.getSIMNumber(stkMessage.iccId)
});
var self = this;
this.icc_alert_btn.onclick = function closeICCalert() {
self.hideViews();
};
this.icc_alert_msg.textContent = message;
this.icc_alert.classList.add('visible');
this.icc_view.classList.add('visible');
},
/**
* callback responds with "userCleared"
*/
confirm: function(stkMessage, message, timeout, callback) {
var _ = navigator.mozL10n.get;
if (!this.icc_confirm) {
this.icc_confirm = document.getElementById('icc-confirm');
this.icc_confirm_title = document.getElementById('icc-confirm-title');
this.icc_confirm_msg = document.getElementById('icc-confirm-msg');
this.icc_confirm_btn = document.getElementById('icc-confirm-btn');
this.icc_confirm_btn_back =
document.getElementById('icc-confirm-btn_back');
this.icc_confirm_btn_close =
document.getElementById('icc-confirm-btn_close');
}
if (typeof callback != 'function') {
callback = function() {};
}
this.icc_confirm_title.textContent = _('icc-message-title', {
'id': this.getSIMNumber(stkMessage.iccId)
});
var self = this;
// STK Default response (BACK and CLOSE)
this.icc_confirm_btn_back.onclick = function() {
clearTimeout(timeoutId);
self.hideViews();
self.backResponse(stkMessage);
callback(null);
};
this.icc_confirm_btn_close.onclick = function() {
clearTimeout(timeoutId);
self.hideViews();
self.terminateResponse(stkMessage);
callback(null);
};
// User acceptance
if (timeout) {
var timeoutId = setTimeout(function() {
self.hideViews();
callback(false);
}, timeout);
}
this.icc_confirm_btn.onclick = function() {
clearTimeout(timeoutId);
self.hideViews();
callback(true);
};
this.icc_confirm_msg.textContent = message;
this.icc_confirm.classList.add('visible');
this.icc_view.classList.add('visible');
},
asyncConfirm: function(stkMessage, message, callback) {
var _ = navigator.mozL10n.get;
if (typeof callback != 'function') {
callback = function() {};
}
if (!this.icc_asyncconfirm) {
this.icc_asyncconfirm =
document.getElementById('icc-asyncconfirm');
this.icc_asyncconfirm_title =
document.getElementById('icc-asyncconfirm-title');
this.icc_asyncconfirm_msg =
document.getElementById('icc-asyncconfirm-msg');
this.icc_asyncconfirm_btn_no =
document.getElementById('icc-asyncconfirm-btn-no');
this.icc_asyncconfirm_btn_yes =
document.getElementById('icc-asyncconfirm-btn-yes');
}
this.icc_asyncconfirm_title.textContent = _('icc-message-title', {
'id': this.getSIMNumber(stkMessage.iccId)
});
var self = this;
this.icc_asyncconfirm_btn_no.onclick = function rejectConfirm() {
self.hideViews();
callback(false);
};
this.icc_asyncconfirm_btn_yes.onclick = function acceptConfirm() {
self.hideViews();
callback(true);
};
this.icc_asyncconfirm_msg.textContent = message;
this.icc_asyncconfirm.classList.add('visible');
this.icc_view.classList.add('visible');
},
/**
* Open URL
*/
showURL: function(stkMessage, url, confirmMessage) {
function openURL(url) {
// Sanitise url just in case it doesn't start with http or https
// the web activity won't work, so add by default the http protocol
if (url.search('^https?://') == -1) {
// Our url doesn't contains the protocol
url = 'http://' + url;
}
new MozActivity({
name: 'view',
data: { type: 'url', url: url }
});
}
if (url == null || url.length == 0) {
url = this._defaultURL;
}
DUMP('Final URL to open: ' + url);
if (url != null || url.length != 0) {
if (confirmMessage) {
this.asyncConfirm(stkMessage, confirmMessage, function(res) {
if (res) {
openURL(url);
}
});
} else {
openURL(url);
}
}
},
input: function(stkMessage, message, timeout, options, callback) {
var _ = navigator.mozL10n.get;
var self = this;
var timeoutId = null;
/**
* Check if the length of the input is valid.
*
* @param {Integer} inputLen The length of the input.
* @param {Integer} minLen Minimum length required of the input.
* @param {Integer} maxLen Maximum length required of the input.
*/
function checkInputLengthValid(inputLen, minLen, maxLen) {
return (inputLen >= minLen) && (inputLen <= maxLen);
}
function clearInputTimeout() {
if (timeoutId) {
DUMP('clearing previous STK INPUT timeout');
clearTimeout(timeoutId);
timeoutId = null;
}
}
function setInputTimeout() {
DUMP('setting new STK INPUT timeout to - ', timeout);
if (timeout) {
clearInputTimeout();
timeoutId = setTimeout(function() {
self.hideViews();
callback(false);
}, timeout);
}
}
if (!this.icc_input) {
this.icc_input = document.getElementById('icc-input');
this.icc_input_title = document.getElementById('icc-input-title');
this.icc_input_msg = document.getElementById('icc-input-msg');
this.icc_input_box = document.getElementById('icc-input-box');
this.icc_input_btn = document.getElementById('icc-input-btn');
this.icc_input_btn_yes = document.getElementById('icc-input-btn_yes');
this.icc_input_btn_no = document.getElementById('icc-input-btn_no');
this.icc_input_btn_back = document.getElementById('icc-input-btn_back');
this.icc_input_btn_help = document.getElementById('icc-input-btn_help');
}
if (typeof callback != 'function') {
callback = function() {};
}
setInputTimeout();
this.icc_input_title.textContent = _('icc-inputbox-title', {
'id': this.getSIMNumber(stkMessage.iccId)
});
// Help
this.icc_input_btn_help.disabled = !options.isHelpAvailable;
if (!options.isYesNoRequired && !options.isYesNoRequested) {
this.icc_input.classList.remove('yesnomode');
// Workaround. See bug #818270. Followup: #895314
setTimeout(function workaround_bug818270() {
self.icc_input_box.maxLength = options.maxLength;
self.icc_input_box.value = options.defaultText || '';
self.icc_input_btn.disabled = !checkInputLengthValid(
self.icc_input_box.value.length, options.minLength,
options.maxLength);
}, 500);
this.icc_input_box.placeholder = message;
this.icc_input_box.type = options.isAlphabet ? 'text' : 'tel';
if (options.hideInput) {
this.icc_input_box.type = 'password';
}
if (options.hidden) {
this.icc_input_box.type = 'hidden';
}
this.icc_input_btn.disabled = !checkInputLengthValid(
this.icc_input_box.value.length, options.minLength, options.maxLength);
this.icc_input_box.onkeyup = function(event) {
setInputTimeout();
if (self.icc_input_box.type === 'tel') {
// Removing unauthorized characters
self.icc_input_box.value =
self.icc_input_box.value.replace(/[()-]/g, '');
}
self.icc_input_btn.disabled = !checkInputLengthValid(
self.icc_input_box.value.length, options.minLength,
options.maxLength);
if (self.icc_input_box.value.length == options.maxLength) {
self.icc_input_btn.focus();
}
};
this.icc_input_btn.onclick = function() {
clearInputTimeout();
self.hideViews();
callback(true, self.icc_input_box.value);
};
this.icc_input_box.focus();
} else {
this.icc_input.classList.add('yesnomode');
this.icc_input_box.type = 'hidden';
this.icc_input_btn_yes.onclick = function(event) {
clearInputTimeout();
self.hideViews();
callback(true, 1);
};
this.icc_input_btn_no.onclick = function(event) {
clearInputTimeout();
self.hideViews();
callback(true, 0);
};
}
this.icc_input_box.value = '';
this.icc_input_msg.textContent = message;
this.icc_input.classList.add('visible');
this.icc_view.classList.add('visible');
// STK Default response (BACK and HELP)
this.icc_input_btn_back.onclick = function() {
clearInputTimeout();
self.hideViews();
self.backResponse(stkMessage);
callback(null);
};
this.icc_input_btn_help.onclick = function() {
clearInputTimeout();
self.hideViews();
self.responseSTKCommand({
resultCode: self._iccManager.STK_RESULT_HELP_INFO_REQUIRED
});
callback(null);
};
}
};
// Initialize icc management
icc.init();