Retrieve those contacts from phonebook that has facebook - android

I want to retrive all the contact from phone book that has facebook account.
Steps i did
I got contact id from Contacts.
Based on id get Accounts from RawContact table.
All account associated with contact found but facebook account not found.

You can follow below algorithm to get contacts with specific account type.
1. First, query on raw contacts table and find all raw contacts. Check what are different Account type and Account name are available. http://developer.android.com/reference/android/provider/ContactsContract.SyncColumns.html#ACCOUNT_TYPE
Facebook contact will have account type with text "facebook" in it.
2. Once you got exact account type, you can query on raw contacts with account type = facebook.
3. This will give all Facebook raw contacts. Raw contact table has contact_id field using which you can query on Contacts table to get all Facebook contacts.
Below is the code snippet which can print all account names and types which are currently configured on device for which contacts are present. This is just Sample code to check what all account types are there. If you don't find any account name/type related to facebook, I guess there are no facebook contacts present.
String[] projection = { RawContacts._ID, RawContacts.ACCOUNT_TYPE,
RawContacts.ACCOUNT_NAME };
Cursor cur = cr.query(RawContacts.CONTENT_URI, projection, null, null,
null);
if (cur != null) {
while (cur.moveToNext()) {
long rawContactId = cur.getLong(cur
.getColumnIndexOrThrow(RawContacts._ID));
String accountType = cur.getString(cur
.getColumnIndexOrThrow(RawContacts.ACCOUNT_TYPE));
String accountName = cur.getString(cur
.getColumnIndexOrThrow(RawContacts.ACCOUNT_NAME));
}
}

Related

How to get all the work profile contacts using android platform

How to retrieve all the work profile contacts from cursor using android.
Please update cursor URI formation here using ENTERPRISE_CONTENT_FILTER_URI
I can search any contact in work profile using the below piece of code, but am looking for to get all the work profile contacts instead of search behavior
Reference the below piece of code for search contact in work profile.
// Build the URI to look up work profile contacts whose name matches. Query
// the default work profile directory which is the locally stored contacts.
Uri contentFilterUri = ContactsContract.Contacts.ENTERPRISE_CONTENT_FILTER_URI
.buildUpon()
.appendPath(nameQuery)
.appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY,
String.valueOf(ContactsContract.Directory.ENTERPRISE_DEFAULT))
.build();
// Query the content provider using the generated URI.
Cursor cursor = getContentResolver().query(
contentFilterUri,
new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY
},
null,
null,
null);
if (cursor == null) {
return;
}
// Print any results found using the work profile contacts' display name.
try {
while (cursor.moveToNext()) {
Log.i(TAG, "Work profile contact: " + cursor.getString(2));
}
} finally {
cursor.close();
}
Let me know how to retrieve all the contacts info(Name/phone/profile pic url) from work profile.
This is not supported by the API by design.
Consider an organization with thousands of employees, on an old, budget phone.
That might choke the little phone's memory.
So instead the API allows organizations to implement a fetch of certain employees based on search.
To get the entire list, you can just brute force that API, searching all possible name prefixes, and storing all the results. Just make sure you don't crash your app if it runs on organizations with many employees.

Get cross-device unique ID for Android phone contacts

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.

What kind of information can i find on android's Contacts Provider

I am developing an android application and I need to know all the information about phone contacts.
I developed a function to get the name and number of all the contacts, but I need all the information about particular contact such as email, date, favorite or not, image, social links if available.
I got id, name and number from following:
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
I used ContactsContract.Contacts to get _ID and DISPLAY_NAME, but
ContactsContract.CommenDataKinds.Phone to get the NUMBER. Is it correct?
Please explain the difference between the two methods.
Is the _ID a unique ID for all the contacts?
After a long discussion with #pskink I finally found the solution to list all related information for each contact in the directory.
First of all, create a cursor:
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI, null, null, null, null);
And after that, you can dumb the cursor to show all the informations and see each contact and keywords it needs to use, like (custom_ringtone, display_name, photo_uri, is_primary, ..) by using this line of code:
DatabaseUtils.dumpCursor(cursor);
Special thanks to #pskink

No contacts for my AccountManager account

I am currently working on a project in which I want to access the mobile contacts, So I have managed to create account with accountmanager and also able to perform Syncadapter operation. I could see my account got created in the mobile settings->Accounts. However, when I try to get all the contacts with my account with below code ,it does not work. Its showing all apps(google.com and WhatsApp.com) contacts except my app account contacts.
Cursor cursor = getContext().getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI,
new String[]{ContactsContract.RawContacts.DIRTY, ContactsContract.RawContacts.ACCOUNT_TYPE},
null,
null,
null);
if (cursor != null && cursor.getCount() >0) {
cursor.moveToFirst();
while(!cursor.isAfterLast()) {
Log.d("Dirty",cursor.getString(cursor.getColumnIndex(ContactsContract.RawContacts.DIRTY)));
Log.d("ACCountType",cursor.getString(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE)));
cursor.moveToNext();
}
cursor.close();
}
What I dont understand is do I need to create ContentProvider and insert all contacts back to Contactsprovider on behalf of my account?
Not sure if you've fully understood how the ContactsProvider works.
There are a few things that you should know:
Every RawContact is uniquely assigned to one specific account, it can not belong to more than one account (hence your app usually can't sync existing contacts, because they already have an account).
All apps have the same view on all the contacts, in particular all apps can see and modify all contacts (given they have the permissions), though there are a few exceptions to that rule.
When you sync a contact to your account you must specify your account as shown on ContactsContract.RawContacts
ContentValues values = new ContentValues();
values.put(RawContacts.ACCOUNT_TYPE, accountType);
values.put(RawContacts.ACCOUNT_NAME, accountName);
Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
When you read contacts you get contacts of all accounts, unless you specify Uri query parameters or a selection:
Uri rawContactUri = RawContacts.CONTENT_URI.buildUpon()
.appendQueryParameter(RawContacts.ACCOUNT_NAME, accountName)
.appendQueryParameter(RawContacts.ACCOUNT_TYPE, accountType)
.build();
Cursor c1 = getContentResolver().query(rawContactUri,
RawContacts.STARRED + "<>0", null, null, null)
This query returns all starred contacts of the specified account.
If your code operates as a sync adapter you also have to add the Uri query parameter CALLER_IS_SYNC_ADAPTER, otherwise you may get different results for many operations.

Android: Read contacts

I am developing an application in which i am able to read the contacts. I refereed this Link.
My code to read contact and basic information is as follow:
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phones.moveToNext()) {
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String contactID = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
}
phones.close();
Now I want to get the following things from contact as :
1.Type of contact as it is of Device, sim, google, face book etc contact.
2.The number of email for particular contact.
3.Total account(Face book, whats app, skype etc) count for the contact.
4.Total Event(Birthday, anniversary etc) count.
5.The contact is favorite or not
6.The contact is in any device group(Family, Friends) and yes then which groups?
I searched on google but not able to do the desired task.
Please suggest me what should i do.

Categories

Resources