|
4 | 4 | 'use strict'; |
5 | 5 |
|
6 | 6 | (function OperatorVariant() { |
7 | | - /** |
8 | | - * Get the mcc/mnc info that has been stored in the settings. |
9 | | - */ |
10 | | - |
11 | | - var settings = window.navigator.mozSettings; |
12 | | - if (!settings) |
13 | | - return; |
14 | | - |
15 | | - var iccSettings = { mcc: '-1', mnc: '-1' }; |
16 | 7 |
|
17 | | - // Read the mcc/mnc settings, then trigger callback. |
18 | | - function getICCSettings(callback) { |
19 | | - var transaction = settings.createLock(); |
20 | | - var mccKey = 'operatorvariant.mcc'; |
21 | | - var mncKey = 'operatorvariant.mnc'; |
| 8 | + // Cache the values we've seen. |
| 9 | + var iccSettings = { mcc: -1, mnc: -1 }; |
22 | 10 |
|
23 | | - var mccRequest = transaction.get(mccKey); |
24 | | - mccRequest.onsuccess = function() { |
25 | | - iccSettings.mcc = mccRequest.result[mccKey] || '0'; |
26 | | - var mncRequest = transaction.get(mncKey); |
27 | | - mncRequest.onsuccess = function() { |
28 | | - iccSettings.mnc = mncRequest.result[mncKey] || '0'; |
29 | | - callback(); |
30 | | - }; |
31 | | - }; |
| 11 | + /** |
| 12 | + * Utility function to pad a number with leading zeros and transform it |
| 13 | + * into a string. |
| 14 | + * |
| 15 | + * @param {Number} num The number to pad with leading zeros. |
| 16 | + * @param {Number} length The final length the number should have, |
| 17 | + * in characters. |
| 18 | + */ |
| 19 | + function padLeft(num, length) { |
| 20 | + var r = String(num); |
| 21 | + while (r.length < length) { |
| 22 | + r = '0' + r; |
| 23 | + } |
| 24 | + return r; |
32 | 25 | } |
33 | 26 |
|
34 | | - |
35 | 27 | /** |
36 | | - * Compare the cached mcc/mnc info with the one in the SIM card, |
37 | | - * and retrieve/apply APN settings if they differ. |
| 28 | + * Check the APN settings on startup and when the SIM card is changed. |
38 | 29 | */ |
| 30 | + var operatorVariantHelper = |
| 31 | + new OperatorVariantHelper(applySettings, |
| 32 | + 'operatorvariant.customization', |
| 33 | + true); |
39 | 34 |
|
40 | | - if (!IccHelper.enabled) |
41 | | - return; |
42 | | - |
43 | | - // Check the mcc/mnc information on the SIM card. |
44 | | - function checkICCInfo() { |
45 | | - if (!IccHelper.iccInfo || IccHelper.cardState !== 'ready') |
46 | | - return; |
47 | | - |
48 | | - // ensure that the iccSettings have been retrieved |
49 | | - if ((iccSettings.mcc < 0) || (iccSettings.mnc < 0)) |
50 | | - return; |
51 | | - |
52 | | - // XXX sometimes we get 0/0 for mcc/mnc, even when cardState === 'ready'... |
53 | | - var mcc = IccHelper.iccInfo.mcc || '0'; |
54 | | - var mnc = IccHelper.iccInfo.mnc || '0'; |
55 | | - if (mcc === '0') |
56 | | - return; |
57 | | - |
58 | | - // avoid setting APN (and operator variant) settings if mcc/mnc codes |
59 | | - // changes. |
60 | | - IccHelper.removeEventListener('iccinfochange', checkICCInfo); |
| 35 | + // Listen for future changes in MCC/MNC values to support hot swapping |
| 36 | + // of SIM cards. |
| 37 | + operatorVariantHelper.listen(); |
61 | 38 |
|
| 39 | + function applySettings(mcc, mnc) { |
62 | 40 | // same SIM card => do nothing |
63 | 41 | if ((mcc == iccSettings.mcc) && (mnc == iccSettings.mnc)) { |
64 | 42 | var apnSettingsKey = 'ril.data.apnSettings'; |
|
79 | 57 | iccSettings.mcc = mcc; |
80 | 58 | iccSettings.mnc = mnc; |
81 | 59 | retrieveOperatorVariantSettings(applyOperatorVariantSettings); |
| 60 | + |
82 | 61 | // use mcc, mnc to load and apply WAP user agent profile url |
83 | 62 | retrieveWAPUserAgentProfileSettings(applyWAPUAProfileUrl); |
84 | | - }; |
| 63 | + } |
85 | 64 |
|
86 | 65 | // Load and query APN database, then trigger callback on results. |
87 | 66 | function retrieveOperatorVariantSettings(callback) { |
88 | | - var OPERATOR_VARIANT_FILE = 'shared/resources/apn.json'; |
| 67 | + // This json file should always be accessed from the root instead of the |
| 68 | + // current working base URL so that it can work in unit-tests as well |
| 69 | + // as during normal run time. |
| 70 | + var OPERATOR_VARIANT_FILE = '/shared/resources/apn.json'; |
89 | 71 |
|
90 | 72 | var xhr = new XMLHttpRequest(); |
91 | 73 | xhr.open('GET', OPERATOR_VARIANT_FILE, true); |
92 | 74 | xhr.responseType = 'json'; |
93 | 75 | xhr.onreadystatechange = function() { |
94 | 76 | if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status === 0)) { |
95 | 77 | var apn = xhr.response; |
| 78 | + |
| 79 | + // The apn.json generator strips out leading zeros for mcc values. No |
| 80 | + // need for padding in this instance. |
96 | 81 | var mcc = iccSettings.mcc; |
97 | | - var mnc = iccSettings.mnc; |
| 82 | + |
| 83 | + // We must pad the mnc value and turn it into a string otherwise |
| 84 | + // we could *fail* to load the appropriate settings for single digit |
| 85 | + // *mnc* values! |
| 86 | + var mnc = padLeft(iccSettings.mnc, 2); |
| 87 | + |
98 | 88 | // get a list of matching APNs |
99 | 89 | var compatibleAPN = apn[mcc] ? (apn[mcc][mnc] || []) : []; |
100 | 90 | callback(compatibleAPN); |
|
152 | 142 | const DEFAULT_MMS_SIZE_LIMITATION = 300 * 1024; |
153 | 143 |
|
154 | 144 | // store relevant APN settings |
155 | | - var transaction = settings.createLock(); |
| 145 | + var transaction = navigator.mozSettings.createLock(); |
156 | 146 | for (var type in apnPrefNames) { |
157 | 147 | var apn = {}; |
158 | 148 | for (var i = 0; i < result.length; i++) { |
|
203 | 193 | var apnSettings = []; |
204 | 194 | var apnTypeCandidates = ['default', 'supl', 'mms']; |
205 | 195 | var checkedType = []; |
206 | | - var transaction = settings.createLock(); |
| 196 | + var transaction = navigator.mozSettings.createLock(); |
207 | 197 | // converts apns to new format |
208 | 198 | for (var i = 0; i < result.length; i++) { |
209 | 199 | var sourceAPNItem = result[i]; |
|
251 | 241 | var WAP_UA_PROFILE_FILE = '/resources/wapuaprof.json'; |
252 | 242 | var DEFAULT_KEY = '000000'; |
253 | 243 |
|
254 | | - function padLeft(num, length) { |
255 | | - var r = String(num); |
256 | | - while (r.length < length) { |
257 | | - r = '0' + r; |
258 | | - } |
259 | | - return r; |
260 | | - } |
261 | | - |
262 | 244 | var xhr = new XMLHttpRequest(); |
263 | 245 | xhr.open('GET', WAP_UA_PROFILE_FILE, true); |
264 | 246 | xhr.responseType = 'json'; |
|
281 | 263 |
|
282 | 264 | // apply the user agent profile to mozsettings. |
283 | 265 | function applyWAPUAProfileUrl(uaProfile) { |
284 | | - var transaction = settings.createLock(); |
| 266 | + var transaction = navigator.mozSettings.createLock(); |
285 | 267 | var urlValue = uaProfile ? uaProfile.url : undefined; |
286 | 268 | transaction.set({'wap.UAProf.url': urlValue}); |
287 | 269 | } |
288 | 270 |
|
289 | | - /** |
290 | | - * Check the APN settings on startup and when the SIM card is changed. |
291 | | - */ |
292 | | - |
293 | | - getICCSettings(checkICCInfo); |
294 | | - IccHelper.addEventListener('iccinfochange', checkICCInfo); |
295 | 271 | })(); |
296 | | - |
0 commit comments