Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 16 | package com.android.systemui.statusbar.notification; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 17 | |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 18 | import static android.service.notification.NotificationListenerService.REASON_CANCEL; |
| 19 | import static android.service.notification.NotificationListenerService.REASON_ERROR; |
| 20 | |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 22 | import android.app.Notification; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 23 | import android.content.Context; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 24 | import android.service.notification.NotificationListenerService; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 25 | import android.service.notification.StatusBarNotification; |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 26 | import android.util.ArrayMap; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 27 | import android.util.Log; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 28 | |
Julia Reynolds | 9159006 | 2018-04-02 16:24:11 -0400 | [diff] [blame] | 29 | import com.android.internal.annotations.VisibleForTesting; |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 30 | import com.android.internal.statusbar.NotificationVisibility; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 31 | import com.android.systemui.Dependency; |
| 32 | import com.android.systemui.Dumpable; |
Kevin | 38ce6fa | 2018-10-17 16:00:14 -0700 | [diff] [blame] | 33 | import com.android.systemui.statusbar.NotificationLifetimeExtender; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 34 | import com.android.systemui.statusbar.NotificationPresenter; |
| 35 | import com.android.systemui.statusbar.NotificationRemoteInputManager; |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 36 | import com.android.systemui.statusbar.NotificationRemoveInterceptor; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 37 | import com.android.systemui.statusbar.NotificationUiAdjustment; |
| 38 | import com.android.systemui.statusbar.NotificationUpdateHandler; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 39 | import com.android.systemui.statusbar.notification.collection.NotificationData; |
| 40 | import com.android.systemui.statusbar.notification.collection.NotificationData.KeyguardEnvironment; |
| 41 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Ned Burns | c586467 | 2019-02-20 12:57:29 -0500 | [diff] [blame] | 42 | import com.android.systemui.statusbar.notification.collection.NotificationRowBinder; |
Gustav Sennton | f892fe9 | 2019-01-22 15:31:42 +0000 | [diff] [blame] | 43 | import com.android.systemui.statusbar.notification.logging.NotificationLogger; |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 44 | import com.android.systemui.statusbar.notification.row.NotificationContentInflater; |
| 45 | import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 46 | import com.android.systemui.statusbar.notification.stack.NotificationListContainer; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 47 | import com.android.systemui.statusbar.policy.HeadsUpManager; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 48 | import com.android.systemui.util.leak.LeakDetector; |
| 49 | |
| 50 | import java.io.FileDescriptor; |
| 51 | import java.io.PrintWriter; |
| 52 | import java.util.ArrayList; |
| 53 | import java.util.HashMap; |
| 54 | import java.util.List; |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 55 | import java.util.Map; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * NotificationEntryManager is responsible for the adding, removing, and updating of notifications. |
| 59 | * It also handles tasks such as their inflation and their interaction with other |
| 60 | * Notification.*Manager objects. |
| 61 | */ |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 62 | public class NotificationEntryManager implements |
| 63 | Dumpable, |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 64 | NotificationContentInflater.InflationCallback, |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 65 | NotificationUpdateHandler, |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 66 | VisualStabilityManager.Callback { |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 67 | private static final String TAG = "NotificationEntryMgr"; |
Ned Burns | d35708c | 2019-01-09 15:38:03 -0500 | [diff] [blame] | 68 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Eliot Courtney | 6c313d3 | 2017-12-14 19:57:51 +0900 | [diff] [blame] | 69 | |
Mady Mellor | 1a4e86f | 2019-05-03 16:07:23 -0700 | [diff] [blame] | 70 | /** |
| 71 | * Used when a notification is removed and it doesn't have a reason that maps to one of the |
| 72 | * reasons defined in NotificationListenerService |
| 73 | * (e.g. {@link NotificationListenerService.REASON_CANCEL}) |
| 74 | */ |
| 75 | public static final int UNDEFINED_DISMISS_REASON = 0; |
| 76 | |
Ned Burns | d35708c | 2019-01-09 15:38:03 -0500 | [diff] [blame] | 77 | @VisibleForTesting |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 78 | protected final HashMap<String, NotificationEntry> mPendingNotifications = new HashMap<>(); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 79 | |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 80 | private final Map<NotificationEntry, NotificationLifetimeExtender> mRetainedNotifications = |
| 81 | new ArrayMap<>(); |
| 82 | |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 83 | // Lazily retrieved dependencies |
| 84 | private NotificationRemoteInputManager mRemoteInputManager; |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 85 | private NotificationRowBinder mNotificationRowBinder; |
Eliot Courtney | 6c313d3 | 2017-12-14 19:57:51 +0900 | [diff] [blame] | 86 | |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 87 | private NotificationPresenter mPresenter; |
Ned Burns | dc10c95 | 2018-11-21 14:36:21 -0500 | [diff] [blame] | 88 | private NotificationListenerService.RankingMap mLatestRankingMap; |
Ned Burns | d35708c | 2019-01-09 15:38:03 -0500 | [diff] [blame] | 89 | @VisibleForTesting |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 90 | protected NotificationData mNotificationData; |
Ned Burns | f36c625 | 2019-01-09 19:12:33 -0500 | [diff] [blame] | 91 | |
Ned Burns | dc10c95 | 2018-11-21 14:36:21 -0500 | [diff] [blame] | 92 | @VisibleForTesting |
| 93 | final ArrayList<NotificationLifetimeExtender> mNotificationLifetimeExtenders |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 94 | = new ArrayList<>(); |
Gus Prevas | d65c2db | 2018-12-18 17:13:38 -0500 | [diff] [blame] | 95 | private final List<NotificationEntryListener> mNotificationEntryListeners = new ArrayList<>(); |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 96 | private NotificationRemoveInterceptor mRemoveInterceptor; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 97 | |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 98 | @Override |
| 99 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 100 | pw.println("NotificationEntryManager state:"); |
| 101 | pw.print(" mPendingNotifications="); |
| 102 | if (mPendingNotifications.size() == 0) { |
| 103 | pw.println("null"); |
| 104 | } else { |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 105 | for (NotificationEntry entry : mPendingNotifications.values()) { |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 106 | pw.println(entry.notification); |
| 107 | } |
| 108 | } |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 109 | pw.println(" Lifetime-extended notifications:"); |
| 110 | if (mRetainedNotifications.isEmpty()) { |
| 111 | pw.println(" None"); |
| 112 | } else { |
| 113 | for (Map.Entry<NotificationEntry, NotificationLifetimeExtender> entry |
| 114 | : mRetainedNotifications.entrySet()) { |
| 115 | pw.println(" " + entry.getKey().notification + " retained by " |
| 116 | + entry.getValue().getClass().getName()); |
| 117 | } |
| 118 | } |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 119 | } |
| 120 | |
Eliot Courtney | 6c313d3 | 2017-12-14 19:57:51 +0900 | [diff] [blame] | 121 | public NotificationEntryManager(Context context) { |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 122 | mNotificationData = new NotificationData(); |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Gus Prevas | d65c2db | 2018-12-18 17:13:38 -0500 | [diff] [blame] | 125 | /** Adds a {@link NotificationEntryListener}. */ |
| 126 | public void addNotificationEntryListener(NotificationEntryListener listener) { |
| 127 | mNotificationEntryListeners.add(listener); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 128 | } |
| 129 | |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 130 | /** Sets the {@link NotificationRemoveInterceptor}. */ |
| 131 | public void setNotificationRemoveInterceptor(NotificationRemoveInterceptor interceptor) { |
| 132 | mRemoveInterceptor = interceptor; |
| 133 | } |
| 134 | |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 135 | /** |
| 136 | * Our dependencies can have cyclic references, so some need to be lazy |
| 137 | */ |
| 138 | private NotificationRemoteInputManager getRemoteInputManager() { |
| 139 | if (mRemoteInputManager == null) { |
| 140 | mRemoteInputManager = Dependency.get(NotificationRemoteInputManager.class); |
| 141 | } |
| 142 | return mRemoteInputManager; |
| 143 | } |
| 144 | |
Ned Burns | c586467 | 2019-02-20 12:57:29 -0500 | [diff] [blame] | 145 | public void setRowBinder(NotificationRowBinder notificationRowBinder) { |
Ned Burns | ef2ef6c | 2019-01-02 16:48:08 -0500 | [diff] [blame] | 146 | mNotificationRowBinder = notificationRowBinder; |
| 147 | } |
| 148 | |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 149 | public void setUpWithPresenter(NotificationPresenter presenter, |
Gus Prevas | 8621bd2 | 2018-12-20 15:04:25 -0500 | [diff] [blame] | 150 | NotificationListContainer listContainer, |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 151 | HeadsUpManager headsUpManager) { |
| 152 | mPresenter = presenter; |
Gus Prevas | 9258646 | 2019-01-04 16:06:12 -0500 | [diff] [blame] | 153 | mNotificationData.setHeadsUpManager(headsUpManager); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 154 | } |
| 155 | |
Gus Prevas | 5c84abb | 2018-12-28 16:41:49 -0500 | [diff] [blame] | 156 | /** Adds multiple {@link NotificationLifetimeExtender}s. */ |
| 157 | public void addNotificationLifetimeExtenders(List<NotificationLifetimeExtender> extenders) { |
| 158 | for (NotificationLifetimeExtender extender : extenders) { |
| 159 | addNotificationLifetimeExtender(extender); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** Adds a {@link NotificationLifetimeExtender}. */ |
| 164 | public void addNotificationLifetimeExtender(NotificationLifetimeExtender extender) { |
| 165 | mNotificationLifetimeExtenders.add(extender); |
Mady Mellor | 1a4e86f | 2019-05-03 16:07:23 -0700 | [diff] [blame] | 166 | extender.setCallback(key -> removeNotification(key, mLatestRankingMap, |
| 167 | UNDEFINED_DISMISS_REASON)); |
Gus Prevas | 5c84abb | 2018-12-28 16:41:49 -0500 | [diff] [blame] | 168 | } |
| 169 | |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 170 | public NotificationData getNotificationData() { |
| 171 | return mNotificationData; |
| 172 | } |
| 173 | |
Eliot Courtney | 2b4c3a0 | 2017-11-27 13:27:46 +0900 | [diff] [blame] | 174 | @Override |
| 175 | public void onReorderingAllowed() { |
| 176 | updateNotifications(); |
| 177 | } |
| 178 | |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 179 | /** |
| 180 | * Requests a notification to be removed. |
| 181 | * |
| 182 | * @param n the notification to remove. |
| 183 | * @param reason why it is being removed e.g. {@link NotificationListenerService#REASON_CANCEL}, |
| 184 | * or 0 if unknown. |
| 185 | */ |
| 186 | public void performRemoveNotification(StatusBarNotification n, int reason) { |
Selim Cinek | 7bc9be7 | 2019-03-06 18:42:05 -0800 | [diff] [blame] | 187 | final NotificationVisibility nv = obtainVisibility(n.getKey()); |
Gus Prevas | ca1b6f7 | 2018-12-28 10:53:11 -0500 | [diff] [blame] | 188 | removeNotificationInternal( |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 189 | n.getKey(), null, nv, false /* forceRemove */, true /* removedByUser */, |
| 190 | reason); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 191 | } |
| 192 | |
Selim Cinek | 7bc9be7 | 2019-03-06 18:42:05 -0800 | [diff] [blame] | 193 | private NotificationVisibility obtainVisibility(String key) { |
| 194 | final int rank = mNotificationData.getRank(key); |
| 195 | final int count = mNotificationData.getActiveNotifications().size(); |
| 196 | NotificationVisibility.NotificationLocation location = |
| 197 | NotificationLogger.getNotificationLocation(getNotificationData().get(key)); |
| 198 | return NotificationVisibility.obtain(key, rank, count, true, location); |
| 199 | } |
| 200 | |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 201 | private void abortExistingInflation(String key) { |
| 202 | if (mPendingNotifications.containsKey(key)) { |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 203 | NotificationEntry entry = mPendingNotifications.get(key); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 204 | entry.abortTask(); |
| 205 | mPendingNotifications.remove(key); |
| 206 | } |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 207 | NotificationEntry addedEntry = mNotificationData.get(key); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 208 | if (addedEntry != null) { |
| 209 | addedEntry.abortTask(); |
| 210 | } |
| 211 | } |
| 212 | |
Ned Burns | dc10c95 | 2018-11-21 14:36:21 -0500 | [diff] [blame] | 213 | /** |
| 214 | * Cancel this notification and tell the StatusBarManagerService / NotificationManagerService |
| 215 | * about the failure. |
| 216 | * |
| 217 | * WARNING: this will call back into us. Don't hold any locks. |
| 218 | */ |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 219 | @Override |
Ned Burns | dc10c95 | 2018-11-21 14:36:21 -0500 | [diff] [blame] | 220 | public void handleInflationException(StatusBarNotification n, Exception e) { |
Gus Prevas | dca2be5 | 2018-12-21 11:25:10 -0500 | [diff] [blame] | 221 | removeNotificationInternal( |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 222 | n.getKey(), null, null, true /* forceRemove */, false /* removedByUser */, |
| 223 | REASON_ERROR); |
Gus Prevas | ca1b6f7 | 2018-12-28 10:53:11 -0500 | [diff] [blame] | 224 | for (NotificationEntryListener listener : mNotificationEntryListeners) { |
| 225 | listener.onInflationError(n, e); |
Ned Burns | dc10c95 | 2018-11-21 14:36:21 -0500 | [diff] [blame] | 226 | } |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 227 | } |
| 228 | |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 229 | @Override |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 230 | public void onAsyncInflationFinished(NotificationEntry entry, |
Kevin | d4660b2 | 2018-09-27 10:57:35 -0700 | [diff] [blame] | 231 | @InflationFlag int inflatedFlags) { |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 232 | mPendingNotifications.remove(entry.key); |
| 233 | // If there was an async task started after the removal, we don't want to add it back to |
| 234 | // the list, otherwise we might get leaks. |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 235 | if (!entry.isRowRemoved()) { |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 236 | boolean isNew = mNotificationData.get(entry.key) == null; |
| 237 | if (isNew) { |
Gus Prevas | b43dc65 | 2018-12-20 13:11:45 -0500 | [diff] [blame] | 238 | for (NotificationEntryListener listener : mNotificationEntryListeners) { |
| 239 | listener.onEntryInflated(entry, inflatedFlags); |
| 240 | } |
Ned Burns | 6976087 | 2019-01-03 15:36:38 -0500 | [diff] [blame] | 241 | mNotificationData.add(entry); |
Ned Burns | f36c625 | 2019-01-09 19:12:33 -0500 | [diff] [blame] | 242 | for (NotificationEntryListener listener : mNotificationEntryListeners) { |
| 243 | listener.onBeforeNotificationAdded(entry); |
| 244 | } |
Ned Burns | 6976087 | 2019-01-03 15:36:38 -0500 | [diff] [blame] | 245 | updateNotifications(); |
| 246 | for (NotificationEntryListener listener : mNotificationEntryListeners) { |
| 247 | listener.onNotificationAdded(entry); |
| 248 | } |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 249 | } else { |
Gus Prevas | d65c2db | 2018-12-18 17:13:38 -0500 | [diff] [blame] | 250 | for (NotificationEntryListener listener : mNotificationEntryListeners) { |
| 251 | listener.onEntryReinflated(entry); |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 252 | } |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 253 | } |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 254 | } |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | @Override |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 258 | public void removeNotification(String key, NotificationListenerService.RankingMap ranking, |
| 259 | int reason) { |
Selim Cinek | 7bc9be7 | 2019-03-06 18:42:05 -0800 | [diff] [blame] | 260 | removeNotificationInternal(key, ranking, obtainVisibility(key), false /* forceRemove */, |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 261 | false /* removedByUser */, reason); |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Gus Prevas | dca2be5 | 2018-12-21 11:25:10 -0500 | [diff] [blame] | 264 | private void removeNotificationInternal( |
| 265 | String key, |
| 266 | @Nullable NotificationListenerService.RankingMap ranking, |
Gus Prevas | ca1b6f7 | 2018-12-28 10:53:11 -0500 | [diff] [blame] | 267 | @Nullable NotificationVisibility visibility, |
Gus Prevas | dca2be5 | 2018-12-21 11:25:10 -0500 | [diff] [blame] | 268 | boolean forceRemove, |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 269 | boolean removedByUser, |
| 270 | int reason) { |
| 271 | |
| 272 | if (mRemoveInterceptor != null |
| 273 | && mRemoveInterceptor.onNotificationRemoveRequested(key, reason)) { |
| 274 | // Remove intercepted; skip |
| 275 | return; |
| 276 | } |
| 277 | |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 278 | final NotificationEntry entry = mNotificationData.get(key); |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 279 | |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 280 | abortExistingInflation(key); |
Gus Prevas | f37435a | 2018-12-20 15:40:01 -0500 | [diff] [blame] | 281 | |
Gus Prevas | f37435a | 2018-12-20 15:40:01 -0500 | [diff] [blame] | 282 | boolean lifetimeExtended = false; |
| 283 | |
Gus Prevas | d65c2db | 2018-12-18 17:13:38 -0500 | [diff] [blame] | 284 | if (entry != null) { |
Gus Prevas | f37435a | 2018-12-20 15:40:01 -0500 | [diff] [blame] | 285 | // If a manager needs to keep the notification around for whatever reason, we |
| 286 | // keep the notification |
Selim Cinek | 7bc9be7 | 2019-03-06 18:42:05 -0800 | [diff] [blame] | 287 | boolean entryDismissed = entry.isRowDismissed(); |
| 288 | if (!forceRemove && !entryDismissed) { |
Gus Prevas | f37435a | 2018-12-20 15:40:01 -0500 | [diff] [blame] | 289 | for (NotificationLifetimeExtender extender : mNotificationLifetimeExtenders) { |
| 290 | if (extender.shouldExtendLifetime(entry)) { |
| 291 | mLatestRankingMap = ranking; |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 292 | extendLifetime(entry, extender); |
Gus Prevas | f37435a | 2018-12-20 15:40:01 -0500 | [diff] [blame] | 293 | lifetimeExtended = true; |
| 294 | break; |
| 295 | } |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
Gus Prevas | f37435a | 2018-12-20 15:40:01 -0500 | [diff] [blame] | 298 | |
| 299 | if (!lifetimeExtended) { |
| 300 | // At this point, we are guaranteed the notification will be removed |
| 301 | |
| 302 | // Ensure any managers keeping the lifetime extended stop managing the entry |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 303 | cancelLifetimeExtension(entry); |
Gus Prevas | f37435a | 2018-12-20 15:40:01 -0500 | [diff] [blame] | 304 | |
Gus Prevas | f37435a | 2018-12-20 15:40:01 -0500 | [diff] [blame] | 305 | if (entry.rowExists()) { |
| 306 | entry.removeRow(); |
Gus Prevas | f37435a | 2018-12-20 15:40:01 -0500 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | // Let's remove the children if this was a summary |
| 310 | handleGroupSummaryRemoved(key); |
| 311 | |
Ned Burns | 6976087 | 2019-01-03 15:36:38 -0500 | [diff] [blame] | 312 | mNotificationData.remove(key, ranking); |
| 313 | updateNotifications(); |
| 314 | Dependency.get(LeakDetector.class).trackGarbage(entry); |
Selim Cinek | 7bc9be7 | 2019-03-06 18:42:05 -0800 | [diff] [blame] | 315 | removedByUser |= entryDismissed; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 316 | |
Ned Burns | ef2ef6c | 2019-01-02 16:48:08 -0500 | [diff] [blame] | 317 | for (NotificationEntryListener listener : mNotificationEntryListeners) { |
| 318 | listener.onEntryRemoved(entry, visibility, removedByUser); |
| 319 | } |
| 320 | } |
Gus Prevas | 8621bd2 | 2018-12-20 15:04:25 -0500 | [diff] [blame] | 321 | } |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 322 | } |
| 323 | |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 324 | /** |
| 325 | * Ensures that the group children are cancelled immediately when the group summary is cancelled |
| 326 | * instead of waiting for the notification manager to send all cancels. Otherwise this could |
| 327 | * lead to flickers. |
| 328 | * |
| 329 | * This also ensures that the animation looks nice and only consists of a single disappear |
| 330 | * animation instead of multiple. |
| 331 | * @param key the key of the notification was removed |
| 332 | * |
| 333 | */ |
| 334 | private void handleGroupSummaryRemoved(String key) { |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 335 | NotificationEntry entry = mNotificationData.get(key); |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 336 | if (entry != null && entry.rowExists() && entry.isSummaryWithChildren()) { |
| 337 | if (entry.notification.getOverrideGroupKey() != null && !entry.isRowDismissed()) { |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 338 | // We don't want to remove children for autobundled notifications as they are not |
| 339 | // always cancelled. We only remove them if they were dismissed by the user. |
| 340 | return; |
| 341 | } |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 342 | List<NotificationEntry> childEntries = entry.getChildren(); |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 343 | if (childEntries == null) { |
| 344 | return; |
| 345 | } |
| 346 | for (int i = 0; i < childEntries.size(); i++) { |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 347 | NotificationEntry childEntry = childEntries.get(i); |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 348 | boolean isForeground = (entry.notification.getNotification().flags |
Selim Cinek | 038e259 | 2018-08-16 16:30:45 -0700 | [diff] [blame] | 349 | & Notification.FLAG_FOREGROUND_SERVICE) != 0; |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 350 | boolean keepForReply = |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 351 | getRemoteInputManager().shouldKeepForRemoteInputHistory(childEntry) |
| 352 | || getRemoteInputManager().shouldKeepForSmartReplyHistory(childEntry); |
Selim Cinek | 038e259 | 2018-08-16 16:30:45 -0700 | [diff] [blame] | 353 | if (isForeground || keepForReply) { |
| 354 | // the child is a foreground service notification which we can't remove or it's |
| 355 | // a child we're keeping around for reply! |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 356 | continue; |
| 357 | } |
Evan Laird | ce2d1af | 2019-05-30 16:00:22 -0400 | [diff] [blame] | 358 | childEntry.setKeepInParent(true); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 359 | // we need to set this state earlier as otherwise we might generate some weird |
| 360 | // animations |
Evan Laird | ce2d1af | 2019-05-30 16:00:22 -0400 | [diff] [blame] | 361 | childEntry.removeRow(); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 366 | private void addNotificationInternal(StatusBarNotification notification, |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 367 | NotificationListenerService.RankingMap rankingMap) throws InflationException { |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 368 | String key = notification.getKey(); |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 369 | if (DEBUG) { |
| 370 | Log.d(TAG, "addNotification key=" + key); |
| 371 | } |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 372 | |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 373 | mNotificationData.updateRanking(rankingMap); |
| 374 | NotificationListenerService.Ranking ranking = new NotificationListenerService.Ranking(); |
| 375 | rankingMap.getRanking(key, ranking); |
Ned Burns | 6976087 | 2019-01-03 15:36:38 -0500 | [diff] [blame] | 376 | |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 377 | NotificationEntry entry = new NotificationEntry(notification, ranking); |
Ned Burns | 6976087 | 2019-01-03 15:36:38 -0500 | [diff] [blame] | 378 | |
| 379 | Dependency.get(LeakDetector.class).trackInstance(entry); |
| 380 | // Construct the expanded view. |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 381 | requireBinder().inflateViews(entry, () -> performRemoveNotification(notification, |
| 382 | REASON_CANCEL)); |
Ned Burns | 6976087 | 2019-01-03 15:36:38 -0500 | [diff] [blame] | 383 | |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 384 | abortExistingInflation(key); |
| 385 | |
Gus Prevas | d65c2db | 2018-12-18 17:13:38 -0500 | [diff] [blame] | 386 | mPendingNotifications.put(key, entry); |
| 387 | for (NotificationEntryListener listener : mNotificationEntryListeners) { |
| 388 | listener.onPendingEntryAdded(entry); |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 389 | } |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public void addNotification(StatusBarNotification notification, |
| 394 | NotificationListenerService.RankingMap ranking) { |
| 395 | try { |
| 396 | addNotificationInternal(notification, ranking); |
| 397 | } catch (InflationException e) { |
| 398 | handleInflationException(notification, e); |
| 399 | } |
| 400 | } |
| 401 | |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 402 | private void updateNotificationInternal(StatusBarNotification notification, |
| 403 | NotificationListenerService.RankingMap ranking) throws InflationException { |
| 404 | if (DEBUG) Log.d(TAG, "updateNotification(" + notification + ")"); |
| 405 | |
| 406 | final String key = notification.getKey(); |
| 407 | abortExistingInflation(key); |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 408 | NotificationEntry entry = mNotificationData.get(key); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 409 | if (entry == null) { |
| 410 | return; |
| 411 | } |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 412 | |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 413 | // Notification is updated so it is essentially re-added and thus alive again. Don't need |
Kevin | e9e938c | 2018-09-06 13:38:11 -0700 | [diff] [blame] | 414 | // to keep its lifetime extended. |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 415 | cancelLifetimeExtension(entry); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 416 | |
Gus Prevas | 26d9127 | 2018-12-21 16:43:28 -0500 | [diff] [blame] | 417 | mNotificationData.update(entry, ranking, notification); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 418 | |
Mady Mellor | 0ad5b9d | 2019-01-08 14:59:55 -0800 | [diff] [blame] | 419 | for (NotificationEntryListener listener : mNotificationEntryListeners) { |
| 420 | listener.onPreEntryUpdated(entry); |
| 421 | } |
| 422 | |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 423 | requireBinder().inflateViews(entry, () -> performRemoveNotification(notification, |
| 424 | REASON_CANCEL)); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 425 | updateNotifications(); |
| 426 | |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 427 | if (DEBUG) { |
| 428 | // Is this for you? |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 429 | boolean isForCurrentUser = Dependency.get(KeyguardEnvironment.class) |
| 430 | .isNotificationForCurrentProfiles(notification); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 431 | Log.d(TAG, "notification is " + (isForCurrentUser ? "" : "not ") + "for you"); |
| 432 | } |
| 433 | |
Gus Prevas | b43dc65 | 2018-12-20 13:11:45 -0500 | [diff] [blame] | 434 | for (NotificationEntryListener listener : mNotificationEntryListeners) { |
Mady Mellor | 0ad5b9d | 2019-01-08 14:59:55 -0800 | [diff] [blame] | 435 | listener.onPostEntryUpdated(entry); |
Gus Prevas | b43dc65 | 2018-12-20 13:11:45 -0500 | [diff] [blame] | 436 | } |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | @Override |
| 440 | public void updateNotification(StatusBarNotification notification, |
| 441 | NotificationListenerService.RankingMap ranking) { |
| 442 | try { |
| 443 | updateNotificationInternal(notification, ranking); |
| 444 | } catch (InflationException e) { |
| 445 | handleInflationException(notification, e); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | public void updateNotifications() { |
| 450 | mNotificationData.filterAndSort(); |
Ned Burns | f36c625 | 2019-01-09 19:12:33 -0500 | [diff] [blame] | 451 | if (mPresenter != null) { |
| 452 | mPresenter.updateNotificationViews(); |
| 453 | } |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 454 | } |
| 455 | |
Ned Burns | 6b19b08 | 2019-02-12 18:50:47 -0500 | [diff] [blame] | 456 | @Override |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 457 | public void updateNotificationRanking(NotificationListenerService.RankingMap rankingMap) { |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 458 | List<NotificationEntry> entries = new ArrayList<>(); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 459 | entries.addAll(mNotificationData.getActiveNotifications()); |
| 460 | entries.addAll(mPendingNotifications.values()); |
| 461 | |
| 462 | // Has a copy of the current UI adjustments. |
| 463 | ArrayMap<String, NotificationUiAdjustment> oldAdjustments = new ArrayMap<>(); |
Gus Prevas | 33fbc15 | 2018-12-04 13:17:32 -0500 | [diff] [blame] | 464 | ArrayMap<String, Integer> oldImportances = new ArrayMap<>(); |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 465 | for (NotificationEntry entry : entries) { |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 466 | NotificationUiAdjustment adjustment = |
| 467 | NotificationUiAdjustment.extractFromNotificationEntry(entry); |
| 468 | oldAdjustments.put(entry.key, adjustment); |
Gus Prevas | 33fbc15 | 2018-12-04 13:17:32 -0500 | [diff] [blame] | 469 | oldImportances.put(entry.key, entry.importance); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | // Populate notification entries from the new rankings. |
| 473 | mNotificationData.updateRanking(rankingMap); |
| 474 | updateRankingOfPendingNotifications(rankingMap); |
| 475 | |
| 476 | // By comparing the old and new UI adjustments, reinflate the view accordingly. |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 477 | for (NotificationEntry entry : entries) { |
Ned Burns | c586467 | 2019-02-20 12:57:29 -0500 | [diff] [blame] | 478 | requireBinder().onNotificationRankingUpdated( |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 479 | entry, |
| 480 | oldImportances.get(entry.key), |
| 481 | oldAdjustments.get(entry.key), |
Ned Burns | f529ec2 | 2019-02-12 19:33:50 -0500 | [diff] [blame] | 482 | NotificationUiAdjustment.extractFromNotificationEntry(entry)); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 483 | } |
| 484 | |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 485 | updateNotifications(); |
Mark Renouf | bbcf07f | 2019-05-09 10:42:43 -0400 | [diff] [blame] | 486 | |
| 487 | for (NotificationEntryListener listener : mNotificationEntryListeners) { |
| 488 | listener.onNotificationRankingUpdated(rankingMap); |
| 489 | } |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 490 | } |
| 491 | |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 492 | private void updateRankingOfPendingNotifications( |
| 493 | @Nullable NotificationListenerService.RankingMap rankingMap) { |
| 494 | if (rankingMap == null) { |
| 495 | return; |
| 496 | } |
| 497 | NotificationListenerService.Ranking tmpRanking = new NotificationListenerService.Ranking(); |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 498 | for (NotificationEntry pendingNotification : mPendingNotifications.values()) { |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 499 | rankingMap.getRanking(pendingNotification.key, tmpRanking); |
| 500 | pendingNotification.populateFromRanking(tmpRanking); |
| 501 | } |
| 502 | } |
| 503 | |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 504 | /** |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 505 | * @return An iterator for all "pending" notifications. Pending notifications are newly-posted |
| 506 | * notifications whose views have not yet been inflated. In general, the system pretends like |
| 507 | * these don't exist, although there are a couple exceptions. |
| 508 | */ |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 509 | public Iterable<NotificationEntry> getPendingNotificationsIterator() { |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 510 | return mPendingNotifications.values(); |
| 511 | } |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 512 | |
| 513 | private void extendLifetime(NotificationEntry entry, NotificationLifetimeExtender extender) { |
| 514 | NotificationLifetimeExtender activeExtender = mRetainedNotifications.get(entry); |
| 515 | if (activeExtender != null && activeExtender != extender) { |
| 516 | activeExtender.setShouldManageLifetime(entry, false); |
| 517 | } |
| 518 | mRetainedNotifications.put(entry, extender); |
| 519 | extender.setShouldManageLifetime(entry, true); |
| 520 | } |
| 521 | |
| 522 | private void cancelLifetimeExtension(NotificationEntry entry) { |
| 523 | NotificationLifetimeExtender activeExtender = mRetainedNotifications.remove(entry); |
| 524 | if (activeExtender != null) { |
| 525 | activeExtender.setShouldManageLifetime(entry, false); |
| 526 | } |
| 527 | } |
Ned Burns | c586467 | 2019-02-20 12:57:29 -0500 | [diff] [blame] | 528 | |
| 529 | private NotificationRowBinder requireBinder() { |
| 530 | if (mNotificationRowBinder == null) { |
| 531 | throw new RuntimeException("You must initialize NotificationEntryManager by calling" |
| 532 | + "setRowBinder() before using."); |
| 533 | } |
| 534 | return mNotificationRowBinder; |
| 535 | } |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 536 | } |