10

I'm building an App which relies heavily on the user's contacts.

I've created an Account and create RawContacts on behalf of this account when needed.
And I use SyncAdapter and things are great.

But I'm still missing some parts of this puzzle:

I'm trying to implement a behavior similar to WhatsApp. Such that-

When a change happens in the name or phone of any contact - it would be sent to the server for evaluation.

Whether this contact was already in "my" RawContacts, whether he was just created now by the user.

I know there's the ContentObserver but tracking ContactsContract.Contacts.CONTENT_URI seems like the wrong thing to do because it doesn't give the specific change, plus it gets raised to many times, and from to many events that don't interest me.

I know that WhatsApp are using SyncAdapter but I think they might be doing something more.

Any idea would be much appreciated.

Hagai L
  • 1,593
  • 1
  • 18
  • 40
  • @Hagai...hello any update about how you tackled this ?? I have used CONTACT_LAST_UPDATED_TIMESTAMP in my code to get the latest updated contacts but this stamp does not change if you update any field in a contact which has only email id with it. – Mohit Mathur Feb 27 '17 at 08:59
  • @Hagai... hello... it would be great if you could share your findings by answering your own question if you have succeeded in solving it. – Aliton Oliveira Aug 02 '19 at 22:29
  • Hi, years have passed... and as far as I remember - I didn't get an answer that was good enough for what I've needed. – Hagai L Aug 18 '19 at 13:07

1 Answers1

1

ContentObserver tracking ContactsContract.Contacts.CONTENT_URI is indeed the way to go.

When you get onChange() you query the raw_contacts table with following condition:

String where = "(rawcontacts.dirty = true or rawcontacts.deleted = 1) and rawcontacts.account_type = <your_custom_type>"

The result will give you contacts added, updated or deleted.

If you are interested in knowing more details - the reason why you need to subscribe to ContactsContract.Contacts.CONTENT_URI is that the contacts provider currently does not notify which contacts were modified in onChange(). This is because of the applyBatch() transactions where multiple contacts may change. May be in future there will be a way to subscribe to a subset - but currently there is none.

RocketRandom
  • 1,102
  • 7
  • 20