4

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?

Random Human
  • 946
  • 1
  • 14
  • 31
nr1
  • 777
  • 3
  • 12
  • 31
  • It's been awhile - but what I remember from the `ContactAggregator` class is that a `Contact` will **not** contain two `RawContact`s from the *same* account - in that case it will actually split the other aggregate *also*. – Jens Jun 04 '12 at 11:55

2 Answers2

1

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

Sarang
  • 2,143
  • 24
  • 21
  • I have seen Google Contacts of the same account getting aggregated before, do you have source code or documentation to support your finding? – Ricardo Freitas May 09 '16 at 10:32
0

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.

Goofyahead
  • 5,874
  • 6
  • 29
  • 33
  • HI and thx for your answer. Can you post how you did it (or how to change the example above for getting it working)? – nr1 Jun 01 '12 at 11:58
  • 1
    I did some more hours of research now and discovered following: If the adding contacts code gets executed multiple times on android 2.0 - 2.2, only one contact is visible in the dialer app. From Android 2.3.X each insert creates a new contact in the dialer app. I tried this with 2.3.3, 3.0, 3.1, 4.0.3 => So it seems that below 2.3 the aggregation is done automatically, as written in the docs, but above this isn't working / foreseen anymore. – nr1 Jun 09 '12 at 11:13