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
diff --git a/src/com/android/launcher3/notification/NotificationListener.java b/src/com/android/launcher3/notification/NotificationListener.java
index 5c16176..a627d23 100644
--- a/src/com/android/launcher3/notification/NotificationListener.java
+++ b/src/com/android/launcher3/notification/NotificationListener.java
@@ -40,7 +40,7 @@
* 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 @@
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 @@
mUiHandler.obtainMessage(message.what, message.obj).sendToTarget();
break;
case MSG_NOTIFICATION_FULL_REFRESH:
- final List<StatusBarNotification> activeNotifications
- = filterNotifications(getActiveNotifications());
+ final List<StatusBarNotification> activeNotifications = sIsConnected
+ ? filterNotifications(getActiveNotifications())
+ : new ArrayList<StatusBarNotification>();
mUiHandler.obtainMessage(message.what, activeNotifications).sendToTarget();
break;
}
@@ -107,10 +109,11 @@
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 @@
}
sNotificationsChangedListener = listener;
- NotificationListener notificationListener = getInstance();
- if (notificationListener != null) {
- notificationListener.onNotificationFullRefresh();
+ if (sNotificationListenerInstance != null) {
+ sNotificationListenerInstance.onNotificationFullRefresh();
}
}
@@ -132,7 +134,7 @@
@Override
public void onListenerConnected() {
super.onListenerConnected();
- sNotificationListenerInstance = this;
+ sIsConnected = true;
onNotificationFullRefresh();
}
@@ -143,7 +145,7 @@
@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 e314b64..ee2930f 100644
--- a/src/com/android/launcher3/popup/PopupDataProvider.java
+++ b/src/com/android/launcher3/popup/PopupDataProvider.java
@@ -172,7 +172,7 @@
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 @@
/** This makes a potentially expensive binder call and should be run on a background thread. */
public List<StatusBarNotification> 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;
}