0

I am passing account name from a list to this method. Now i want to know which of these account names are read only in contacts table so i am iterating the cursor only once to get the contact id from raw cursor. After getting the contact_id I am using phone cursor to check whether the given id is read only or not but i am unable to do it. please have a look below

 private void displayAllContactsByType(String accountName) {

    Cursor rawCursor,phones = null;

    rawCursor = cResolver.query(
            ContactsContract.RawContacts.CONTENT_URI,
            new String[]{ContactsContract.RawContacts.CONTACT_ID},
            ContactsContract.RawContacts.ACCOUNT_NAME + "= ?",
            new String[]{accountName},
            null);


    int contactIdColumn = rawCursor.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID);
    int rawCursorCount = rawCursor.getCount();


    Utils.Log("Account Name",  accountName);

    Utils.Log("Raw Size", " " + rawCursorCount);
    rawCursor.moveToFirst();
    Long contactId = rawCursor.getLong(contactIdColumn);


    phones = cResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
           null,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND "+ContactsContract.RawContacts.ACCOUNT_NAME + "= ?",
            new String[]{String.valueOf(contactId),accountName},
            null);

    phones.moveToFirst();




   String isReadOnly= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.IS_READ_ONLY));
    Utils.Log("Raw Size", isReadOnly);


}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Akshat Vajpayee
  • 274
  • 1
  • 3
  • 15

1 Answers1

4

You don't have to go over an account's contacts to check that, you can simply iterate over the SyncAdapters on the device, and check their properties:

final SyncAdapterType[] syncs = ContentResolver.getSyncAdapterTypes();
for (SyncAdapterType sync : syncs) {
    Log.d(TAG, "found SyncAdapter: " + sync.accountType);
    if (ContactsContract.AUTHORITY.equals(sync.authority)) {
        Log.d(TAG, "SyncAdapter supports contacts: " + sync.accountType);
        boolean readOnly = !sync.supportsUploading();
        Log.d(TAG, "SyncAdapter read-only mode: " + readOnly);
        if (readOnly) {
            // we'll now get a list of all accounts under that accountType:
            Account[] accounts = AccountManager.get(this).getAccountsByType(sync.accountType);
            for (Account account : accounts) {
               Log.d(TAG, account.type + " / " + account.name);
            }
        }
    }
}

Hope this helps.

marmor
  • 27,641
  • 11
  • 107
  • 150
  • Thankyou Very Much. – Akshat Vajpayee Aug 21 '17 at 10:17
  • I would also like to get only those account with sync adapter having phone contacts only. – Akshat Vajpayee Aug 23 '17 at 06:43
  • what do you mean by `having phone contacts only`? you mean the special contacts account called "Phone only (unsynced)" if you do, it has no SyncAdapter, because it's an unsynced account – marmor Aug 23 '17 at 07:11
  • Actually I am creating Contact duplicator app in which i need Account name where all my contacts are synced but the above code also gives those account name like linkedin, messanger(FB), Office etc which dont have synced contacts i guess. I only need accounts like google duo,whats app, freecharge , mobikwik etc where all my contacts are synced. – Akshat Vajpayee Aug 23 '17 at 08:19
  • linkedin, office, etc. are **able** to sync contacts, and if you go into the linkedin app settings, for example, you can turn on contacts sync, in which case you'll start seeing linkedin contacts in your phone. if you want to actually verify if there are contacts from this account, you can query using `ACCOUNT_NAME + "='" accountName + "'"` on RawContacts table – marmor Aug 25 '17 at 11:31