-
Notifications
You must be signed in to change notification settings - Fork 32
/
get-contacts-by-name-worker.android.js
36 lines (33 loc) · 1.27 KB
/
get-contacts-by-name-worker.android.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
require('globals'); // necessary to bootstrap tns modules on the new thread
var Contact = require("./contact-model");
var helper = require("./contact-helper");
/* pass debug messages to main thread since web workers do not have console access */
function console_log(msg) { postMessage({ type: 'debug', message: msg }); }
function console_dump(msg) { postMessage({ type: 'dump', message: msg }); }
self.onmessage = (event) => {
try {
var Contacts = android.provider.ContactsContract.Contacts,
SELECTION = android.provider.ContactsContract.ContactNameColumns.DISPLAY_NAME_PRIMARY;
var c = helper.getContext().getContentResolver().query(
Contacts.CONTENT_URI,
null,
SELECTION + " like ?",
["%" + event.data.searchPredicate + "%"],
null
);
if(c.getCount() > 0){
var cts = [];
while(c.moveToNext()){
var contactModel = new Contact();
contactModel.initializeFromNative(c,event.data.contactFields);
cts.push(contactModel);
}
c.close();
postMessage({ type: 'result', message: { data: cts, response: "fetch" }});
}
else{
c.close();
postMessage({ type: 'result', message: { data: null, response: "fetch" }});
}
} catch (e) { postMessage({ type: 'result', message: e }); }
}