I want to check how new permission model works so in app's settings I disable Contacts
. Then I go to app and try to read Contacts
and ... it kinda works:
try {
Uri result = data.getData();
int contentIdx;
cursor = getContentResolver().query(result, null, null, null, null);
contentIdx = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
if(cursor.moveToFirst()) {
content = cursor.getInt(contentIdx);
}
if(content > 0) {
contentIdx = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
if(cursor.moveToFirst()) {
name = cursor.getString(contentIdx);
}
contentIdx = cursor.getColumnIndex(BaseColumns._ID);
if(cursor.moveToFirst()) {
content = cursor.getLong(contentIdx);
}
cursor = managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] { Phone.NUMBER }, Data.CONTACT_ID + "=?", new String[] { String.valueOf(content) }, null);
if(cursor.moveToFirst()) {
number = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
}
}
} catch (Exception e) {
//SecurityException
}
- I'm able to read Contact's Name
- when I try to read Contact's Number
SecurityException
is thrown
java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.HtcContactsProvider2 uri content://com.android.contacts/data/phones from pid=20123, uid=10593 requires android.permission.READ_CONTACTS, or grantUriPermission()
Why is that?
Related stuff: Contact data leakage via pick activities