From bee13b63051b637c7961a575a45bf79aefb1ce8a Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Fri, 15 Apr 2022 10:18:19 -0700 Subject: [PATCH] Catch SecurityException from NoMan to workaround possible race condition Bug: 63636581 Bug: 227445069 Test: n/a issue not reproducible Change-Id: Ib3dcf013ba9220d3d2d59032163191f0aef5d062 --- .../notification/NotificationListener.java | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/com/android/launcher3/notification/NotificationListener.java b/src/com/android/launcher3/notification/NotificationListener.java index bbeb886f3c..04eb38a3c5 100644 --- a/src/com/android/launcher3/notification/NotificationListener.java +++ b/src/com/android/launcher3/notification/NotificationListener.java @@ -35,6 +35,7 @@ import android.util.Log; import android.util.Pair; import androidx.annotation.AnyThread; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.WorkerThread; @@ -154,14 +155,9 @@ public class NotificationListener extends NotificationListenerService { case MSG_NOTIFICATION_FULL_REFRESH: List 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 @@ public class NotificationListener extends NotificationListenerService { } 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 @@ public class NotificationListener extends NotificationListenerService { 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 @@ public class NotificationListener extends NotificationListenerService { */ @WorkerThread public List getNotificationsForKeys(List 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))); } /**