From 302b668d585fba903b38ca3486394ff45668c8cc Mon Sep 17 00:00:00 2001 From: Philip Date: Fri, 7 Oct 2016 23:54:15 +0100 Subject: [PATCH] On removing notification, mark it's ID no longer in use (#1677) On removing notification, mark it's ID no longer in use Fixes #1662 --- .../fsck/k9/notification/NotificationData.java | 6 ++++++ .../k9/notification/NotificationDataTest.java | 15 ++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/notification/NotificationData.java b/k9mail/src/main/java/com/fsck/k9/notification/NotificationData.java index 1ea9dbc4a..d390b7de6 100644 --- a/k9mail/src/main/java/com/fsck/k9/notification/NotificationData.java +++ b/k9mail/src/main/java/com/fsck/k9/notification/NotificationData.java @@ -85,6 +85,10 @@ class NotificationData { notificationIdsInUse.put(notificationId, true); } + private void markNotificationIdAsFree(int notificationId) { + notificationIdsInUse.delete(notificationId); + } + NotificationHolder createNotificationHolder(int notificationId, NotificationContent content) { return new NotificationHolder(notificationId, content); } @@ -165,6 +169,8 @@ class NotificationData { activeNotifications.remove(holder); int notificationId = holder.notificationId; + markNotificationIdAsFree(notificationId); + if (!additionalNotifications.isEmpty()) { NotificationContent newContent = additionalNotifications.removeFirst(); NotificationHolder replacement = createNotificationHolder(notificationId, newContent); diff --git a/k9mail/src/test/java/com/fsck/k9/notification/NotificationDataTest.java b/k9mail/src/test/java/com/fsck/k9/notification/NotificationDataTest.java index bd30637d6..e56fe0e95 100644 --- a/k9mail/src/test/java/com/fsck/k9/notification/NotificationDataTest.java +++ b/k9mail/src/test/java/com/fsck/k9/notification/NotificationDataTest.java @@ -5,6 +5,7 @@ import java.util.List; import com.fsck.k9.Account; import com.fsck.k9.activity.MessageReference; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -52,8 +53,7 @@ public class NotificationDataTest { @Test public void testAddNotificationContentWithReplacingNotification() throws Exception { - NotificationContent content = createNotificationContent("1"); - notificationData.addNotificationContent(content); + notificationData.addNotificationContent(createNotificationContent("1")); notificationData.addNotificationContent(createNotificationContent("2")); notificationData.addNotificationContent(createNotificationContent("3")); notificationData.addNotificationContent(createNotificationContent("4")); @@ -107,6 +107,15 @@ public class NotificationDataTest { assertEquals(content, holder.content); } + @Test + public void testRemoveDoesNotLeakNotificationIds() { + for (int i = 1; i <= NotificationData.MAX_NUMBER_OF_STACKED_NOTIFICATIONS + 1; i++) { + NotificationContent content = createNotificationContent("" + i); + notificationData.addNotificationContent(content); + notificationData.removeNotificationForMessage(content.messageReference); + } + } + @Test public void testNewMessagesCount() throws Exception { assertEquals(0, notificationData.getNewMessagesCount()); @@ -267,7 +276,7 @@ public class NotificationDataTest { private MessageReference createMessageReference(String uid) { return new MessageReference(ACCOUNT_UUID, FOLDER_NAME, uid, null); } - + private NotificationContent createNotificationContent(String uid) { MessageReference messageReference = createMessageReference(uid); return createNotificationContent(messageReference);