This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
icc.js
executable file
·604 lines (548 loc) · 19.6 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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/* -*- Mode: js; js-indent-level: 2; indent-tabs-mode: nil -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* global LazyLoader, DUMP */
'use strict';
var icc = {
_displayTextTimeout: 40000,
_defaultURL: null,
_inputTimeout: 40000,
_toneDefaultTimeout: 5000,
checkPlatformCompatibility: function icc_checkPlatformCompat() {
// The STK_RESULT_ACTION_CONTRADICTION_TIMER_STATE constant will be added
// in the next versions of the platform. This code avoid errors if running
// in old versions. See bug #1026556
// Remove this workaround as soon as Gecko has the constant defined.
// Followup bug #1059166
if (!('STK_RESULT_ACTION_CONTRADICTION_TIMER_STATE' in this._iccManager)) {
this._iccManager.STK_RESULT_ACTION_CONTRADICTION_TIMER_STATE = 0x24;
}
},
init: function icc_init() {
this._iccManager = window.navigator.mozIccManager;
this.checkPlatformCompatibility();
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 url = '/resources/icc.json';
LazyLoader.getJSON(url).then(function(json) {
self._defaultURL = json.defaultURL;
DUMP('ICC default URL: ', self._defaultURL);
}, function(error) {
DUMP('Failed to fetch file: ' + url + ',' + error);
});
},
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) {
// Protection to bad formed messages
if (!message || !message.iccId || !message.command ||
!message.command.typeOfCommand || !message.command.options) {
return DUMP('STK Proactive Command bad formed: ', 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...');
}
/* TODO: cleanup branching after bug 819831 landed */
var cmdId;
if (typeof message.command.typeOfCommand === 'string') {
cmdId = message.command.typeOfCommand;
} else {
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
if (!this.icc_view) {
return;
}
var forms = this.icc_view.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 *= 60000;
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');
}
window.removeEventListener('keyboardchange', this.keyboardChangedEvent);
window.removeEventListener('keyboardhide', this.keyboardChangedEvent);
},
setupView: function icc_setupView(viewId) {
viewId.style.marginTop = StatusBar.height + 'px';
// If the view has a form, we should be care of the keyboard changes
if (viewId.getElementsByTagName('form').length > 0) {
this.keyboardChangedEvent(viewId);
window.addEventListener('keyboardchange',
this.keyboardChangedEvent.bind(undefined, viewId, false));
window.addEventListener('keyboardhide',
this.keyboardChangedEvent.bind(undefined, viewId, true));
}
},
keyboardChangedEvent: function(viewId, hidden) {
var keyboardHeight = 0;
if (!hidden) {
keyboardHeight = KeyboardManager.getHeight() || 0;
}
var form = viewId.getElementsByTagName('form');
viewId.style.height =
(window.innerHeight - keyboardHeight - StatusBar.height) + 'px';
if (form && viewId.clientHeight > 0) {
var input = viewId.getElementsByTagName('input')[0];
var header = viewId.getElementsByTagName('gaia-header')[0];
var headerSubtitle = viewId.getElementsByTagName('gaia-subheader')[0];
var menu = viewId.getElementsByTagName('menu')[0];
form[0].style.height = viewId.clientHeight -
(header.clientHeight + headerSubtitle.clientHeight) -
menu.clientHeight + 'px';
input.scrollIntoView();
}
},
alert: function icc_alert(stkMessage, message) {
var _ = navigator.mozL10n.get;
if (!this.icc_alert) {
this.icc_alert = document.getElementById('icc-alert');
this.icc_alert_maintitle = document.getElementById('icc-alert-maintitle');
this.icc_alert_subtitle = document.getElementById('icc-alert-subtitle');
this.icc_alert_msg = document.getElementById('icc-alert-msg');
this.icc_alert_btn = document.getElementById('icc-alert-btn');
this.setupView(this.icc_alert);
}
this.icc_alert_maintitle.textContent = _('icc-message-maintitle');
this.icc_alert_subtitle.textContent = _('icc-message-subtitle', {
'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_maintitle =
document.getElementById('icc-confirm-maintitle');
this.icc_confirm_subtitle =
document.getElementById('icc-confirm-subtitle');
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');
this.setupView(this.icc_confirm);
}
if (typeof callback != 'function') {
callback = function() {};
}
this.icc_confirm_maintitle.textContent = _('icc-message-maintitle');
this.icc_confirm_subtitle.textContent = _('icc-message-subtitle', {
'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_maintitle =
document.getElementById('icc-asyncconfirm-maintitle');
this.icc_asyncconfirm_subtitle =
document.getElementById('icc-asyncconfirm-subtitle');
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.setupView(this.icc_asyncconfirm);
}
this.icc_asyncconfirm_maintitle.textContent = _('icc-message-maintitle');
this.icc_asyncconfirm_subtitle.textContent = _('icc-message-subtitle', {
'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) {
// Update input counter
var charactersLeft = maxLen - inputLen;
self.icc_input_btn.textContent = _('ok') + ' (' + charactersLeft + ')';
// Input box full feedback
if (charactersLeft === 0) {
self.icc_input_box.classList.add('full');
navigator.vibrate([500]);
} else {
self.icc_input_box.classList.remove('full');
}
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_maintitle = document.getElementById('icc-input-maintitle');
this.icc_input_subtitle = document.getElementById('icc-input-subtitle');
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_header = document.getElementById('icc-input-header');
this.icc_input_btn_close = document.getElementById('icc-input-btn_close');
this.icc_input_btn_help = document.getElementById('icc-input-btn_help');
this.setupView(this.icc_input);
}
if (typeof callback != 'function') {
callback = function() {};
}
setInputTimeout();
this.icc_input_maintitle.textContent = _('icc-inputbox-maintitle');
this.icc_input_subtitle.textContent = _('icc-inputbox-subtitle', {
'id': this.getSIMNumber(stkMessage.iccId)
});
// Help
this.icc_input_btn_help.disabled = !options.isHelpAvailable;
if (!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);
};
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, true);
};
this.icc_input_btn_no.onclick = function(event) {
clearInputTimeout();
self.hideViews();
callback(true, false);
};
}
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, CLOSE and HELP)
this.icc_input_header.addEventListener('action', function() {
clearInputTimeout();
self.hideViews();
self.backResponse(stkMessage);
callback(null);
});
this.icc_input_btn_close.onclick = function() {
clearInputTimeout();
self.hideViews();
self.terminateResponse(stkMessage);
callback(null);
};
this.icc_input_btn_help.onclick = function() {
clearInputTimeout();
self.hideViews();
self.responseSTKCommand(stkMessage, {
resultCode: self._iccManager.STK_RESULT_HELP_INFO_REQUIRED
});
callback(null);
};
}
};
// Initialize icc management
icc.init();