Automatic contact aggregation not working when inserting a new contact

1k views Asked by At

I'm inserting 2 times the same contact to the Android emulator (2.3.3) with the following code:

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
   .withValue(RawContacts.AGGREGATION_MODE, RawContacts.AGGREGATION_MODE_DEFAULT)
   .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
   .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build());

// structuredname
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
   .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
   .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
   .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "Test")
   .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, "Tester")
   .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, "Test")
   .build());

// PHONE
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
   .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
   .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
   .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, "0")
   .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, "12345678").build());

try {
  getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
  e.printStackTrace();
} catch (OperationApplicationException e) {
  e.printStackTrace();
}

According to my understanding Android should normally aggregate the contacts automatically, making one entry of my 2 inserts. However this doesn't happen, I can see 2 contacts with the same data in the dialer app.

Any ideas?

2

There are 2 answers

2
Goofyahead On

Hi @nr1 i had the same problem with a similar code that added a custom type to an existing contact, and i realized that if you add two time the same data to a contact it gets separated.

as API documentation says:

Automatic aggregation is not permanent; any change of a constituent raw contact may create a new aggregate or break up an existing one.

I dont know why but its always like that, if you do it more times more empty contacts with data are created (in my case).

Im now looking if the contact already exists so custom data is not added again.

1
Sarang On

Aggregation only works across inserted contacts from different accounts. If you insert two raw contacts from the same account, they will not be aggregated.