Merge "Add a field to ServiceRecord for FGS notification permissions."

This commit is contained in:
Yuri Lin
2022-01-12 16:10:18 +00:00
committed by Android (Google) Code Review
5 changed files with 28 additions and 1 deletions

View File

@@ -174,6 +174,9 @@ final class ServiceRecord extends Binder implements ComponentName.WithComponentN
boolean mFgsNotificationWasDeferred;
// FGS notification was shown before the FGS finishes, or it wasn't deferred in the first place.
boolean mFgsNotificationShown;
// Whether FGS package has permissions to show notifications.
// TODO(b/194833441): Output this field to logs in ActiveServices#logFGSStateChangeLocked.
boolean mFgsHasNotificationPermission;
// allow the service becomes foreground service? Service started from background may not be
// allowed to become a foreground service.
@@ -968,6 +971,9 @@ final class ServiceRecord extends Binder implements ComponentName.WithComponentN
if (nm == null) {
return;
}
// Record whether the package has permission to notify the user
mFgsHasNotificationPermission = nm.areNotificationsEnabledForPackage(
localPackageName, appUid);
Notification localForegroundNoti = _foregroundNoti;
try {
if (localForegroundNoti.getSmallIcon() == null) {

View File

@@ -39,4 +39,7 @@ public interface NotificationManagerInternal {
/** Get the number of notification channels for a given package */
int getNumNotificationChannelsForPackage(String pkg, int uid, boolean includeDeleted);
/** Does the specified package/uid have permission to post notifications? */
boolean areNotificationsEnabledForPackage(String pkg, int uid);
}

View File

@@ -6205,6 +6205,11 @@ public class NotificationManagerService extends SystemService {
return NotificationManagerService.this
.getNumNotificationChannelsForPackage(pkg, uid, includeDeleted);
}
@Override
public boolean areNotificationsEnabledForPackage(String pkg, int uid) {
return areNotificationsEnabledForPackageInt(pkg, uid);
}
};
int getNumNotificationChannelsForPackage(String pkg, int uid, boolean includeDeleted) {

View File

@@ -182,7 +182,6 @@ import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.AtomicFile;
import android.util.Slog;
import android.util.TypedXmlPullParser;
import android.util.TypedXmlSerializer;
import android.util.Xml;
@@ -7173,6 +7172,14 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
verify(mPermissionHelper, never()).hasPermission(anyInt());
}
@Test
public void testAreNotificationsEnabledForPackage_viaInternalService() throws Exception {
assertEquals(mInternalService.areNotificationsEnabledForPackage(
mContext.getPackageName(), mUid),
mBinderService.areNotificationsEnabledForPackage(mContext.getPackageName(), mUid));
verify(mPermissionHelper, never()).hasPermission(anyInt());
}
@Test
public void testAreBubblesAllowedForPackage_crossUser() throws Exception {
try {

View File

@@ -535,6 +535,12 @@ public class NotificationPermissionMigrationTest extends UiServiceTestCase {
verify(mPermissionHelper).hasPermission(mUid + UserHandle.PER_USER_RANGE);
}
@Test
public void testAreNotificationsEnabledForPackage_viaInternalService() {
mInternalService.areNotificationsEnabledForPackage(mContext.getPackageName(), mUid);
verify(mPermissionHelper).hasPermission(mUid);
}
@Test
public void testGetPackageImportance() throws Exception {
when(mPermissionHelper.hasPermission(mUid)).thenReturn(true);