1

Is there an efficient query to find all aggregate contacts for a specific account name & type?

The ContactsContract.Contacts table has the IN_VISIBLE_GROUP column which is effective for contact group membership. So, if the user has selected various group memberships for accounts, this column will be set.

There does not appear to be any equivalent in-visible-account column. However, the Android contacts app allows selecting a specific account to view without tapping "customize" and selecting groups. Doing this updates the ContactsContract.Settings table. The effect does not appear to reach the Contacts table.

I would like to be able to do one query and get one cursor back that has exactly the right set of aggregate (meaning from the Contacts table) contacts. Is this possible?

Peri Hartman
  • 19,314
  • 18
  • 55
  • 101
  • It appears that the Settings table is improperly used by the Android contacts app. Once you choose "customize", it appears to never update Settings.IN_VISIBLE_GROUP again, rendering the table useless for this purpose. I've observed this on several devices. – Peri Hartman Dec 21 '16 at 16:29

1 Answers1

2

find all aggregate contacts for a specific account name & type

Contacts are account specific, they're combined of multiple RawContacts, each can be saved on a different Account.

You can query for all the RawContacts for a specific Account:

Cursor c = cr.query(RawContacts.CONTENT_URI, 
   new String[]{ RawContacts._ID, RawContacts.CONTACT_ID, RawContacts._ID, ... },
   RawContacts.ACCOUNT_NAME + " = '" + accountName + "' AND " + RawContacts.ACCOUNT_TYPE + " = '" + accountType + "'", null, null);

If you want Contacts you can put all contact-ids you found in the above query in an array, and query on Contacts table:

Cursor c = cr.query(Contacts.CONTENT_URI, 
   new String[]{ Contacts._ID, Contacts.DISPLAY_NAME, ... },
   Contacts._ID + " IN (" + arrayOfIds + ")", null, null);
marmor
  • 27,641
  • 11
  • 107
  • 150
  • I gave you +1 because, in theory, I think your answer works. I considered this but didn't try it because I am concerned about the enormous potential size of "arrayOfIds". Do you think this is not a problem both for query limitations and speed efficiency? – Peri Hartman Dec 27 '16 at 17:11
  • I have something similar in my app, in rare cases there can be an issue with MySQL query size limitations, but that can be worked around by performing the second query in large batches, with speed I had no issues – marmor Dec 27 '16 at 19:41
  • Sounds reasonable. I'll mark this as the right answer because I doubt google provided for any better alternatives. – Peri Hartman Dec 27 '16 at 22:50