This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
import_sim_contacts.js
237 lines (205 loc) · 6.41 KB
/
import_sim_contacts.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
/*jshint loopfunc: true */
/* global LazyLoader, utils, contacts */
/* exported SimContactsImporter */
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
'use strict';
/**
* Imports contacts stored in the SIM card and saves them into
* navigator.mozContacts. Three steps => three callback as arguments:
* - onread: SIM card has been read properly;
* - onimport: A Contact has been imported
* - onfinish: contacts have been saved into navigator.mozContacts;
* - onerror: SIM card is empty or could not be read.
*/
function SimContactsImporter(targetIcc) {
if (targetIcc === null) {
throw new Error('We need an icc to continue with this operation');
}
var pointer = 0;
var CHUNK_SIZE = 5;
var numResponses = 0;
var self = this;
var mustFinish = false;
var loadedMatch = false;
var DEFAULT_TEL_TYPE = 'other';
var icc = targetIcc;
var iccId = icc.iccInfo && icc.iccInfo.iccid;
var numDupsMerged = 0;
function generateIccContactUrl(contactid) {
var urlValue = 'urn:' + 'uuid:' + (iccId || 'iccId') + '-' + contactid;
return [{
type: ['source', 'sim'],
value: urlValue
}];
}
function notifyFinish() {
if (typeof self.onfinish === 'function') {
window.setTimeout(self.onfinish, 0, numDupsMerged);
}
}
function notifyImported() {
if (typeof self.onimported === 'function') {
window.setTimeout(self.onimported, 0);
}
}
function continueCb() {
numResponses++;
pointer++;
notifyImported();
if (pointer < self.items.length && numResponses === CHUNK_SIZE) {
numResponses = 0;
mustFinish ? notifyFinish() : importSlice(pointer);
}
else if (pointer >= self.items.length) {
notifyFinish();
}
}
function startMigration() {
if (!mustFinish && Array.isArray(self.items) && self.items.length > 0) {
importSlice(0);
}
else {
notifyFinish();
}
}
function onContactsReadyForImport() {
if (typeof self.onread === 'function') {
// This way the total number can be known by the caller
self.onread(self.items.length);
}
if (loadedMatch) {
startMigration();
}
else {
document.addEventListener('matchLoaded', function mloaded() {
document.removeEventListener('matchLoaded', mloaded);
startMigration();
});
}
}
this.start = function() {
numDupsMerged = 0;
if (mustFinish) {
notifyFinish();
return;
}
LazyLoader.load([
'/shared/js/simple_phone_matcher.js',
'/shared/js/contacts/contacts_matcher.js',
'/shared/js/contacts/contacts_merger.js',
'/shared/js/contacts/utilities/image_thumbnail.js',
'/shared/js/contacts/merger_adapter.js'
], function loaded() {
loadedMatch = true;
document.dispatchEvent(new CustomEvent('matchLoaded'));
});
var requestAdn, requestSdn;
// request contacts with readContacts() -- valid types are:
// 'adn': Abbreviated Dialing Numbers
// 'fdn': Fixed Dialing Numbers
// 'sdn': Service Dialing Numbers
if (icc && icc.readContacts) {
requestAdn = icc.readContacts('adn');
} else {
throw new Error('Not able to obtain a SIM import function from platform');
}
requestAdn.onsuccess = function onsuccess() {
if (mustFinish) {
notifyFinish();
return;
}
self.items = requestAdn.result || [];
requestSdn = icc.readContacts('sdn');
requestSdn.onsuccess = function onsuccess() {
if (mustFinish) {
notifyFinish();
return;
}
if (Array.isArray(requestSdn.result)) {
self.items = self.items.concat(requestSdn.result);
}
onContactsReadyForImport();
};
requestSdn.onerror = function error() {
if (mustFinish) {
notifyFinish();
return;
}
console.warn('Could not read SDN Contacts from SIM Card', error.name);
onContactsReadyForImport();
};
};
requestAdn.onerror = function error() {
if (typeof self.onerror === 'function') {
self.onerror(requestAdn.error);
}
};
};
this.finish = function() {
mustFinish = true;
};
/**
* store mozContact elements -- each returned mozContact has two properties:
* .name : [ string ]
* .tel : [{ number: string, type: string }]
* The 'name' property is only related to the mozContact element itself --
* let's use it as the default 'givenName' value.
*/
function importSlice(from) {
for (var i = from; i < from + CHUNK_SIZE && i < self.items.length; i++) {
var item = self.items[i];
var parsedName = utils.misc.parseName(item.name[0]);
item.givenName = [parsedName.givenName];
item.familyName = [parsedName.familyName];
if (Array.isArray(item.tel)) {
var telItems = [];
for (var j = 0; j < item.tel.length; j++) {
var aTel = item.tel[j];
// Filtering out empty values
if (aTel.value && aTel.value.trim()) {
aTel.type = [DEFAULT_TEL_TYPE];
telItems.push(aTel);
}
}
item.tel = telItems;
}
item.category = ['sim'];
item.url = generateIccContactUrl(item.id);
delete item.id;
// Item is presumably a mozContact but for some reason if
// we don't create a new mozContact sometimes the save call fails
var contact = utils.misc.toMozContact(item);
var cbs = {
onmatch: function(results) {
var mergeCbs = {
success: function() {
numDupsMerged++;
continueCb();
},
error: function(e) {
window.console.error('Error while merging: ', e.name);
continueCb();
}
};
contacts.adaptAndMerge(this, results, mergeCbs);
}.bind(contact),
onmismatch: function() {
saveContact(this);
}.bind(contact)
};
contacts.Matcher.match(item, 'passive', cbs);
}
} // importSlice
function saveContact(contact) {
var req = window.navigator.mozContacts.save(contact);
req.onsuccess = function saveSuccess() {
continueCb();
};
req.onerror = function saveError() {
console.error('SIM Import: Error importing ', contact.id,
req.error.name);
continueCb();
};
}
}