Merge "Catch SecurityException from NoMan to workaround possible race condition" into tm-dev am: 1cd4d369a2

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Launcher3/+/17775689

Change-Id: If53aeb575fac03a7bd91e9c8fc1d480eaaf844ce
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/src/com/android/launcher3/notification/NotificationListener.java b/src/com/android/launcher3/notification/NotificationListener.java
index bbeb886..04eb38a 100644
--- a/src/com/android/launcher3/notification/NotificationListener.java
+++ b/src/com/android/launcher3/notification/NotificationListener.java
@@ -35,6 +35,7 @@
 import android.util.Pair;
 
 import androidx.annotation.AnyThread;
+import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 import androidx.annotation.WorkerThread;
 
@@ -154,14 +155,9 @@
             case MSG_NOTIFICATION_FULL_REFRESH:
                 List<StatusBarNotification> activeNotifications = null;
                 if (sIsConnected) {
-                    try {
-                        activeNotifications = Arrays.stream(getActiveNotifications())
-                                .filter(this::notificationIsValidForUI)
-                                .collect(Collectors.toList());
-                    } catch (SecurityException ex) {
-                        Log.e(TAG, "SecurityException: failed to fetch notifications");
-                        activeNotifications = new ArrayList<>();
-                    }
+                    activeNotifications = Arrays.stream(getActiveNotificationsSafely(null))
+                            .filter(this::notificationIsValidForUI)
+                            .collect(Collectors.toList());
                 } else {
                     activeNotifications = new ArrayList<>();
                 }
@@ -175,7 +171,7 @@
             }
             case MSG_RANKING_UPDATE: {
                 String[] keys = ((RankingMap) message.obj).getOrderedKeys();
-                for (StatusBarNotification sbn : getActiveNotifications(keys)) {
+                for (StatusBarNotification sbn : getActiveNotificationsSafely(keys)) {
                     updateGroupKeyIfNecessary(sbn);
                 }
                 return true;
@@ -214,6 +210,16 @@
         return true;
     }
 
+    private @NonNull StatusBarNotification[] getActiveNotificationsSafely(@Nullable String[] keys) {
+        StatusBarNotification[] result = null;
+        try {
+            result = getActiveNotifications(keys);
+        } catch (SecurityException e) {
+            Log.e(TAG, "SecurityException: failed to fetch notifications");
+        }
+        return result == null ? new StatusBarNotification[0] : result;
+    }
+
     @Override
     public void onListenerConnected() {
         super.onListenerConnected();
@@ -313,9 +319,8 @@
      */
     @WorkerThread
     public List<StatusBarNotification> getNotificationsForKeys(List<NotificationKeyData> keys) {
-        StatusBarNotification[] notifications = getActiveNotifications(
-                keys.stream().map(n -> n.notificationKey).toArray(String[]::new));
-        return notifications == null ? Collections.emptyList() : Arrays.asList(notifications);
+        return Arrays.asList(getActiveNotificationsSafely(
+                keys.stream().map(n -> n.notificationKey).toArray(String[]::new)));
     }
 
     /**