Get cross-device unique ID for Android phone contacts - android

I need to query the contacts from an Android device for a project I'm working on and I need to save them in a way I can link between the instance in the app to the contact in the phonebook.
I found that the CONTACT_ID (which is a reference to _ID) of each contact might change between devices, so if I switch to other Android device that ID will not be valid.
A temp solution was using the contact's SOURCE_ID, which is a String that uniquely identifies this row to its source account. The solution was pretty good, because if the contact came from (for example) the Google account, it will stay the exact same ID on every device I'll have. The problem is - not every contact has a SOURCE_ID.
It is also possible to query a specific contact using it's data as filters, which may work as a unique ID, such as his phone number, etc... However every piece of data has a flaw. For example: A contact may have multiple phone numbers (which is still ok) and the numbers can be varied (for example: 202-555-0105 is the same as +1-202-555-0105 which is also the same as (202) 555 0105 and also 2025550105).Edit: Also not every contact has a phone number, so then what?
So after given the problem -
How can I get a unique ID for the contacts in the Android phonebook so they'll be the same cross-device?
Note: It's possible on IOS by default (see documentation) -
Contacts in different accounts that represent the same person may be automatically linked together. Linked contacts are displayed in OS X and iOS apps as unified contacts. A unified contact is an in-memory, temporary view of the set of linked contacts that are merged into one contact.
By default the Contacts framework returns unified contacts. Each fetched unified contact (CNContact) object has its own unique identifier that is different from any individual contact’s identifier in the set of linked contacts. A refetch of a unified contact should be done with its identifier.

What you are looking for is LOOKUP_KEY
An opaque value that contains hints on how to find the contact if its row id changed as a result of a sync or aggregation.
To get the contact LOOKUP_KEY loop through your contacts, here's an example:
Note: <uses-permission android:name="android.permission.READ_CONTACTS" /> is required.
val contentUri = ContactsContract.Contacts.CONTENT_URI
val cursor = context?.contentResolver?.query(contentUri, null, null, null, null)
if (cursor != null) {
while (cursor.moveToNext()) {
val id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID))
val name =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY))
val lookupKey =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY))
}
}
cursor?.close()
Here is how you would go about retrieving the contact with the LOOKUP_KEY:
val lookupKey = "0r1-3C263544104632"
val lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey)
val uri = ContactsContract.Contacts.lookupContact(contentResolver, lookupUri)
val cursor = context?.contentResolver?.query(uri, null, null, null, null)
if (cursor != null) {
while (cursor.moveToNext()) {
val id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID))
val name =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY))
}
}
cursor?.close()
}
Please note that contacts have to be synchronised in order for the LOOKUP_KEY not to be re-generated.
The LOOKUP_KEY is guaranteed to be generated for each contact, however, if the account is not set up and not synchronised then the LOOKUP_KEY will be re-generated whenever the contact gets modified.
With that in mind, you'll always have a unique LOOKUP_KEY if the device is synchronised. The LOOKUP_KEY is relying on Google Cloud which may be the same solution that Apple uses.
It will be very unlikely that an Android device will not have a google account since most Android users rely on Google services.
I am afraid this is the best way to have a unique identifier, however, if you'd like, you could hash user phone number combined with other contact details, but this method can not be guaranteed to work as contacts may change. If your users are registered and you'll have their information then you could check on the backend which hash values match your expectation and then work based on your own synchronisation.
If you want to play around, I have created a sample app with the implementation where you can look through your contacts and find the lookup key as well as retrieve the contact with the lookup key.
I would also recommend you to take a look at SyncAdapter.

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.

How to uniquely identify a contact on ContactsContract.Contacts table

I have an app that gets the ContactsContract.Contacts.LOOKUP_KEY of a contact on the device and saves it on the app Db.
After reading this page I thought I could use the LOOKUP_KEY to uniquely identify a contact, even when a contact is edited (for example after editing the name of the contact).
Actually I saw that after editing a contact, its LOOKUP_KEY changes, so I cannot use anymore the LOOKUP_KEY I saved on my app DB.
My question is: is there a way to uniquely identify a contact on ContactsContract.Contacts from when it is created for the first time on the device until it is deleted from the device?
Thank you
A LOOKUP_KEY is not meant to be used as a key on its own, instead it should be used together with a contact's _ID to form a full lookupUri.
The lookupUri can then be used to find a contact in CONTENT_LOOKUP_URI tables.
The CONTENT_LOOKUP_URI basically first looks for the contact by _ID, if it fails to find it, or the _ID seems like the wrong contact, it uses hints from the LOOKUP_KEY part to try and track down the correct contact for you.
From CONTENT_LOOKUP_URI
A content:// style URI for this table that should be used to create
shortcuts or otherwise create long-term links to contacts. This URI
should always be followed by a "/" and the contact's LOOKUP_KEY. It
can optionally also have a "/" and last known contact ID appended
after that. This "complete" format is an important optimization and is
highly recommended.
As long as the contact's row ID remains the same, this URI is
equivalent to CONTENT_URI. If the contact's row ID changes as a result
of a sync or aggregation, this URI will look up the contact using
indirect information (sync IDs or constituent raw contacts).
Lookup key should be appended unencoded - it is stored in the encoded
form, ready for use in a URI.
From getLookupUri(long contactId, String lookupKey)
Build a CONTENT_LOOKUP_URI lookup Uri using the given _ID and
LOOKUP_KEY.
From LOOKUP_KEY
An opaque value that contains hints on how to find the contact if its
row id changed as a result of a sync or aggregation.
The row id (primary key) for each contact called _ID.

reading specific contact information

I'm currently doing development on an Android app that requires me to read all the contacts on a device and select only specific contacts based on criteria (only contacts that have at least one valid mobile number and all email addresses linked to that contact).
I've tried the recommended approach at https://stackoverflow.com/a/19563999/3262731, but on a test device with approximately 800 contacts, retrieving all the records and then filtering takes about 17-20 seconds.
Ideally I'd love to build the criteria into a query that joins the contacts, phone, and email store tables in the contacts db as opposed to filtering in my code.
Does anyone have any suggestions please?
The android documentation seems to contain information in what you're looking for found here.
private static final String[] PROJECTION =
{
/*
* The detail data row ID. To make a ListView work,
* this column is required.
*/
Data._ID,
// The primary display name
Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
Data.DISPLAY_NAME_PRIMARY :
Data.DISPLAY_NAME,
// The contact's _ID, to construct a content URI
Data.CONTACT_ID
// The contact's LOOKUP_KEY, to construct a content URI
Data.LOOKUP_KEY (a permanent link to the contact
};
return new CursorLoader(getActivity(), contentUri, PROJECTION, SELECTION, SELECTION_ARGS, SORT_ORDER);
More details on how to define your criteria in the documentation. I would think this would be faster than using a ContentResolver as well.
According to http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html
Query
If you need to read an individual contact, consider using CONTENT_LOOKUP_URI instead of CONTENT_URI.
If you need to look up a contact by the phone number, use PhoneLookup.CONTENT_FILTER_URI, which is optimized for this purpose.
If you need to look up a contact by partial name, e.g. to produce filter-as-you-type suggestions, use the CONTENT_FILTER_URI URI.
If you need to look up a contact by some data element like email address, nickname, etc, use a query against the ContactsContract.Data
table. The result will contain contact ID, name etc.

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.

Picking the correct rawContactID when adding a new number to existing contact

I am building a custom phonebook application. As part of this, I need to add new contacts, or additional info (phone numbers or emails) under existing contacts. While this works well under simple test cases, there are certain situations (and it's not obvious when) where I end up with two issues:
Multiple copies of a contact get created, often hundreds! These are all blank, and simply share the same display name with the original contact that was modified
Multiple numbers (exactly the same) under the contact. The really unusual thing is that these numbers have the same _ID in the contactContracts.Data table, so can't understand how there can be multiple instances with the same unique ID. I checked to see if it's a display issue, but doesn't seem to be
Both of the above makes me question if I am inserting information correctly. So what I want to know is:
When creating a new contact, is there a default account_name and account_type I should be specifying? I am currently using null for both
When I add a new phone number to an existing contact, what is the right approach to inserting data into the contactContracts.Data table? I am currently inserting this number with rawContactID as a reference ID, and I am getting this rawContactID from the contactContracts.RawContacts table, by using my contact's contactID as reference. Note that because I often get multiple raw contacts, I end up picking up the last one (arbitrary choice) to do my phone number insert.
You can use AccountManager Class for getting account name & account type information.
final AccountManager accountTypes = AccountManager.get(getApplicationContext());
final Account accounts[] = accountTypes.getAccounts();
for (final Account account: accounts) {
Log.i(TAG, account.name);
}

Categories

Resources