From 580edcf529b0755f2bba442e97dd02d8cdad3b82 Mon Sep 17 00:00:00 2001 From: Tony Wickham Date: Thu, 9 Feb 2017 08:28:52 -0800 Subject: [PATCH] Create a singleton HashMap instead of using Collections.singleton() The Set returned by Collections.singleton() doesn't support all operations, causing crashes in certain situations (namely, whenever a notification is updated rather than added or removed). Change-Id: Ie104b7f99c4a32db5f1f7e43ec3775d34dc26ce1 --- src/com/android/launcher3/Utilities.java | 11 +++++++++++ .../android/launcher3/popup/PopupDataProvider.java | 6 ++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/com/android/launcher3/Utilities.java b/src/com/android/launcher3/Utilities.java index 267cb2aaa9..be3297c064 100644 --- a/src/com/android/launcher3/Utilities.java +++ b/src/com/android/launcher3/Utilities.java @@ -60,6 +60,7 @@ import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Collection; +import java.util.HashSet; import java.util.Locale; import java.util.Set; import java.util.concurrent.Executor; @@ -648,4 +649,14 @@ public final class Utilities { throw new RuntimeException(e); } } + + /** + * Returns a HashSet with a single element. We use this instead of Collections.singleton() + * because HashSet ensures all operations, such as remove, are supported. + */ + public static HashSet singletonHashSet(T elem) { + HashSet hashSet = new HashSet<>(1); + hashSet.add(elem); + return hashSet; + } } diff --git a/src/com/android/launcher3/popup/PopupDataProvider.java b/src/com/android/launcher3/popup/PopupDataProvider.java index 4f3b8a6b1a..c754fda990 100644 --- a/src/com/android/launcher3/popup/PopupDataProvider.java +++ b/src/com/android/launcher3/popup/PopupDataProvider.java @@ -22,6 +22,7 @@ import android.util.Log; import com.android.launcher3.ItemInfo; import com.android.launcher3.Launcher; +import com.android.launcher3.Utilities; import com.android.launcher3.badge.BadgeInfo; import com.android.launcher3.notification.NotificationInfo; import com.android.launcher3.notification.NotificationListener; @@ -68,7 +69,8 @@ public class PopupDataProvider implements NotificationListener.NotificationsChan } else { notificationWasAdded = badgeInfo.addNotificationKeyIfNotExists(notificationKey); } - updateLauncherIconBadges(Collections.singleton(postedPackageUserKey), notificationWasAdded); + updateLauncherIconBadges(Utilities.singletonHashSet(postedPackageUserKey), + notificationWasAdded); } @Override @@ -78,7 +80,7 @@ public class PopupDataProvider implements NotificationListener.NotificationsChan if (oldBadgeInfo.getNotificationCount() == 0) { mPackageUserToBadgeInfos.remove(removedPackageUserKey); } - updateLauncherIconBadges(Collections.singleton(removedPackageUserKey)); + updateLauncherIconBadges(Utilities.singletonHashSet(removedPackageUserKey)); PopupContainerWithArrow openContainer = PopupContainerWithArrow.getOpen(mLauncher); if (openContainer != null) {