1

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?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Dominique
  • 2,356
  • 1
  • 19
  • 19
  • once you got the contact-id, have you tried querying for it in the standard ContactsContacts DB? i.e. `Uri uri = StructuredPostal.CONTENT_URI;` – marmor Nov 29 '17 at 08:07
  • Yes, I have tried that. But the content-id does not exist in the default directory. When I omit the "DIRECTORY_PARAM_KEY" and just access StructuredPostal.CONTENT_URI I get a cursor but "c.moveToNext()" returns false, i.e. there isn't a row for that content-id. That's probably because when omitting the "DIRECTORY_PARAM_KEY" the default directory is queried and that directory does not contain any information about that contact. Only the directory "3" (G Suite) knows the contact. – Dominique Nov 30 '17 at 11:00

1 Answers1

0

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.

marmor
  • 27,641
  • 11
  • 107
  • 150
  • No, unfortunately the "cr.query" returns a null pointer. It works fine for directoryId 0 (when adding the missing quotes around "StructuredPostal.CONTENT_ITEM_TYPE" to get valid SQL) but when using it with directoryId 3 a null pointer is returned. So it seems that G Suite also does not implement ContactsContract.Data.CONTENT_URI . According to https://developer.android.com/reference/android/provider/ContactsContract.Directory.html they have to implement only Contacts.CONTENT_FILTER_URI. But I cannot figure out how the contacts app gets the address from the G Suite directory. :-( – Dominique Nov 30 '17 at 14:28
  • How can I query ContactsContract.Contacts.Entity? It has no Uri. Do you mean "RawContacts" instead, which seems to have the triplet? No, if I access RawContacts.CONTENT_URI without a directoryId, I get an empty cursor (because the contact-id was not found) and if I use the directoryId, I get a null pointer instead of the Cursor (again, it seems that G Suite does not implement that Uri). I think RawContacts contains only local contacts, not GAL/GSuite contacts. – Dominique Dec 07 '17 at 11:49
  • And the source code you mentioned gets its Uri from somewhere else, I have followed it to QuickContactsActivity where it seems to get it from another activity in the onActivityResult function. Were you able to get the "Contacts" app compiled and running? So that one could check which Uri is used? Regarding the RawContacts table I have found this post which might be interesting but doesn't help in this case: https://stackoverflow.com/questions/8788053/modifying-contact-information/8802659#8802659 It is a pity that there seems to be no documentation about this problem anywhere. – Dominique Dec 07 '17 at 11:52
  • No, i meant `ContactsContract.Contacts.Entity`, you don't query it via CONTENT_URI, but by adding `Entity.CONTENT_DIRECTORY` to a contactUri, for the `contactUri` part, see the code I've added above – marmor Dec 07 '17 at 12:07
  • I have tried your new code but I get the same result. If I don't use directoryId the system cannot find anything with that contact-id and returns a Cursor but no content. If I add .appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, directoryId) then I get a null pointer instead of a Cursor. :-( – Dominique Dec 07 '17 at 12:17
  • finally had some time to play with this problem myself, and try out some code, and I can agree that it doesn't seems to have any means of getting anything other then emails or phones due to lack of `CONTENT_FILTER_URI` in other Data types – marmor Mar 29 '18 at 14:33