Repository navigation
Expand file tree
/
Copy pathcall_iccs.js
More file actions
277 lines (245 loc) · 8.45 KB
/
Copy pathcall_iccs.js
File metadata and controls
277 lines (245 loc) · 8.45 KB
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
/* -*- Mode: js; js-indent-level: 2; indent-tabs-mode: nil -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
'use strict';
/**
* Singleton object that helps to populate and manage the 'Select a SIM card'
* panel in the call settings panel.
*/
var IccHandlerForCallSettings = (function(window, document, undefined) {
/** Card state mapping const. */
var CARDSTATE_MAPPING = {
'pinRequired' : 'simCardLockedMsg',
'pukRequired' : 'simCardLockedMsg',
'networkLocked' : 'simLockedPhone',
'serviceProviderLocked' : 'simLockedPhone',
'corporateLocked' : 'simLockedPhone',
'unknown' : 'unknownSimCardState',
'illegal' : 'simCardIllegal',
'absent' : 'noSimCard',
'null' : 'simCardNotReady',
'ready': ''
};
var _ = navigator.mozL10n.get;
var _settings = window.navigator.mozSettings;
var _iccManager = window.navigator.mozIccManager;
var _mobileConnections = null;
var _telephony = window.navigator.mozTelephony;
/** Array of menu item ids. */
var _menuItemIds = ['menuItem-call-sim1', 'menuItem-call-sim2'];
/** Array of menu item descriptions. */
var _menuItemDescriptions = [];
/**
* Holds the ICC code from the ICC card that might be active during
* the time the settings app is open. We need to keep this value in order to
* disable the call settings from the ICC card not being the active one for
* the current call. It will be used for multi ICC card devices only.
*/
var _iccIdOnCall = null;
/**
* Init function.
*/
function ihfcs_init() {
_mobileConnections = window.navigator.mozMobileConnections;
if (!_settings || !_mobileConnections || !_iccManager || !_telephony) {
return;
}
if (DsdsSettings.getNumberOfIccSlots() === 1) {
return;
}
function addListeners(iccId) {
var eventHandler = ihfcs_showICCCardDetails.bind(null, iccId);
var iccCard = _iccManager.getIccById(iccId);
if (!iccCard) {
return;
}
iccCard.addEventListener('cardstatechange', eventHandler);
var mobileConnection = ihfcs_getMobileConnectionFromIccId(iccId);
if (!mobileConnection) {
return;
}
mobileConnection.addEventListener('datachange', eventHandler);
mobileConnection.addEventListener('radiostatechange', eventHandler);
}
_menuItemIds.forEach(function forEachFunction(id) {
var selector = document.getElementById(id);
_menuItemDescriptions[_menuItemIds.indexOf(id)] =
selector.querySelector('small');
});
_menuItemIds.forEach(function forEachFunction(id) {
var selector = document.getElementById(id);
var element = selector.querySelector('a');
element.addEventListener('click', function eventListenerHandler() {
DsdsSettings.setIccCardIndexForCallSettings(
_menuItemIds.indexOf(id)
);
});
});
var numberOfIccCards = _mobileConnections.length;
for (var i = 0; i < numberOfIccCards; i++) {
var mobileConnection = _mobileConnections[i];
if (!mobileConnection.iccId) {
// TODO: this could mean there is no ICC card or the ICC card is
// locked. If locked we would need to figure out how to check the
// current card state.
ihfcs_disableItems(_menuItemIds[i], true);
continue;
}
ihfcs_showICCCardDetails(mobileConnection.iccId);
addListeners(mobileConnection.iccId);
}
_iccManager.addEventListener('iccdetected',
function iccDetectedHandler(evt) {
var iccId = evt.iccId;
ihfcs_showICCCardDetails(iccId);
addListeners(iccId);
});
_iccManager.addEventListener('iccundetected',
function iccUndetectedHandler(evt) {
var iccId = evt.iccId;
var eventHandler = ihfcs_showICCCardDetails.bind(null, iccId);
var mobileConnection = ihfcs_getMobileConnectionFromIccId(iccId);
if (!mobileConnection) {
return;
}
mobileConnection.removeEventListener('datachange', eventHandler);
mobileConnection.removeEventListener('radiostatechange', eventHandler);
});
function callsChangedHandler() {
if (!_telephony.active && !_iccIdOnCall) {
return;
}
if (_telephony.active) {
_iccIdOnCall = _mobileConnections[_telephony.active.serviceId].iccId;
} else {
_iccIdOnCall = null;
}
var numberOfIccCards = _mobileConnections.length;
for (var i = 0; i < numberOfIccCards; i++) {
var mobileConnection = _mobileConnections[i];
if (mobileConnection.iccId) {
ihfcs_showICCCardDetails(mobileConnection.iccId);
}
}
}
// Disable the call settings from the ICC card not being the active one for
// the current call (if any).
callsChangedHandler();
_telephony.addEventListener('callschanged', callsChangedHandler);
}
/**
* Show some details (card state or carrier) of the ICC card.
*
* @param {String} iccId ICC id.
*/
function ihfcs_showICCCardDetails(iccId) {
var iccCardIndex = ihfcs_getIccCardIndex(iccId);
var desc = _menuItemDescriptions[iccCardIndex];
desc.style.fontStyle = 'italic';
var mobileConnection = ihfcs_getMobileConnectionFromIccId(iccId);
if (!mobileConnection) {
localize(desc, '');
ihfcs_disableItems(_menuItemIds[iccCardIndex], true);
return;
}
if (mobileConnection.radioState !== 'enabled') {
// Airplane is enabled. Well, radioState property could be changing but
// let's disable the items during the transitions also.
localize(desc, CARDSTATE_MAPPING['null']);
ihfcs_disableItems(_menuItemIds[iccCardIndex], true);
return;
}
if (mobileConnection.radioState === 'enabled') {
localize(desc, '');
ihfcs_disableItems(_menuItemIds[iccCardIndex], false);
}
var iccCard = _iccManager.getIccById(iccId);
if (!iccCard) {
localize(desc, '');
ihfcs_disableItems(_menuItemIds[iccCardIndex], true);
return;
}
var cardState = iccCard.cardState;
if (cardState !== 'ready') {
localize(desc, CARDSTATE_MAPPING[cardState || 'null']);
ihfcs_disableItems(_menuItemIds[iccCardIndex], true);
return;
}
desc.style.fontStyle = 'normal';
var network = mobileConnection.voice.network;
var iccInfo = iccCard.iccInfo;
var carrier = network ? (network.shortName || network.longName) : null;
if (carrier && iccInfo && iccInfo.isDisplaySpnRequired && iccInfo.spn) {
if (iccInfo.isDisplayNetworkNameRequired && carrier !== iccInfo.spn) {
carrier = carrier + ' ' + iccInfo.spn;
} else {
carrier = iccInfo.spn;
}
}
desc.textContent = carrier;
ihfcs_disableItems(_menuItemIds[iccCardIndex],
_iccIdOnCall && (_iccIdOnCall !== iccId) ?
true : false);
}
/**
* Disable the items listed in the panel in order to avoid user interaction.
*
* @param {String} id Element id from the element to disable.
* @param {Boolean} disable This flag tells the function what to do.
*/
function ihfcs_disableItems(id, disable) {
var element = document.getElementById(id);
if (disable) {
element.setAttribute('aria-disabled', true);
} else {
element.removeAttribute('aria-disabled');
}
}
/**
* Helper function. Return the index of the ICC card given the ICC code in the
* ICC card.
*
* @param {String} iccId The iccId code form the ICC card.
*
* @return {Numeric} The index.
*/
function ihfcs_getIccCardIndex(iccId) {
for (var i = 0; i < _mobileConnections.length; i++) {
if (_mobileConnections[i].iccId === iccId) {
return i;
}
}
return -1;
}
/**
* Helper function. Return the mozMobileConnection for the ICC card given the
* ICC code in the ICC card.
*
* @param {String} iccId The iccId code form the ICC card.
*
* @return {mozMobileConnection} mozMobileConnection object.
*/
function ihfcs_getMobileConnectionFromIccId(iccId) {
for (var i = 0; i < _mobileConnections.length; i++) {
if (_mobileConnections[i].iccId === iccId) {
return _mobileConnections[i];
}
}
return null;
}
return {
init: ihfcs_init
};
})(this, document);
/**
* Startup.
*/
navigator.mozL10n.ready(function loadWhenIdle() {
var idleObserver = {
time: 3,
onidle: function() {
IccHandlerForCallSettings.init();
navigator.removeIdleObserver(idleObserver);
}
};
navigator.addIdleObserver(idleObserver);
});