0

When updating a ContactsContract.Data StructuredName row for a RawContact, my code sets the DISPLAY_NAME column with a value provided by the user. It also updates the first, middle, last with values provided by the user.

When fetching the contact DISPLAY_NAME back from ContactsContract.Contacts, the display name provided to the RawContact is ignored and, instead, Android has fabricated one based on the StructuredName name parts.

Is there a way to tell ContactsContract to use the display name provided?

For example, consider that the following are written to a StructuredName row:

DISPLAY_NAME: F L
GIVEN_NAME: F
MIDDLE_NAME: X
LAST_NAME: L

In this case, I would expect the aggregate contact display name to be "F L". However, it will be "F X L".

Here is the code to write a StructureName row, where the values being set to each column are member variables:

protected void prepareUpdate (ArrayList<ContentProviderOperation> ops)
{
  String where = ContactsContract.Data._ID + " = " + dataId;

  ContentProviderOperation.Builder builder;
  builder = ContentProviderOperation.newUpdate (ContactsContract.Data.CONTENT_URI);
  builder.withSelection (where, null);
  builder.withValue (ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
  builder.withValue (ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName);
  builder.withValue (ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, givenName);
  builder.withValue (ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, familyName);
  builder.withValue (ContactsContract.CommonDataKinds.StructuredName.PREFIX, prefix);
  builder.withValue (ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName);
  builder.withValue (ContactsContract.CommonDataKinds.StructuredName.SUFFIX, suffix);
  builder.withValue (ContactsContract.CommonDataKinds.StructuredName.PHONETIC_FAMILY_NAME, phoneticFamilyName);
  builder.withValue (ContactsContract.CommonDataKinds.StructuredName.PHONETIC_MIDDLE_NAME, phoneticMiddleName);
  builder.withValue (ContactsContract.CommonDataKinds.StructuredName.PHONETIC_GIVEN_NAME, phoneticGivenName);

  ops.add (builder.build());
}

Here's the code that executes the "ops":

  ContentResolver resolver = context.getContentResolver();
  ContentProviderResult[] results = resolver.applyBatch(ContactsContract.AUTHORITY, ops);

And, here's the code to fetch the aggregate contact's display name:

public static ContactData fetchContact (Cursor cursor)
{
  String name = cursor.getString (cursor.getColumnIndex (ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
  ... do something with the feteched data ...
}

If Android is ignoring the display name provided by the user, what is the point of that column?

Peri Hartman
  • 19,314
  • 18
  • 55
  • 101

1 Answers1

0

If you look at the docs for DISPLAY_NAME it says:

The name that should be used to display the contact. Unstructured component of the name should be consistent with its structured representation.

This means that whenever you update DISPLAY_NAME, it updates the name parts (GIVEN_NAME, etc.), and whenever you update one of the parts, it updates the DISPLAY_NAME, so at all times the name parts and the DISPLAY_NAME are consistent.

I'm guessing they do this to avoid issues like a user fixes a wrong name set to a phone number from "John Doe" to "Bob Dylan", but the name parts aren't being updated so they remain with "John" and "Doe", this is a obviously an undesired situation.

marmor
  • 27,641
  • 11
  • 107
  • 150
  • Thank you, but that is subjective. What does "consistent" mean? And what are the consequences of being inconsistent? They don't explain either. Further, my app successfully saves whatever values the user puts into the structured name parts or the display name. There is no problem at this level. The problem is that the aggregate level is using the name parts, not the display name, to determine the aggregate display name. – Peri Hartman Mar 20 '17 at 14:49
  • you can experiment with it, try updating just the name parts, and see what happens to the display_name and vice versa... i agree that it is subjective, but it all depends on how the ContactsContract code is written, so at the very least it should be deterministic. – marmor Mar 21 '17 at 12:28
  • just another little note - see how Google's contact editor is behaving - you can either edit the display_name, or click on the expand button and only edit the name-parts, it doesn't let you edit both – marmor Mar 21 '17 at 12:30
  • Yes, I'm aware of that. Thanks. – Peri Hartman Mar 21 '17 at 14:55