Search code examples
javaandroidandroid-permissionsandroid-contacts

Why WRITE_CONTACTS permission is granted without request?


I have 2 devices with API 28 and an app which needs WRITE_CONTACTS permission. I have added it in manifest <uses-permission android:name="android.permission.WRITE_CONTACTS" />. In the list of contacts in my app, when user click on delete contact, I have made a check to see if there is a permission and request if not. So, in 1st device the method

public static boolean checkWriteContactsPermission(Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        return true;
    }
    return ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_CONTACTS) == PackageManager.PERMISSION_GRANTED;
}

return true, at the same time on the 2nd device it reutrns false. I don`t request this permission earlier. Why does it work differently?


Solution

  • Android Permissions are divided into Permission groups, WRITE_CONTACTS is a part of the Contacts permission group. if you've already asked for another permission in that same group, you are actually granted permissions for the entire group. So if you've already asked for, and been granted permission for READ_CONTACTS, you'll get WRITE_CONTACTS for free.

    But groups may change between different Android versions, see this part in the linked docs:

    If the app has already been granted another dangerous permission in the same permission group, the system immediately grants the permission without any interaction with the user. For example, if an app had previously requested and been granted the READ_CONTACTS permission, and it then requests WRITE_CONTACTS, the system immediately grants that permission without showing the permissions dialog to the user.

    And then:

    Caution: Future versions of the Android SDK might move a particular permission from one group to another. Therefore, don't base your app's logic on the structure of these permission groups.

    For example, READ_CONTACTS is in the same permission group as WRITE_CONTACTS as of Android 8.1 (API level 27). If your app requests the READ_CONTACTS permission, and then requests the WRITE_CONTACTS permission, don't assume that the system can automatically grant the WRITE_CONTACTS permission.