Merge "Fix nomanservice notificationrecord leak" into oc-dev

This commit is contained in:
TreeHugger Robot
2017-09-18 17:35:06 +00:00
committed by Android (Google) Code Review
2 changed files with 74 additions and 3 deletions

View File

@@ -519,7 +519,8 @@ public class NotificationManagerService extends SystemService {
} }
} }
private final NotificationDelegate mNotificationDelegate = new NotificationDelegate() { @VisibleForTesting
final NotificationDelegate mNotificationDelegate = new NotificationDelegate() {
@Override @Override
public void onSetDisabled(int status) { public void onSetDisabled(int status) {
@@ -1010,6 +1011,25 @@ public class NotificationManagerService extends SystemService {
mScreenOn = on; mScreenOn = on;
} }
@VisibleForTesting
int getNotificationRecordCount() {
synchronized (mNotificationLock) {
int count = mNotificationList.size() + mNotificationsByKey.size()
+ mSummaryByGroupKey.size() + mEnqueuedNotifications.size();
// subtract duplicates
for (NotificationRecord posted : mNotificationList) {
if (mNotificationsByKey.containsKey(posted.getKey())) {
count--;
}
if (posted.sbn.isGroup() && posted.getNotification().isGroupSummary()) {
count --;
}
}
return count;
}
}
@VisibleForTesting @VisibleForTesting
void addNotification(NotificationRecord r) { void addNotification(NotificationRecord r) {
mNotificationList.add(r); mNotificationList.add(r);
@@ -4506,6 +4526,7 @@ public class NotificationManagerService extends SystemService {
canceledNotifications = new ArrayList<>(); canceledNotifications = new ArrayList<>();
} }
notificationList.remove(i); notificationList.remove(i);
mNotificationsByKey.remove(r.getKey());
canceledNotifications.add(r); canceledNotifications.add(r);
cancelNotificationLocked(r, sendDelete, reason, wasPosted); cancelNotificationLocked(r, sendDelete, reason, wasPosted);
} }
@@ -4615,6 +4636,7 @@ public class NotificationManagerService extends SystemService {
EventLogTags.writeNotificationCancel(callingUid, callingPid, pkg, childSbn.getId(), EventLogTags.writeNotificationCancel(callingUid, callingPid, pkg, childSbn.getId(),
childSbn.getTag(), userId, 0, 0, reason, listenerName); childSbn.getTag(), userId, 0, 0, reason, listenerName);
notificationList.remove(i); notificationList.remove(i);
mNotificationsByKey.remove(childR.getKey());
cancelNotificationLocked(childR, sendDelete, reason, wasPosted); cancelNotificationLocked(childR, sendDelete, reason, wasPosted);
} }
} }

View File

@@ -302,9 +302,9 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
generateNotificationRecord(null).getNotification(), 0); generateNotificationRecord(null).getNotification(), 0);
waitForIdle(); waitForIdle();
StatusBarNotification[] notifs = StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG);
mBinderService.getActiveNotifications(PKG);
assertEquals(1, notifs.length); assertEquals(1, notifs.length);
assertEquals(1, mNotificationManagerService.getNotificationRecordCount());
} }
@Test @Test
@@ -316,6 +316,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
StatusBarNotification[] notifs = StatusBarNotification[] notifs =
mBinderService.getActiveNotifications(PKG); mBinderService.getActiveNotifications(PKG);
assertEquals(0, notifs.length); assertEquals(0, notifs.length);
assertEquals(0, mNotificationManagerService.getNotificationRecordCount());
} }
@Test @Test
@@ -330,6 +331,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
StatusBarNotification[] notifs = StatusBarNotification[] notifs =
mBinderService.getActiveNotifications(PKG); mBinderService.getActiveNotifications(PKG);
assertEquals(0, notifs.length); assertEquals(0, notifs.length);
assertEquals(0, mNotificationManagerService.getNotificationRecordCount());
} }
@Test @Test
@@ -342,6 +344,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
StatusBarNotification[] notifs = StatusBarNotification[] notifs =
mBinderService.getActiveNotifications(sbn.getPackageName()); mBinderService.getActiveNotifications(sbn.getPackageName());
assertEquals(0, notifs.length); assertEquals(0, notifs.length);
assertEquals(0, mNotificationManagerService.getNotificationRecordCount());
} }
@Test @Test
@@ -354,6 +357,43 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
StatusBarNotification[] notifs = StatusBarNotification[] notifs =
mBinderService.getActiveNotifications(sbn.getPackageName()); mBinderService.getActiveNotifications(sbn.getPackageName());
assertEquals(0, notifs.length); assertEquals(0, notifs.length);
assertEquals(0, mNotificationManagerService.getNotificationRecordCount());
}
@Test
public void testUserInitiatedClearAll_noLeak() throws Exception {
final NotificationRecord n = generateNotificationRecord(
mTestNotificationChannel, 1, "group", true);
mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
n.sbn.getId(), n.sbn.getNotification(), n.sbn.getUserId());
waitForIdle();
mNotificationManagerService.mNotificationDelegate.onClearAll(uid, Binder.getCallingPid(),
n.getUserId());
waitForIdle();
StatusBarNotification[] notifs =
mBinderService.getActiveNotifications(n.sbn.getPackageName());
assertEquals(0, notifs.length);
assertEquals(0, mNotificationManagerService.getNotificationRecordCount());
}
@Test
public void testCancelAllNotificationsCancelsChildren() throws Exception {
final NotificationRecord parent = generateNotificationRecord(
mTestNotificationChannel, 1, "group1", true);
final NotificationRecord child = generateNotificationRecord(
mTestNotificationChannel, 2, "group1", false);
mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
parent.sbn.getId(), parent.sbn.getNotification(), parent.sbn.getUserId());
mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
child.sbn.getId(), child.sbn.getNotification(), child.sbn.getUserId());
waitForIdle();
mBinderService.cancelAllNotifications(PKG, parent.sbn.getUserId());
waitForIdle();
assertEquals(0, mNotificationManagerService.getNotificationRecordCount());
} }
@Test @Test
@@ -365,6 +405,8 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
} }
mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); mBinderService.cancelAllNotifications(PKG, sbn.getUserId());
waitForIdle(); waitForIdle();
assertEquals(0, mNotificationManagerService.getNotificationRecordCount());
} }
@Test @Test
@@ -391,6 +433,8 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
parentAsChild.sbn.getId(), parentAsChild.sbn.getNotification(), parentAsChild.sbn.getId(), parentAsChild.sbn.getNotification(),
parentAsChild.sbn.getUserId()); parentAsChild.sbn.getUserId());
waitForIdle(); waitForIdle();
assertEquals(0, mNotificationManagerService.getNotificationRecordCount());
} }
@Test @Test
@@ -404,6 +448,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
StatusBarNotification[] notifs = StatusBarNotification[] notifs =
mBinderService.getActiveNotifications(sbn.getPackageName()); mBinderService.getActiveNotifications(sbn.getPackageName());
assertEquals(1, notifs.length); assertEquals(1, notifs.length);
assertEquals(1, mNotificationManagerService.getNotificationRecordCount());
} }
@Test @Test
@@ -417,6 +462,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
StatusBarNotification[] notifs = StatusBarNotification[] notifs =
mBinderService.getActiveNotifications(sbn.getPackageName()); mBinderService.getActiveNotifications(sbn.getPackageName());
assertEquals(1, notifs.length); assertEquals(1, notifs.length);
assertEquals(1, mNotificationManagerService.getNotificationRecordCount());
} }
@Test @Test
@@ -429,6 +475,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
StatusBarNotification[] notifs = StatusBarNotification[] notifs =
mBinderService.getActiveNotifications(sbn.getPackageName()); mBinderService.getActiveNotifications(sbn.getPackageName());
assertEquals(0, notifs.length); assertEquals(0, notifs.length);
assertEquals(0, mNotificationManagerService.getNotificationRecordCount());
} }
@Test @Test
@@ -442,6 +489,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
StatusBarNotification[] notifs = StatusBarNotification[] notifs =
mBinderService.getActiveNotifications(sbn.getPackageName()); mBinderService.getActiveNotifications(sbn.getPackageName());
assertEquals(1, notifs.length); assertEquals(1, notifs.length);
assertEquals(1, mNotificationManagerService.getNotificationRecordCount());
} }
@Test @Test
@@ -471,6 +519,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
mBinderService.cancelNotificationWithTag(PKG, "tag", sbn.getId(), sbn.getUserId()); mBinderService.cancelNotificationWithTag(PKG, "tag", sbn.getId(), sbn.getUserId());
waitForIdle(); waitForIdle();
assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length);
assertEquals(0, mNotificationManagerService.getNotificationRecordCount());
} }
@Test @Test