Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 90d32add authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Check if an app requests POST_NOTIFICATIONS before setting state" into tm-dev

parents a6789266 980ab95c
Loading
Loading
Loading
Loading
+23 −6
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.util.ArrayMap;
import android.util.Pair;
import android.util.Slog;

import com.android.internal.util.ArrayUtils;
import com.android.server.pm.permission.PermissionManagerServiceInternal;

import java.util.Collections;
@@ -178,13 +179,16 @@ public final class PermissionHelper {
            boolean userSet, boolean reviewRequired) {
        assertFlag();
        final long callingId = Binder.clearCallingIdentity();
        // Do not change fixed permissions, and do not change non-user set permissions that are
        // granted by default, or granted by role.
        if (isPermissionFixed(packageName, userId)
        try {
            // Do not change the permission if the package doesn't request it, do not change fixed
            // permissions, and do not change non-user set permissions that are granted by default,
            // or granted by role.
            if (!packageRequestsNotificationPermission(packageName, userId)
                    || isPermissionFixed(packageName, userId)
                    || (isPermissionGrantedByDefaultOrRole(packageName, userId) && !userSet)) {
                return;
            }
        try {

            boolean currentlyGranted = mPmi.checkPermission(packageName, NOTIFICATION_PERMISSION,
                    userId) != PackageManager.PERMISSION_DENIED;
            if (grant && !reviewRequired && !currentlyGranted) {
@@ -278,6 +282,19 @@ public final class PermissionHelper {
        }
    }

    private boolean packageRequestsNotificationPermission(String packageName,
            @UserIdInt int userId) {
        assertFlag();
        try {
            String[] permissions = mPackageManager.getPackageInfo(packageName, GET_PERMISSIONS,
                    userId).requestedPermissions;
            return ArrayUtils.contains(permissions, NOTIFICATION_PERMISSION);
        } catch (RemoteException e) {
            Slog.e(TAG, "Could not reach system server", e);
        }
        return false;
    }

    private void assertFlag() {
        if (!mMigrationEnabled) {
            throw new IllegalStateException("Method called without checking flag value");
+20 −0
Original line number Diff line number Diff line
@@ -89,6 +89,10 @@ public class PermissionHelperTest extends UiServiceTestCase {
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        mPermissionHelper = new PermissionHelper(mPmi, mPackageManager, mPermManager, true);
        PackageInfo testPkgInfo = new PackageInfo();
        testPkgInfo.requestedPermissions = new String[]{ Manifest.permission.POST_NOTIFICATIONS };
        when(mPackageManager.getPackageInfo(anyString(), anyLong(), anyInt()))
                .thenReturn(testPkgInfo);
    }

    // TODO (b/194833441): Remove when the migration is enabled
@@ -383,6 +387,22 @@ public class PermissionHelperTest extends UiServiceTestCase {
                eq("pkg"), eq(Manifest.permission.POST_NOTIFICATIONS), eq(10), anyString());
    }

    @Test
    public void testSetNotificationPermission_doesntRequestNotChanged() throws Exception {
        when(mPmi.checkPermission(anyString(), anyString(), anyInt()))
                .thenReturn(PERMISSION_GRANTED);
        PackageInfo testPkgInfo = new PackageInfo();
        testPkgInfo.requestedPermissions = new String[]{ Manifest.permission.RECORD_AUDIO };
        when(mPackageManager.getPackageInfo(anyString(), anyLong(), anyInt()))
                .thenReturn(testPkgInfo);
        mPermissionHelper.setNotificationPermission("pkg", 10, false, false);

        verify(mPmi, never()).checkPermission(
                eq("pkg"), eq(Manifest.permission.POST_NOTIFICATIONS), eq(10));
        verify(mPermManager, never()).revokeRuntimePermission(
                eq("pkg"), eq(Manifest.permission.POST_NOTIFICATIONS), eq(10), anyString());
    }

    @Test
    public void testIsPermissionFixed() throws Exception {
        when(mPermManager.getPermissionFlags(anyString(),