Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Listening to contact changes like WhatsApp do

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.

like image 653
Hagai L Avatar asked Jun 28 '15 18:06

Hagai L


People also ask

How does apps like WhatsApp show contacts that are only using the app?

In apps that use phone authentication like whatsapp: When any user opens to check his/her contacts, whatsapp will show only the contacts from your phone that are using whatsapp (like they filter your contacts).

What does WhatsApp do with contacts?

When you use contact upload and grant WhatsApp access to your device address book, WhatsApp will access and upload the phone numbers in your address book typically daily, but this depends on various factors including how often a user uses WhatsApp, including those of WhatsApp users and your other contacts.


1 Answers

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.

like image 90
RocketRandom Avatar answered Oct 21 '22 06:10

RocketRandom