-
Notifications
You must be signed in to change notification settings - Fork 7
/
contacts.android.js
112 lines (94 loc) · 5.42 KB
/
contacts.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
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
let helper = require("./contacts-helper");
/*
if running inside a web worker, need to pass debug messages to main thread as workers do not have console access
*/
let console_log = (msg) => { postMessage({ type: 'log', message: msg }); }
let console_dump = (msg) => { postMessage({ type: 'dump', message: msg }); }
/*
fields: desired fields to retrieve from phone storage backend
searchTerm: search only for contacts whose display_name start with this term
debug whether to print debug messages to the console
worker: wether we are running inside a web worker
*/
exports.getContactsFromBackend = ((fields,searchTerm,debug,worker) => {
try {
/* variables used in android backend query */
let content_uri = android.provider.ContactsContract.Data.CONTENT_URI, // The content URI of the words table
columnsToFetch = helper.getAndroidQueryColumns(fields), // The columns to return for each row
selectionClause = helper.getSelectionClause({ // Either null, or and SQL like WHERE clause
fields: fields,
searchTerm: searchTerm
}),
selectionArgs = helper.getSelectionArgs({ // Either null, or an array of string args for selection clause
fields: fields,
searchTerm: searchTerm
}),
sortOrder = null; // The sort order for the returned rows
/* load raw data from android backend */
var timer = new Date().getTime();
let c = helper.getAndroidContext().getContentResolver().query(content_uri, columnsToFetch, selectionClause, selectionArgs, sortOrder);
if (debug && worker) { console_log(`Querying storage backend completed in ${(new Date().getTime() - timer)} ms!`); }
if (debug && !worker) { console.log(`Querying storage backend completed in ${(new Date().getTime() - timer)} ms!`); }
/*
transform raw data into desired data structure
moveToNext() moves the row pointer to the next row in the cursor
initial position is -1 and retrieving data at that position you will get an exception
*/
let contacts = []; // array containing contacts object to return
var timer = new Date().getTime();
while (c.moveToNext()) {
let contact_id = helper.getColumnValue(c,columnsToFetch.indexOf("contact_id"));
// see if contact_id already exists in contacts to pass existing object for appending
let existingContactObj = undefined;
let existingContactIndex = contacts.findIndex((item,index) => { return item.contact_id === contact_id });
if (existingContactIndex > -1) { existingContactObj=contacts[existingContactIndex]; }
let contact = helper.convertNativeCursorToContact(c,fields,columnsToFetch,existingContactObj);
if (existingContactIndex > -1) { contacts[existingContactIndex] = contact; }
else { contacts.push(contact); }
}
c.close();
if (debug && worker) { console_log(`Processing data completed in ${(new Date().getTime() - timer)} ms!`); }
if (debug && !worker) { console.log(`Processing data completed in ${(new Date().getTime() - timer)} ms!`); }
return({ type: 'result', message: contacts });
} catch (e) { return({ type: 'error', message: e }); }
});
/*
fields: desired fields to retrieve from phone storage backend
searchTerm: search only for contacts whose display_name start with this term
debug whether to print debug messages to the console
worker: wether we are running inside a web worker
*/
exports.getContactFromBackendById = ((contactId,fields,debug) => {
try {
/* variables used in android backend query */
let content_uri = android.provider.ContactsContract.Data.CONTENT_URI, // The content URI of the words table
columnsToFetch = helper.getAndroidQueryColumns(fields), // The columns to return for each row
selectionClause = helper.getSelectionClause({ // Either null, or and SQL like WHERE clause
fields: fields,
contactId: contactId
}),
selectionArgs = helper.getSelectionArgs({ // Either null, or an array of string args for selection clause
fields: fields,
contactId: contactId
}),
sortOrder = null; // The sort order for the returned rows
/* load raw data from android backend */
var timer = new Date().getTime();
let c = helper.getAndroidContext().getContentResolver().query(content_uri, columnsToFetch, selectionClause, selectionArgs, sortOrder);
if (debug) { console.log(`Querying storage backend completed in ${(new Date().getTime() - timer)} ms!`); }
/*
transform raw data into desired data structure
moveToNext() moves the row pointer to the next row in the cursor
initial position is -1 and retrieving data at that position you will get an exception
*/
let contactObj = undefined; // contact object to return
var timer = new Date().getTime();
while (c.moveToNext()) {
let contact = helper.convertNativeCursorToContact(c,fields,columnsToFetch,contactObj);
contactObj = contact;
}
c.close();
if (debug) { console.log(`Processing data completed in ${(new Date().getTime() - timer)} ms!`); }
return({ type: 'result', message: contactObj });
} catch (e) { return({ type: 'error', message: e }); }
});