Merge "Add missing lock" into sc-dev

This commit is contained in:
Julia Reynolds
2021-06-03 13:24:36 +00:00
committed by Android (Google) Code Review
2 changed files with 24 additions and 23 deletions

View File

@@ -6568,7 +6568,7 @@ public class NotificationManagerService extends SystemService {
// limit the number of non-fgs outstanding notificationrecords an app can have // limit the number of non-fgs outstanding notificationrecords an app can have
if (!n.isForegroundService()) { if (!n.isForegroundService()) {
int count = getNotificationCountLocked(pkg, userId, id, tag); int count = getNotificationCount(pkg, userId, id, tag);
if (count >= MAX_PACKAGE_NOTIFICATIONS) { if (count >= MAX_PACKAGE_NOTIFICATIONS) {
mUsageStats.registerOverCountQuota(pkg); mUsageStats.registerOverCountQuota(pkg);
Slog.e(TAG, "Package has already posted or enqueued " + count Slog.e(TAG, "Package has already posted or enqueued " + count
@@ -6647,28 +6647,29 @@ public class NotificationManagerService extends SystemService {
return true; return true;
} }
@GuardedBy("mNotificationLock") protected int getNotificationCount(String pkg, int userId, int excludedId,
protected int getNotificationCountLocked(String pkg, int userId, int excludedId,
String excludedTag) { String excludedTag) {
int count = 0; int count = 0;
final int N = mNotificationList.size(); synchronized (mNotificationLock) {
for (int i = 0; i < N; i++) { final int N = mNotificationList.size();
final NotificationRecord existing = mNotificationList.get(i); for (int i = 0; i < N; i++) {
if (existing.getSbn().getPackageName().equals(pkg) final NotificationRecord existing = mNotificationList.get(i);
&& existing.getSbn().getUserId() == userId) { if (existing.getSbn().getPackageName().equals(pkg)
if (existing.getSbn().getId() == excludedId && existing.getSbn().getUserId() == userId) {
&& TextUtils.equals(existing.getSbn().getTag(), excludedTag)) { if (existing.getSbn().getId() == excludedId
continue; && TextUtils.equals(existing.getSbn().getTag(), excludedTag)) {
continue;
}
count++;
} }
count++;
} }
} final int M = mEnqueuedNotifications.size();
final int M = mEnqueuedNotifications.size(); for (int i = 0; i < M; i++) {
for (int i = 0; i < M; i++) { final NotificationRecord existing = mEnqueuedNotifications.get(i);
final NotificationRecord existing = mEnqueuedNotifications.get(i); if (existing.getSbn().getPackageName().equals(pkg)
if (existing.getSbn().getPackageName().equals(pkg) && existing.getSbn().getUserId() == userId) {
&& existing.getSbn().getUserId() == userId) { count++;
count++; }
} }
} }
return count; return count;

View File

@@ -3394,16 +3394,16 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
// anything that's currently enqueued or posted // anything that's currently enqueued or posted
int userId = UserHandle.getUserId(mUid); int userId = UserHandle.getUserId(mUid);
assertEquals(40, assertEquals(40,
mService.getNotificationCountLocked(PKG, userId, 0, null)); mService.getNotificationCount(PKG, userId, 0, null));
assertEquals(40, assertEquals(40,
mService.getNotificationCountLocked(PKG, userId, 0, "tag2")); mService.getNotificationCount(PKG, userId, 0, "tag2"));
// return all for package "a" - "banana" tag isn't used // return all for package "a" - "banana" tag isn't used
assertEquals(2, assertEquals(2,
mService.getNotificationCountLocked("a", userId, 0, "banana")); mService.getNotificationCount("a", userId, 0, "banana"));
// exclude a known notification - it's excluded from only the posted list, not enqueued // exclude a known notification - it's excluded from only the posted list, not enqueued
assertEquals(39, mService.getNotificationCountLocked( assertEquals(39, mService.getNotificationCount(
PKG, userId, sampleIdToExclude, sampleTagToExclude)); PKG, userId, sampleIdToExclude, sampleTagToExclude));
} }