How to load an address from a contacts directory?

182 Views Asked by At

I am trying to load addresses from a G Suite contacts directory (or an Exchange Global Address List) under Android. This directory does not implement the full ContactsContract but only e.g. CONTENT_FILTER_URI and maybe a few others. I can load the contact details, the phone numbers and the email addresses. But I cannot load the postal addresses. To get contact IDs I use this code:

// The directory I use in this example has the number 3:
String directoryId = "3";     

// A contact with the name "Test" is searched in directory "3":
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI, Uri.encode("Test")).buildUpon()
             .appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, directoryId)
             .build();

// The id of the first result is loaded:
Cursor c = cr.query(uri, new String[]{ContactsContract.Contacts._ID}, null, null, null);
c.moveToNext();
String id = c.getString(0);

Afterward I have the ID of the first contact in "id". Now I would like to load all postal addresses for that contact. However this code returns null instead of the cursor but only when I use directoryId "3":

Uri uri = ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI.buildUpon()
          .appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, directoryId)
          .build();
Cursor c = cr.query(uri, new String[]{ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS}, ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID+"=?", new String[]{id}, null);
c.moveToNext();
c.getString(0);

It works fine for directoryId "0". So it seems that the G Suite contacts directory does not implement ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI. However the Android contacts app is capable of loading the address of that contact.

What is the correct way to get the "StructuredPostal" objects for the contact from the contacts directory?

1

There are 1 best solutions below

6
On

UPDATE

There doesn't seems to be a way of getting anything except from the following tables: ContactsContract.Contacts, CommonDataKinds.Phone, CommonDataKinds.Email, as all others lack CONTENT_FILTER_URI to run queries on.