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