How can I get a contact's version or modified time? - android

I'm trying to figure out if there is a property in Android contacts that states if a contact has been modified.
I read that it was possible to read a contact's version via the field ContactsContract.Contacts.Entity.VERSION, but I wasn't able to retrieve its value. I always get an IllegalArgumentException.
Does someone know how to get the contact's version or if there is another way to find out if a contact has been modified since the last scanning of the address book?

Heres the code, I havent tested it, as I was busy with my work.
public void getVersion() {
//specify which fields of RawContacts table to be retrieved.
String[] projection = new String[] {RawContacts.VERSION};
//query the RawContacts.CONTENT_URI
Cursor cur = mContext.getContentResolver().query(RawContacts.CONTENT_URI, projection,null,null,null);
while(cur.moveToNext()){
String version = cur.getString(cur.getColumnIndex(RawContacts.VERSION));
Log.i("VersionRetriever", version);
}
//Always remember to close the cursor. Otherwise it leads to side-effects.
cur.close();
}

try RawContacts.CONTACT_STATUS_TIMESTAMP instead of RawContacts.VERSION.

If you want to know if a specific data type (like phone or name), was changed you can check Data.DATA_VERSION (but on Data.CONTENT_URI).

Related

Android: Get google's unique contact ID from ContactProvider database

I am working on a requirement, where I need to identify all Google's contact saved/synced with Android device's phonebook. Then I have to fetch unique contact Id (Google's unique contact id)of each contact which will be same on other devices and other platform.
I have read Android developer's documentation regarding RAW_CONTACT_ID. Also, tried to get raw contact id, but I am getting different value of raw contact id on other devices.
If anyone can put me on right direction, it will really helpful.
If require more information, please ask.
Try using ContactsContract.PhoneLookup
A table that represents the result of looking up a phone number, for example for caller ID. To perform a lookup you must append the number you want to find to CONTENT_FILTER_URI. This query is highly optimized.
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME,...
where
PhoneLookup._ID
is what you're looking for.
You may also try the solution provided in this post:
public static int getContactIDFromNumber(String contactNumber,Context context)
{
contactNumber = Uri.encode(contactNumber);
int phoneContactID = new Random().nextInt();
Cursor contactLookupCursor = context.getContentResolver().query(Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,contactNumber),new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, null, null, null);
while(contactLookupCursor.moveToNext()){
phoneContactID = contactLookupCursor.getInt(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup._ID));
}
contactLookupCursor.close();
return phoneContactID;
}
All _ID values in Android's Contacts are local, they are usually incremental, and are not synced between devices.
The values that might get synced by the app's SyncAdapter (in this case Google's SyncAdapter) are SYNC1, SYNC2, SYNC3, SYNC4.
However, note that these fields are not guaranteed to do anything, and the SyncAdapter may use them for whatever purpose it needs, usually, one of them is used as a "server identifier" you just need to print them, and check manually which one.

Detect if an android contact has been deleted

I am trying to maintain a contact database and get a callback for Add/Update/Delete as soon as something changes in the URI.
I have written a ContentObserver to observe on ContactsContract.Contacts.CONTENT_URI on contacts. I get a callback as soon as a contact changes and then I update my database by checking ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP.
While this works fine for add/update, it does does not work for deleting a contact.
I do not want to parse all the contacts that I have in memory and check against android database. That would take time and CPU.
I know there exists many question of these types but I am not able to figure out things specific to deleting the contact.
Does there exist a way to perform this ?
As I have posted in above comment as well, following code works for API level 18 and above.
You can query on a uri ContactsContract.DeletedContacts.CONTENT_URI to get the list of all the contacts that have been deleted.
My query looks like following :
String selection = ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP + " > ?";
String[] selectionArgs = new String[]{String.valueOf(mLastContactDeleteTime)};
Cursor cursor = mContext.getContentResolver().query(ContactsContract.DeletedContacts.CONTENT_URI, null, selection, selectionArgs, null);

Android - get contact where phone number LIKE something being typed

I've been trying to get a contact by it's number, the same way Android has implemented when your dialing a number on the stock dialer. If you're familiar, you know that when you are dialing it starts to show matches that contain those numbers (913284912 would match when dialing 3412 for example). (not sure if this way of search was only implemented on 5.x+)
I got this to work but only when the number is exactly equal to the contact one with the code below:
private Cursor getContacts(String number) {
// Run query
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number.trim()));
String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup.PHOTO_THUMBNAIL_URI, ContactsContract.PhoneLookup._ID};
return getContext().getContentResolver().query(
uri,
projection,
null,
null,
null
);
}
I'm having a bit difficulty on finding solutions for this, as everyone uses this to get a contact. But as I don't want an exact match, this won't do for me.
Thanks in advance.
You can use an Adapter with a Filter. Some examples can be found: Filtering ListView with custom (object) adapter or even better Custom Listview Adapter with filter Android

Android native Contacts application deletes every account contacts

My problem is that I try to disallow to native Android Contacts application to delete my application's contacts (which are specified by my application account type) from device.
For that I've specified my own SyncAdapter with it's own Service and it's meta-data described in syncadapter.xml. The value of supportsUploading is set to false (this way I say that contacts created by my application are read-only)
However, when I try to delete a contact of my application from standard Contacts app I get the message which says:
You can't delete contacts from read-only accounts, but you can hide
them in your contacts list
Everything seems fine until I try to get data of the contact which I previously deleted ("hide") from standard "Contacts" in my own application.
The returned cursor is null because there isn't any row in Data table associated with this contact's RAW_CONTACT_ID. I also check if the contact exists on the device looking for
it's DELETED flag value in RawContacts table and observed that it has been set to 1 which means that contact has been deleted.
As official documentation describes:
Some sync adapters are read-only, meaning that they only sync
server-side changes to the phone, but not the reverse. If one of those
raw contacts is marked for deletion, it will remain on the phone.
However it will be effectively invisible, because it will not be part
of any aggregate contact.
So the problem is that I can't display this contact's data in my application any more because I don't know how to retrieve them from contacts database. If somebody knows how to handle this situation I would appreciate any advice. Thanks
So after more meticulous search I found the way to retrieve data for any RAW_CONTACT in my application independently of was it deleted from some other application or not.
Using of RawContacts.Entity API does this job.
Previously I tried to retrieve contact's data using such logic:
public Cursor getContactData(long rawContactId) {
return getContentResolver().query(ContactsContract.Data.CONTENT_URI, null,
ContactsContract.Data.RAW_CONTACT_ID + "=" + rawContactId, null);
}
And this method always returned null for deleted contact.
But using RawContacts.Entity such way:
public Cursor getContactData(long rawContactId) {
Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, id);
rawContactUri = Uri.withAppendedPath(rawContactUri, RawContacts.Entity.CONTENT_DIRECTORY);
return getResolver().query(rawContactUri, null, null, null);
}
allows to fetch contact's data inside application with appropriate authority regardless was it deleted by 3d-party application or not.

How to retrieve the number of new contacts added to the contacts database

is there a column in the database of contacts indicates the date added to the base or the last modification date, because I want a method used to retrieve the new add
I found SEVERAL method but I did not understand, I am new to android
Edit : Means how to get the newly updated contacts.
you want to retrieve the number of new contacts since when?
if you adding contacts with your app then just get the number of contacts before you add any and save that value.
then after the user has added contacts get the number again and find the difference between the previous number of contacts. that will be the number of new contacts, but the best way to do it would just be to keep an internal counter of when the user adds contacts.
I think you are looking for something like this, You want to get the updated contacts.
have look here....
Contacts ContentObserver called randomly
Android notify when phone book is updated(Content Observer)
ContentObserver for listening contact changes
Just make this change to your cursor query
Cursor cursor = getActivity().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, "_id DESC");

Categories

Resources