Java – Why grant WRITE_CONTACTS permissions without a request?

Why grant WRITE_CONTACTS permissions without a request?… here is a solution to the problem.

Why grant WRITE_CONTACTS permissions without a request?

I have 2 API 28 devices and an app that requires WRITE_CONTACTS permissions. I’ve added it in the list <uses-permission android:name="android.permission.WRITE_CONTACTS" /> In the contact list of my app, when the user clicks Delete Contact, I check if there is a permission and if not, request. So, the method in the first device

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

Returns true and false on the second device. I did not request this permission in advance. Why does it work differently?

Solution

Android permissions are divided into Permission groups, WRITE_CONTACTS is part of the Contacts permission group.
If you have already requested another permission in the same group, you are effectively granted permission for the entire group.
So, if you’ve already requested and obtained permission for READ_CONTACTS, you’ll get WRITE_CONTACTS for free.

However, groups may change between different Android versions, see this section in the linked documentation:

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.

Related Problems and Solutions