This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
apn_utils.js
214 lines (195 loc) · 5.56 KB
/
apn_utils.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
/**
* The apn utilities
*/
define(function(require) {
'use strict';
var SettingsCache = require('modules/settings_cache');
var ApnHelper = require('shared/apn_helper');
var ApnConst = require('modules/apn/apn_const');
var ApnItem = require('modules/apn/apn_item');
var CP_APN_KEY = ApnConst.CP_APN_KEY;
var DEFAULT_APN_KEY = ApnConst.DEFAULT_APN_KEY;
var MCC_SETTINGS_KEY = ApnConst.MCC_SETTINGS_KEY;
var MNC_SETTINGS_KEY = ApnConst.MNC_SETTINGS_KEY;
var APN_PROPS = ApnConst.APN_PROPS;
var EU_ROAMING_FILE_PATH = ApnConst.EU_ROAMING_FILE_PATH;
function _getOperatorCode(serviceId, type) {
var value;
var key;
if (type === 'mcc') {
value = '000';
key = MCC_SETTINGS_KEY;
} else if (type === 'mnc') {
value = '00';
key = MNC_SETTINGS_KEY;
} else {
return Promise.reject('invalid type');
}
return new Promise(function(resolve, reject) {
SettingsCache.getSettings(function(results) {
var values = results[key];
if (values && Array.isArray(values) && values[serviceId]) {
value = values[serviceId];
}
resolve(value);
});
});
}
/**
* Helper function. Filter APNs by apn type.
*
* @param {String} type
* The apn type we would like to include.
* @param {ApnItem} apnItem
*/
function _apnTypeFilter(type, apnItem) {
if (!type || !apnItem || !apnItem.apn.types) {
return false;
} else if (type === '*') {
return true;
}
return apnItem.apn.types.indexOf(type) != -1;
}
/**
* Query <apn> elements matching the mcc/mnc arguments in the apn.json
* database
*
* @param {String} mcc
* @param {String} mnc
* @param {String} networkType
* The network type which the APN must be compatible with.
*/
function _getDefaultApns(mcc, mnc, networkType) {
// XXX: should fallback to the JSON file if we don't get the apns
return new Promise(function(resolve, reject) {
SettingsCache.getSettings(function(results) {
var apns = results[DEFAULT_APN_KEY] || {};
resolve(ApnHelper.getCompatible(apns, mcc, mnc, networkType));
});
});
}
/**
* Query <apn> elements matching the mcc/mnc arguments in the database
* received through client provisioning messages.
*
* @param {String} mcc
* @param {String} mnc
* @param {String} networkType
* The network type which the APN must be compatible with.
*/
function _getCpApns(mcc, mnc, networkType) {
return new Promise(function(resolve, reject) {
SettingsCache.getSettings(function(results) {
var apns = results[CP_APN_KEY] || {};
resolve(ApnHelper.getCompatible(apns, mcc, mnc, networkType));
});
});
}
var _euApnChecked = false;
var _euApns = null;
/**
* Return the EU apns for roaming.
*/
function _getEuApns() {
if (_euApnChecked) {
return Promise.resolve(_euApns);
} else {
return _loadJSON(EU_ROAMING_FILE_PATH).then(function(result) {
_euApnChecked = true;
// Only return eu apns when both home and foreign operators are
// specified.
if (result.home && result.foreign &&
Object.keys(result.home).length > 0 &&
Object.keys(result.foreign).length > 0) {
_euApns = result.defaultApns;
}
return _euApns;
}).catch(function() {
_euApnChecked = true;
return null;
});
}
}
function _generateId() {
// should refine this
return Math.random().toString(36).substr(2, 9);
}
function _cloneApn(apn) {
var newApn = {};
for (var p in apn) {
newApn[p] = apn[p];
}
return newApn;
}
function _separateApnsByType(apns) {
if (!apns) {
return [];
}
return apns.reduce(function(result, apn) {
// separate the apn by type
apn.types.forEach(function(type) {
var cloneApn = _cloneApn(apn);
cloneApn.types = [type];
result.push(cloneApn);
});
return result;
}, []);
}
function _isMatchedApn(apn1, apn2) {
if (apn1 == null || apn2 == null) {
return false;
}
// Check if the type of apn1 is the subset of apn2
if (!apn1.types.every(function(type) {
return (apn2.types.indexOf(type) !== -1);
})) {
return false;
}
return APN_PROPS.every(function(prop) {
if (prop === 'types') {
// we've already check this property
return true;
} else {
return apn1[prop] === apn2[prop];
}
});
}
function _sortByCategory(apn1, apn2) {
if (apn1.category === ApnItem.APN_CATEGORY.PRESET) {
return true;
} else if (apn2.category === ApnItem.APN_CATEGORY.PRESET) {
return false;
} else {
return true;
}
}
function _clone(apn) {
return JSON.parse(JSON.stringify(apn));
}
function _loadJSON(path) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.onerror = function() {
reject('Failed to fetch file: ' + path, xhr.statusText);
};
xhr.onload = function() {
resolve(xhr.response);
};
xhr.open('GET', path, true); // async
xhr.responseType = 'json';
xhr.send();
});
}
return {
getOperatorCode: _getOperatorCode,
apnTypeFilter: _apnTypeFilter,
getDefaultApns: _getDefaultApns,
getCpApns: _getCpApns,
getEuApns: _getEuApns,
generateId: _generateId,
separateApnsByType: _separateApnsByType,
isMatchedApn: _isMatchedApn,
sortByCategory: _sortByCategory,
clone: _clone
};
});