From 360c2b2a5090b0be87dd8a03245e5f198305c590 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Tue, 17 Jan 2017 20:41:24 -0800 Subject: [PATCH] This attempts to work around a ROM crash bug getActiveNotifications() seems to throw an NPE on some Motorola ROMs, all of which appear to be 6.0.1. This change just swallows the exception. 6.0 doesn't support bundled notifications, so I think it's alright if they don't get canceled, since the summary notification will still be displayed correctly. This would only affect users who have an android wear device attached to one of these buggy ROMs. By swallowing this exception, they would not always get notifictions dismissed on their wear device. Fixes #6043 // FREEBIE --- .../notifications/MessageNotifier.java | 45 ++++++++++++------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java index 0325a5f8ad..5b8f91ed45 100644 --- a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java +++ b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java @@ -130,36 +130,47 @@ public class MessageNotifier { notifications.cancel(SUMMARY_NOTIFICATION_ID); if (Build.VERSION.SDK_INT >= 23) { - StatusBarNotification[] activeNotifications = notifications.getActiveNotifications(); + try { + StatusBarNotification[] activeNotifications = notifications.getActiveNotifications(); - for (StatusBarNotification activeNotification : activeNotifications) { - if (activeNotification.getId() != NotificationBarManager.RED_PHONE_NOTIFICATION) { - notifications.cancel(activeNotification.getId()); + for (StatusBarNotification activeNotification : activeNotifications) { + if (activeNotification.getId() != NotificationBarManager.RED_PHONE_NOTIFICATION) { + notifications.cancel(activeNotification.getId()); + } } + } catch (Throwable e) { + // XXX Appears to be a ROM bug, see #6043 + Log.w(TAG, e); + notifications.cancelAll(); } } } private static void cancelOrphanedNotifications(@NonNull Context context, NotificationState notificationState) { if (Build.VERSION.SDK_INT >= 23) { - NotificationManager notifications = ServiceUtil.getNotificationManager(context); - StatusBarNotification[] activeNotifications = notifications.getActiveNotifications(); + try { + NotificationManager notifications = ServiceUtil.getNotificationManager(context); + StatusBarNotification[] activeNotifications = notifications.getActiveNotifications(); - for (StatusBarNotification notification : activeNotifications) { - boolean validNotification = false; + for (StatusBarNotification notification : activeNotifications) { + boolean validNotification = false; - if (notification.getId() != SUMMARY_NOTIFICATION_ID && notification.getId() != NotificationBarManager.RED_PHONE_NOTIFICATION) { - for (NotificationItem item : notificationState.getNotifications()) { - if (notification.getId() == (SUMMARY_NOTIFICATION_ID + item.getThreadId())) { - validNotification = true; - break; + if (notification.getId() != SUMMARY_NOTIFICATION_ID && notification.getId() != NotificationBarManager.RED_PHONE_NOTIFICATION) { + for (NotificationItem item : notificationState.getNotifications()) { + if (notification.getId() == (SUMMARY_NOTIFICATION_ID + item.getThreadId())) { + validNotification = true; + break; + } + } + + if (!validNotification) { + notifications.cancel(notification.getId()); } } - - if (!validNotification) { - notifications.cancel(notification.getId()); - } } + } catch (Throwable e) { + // XXX Android ROM Bug, see #6043 + Log.w(TAG, e); } } }