UNPKG

nativescript-contacts-lite

Version:

NativeScript plugin providing pretty fast read-only access to the iOS and Android contact directory

113 lines (94 loc) 5.54 kB
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 }); } });