Search code examples
androidandroid-contentresolvercontactscontractandroid-cursorloader

ContactsContract.Data.IS_READ_ONLY returns negative value -1


IS_READ_ONLY

flag: "0" by default, "1" if the row cannot be modified or deleted except by a sync adapter. See CALLER_IS_SYNCADAPTER. Type: INTEGER Constant Value: "is_read_only"

When I have apply the above in my code, I am getting -1 as the output for all the contacts. I am using IS_READ_ONLY to identify the read only contacts synced in WhatsApp, PayTM, Duo, etc.

Cursor curContacts = cr.query(ContactsContract.Contacts.CONTENT_URI, null,  null, null, null);
        if (curContacts != null) {
            while (curContacts.moveToNext()) {
                int contactsReadOnly = curContacts.getColumnIndex(ContactsContract.Data.IS_READ_ONLY);
                Log.d(Config.TAG, String.valueOf(contactsReadOnly));
            }
        }

OutPut

-1
-1
-1

Have also tried the below line instead of Data.IS_READ_ONLY, but the output is same.

int contactsReadOnly = curContacts.getColumnIndex(ContactsContract.RawContacts.RAW_CONTACT_IS_READ_ONLY);

Solution

  • I have used the below method to get the read-only accounts and then I have pulled the contacts from them.

    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);
                }
            }
        }
    }