From be3c7d0c56fe35e8822c4bb5ee22ad44ed504850 Mon Sep 17 00:00:00 2001 From: Tony Date: Mon, 13 Feb 2017 10:17:37 -0800 Subject: [PATCH] Remove badges when launcher loses notification access - NotificationListener.getInstance() has been changed to getInstanceIfConnected() (same behavior as before). - When starting launcher, we send a full refresh of badges regardless of whether the NotificationListner is connected. If it is not connected, we pass an empty list for the active notifications, so that all pre-existing badges are removed. Bug: 35221052 Change-Id: If920317f10814c010e02b5a30ce86a58ac7bc61c --- .../notification/NotificationListener.java | 22 ++++++++++++---------- .../android/launcher3/popup/PopupDataProvider.java | 6 +++--- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/com/android/launcher3/notification/NotificationListener.java b/src/com/android/launcher3/notification/NotificationListener.java index 5c16176f5..a627d238e 100644 --- a/src/com/android/launcher3/notification/NotificationListener.java +++ b/src/com/android/launcher3/notification/NotificationListener.java @@ -40,7 +40,7 @@ import java.util.Set; * A {@link NotificationListenerService} that sends updates to its * {@link NotificationsChangedListener} when notifications are posted or canceled, * as well and when this service first connects. An instance of NotificationListener, - * and its methods for getting notifications, can be obtained via {@link #getInstance()}. + * and its methods for getting notifications, can be obtained via {@link #getInstanceIfConnected()}. */ public class NotificationListener extends NotificationListenerService { @@ -50,6 +50,7 @@ public class NotificationListener extends NotificationListenerService { private static NotificationListener sNotificationListenerInstance = null; private static NotificationsChangedListener sNotificationsChangedListener; + private static boolean sIsConnected; private final Handler mWorkerHandler; private final Handler mUiHandler; @@ -65,8 +66,9 @@ public class NotificationListener extends NotificationListenerService { mUiHandler.obtainMessage(message.what, message.obj).sendToTarget(); break; case MSG_NOTIFICATION_FULL_REFRESH: - final List activeNotifications - = filterNotifications(getActiveNotifications()); + final List activeNotifications = sIsConnected + ? filterNotifications(getActiveNotifications()) + : new ArrayList(); mUiHandler.obtainMessage(message.what, activeNotifications).sendToTarget(); break; } @@ -107,10 +109,11 @@ public class NotificationListener extends NotificationListenerService { super(); mWorkerHandler = new Handler(LauncherModel.getWorkerLooper(), mWorkerCallback); mUiHandler = new Handler(Looper.getMainLooper(), mUiCallback); + sNotificationListenerInstance = this; } - public static @Nullable NotificationListener getInstance() { - return sNotificationListenerInstance; + public static @Nullable NotificationListener getInstanceIfConnected() { + return sIsConnected ? sNotificationListenerInstance : null; } public static void setNotificationsChangedListener(NotificationsChangedListener listener) { @@ -119,9 +122,8 @@ public class NotificationListener extends NotificationListenerService { } sNotificationsChangedListener = listener; - NotificationListener notificationListener = getInstance(); - if (notificationListener != null) { - notificationListener.onNotificationFullRefresh(); + if (sNotificationListenerInstance != null) { + sNotificationListenerInstance.onNotificationFullRefresh(); } } @@ -132,7 +134,7 @@ public class NotificationListener extends NotificationListenerService { @Override public void onListenerConnected() { super.onListenerConnected(); - sNotificationListenerInstance = this; + sIsConnected = true; onNotificationFullRefresh(); } @@ -143,7 +145,7 @@ public class NotificationListener extends NotificationListenerService { @Override public void onListenerDisconnected() { super.onListenerDisconnected(); - sNotificationListenerInstance = null; + sIsConnected = false; } @Override diff --git a/src/com/android/launcher3/popup/PopupDataProvider.java b/src/com/android/launcher3/popup/PopupDataProvider.java index e314b646b..ee2930f19 100644 --- a/src/com/android/launcher3/popup/PopupDataProvider.java +++ b/src/com/android/launcher3/popup/PopupDataProvider.java @@ -172,7 +172,7 @@ public class PopupDataProvider implements NotificationListener.NotificationsChan private boolean updateBadgeIcon(BadgeInfo badgeInfo) { boolean hadNotificationToShow = badgeInfo.hasNotificationToShow(); NotificationInfo notificationInfo = null; - NotificationListener notificationListener = NotificationListener.getInstance(); + NotificationListener notificationListener = NotificationListener.getInstanceIfConnected(); if (notificationListener != null && badgeInfo.getNotificationKeys().size() == 1) { StatusBarNotification[] activeNotifications = notificationListener .getActiveNotifications(new String[] {badgeInfo.getNotificationKeys().get(0)}); @@ -222,13 +222,13 @@ public class PopupDataProvider implements NotificationListener.NotificationsChan /** This makes a potentially expensive binder call and should be run on a background thread. */ public List getStatusBarNotificationsForKeys(String[] notificationKeys) { - NotificationListener notificationListener = NotificationListener.getInstance(); + NotificationListener notificationListener = NotificationListener.getInstanceIfConnected(); return notificationListener == null ? Collections.EMPTY_LIST : notificationListener.getNotificationsForKeys(notificationKeys); } public void cancelNotification(String notificationKey) { - NotificationListener notificationListener = NotificationListener.getInstance(); + NotificationListener notificationListener = NotificationListener.getInstanceIfConnected(); if (notificationListener == null) { return; } -- 2.11.0