From 7aa922c912230ada9812f9f61c93594635d1b0ec Mon Sep 17 00:00:00 2001 From: Tony Date: Tue, 17 Apr 2018 18:54:10 -0700 Subject: [PATCH] Make sure notification listener stays unbound on reboot when badging disabled We were requesting unbind in onCreate(), but the NotificationListenerService documentation for requestUnbind() clearly states "The service should wait for the {@link #onListenerConnected()} event before performing this operation. I know it's tempting, but you must wait." I was tempted, and I did not wait. :( The fact that the notification listener was binding even though the setting was off was not only inefficient, but also had at least one user-visible bug: because secure settings are set per user, the global badging setting actually only applies canShowBadge = false for user 0; other users such as work profile still show badges. Repro steps: 1. Have a work profile 2. Get a notification on work profile app and normal app 3. Turn off global badging setting ("Allow notification dots" from home settings) 4. Reboot the device In this case, we get onCreate, call requestUnbind() which is ignored since we aren't bound, then get onBind() and onListenerConnected() etc. Thus the work profile app has a notification dot and other apps don't. Bug: 71545493 Change-Id: I7f7dc219b25c28257f8b98fba7e362b99d3cba45 --- .../notification/NotificationListener.java | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/com/android/launcher3/notification/NotificationListener.java b/src/com/android/launcher3/notification/NotificationListener.java index 117287ba7b..b527b6a8d2 100644 --- a/src/com/android/launcher3/notification/NotificationListener.java +++ b/src/com/android/launcher3/notification/NotificationListener.java @@ -150,22 +150,12 @@ public class NotificationListener extends NotificationListenerService { public void onCreate() { super.onCreate(); sIsCreated = true; - mNotificationBadgingObserver = new SettingsObserver.Secure(getContentResolver()) { - @Override - public void onSettingChanged(boolean isNotificationBadgingEnabled) { - if (!isNotificationBadgingEnabled) { - requestUnbind(); - } - } - }; - mNotificationBadgingObserver.register(NOTIFICATION_BADGING); } @Override public void onDestroy() { super.onDestroy(); sIsCreated = false; - mNotificationBadgingObserver.unregister(); } public static @Nullable NotificationListener getInstanceIfConnected() { @@ -203,6 +193,17 @@ public class NotificationListener extends NotificationListenerService { public void onListenerConnected() { super.onListenerConnected(); sIsConnected = true; + + mNotificationBadgingObserver = new SettingsObserver.Secure(getContentResolver()) { + @Override + public void onSettingChanged(boolean isNotificationBadgingEnabled) { + if (!isNotificationBadgingEnabled) { + requestUnbind(); + } + } + }; + mNotificationBadgingObserver.register(NOTIFICATION_BADGING); + onNotificationFullRefresh(); } @@ -214,6 +215,7 @@ public class NotificationListener extends NotificationListenerService { public void onListenerDisconnected() { super.onListenerDisconnected(); sIsConnected = false; + mNotificationBadgingObserver.unregister(); } @Override