Use Context in Notification PermissionHelper
The context.checkPermission method has some logic not included in the
current PermissionManagerServiceInternal.checkUidPermission, such as
short-circuiting checks for system UID.
Test: atest PermissionHelperTest
Bug: 275709830
Change-Id: If6ac68a243e1222cb91985ef2055b4150c973d3f
diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java
index 33e6a8f1..22da042 100755
--- a/services/core/java/com/android/server/notification/NotificationManagerService.java
+++ b/services/core/java/com/android/server/notification/NotificationManagerService.java
@@ -316,7 +316,6 @@
import com.android.server.notification.toast.ToastRecord;
import com.android.server.pm.PackageManagerService;
import com.android.server.pm.UserManagerInternal;
-import com.android.server.pm.permission.PermissionManagerServiceInternal;
import com.android.server.policy.PermissionPolicyInternal;
import com.android.server.powerstats.StatsPullAtomCallbackImpl;
import com.android.server.statusbar.StatusBarManagerInternal;
@@ -2559,8 +2558,8 @@
Context.STATS_MANAGER),
getContext().getSystemService(TelephonyManager.class),
LocalServices.getService(ActivityManagerInternal.class),
- createToastRateLimiter(), new PermissionHelper(LocalServices.getService(
- PermissionManagerServiceInternal.class), AppGlobals.getPackageManager(),
+ createToastRateLimiter(), new PermissionHelper(getContext(),
+ AppGlobals.getPackageManager(),
AppGlobals.getPermissionManager()),
LocalServices.getService(UsageStatsManagerInternal.class),
getContext().getSystemService(TelecomManager.class),
diff --git a/services/core/java/com/android/server/notification/PermissionHelper.java b/services/core/java/com/android/server/notification/PermissionHelper.java
index b6fd822..93c83e1 100644
--- a/services/core/java/com/android/server/notification/PermissionHelper.java
+++ b/services/core/java/com/android/server/notification/PermissionHelper.java
@@ -25,6 +25,7 @@
import android.Manifest;
import android.annotation.NonNull;
import android.annotation.UserIdInt;
+import android.content.Context;
import android.content.pm.IPackageManager;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
@@ -37,7 +38,6 @@
import android.util.Slog;
import com.android.internal.util.ArrayUtils;
-import com.android.server.pm.permission.PermissionManagerServiceInternal;
import java.util.Collections;
import java.util.HashSet;
@@ -53,13 +53,13 @@
private static final String NOTIFICATION_PERMISSION = Manifest.permission.POST_NOTIFICATIONS;
- private final PermissionManagerServiceInternal mPmi;
+ private final Context mContext;
private final IPackageManager mPackageManager;
private final IPermissionManager mPermManager;
- public PermissionHelper(PermissionManagerServiceInternal pmi, IPackageManager packageManager,
+ public PermissionHelper(Context context, IPackageManager packageManager,
IPermissionManager permManager) {
- mPmi = pmi;
+ mContext = context;
mPackageManager = packageManager;
mPermManager = permManager;
}
@@ -71,7 +71,7 @@
public boolean hasPermission(int uid) {
final long callingId = Binder.clearCallingIdentity();
try {
- return mPmi.checkUidPermission(uid, NOTIFICATION_PERMISSION) == PERMISSION_GRANTED;
+ return mContext.checkPermission(NOTIFICATION_PERMISSION, -1, uid) == PERMISSION_GRANTED;
} finally {
Binder.restoreCallingIdentity(callingId);
}
@@ -193,8 +193,8 @@
return;
}
- boolean currentlyGranted = mPmi.checkPermission(packageName, NOTIFICATION_PERMISSION,
- userId) != PackageManager.PERMISSION_DENIED;
+ int uid = mPackageManager.getPackageUid(packageName, 0, userId);
+ boolean currentlyGranted = hasPermission(uid);
if (grant && !currentlyGranted) {
mPermManager.grantRuntimePermission(packageName, NOTIFICATION_PERMISSION, userId);
} else if (!grant && currentlyGranted) {
diff --git a/services/tests/uiservicestests/src/com/android/server/notification/PermissionHelperTest.java b/services/tests/uiservicestests/src/com/android/server/notification/PermissionHelperTest.java
index 397e3c1..539f329 100644
--- a/services/tests/uiservicestests/src/com/android/server/notification/PermissionHelperTest.java
+++ b/services/tests/uiservicestests/src/com/android/server/notification/PermissionHelperTest.java
@@ -36,6 +36,7 @@
import static org.mockito.Mockito.when;
import android.Manifest;
+import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageManager;
import android.content.pm.PackageInfo;
@@ -47,7 +48,6 @@
import androidx.test.runner.AndroidJUnit4;
import com.android.server.UiServiceTestCase;
-import com.android.server.pm.permission.PermissionManagerServiceInternal;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
@@ -67,7 +67,7 @@
public class PermissionHelperTest extends UiServiceTestCase {
@Mock
- private PermissionManagerServiceInternal mPmi;
+ private Context mContext;
@Mock
private IPackageManager mPackageManager;
@Mock
@@ -80,7 +80,7 @@
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
- mPermissionHelper = new PermissionHelper(mPmi, mPackageManager, mPermManager);
+ mPermissionHelper = new PermissionHelper(mContext, mPackageManager, mPermManager);
PackageInfo testPkgInfo = new PackageInfo();
testPkgInfo.requestedPermissions = new String[]{ Manifest.permission.POST_NOTIFICATIONS };
when(mPackageManager.getPackageInfo(anyString(), anyLong(), anyInt()))
@@ -89,12 +89,12 @@
@Test
public void testHasPermission() throws Exception {
- when(mPmi.checkUidPermission(anyInt(), anyString()))
+ when(mContext.checkPermission(anyString(), anyInt(), anyInt()))
.thenReturn(PERMISSION_GRANTED);
assertThat(mPermissionHelper.hasPermission(1)).isTrue();
- when(mPmi.checkUidPermission(anyInt(), anyString()))
+ when(mContext.checkPermission(anyString(), anyInt(), anyInt()))
.thenReturn(PERMISSION_DENIED);
assertThat(mPermissionHelper.hasPermission(1)).isFalse();
@@ -241,7 +241,7 @@
@Test
public void testSetNotificationPermission_grantUserSet() throws Exception {
- when(mPmi.checkPermission(anyString(), anyString(), anyInt()))
+ when(mContext.checkPermission(anyString(), anyInt(), anyInt()))
.thenReturn(PERMISSION_DENIED);
mPermissionHelper.setNotificationPermission("pkg", 10, true, true);
@@ -255,7 +255,7 @@
@Test
public void testSetNotificationPermission_pkgPerm_grantedByDefaultPermSet_allUserSet()
throws Exception {
- when(mPmi.checkPermission(anyString(), anyString(), anyInt()))
+ when(mContext.checkPermission(anyString(), anyInt(), anyInt()))
.thenReturn(PERMISSION_DENIED);
when(mPermManager.getPermissionFlags(anyString(),
eq(Manifest.permission.POST_NOTIFICATIONS),
@@ -273,7 +273,7 @@
@Test
public void testSetNotificationPermission_revokeUserSet() throws Exception {
- when(mPmi.checkPermission(anyString(), anyString(), anyInt()))
+ when(mContext.checkPermission(anyString(), anyInt(), anyInt()))
.thenReturn(PERMISSION_GRANTED);
mPermissionHelper.setNotificationPermission("pkg", 10, false, true);
@@ -287,7 +287,7 @@
@Test
public void testSetNotificationPermission_grantNotUserSet() throws Exception {
- when(mPmi.checkPermission(anyString(), anyString(), anyInt()))
+ when(mContext.checkPermission(anyString(), anyInt(), anyInt()))
.thenReturn(PERMISSION_DENIED);
mPermissionHelper.setNotificationPermission("pkg", 10, true, false);
@@ -300,7 +300,7 @@
@Test
public void testSetNotificationPermission_revokeNotUserSet() throws Exception {
- when(mPmi.checkPermission(anyString(), anyString(), anyInt()))
+ when(mContext.checkPermission(anyString(), anyInt(), anyInt()))
.thenReturn(PERMISSION_GRANTED);
mPermissionHelper.setNotificationPermission("pkg", 10, false, false);
@@ -340,7 +340,7 @@
@Test
public void testSetNotificationPermission_alreadyGrantedNotRegranted() throws Exception {
- when(mPmi.checkPermission(anyString(), anyString(), anyInt()))
+ when(mContext.checkPermission(anyString(), anyInt(), anyInt()))
.thenReturn(PERMISSION_GRANTED);
mPermissionHelper.setNotificationPermission("pkg", 10, true, false);
@@ -350,7 +350,7 @@
@Test
public void testSetNotificationPermission_alreadyRevokedNotRerevoked() throws Exception {
- when(mPmi.checkPermission(anyString(), anyString(), anyInt()))
+ when(mContext.checkPermission(anyString(), anyInt(), anyInt()))
.thenReturn(PERMISSION_DENIED);
mPermissionHelper.setNotificationPermission("pkg", 10, false, false);
@@ -360,16 +360,19 @@
@Test
public void testSetNotificationPermission_doesntRequestNotChanged() throws Exception {
- when(mPmi.checkPermission(anyString(), anyString(), anyInt()))
+ int testUid = -1;
+ when(mContext.checkPermission(anyString(), anyInt(), anyInt()))
.thenReturn(PERMISSION_GRANTED);
+ when(mPackageManager.getPackageUid(anyString(), anyInt(), anyInt()))
+ .thenReturn(testUid);
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(mContext, never()).checkPermission(
+ eq(Manifest.permission.POST_NOTIFICATIONS), eq(-1), eq(testUid));
verify(mPermManager, never()).revokeRuntimePermission(
eq("pkg"), eq(Manifest.permission.POST_NOTIFICATIONS), eq(10), anyString());
}