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