OSDN Git Service

Remove badges when launcher loses notification access
authorTony <twickham@google.com>
Mon, 13 Feb 2017 18:17:37 +0000 (10:17 -0800)
committerTony Wickham <twickham@google.com>
Tue, 21 Feb 2017 16:44:28 +0000 (08:44 -0800)
- 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

src/com/android/launcher3/notification/NotificationListener.java
src/com/android/launcher3/popup/PopupDataProvider.java

index 5c16176..a627d23 100644 (file)
@@ -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<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 @@ 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
index e314b64..ee2930f 100644 (file)
@@ -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<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;
         }