how to get all social accounts linked with the particular contact in android from phonebook?

Viewed 1536

I fetched all device contacts from phonebook. Now i want to fetch linked accounts(facebook,twitter,instagram,LinkedIn)urls from that particular contact that is fetched from phonebook.What should i do?

Here is the code to fetch the contacts.

public Cursor getContactsCursor(FragmentActivity activity) {
        Cursor cursor = null;
        try {
            String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + "= 0" + " OR " + ContactsContract.Contacts.IN_VISIBLE_GROUP + "= 1";
            String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
            ContentResolver cr = activity.getContentResolver();
            return cr.query(ContactsContract.Contacts.CONTENT_URI, null, selection, null, sortOrder);
        } catch (Exception e) {
            AppLogger.e(Helper.class.getSimpleName(), e.getMessage());
            return cursor;
        }
    }

Now i don't know how to fetch the accounts (like facebook, linkedin etc) linked with the particular contact.

Can someone please guide me.

Update : In below attached image, On clicking the section highlighted in red, opens the linked in user profile in browser. Hence i am willing to fetch the field which is used to open the user profile page.

enter image description here

Thanks in advance.

2 Answers

You'll need to figure out the exact MIMETYPE of all accounts you're interested in, for example, Google+'s MIMETYPE is: vnd.android.cursor.item/vnd.googleplus.profile

You can dump all MIMETYPEs for a contact and figure out manually which you need:

// make sure you import Data from: ContactsContract.Data
String[] projection = new String[] { Data.MIMETYPE };
String selection = Data.CONTACT_ID + " = '"+ contactId + "'";
Cursor cursor = getContentResolver().query(Data.CONTENT_URI, projection, selection, null, null);
DatabaseUtils.dumpCursor(cursor);
cursor.close();

Once you have a fixed list of the MIMETYPEs you want, you can query the info in them for a specific contact:

// Add more
String[] mimetypes = new String[] { 
    "vnd.android.cursor.item/vnd.googleplus.profile",
    "vnd.android.cursor.item/vnd.com.whatsapp.profile" 
};

// Usually the interesting info is on the first few fields, modify this if needed
String[] projection = new String[] { Data.DATA1, Data.DATA2, Data.DATA3, Data.DATA4 };
String selection = Data.CONTACT_ID + " = '"+ contactId + "' AND " + Data.MIMETYPE + " IN (?,?)";

Cursor cursor = getContentResolver().query(Data.CONTENT_URI, projection, selection, mimetypes, null);
DatabaseUtils.dumpCursor(cursor);
cursor.close();

UPDATE:

In case of linkedin, the mimetype is indeed: vnd.android.cursor.item/vnd.com.linkedin.android.profile. Regarding your comment about not having the profile url, in Data1 you should have some long ID like AC...UQ4 (about 40 characters).

Then your url is: https://www.linkedin.com/profile/view?id=<data1Id> like: https://www.linkedin.com/profile/view?id=AC...UQ4

Get first account info. fetch all accounts

ArrayList<String> accountsInfo = new ArrayList<String>();
    AccountManager manager = AccountManager.get(this);
    Account[] accounts = manager.getAccounts();
    for (Account account : accounts) {
        String name = account.name;
        String type = account.type;
        int describeContents = account.describeContents();
        int hashCode = account.hashCode();

        accountsInfo.add("name = " + name +
                         "\ntype = " + type +
                         "\n");
    }
    String[] result = new String[accountsInfo.size()];
    accountsInfo.toArray(result);

Use below sample account type.

    /**
     * com.facebook.auth.login /
     * com.linkedin.android
     * flipkart.com
     * com.facebook.messenger
     * com.twitter.android.auth.login
     * com.truecaller.account
     * net.one97.paytm
     * com.whatsapp
     * com.google
     */

    Cursor c = getContentResolver().query(
            ContactsContract.RawContacts.CONTENT_URI,
            new String[]{ContactsContract.RawContacts.CONTACT_ID, ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY},
            ContactsContract.RawContacts.ACCOUNT_TYPE + "= ?",
            new String[]{"com.whatsapp"},
            null);

    final ArrayList<String> myContacts = new ArrayList<String>();
    int contactNameColumn = c.getColumnIndex(ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY);
    while (c.moveToNext()) {
        // You can also read RawContacts.CONTACT_ID to read the
        // ContactsContract.Contacts table or any of the other related ones.
        myContacts.add(c.getString(contactNameColumn));
    }
    Log.d("contact size", String.valueOf(myContacts.size()));
Related