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