Contact sync adapter in android - android

I want to use sync adapter in my application to sync the native and third party contacts (except FB) with server. ( Only client to server one way sync)
I have two doubts here -
1) If there are multiple sync adapters in the device and If dirty bit is already cleared by another sync adapter , would my sync adapter be able to detect the contacts change/update immediately.
2) I have used
ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1);
ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true);
to sync the contacts with server .
It does start the sync in every 30 seconds .
But I want it to get notified immediately when ever contacts are added Or Deleted or Updated to start perfromSync operation.
Do I need to use observer too ?

In order to see changes to the contacts you will have to register an observer with the ContentResolver. The sync API is not intended to watch for every change, it is intended to be a background API that Android runs periodically automatically for you, so that it can batch operations and use the network more efficiently, not run when network is disabled, etc...
The dirty bit is used on a per account basis. In general the sync is designed to have an "owning" account per contact, and is not designed for exporting all the contacts from the device to a particular service. There is an aggregation process that handles joining contacts from multiple accounts into one "Contact" as seen by the user. See:
http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html
For more information on how this works.
If you intend to export all contacts on the device to your service you will need to walk the contacts and insert a raw contact for your service. You will then also want to watch the ContentResolver with an observer to see when the user adds a new contact on some service or periodically walk the table looking for contacts you have not yet exported.

use the version bit. If any contact edited
It automatically increased by 1.
so you create local database for compare previous version to current version. If any changes happen you trigger the update query.
for more information visit here.
This version helps for my sync updates. So I share this answer for all.

Related

How apps like Truecaller, messenger maintains sync with native contacts application if any contact changes in the native contact app

I have observed if we update/delete a the contact in native contacts application same changes is reflected into the TrueCaller instantly.
I can think of two ways it get the changes:
It refresh the whole contact list by deleting all the existing entries in app and query all the entries from native contact using contact provider every time app comes to foreground.But this approach might involve querying the RawContact table and Data table on every launch. (This might be a costly in terms of computation)
Each raw contact maintains VERSION which increments if any change is made to the raw contact via native app. This can be leveraged for modifications but will require to maintain the old values in the truecaller(or similar app) so that we can compare.This approach will still require you query the RawContacts table.
The later approach requires to maintain the Version on the TrueCaller app database as well.
Following operations will be required to detect any changes :
Check the version of the raw contact in the TrueCaller if the version is less than that of raw contact in native contacts db, then, query the corresponding Data table for all the changes.
If any raw contact is deleted from native then it's either removed from the RawContact table or marked Deleted. Thus we need to check for Deleted flag or if any pervious RawContact entry is missing in the RawContact table.
Other cases like a new contact is added hence a new raw contact entry in the RawContact table will be found etc.
Is there any better way to import the contact in your app and maintain proper synchronization if any operation like addition, deletion or modification is performed in the native app?
Certainly, app like TrueCaller does a great job with this. Any article on import contacts etc any relevant source will be appreciated.
As apps such as TrueCaller are closed source, there's no way to give you a definite answer, however option 2 would be my course of action to manage the sync with the device contacts.
You can add to that ContentObserver on the ContactsContract ContentProvider which will call your code whenever something changes so you can run the suggested code at option 2, however note that contacts tend to change very very very frequently, so I would limit such background syncs if they are needed to once a day to prevent hogging the battery/CPU.

Android contacts sync in background

I want to show the contacts whoever using my app. I know this is an old one but nothing satisfies me. I'm already using the method that gets all the contacts from phone and send to server and checking the available contacts who all are
using my app then return to my app.
Finally, it will save into the local database. But the problem is when adding new contact we have to sync again. For that time it will take more time and other functionality may become slow.
What I need is when I open the app at first time it should sync all the contacts and then it should monitor the changes like add, delete and update of the contacts then only it should check the changed contact.
I refers this blog, but there is no proper code. So many files are missed. How can i sync contacts?
finally,I have done with this library.

How to cancel contentresolver applyBatch?

I've built an application that requires to export 2000 contacts to native Android Contacts. I use contentresolver.applyBatch to make this happen, but if the user logs out I want to delete all of the apps exported contacts. It can occur that the contentresolver is not fully finished with saving the contacts.
My question:
How can I cancel the following opperation?:
resolver.applyBatch(ContactsContract.AUTHORITY, ops);
What I've tried so far:
I've tried to cancel the sync of the contentresolver like this:
ContentResolver.cancelSync(null, ContactsContract.AUTHORITY);
This doesn't work and I'm also not sure about the null value. It says it needs an account to stop syncing it for. But I'm not starting the sync on a specific account so why would it be needed?
What I need?
Something like resolver.stopBatch (a way to stop applying the batch).
Thank you for all your effort reading this.
If I understand the use case, you're syncing in new RawContacts into the device, when a user logs into your app.
If that's the case, you should create a SyncProvider, and have it sync in RawContacts on an account that is tied to your app and package name.
That way, when a user uninstalls your app, the account is automatically removed along with the data you've synced into the device, and when a user logs out of her account, you simply remove the contacts account programatically, which will also remove all data synced to the device.
Read these tutorials on how to create a contacts account, a SyncProvider and use it to sync contacts on your account:
http://blog.udinic.com/2013/04/24/write-your-own-android-authenticator/
http://blog.udinic.com/2013/07/24/write-your-own-android-sync-adapter/
https://developer.android.com/training/sync-adapters/creating-sync-adapter.html

Contacts backup and sync application

I am working on a contacts backup and sync.
The target is to send all details of all contacts to a server (custom protocol defined) as a backup.
During initial launch of application, all the contacts will be queued and sent to the server.
And after that, a background service will be running all time which will listen to new contact addition/ contact update, and this new/updated contact will be sent to server by the background service.
I am using RawContactsEntity for fetching the records.
I tried using ContentObserver on RawContacts/Data tables to get notification of contact addition or contact-change. But, AFAIK, the ContentObserver gives notification (onChange()) of changed data in table as a whole and not ID of individual record.
Now my problem is, how to get the exact id's of changed/new records?
I thought of creating a backup-table to compare with native contacts table and get the changed records. But as the number of contacts increase, the performance will decease drastically and this will hamper the battery life too.
Can you suggest me, The best way for achieving this contacts backup operation from performance and memory usage point of view?
Is there any other way for contacts sync operation?
It would be very helpful if anyone can share examples which can help me in this.
If the contacts are stored within your account(AccountManger), Android will mark the dirty flag in raw contacts. If it is not your account then you can not trust the dirty flag as the accounts sync adapter might have updated the contact to the server and reset the dirty flag. Your only option is to either re-upload the full contacts(simple and easy to code) or keep track of the version column in RawContacts and check which one has changed. It is actually not recommended to copy and upload contacts from other accounts as the corresponding sync adapter will anyways maintain a backup of those. Like Google will have a copy of Google contacts on their server.

Contact Detail syncing between people app and my app

I am involved in a project which has the functionality like whatsapp does for contacts.My app have to get the contact from phone(ie Peoples app) and sync it to server to find the users who are all already involved in my app.If a user not involved already i need to place a invite button.
For sync there is no problem i implemented a sync adapter,I have to get the updates like new phone no added or name changed in the contacts of people app.
I think VERSION and CONTACT_LAST_UPDATED_TIMESTAMP fields are not exactly what i need.Because they may be updated when other details in the
are changed.
I need to monitor the contacts change when my app starts,if there any changes in contacts i need to get that contact details and send it to server to know his status.Is running a service with content observer
will work for this case.
If it works how can i get the updated contact details.
Could someone help me?

Categories

Resources