From 288972fd79234c4a90de1e6aac060a313cddfe6d Mon Sep 17 00:00:00 2001 From: tafelnl <35837839+tafelnl@users.noreply.github.com> Date: Thu, 19 Jan 2023 14:06:44 +0100 Subject: [PATCH] refactor(android): make querying contacts consistent with ios --- .../community/contacts/Contacts.java | 36 +++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/android/src/main/java/getcapacitor/community/contacts/Contacts.java b/android/src/main/java/getcapacitor/community/contacts/Contacts.java index cba24a1..c324cb1 100644 --- a/android/src/main/java/getcapacitor/community/contacts/Contacts.java +++ b/android/src/main/java/getcapacitor/community/contacts/Contacts.java @@ -134,16 +134,42 @@ public ContactPayload getContact(@NonNull String contactId, GetContactsProjectio public HashMap getContacts(GetContactsProjectionInput projectionInput) { String[] projection = projectionInput.getProjection(); - // String[] selectionArgs = projectionInput.getSelectionArgs(); + String[] selectionArgs = projectionInput.getSelectionArgs(); - // String selection = GetContactsProjectionInput.getSelection(selectionArgs); + String selection = GetContactsProjectionInput.getSelection(selectionArgs); HashMap contacts = new HashMap<>(); ContentResolver cr = this.mActivity.getContentResolver(); - // Cursor cursor = contentResolver.query(ContactsContract.Data.CONTENT_URI, projection, selectionArgs.length > 0 ? selection : null, selectionArgs.length > 0 ? selectionArgs : null, null); - Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI, projection, null, null, null); + if (selectionArgs.length > 0) { + // At least some fields (besides the contactId) are being queried. + // First we'll retrieve all contacts having at least one of these fields (this is called the selection). + // Also we only retrieve the requested fields (and the contactId field) (this is called the projection). + Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI, projection, selection, selectionArgs, null); + moveOverCursor(contacts, cursor); + + // Now that we have all the contacts matching the selection and projection, + // we'll also want to retrieve the remaining contacts. + // Since there is no way really to retrieve "the remaining" contacts efficiently, + // we'll just retrieve them all. + // Also since these contacts apparently do not have any matching fields, + // we only need to retrieve the contactId field. + String[] contactIdOnlyProjection = new String[] { ContactsContract.Data._ID, ContactsContract.Data.CONTACT_ID }; + Cursor cursorAllContacts = cr.query(ContactsContract.Data.CONTENT_URI, contactIdOnlyProjection, null, null, null); + moveOverCursor(contacts, cursorAllContacts); + } else { + // No specific fields are being queried. + // This can happen if the projection is empty. + // Probably the user only wants a list of all contacts and their contactId returned. + Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI, projection, null, null, null); + moveOverCursor(contacts, cursor); + } + + return contacts; + } + + private void moveOverCursor(HashMap contacts, Cursor cursor) { if (cursor != null && cursor.getCount() > 0) { while (cursor.moveToNext()) { // String _id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); @@ -167,8 +193,6 @@ public HashMap getContacts(GetContactsProjectionInput pr if (cursor != null) { cursor.close(); } - - return contacts; } public String createContact(CreateContactInput contactInput) {