Add missing lock

Test: atest
Fixes: 189743705
Change-Id: I23b07e1c3b0c8a45fa2059501f666346b27273ce
This commit is contained in:
Julia Reynolds
2021-06-02 15:35:55 -04:00
parent c0c482736b
commit f13f4eb0fe
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
if (!n.isForegroundService()) {
int count = getNotificationCountLocked(pkg, userId, id, tag);
int count = getNotificationCount(pkg, userId, id, tag);
if (count >= MAX_PACKAGE_NOTIFICATIONS) {
mUsageStats.registerOverCountQuota(pkg);
Slog.e(TAG, "Package has already posted or enqueued " + count
@@ -6647,28 +6647,29 @@ public class NotificationManagerService extends SystemService {
return true;
}
@GuardedBy("mNotificationLock")
protected int getNotificationCountLocked(String pkg, int userId, int excludedId,
protected int getNotificationCount(String pkg, int userId, int excludedId,
String excludedTag) {
int count = 0;
final int N = mNotificationList.size();
for (int i = 0; i < N; i++) {
final NotificationRecord existing = mNotificationList.get(i);
if (existing.getSbn().getPackageName().equals(pkg)
&& existing.getSbn().getUserId() == userId) {
if (existing.getSbn().getId() == excludedId
&& TextUtils.equals(existing.getSbn().getTag(), excludedTag)) {
continue;
synchronized (mNotificationLock) {
final int N = mNotificationList.size();
for (int i = 0; i < N; i++) {
final NotificationRecord existing = mNotificationList.get(i);
if (existing.getSbn().getPackageName().equals(pkg)
&& existing.getSbn().getUserId() == userId) {
if (existing.getSbn().getId() == excludedId
&& TextUtils.equals(existing.getSbn().getTag(), excludedTag)) {
continue;
}
count++;
}
count++;
}
}
final int M = mEnqueuedNotifications.size();
for (int i = 0; i < M; i++) {
final NotificationRecord existing = mEnqueuedNotifications.get(i);
if (existing.getSbn().getPackageName().equals(pkg)
&& existing.getSbn().getUserId() == userId) {
count++;
final int M = mEnqueuedNotifications.size();
for (int i = 0; i < M; i++) {
final NotificationRecord existing = mEnqueuedNotifications.get(i);
if (existing.getSbn().getPackageName().equals(pkg)
&& existing.getSbn().getUserId() == userId) {
count++;
}
}
}
return count;

View File

@@ -3394,16 +3394,16 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
// anything that's currently enqueued or posted
int userId = UserHandle.getUserId(mUid);
assertEquals(40,
mService.getNotificationCountLocked(PKG, userId, 0, null));
mService.getNotificationCount(PKG, userId, 0, null));
assertEquals(40,
mService.getNotificationCountLocked(PKG, userId, 0, "tag2"));
mService.getNotificationCount(PKG, userId, 0, "tag2"));
// return all for package "a" - "banana" tag isn't used
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
assertEquals(39, mService.getNotificationCountLocked(
assertEquals(39, mService.getNotificationCount(
PKG, userId, sampleIdToExclude, sampleTagToExclude));
}