0

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);
Community
  • 1
  • 1
Gvtha
  • 1,463
  • 1
  • 11
  • 18

2 Answers2

0

You have two bugs in your code:

  1. As mentioned in the comments, you're querying on the wrong table, to access Data.* columns you need to query over Data.CONTENT_URI
  2. Cursor.getColumnIndex will return the index of the specified column within your projection, not the value stored in that field, -1 means that this column does not exist in your projection.

Try this:

String[] projection = new String[] { Data.IS_READ_ONLY };
Cursor curData = cr.query(ContactsContract.Data.CONTENT_URI, projection, null, null, null);
while (curData != null && curData.moveToNext()) {
    int dataReadOnly = curData.getInt(0); // 0 because it is the first field in the projection
    Log.d(Config.TAG, "data is: " + dataReadOnly);
}
marmor
  • 27,641
  • 11
  • 107
  • 150
  • I am getting the below error when executing your code. `java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.lang.IllegalArgumentException: Invalid column is_read_only` – Gvtha Mar 20 '18 at 17:07
  • `start activity` ? where are you running this from? – marmor Mar 21 '18 at 10:53
  • I ran this code from `onCreate` method in the class extends `AppCompatActivity`. – Gvtha Mar 21 '18 at 11:30
  • sounds like you're querying from the wrong table, make sure you're importing the right `Data` table - that is: `android.provider.ContactsContract.Data` – marmor Mar 21 '18 at 13:07
0

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);
            }
        }
    }
}
Gvtha
  • 1,463
  • 1
  • 11
  • 18