Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | */ |
| 16 | |
| 17 | package com.android.systemui.statusbar.phone; |
| 18 | |
| 19 | import android.annotation.NonNull; |
Ned Burns | 1dd6b40 | 2019-01-02 15:25:23 -0500 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 21 | import android.app.Notification; |
| 22 | import android.os.SystemClock; |
| 23 | import android.service.notification.StatusBarNotification; |
| 24 | import android.util.ArrayMap; |
| 25 | |
Gus Prevas | ca1b6f7 | 2018-12-28 10:53:11 -0500 | [diff] [blame] | 26 | import com.android.internal.statusbar.NotificationVisibility; |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 27 | import com.android.systemui.Dependency; |
Beverly | 8fdb533 | 2019-02-04 14:29:49 -0500 | [diff] [blame] | 28 | import com.android.systemui.plugins.statusbar.StatusBarStateController; |
| 29 | import com.android.systemui.plugins.statusbar.StatusBarStateController.StateListener; |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 30 | import com.android.systemui.statusbar.AlertingNotificationManager; |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 31 | import com.android.systemui.statusbar.InflationTask; |
Gus Prevas | d65c2db | 2018-12-18 17:13:38 -0500 | [diff] [blame] | 32 | import com.android.systemui.statusbar.notification.NotificationEntryListener; |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 33 | import com.android.systemui.statusbar.notification.NotificationEntryManager; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 34 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 35 | import com.android.systemui.statusbar.notification.row.NotificationContentInflater.AsyncInflationTask; |
| 36 | import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag; |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 37 | import com.android.systemui.statusbar.phone.NotificationGroupManager.NotificationGroup; |
| 38 | import com.android.systemui.statusbar.phone.NotificationGroupManager.OnGroupChangeListener; |
| 39 | import com.android.systemui.statusbar.policy.HeadsUpManager; |
| 40 | import com.android.systemui.statusbar.policy.OnHeadsUpChangedListener; |
| 41 | |
| 42 | import java.util.ArrayList; |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 43 | import java.util.Objects; |
| 44 | |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 45 | import javax.inject.Inject; |
| 46 | import javax.inject.Singleton; |
| 47 | |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 48 | /** |
Selim Cinek | c3fec68 | 2019-06-06 18:11:07 -0700 | [diff] [blame] | 49 | * A helper class dealing with the alert interactions between {@link NotificationGroupManager} and |
| 50 | * {@link HeadsUpManager}. In particular, this class deals with keeping |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 51 | * the correct notification in a group alerting based off the group suppression. |
| 52 | */ |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 53 | @Singleton |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 54 | public class NotificationGroupAlertTransferHelper implements OnHeadsUpChangedListener, |
Selim Cinek | c3fec68 | 2019-06-06 18:11:07 -0700 | [diff] [blame] | 55 | StateListener { |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 56 | |
| 57 | private static final long ALERT_TRANSFER_TIMEOUT = 300; |
| 58 | |
| 59 | /** |
| 60 | * The list of entries containing group alert metadata for each group. Keyed by group key. |
| 61 | */ |
| 62 | private final ArrayMap<String, GroupAlertEntry> mGroupAlertEntries = new ArrayMap<>(); |
| 63 | |
| 64 | /** |
| 65 | * The list of entries currently inflating that should alert after inflation. Keyed by |
| 66 | * notification key. |
| 67 | */ |
| 68 | private final ArrayMap<String, PendingAlertInfo> mPendingAlerts = new ArrayMap<>(); |
| 69 | |
| 70 | private HeadsUpManager mHeadsUpManager; |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 71 | private final NotificationGroupManager mGroupManager = |
| 72 | Dependency.get(NotificationGroupManager.class); |
| 73 | |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 74 | private NotificationEntryManager mEntryManager; |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 75 | |
| 76 | private boolean mIsDozing; |
| 77 | |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 78 | @Inject |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 79 | public NotificationGroupAlertTransferHelper() { |
Jason Monk | af08c15 | 2018-12-04 11:12:39 -0500 | [diff] [blame] | 80 | Dependency.get(StatusBarStateController.class).addCallback(this); |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 83 | /** Causes the TransferHelper to register itself as a listener to the appropriate classes. */ |
| 84 | public void bind(NotificationEntryManager entryManager, |
| 85 | NotificationGroupManager groupManager) { |
| 86 | if (mEntryManager != null) { |
| 87 | throw new IllegalStateException("Already bound."); |
| 88 | } |
| 89 | |
| 90 | // TODO(b/119637830): It would be good if GroupManager already had all pending notifications |
| 91 | // as normal children (i.e. add notifications to GroupManager before inflation) so that we |
| 92 | // don't have to have this dependency. We'd also have to worry less about the suppression |
| 93 | // not being up to date. |
| 94 | mEntryManager = entryManager; |
| 95 | |
Gus Prevas | d65c2db | 2018-12-18 17:13:38 -0500 | [diff] [blame] | 96 | mEntryManager.addNotificationEntryListener(mNotificationEntryListener); |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 97 | groupManager.addOnGroupChangeListener(mOnGroupChangeListener); |
| 98 | } |
| 99 | |
Kevin | 4b8bbda | 2018-11-19 14:36:31 -0800 | [diff] [blame] | 100 | /** |
| 101 | * Whether or not a notification has transferred its alert state to the notification and |
| 102 | * the notification should alert after inflating. |
| 103 | * |
| 104 | * @param entry notification to check |
| 105 | * @return true if the entry was transferred to and should inflate + alert |
| 106 | */ |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 107 | public boolean isAlertTransferPending(@NonNull NotificationEntry entry) { |
Kevin | 4b8bbda | 2018-11-19 14:36:31 -0800 | [diff] [blame] | 108 | PendingAlertInfo alertInfo = mPendingAlerts.get(entry.key); |
| 109 | return alertInfo != null && alertInfo.isStillValid(); |
| 110 | } |
| 111 | |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 112 | public void setHeadsUpManager(HeadsUpManager headsUpManager) { |
| 113 | mHeadsUpManager = headsUpManager; |
| 114 | } |
| 115 | |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 116 | @Override |
| 117 | public void onStateChanged(int newState) {} |
| 118 | |
| 119 | @Override |
| 120 | public void onDozingChanged(boolean isDozing) { |
| 121 | if (mIsDozing != isDozing) { |
| 122 | for (GroupAlertEntry groupAlertEntry : mGroupAlertEntries.values()) { |
| 123 | groupAlertEntry.mLastAlertTransferTime = 0; |
| 124 | groupAlertEntry.mAlertSummaryOnNextAddition = false; |
| 125 | } |
| 126 | } |
| 127 | mIsDozing = isDozing; |
| 128 | } |
| 129 | |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 130 | private final OnGroupChangeListener mOnGroupChangeListener = new OnGroupChangeListener() { |
| 131 | @Override |
| 132 | public void onGroupCreated(NotificationGroup group, String groupKey) { |
| 133 | mGroupAlertEntries.put(groupKey, new GroupAlertEntry(group)); |
| 134 | } |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 135 | |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 136 | @Override |
| 137 | public void onGroupRemoved(NotificationGroup group, String groupKey) { |
| 138 | mGroupAlertEntries.remove(groupKey); |
| 139 | } |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 140 | |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 141 | @Override |
| 142 | public void onGroupSuppressionChanged(NotificationGroup group, boolean suppressed) { |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 143 | if (suppressed) { |
Selim Cinek | c3fec68 | 2019-06-06 18:11:07 -0700 | [diff] [blame] | 144 | if (mHeadsUpManager.isAlerting(group.summary.key)) { |
| 145 | handleSuppressedSummaryAlerted(group.summary, mHeadsUpManager); |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 146 | } |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 147 | } else { |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 148 | // Group summary can be null if we are no longer suppressed because the summary was |
| 149 | // removed. In that case, we don't need to alert the summary. |
| 150 | if (group.summary == null) { |
| 151 | return; |
| 152 | } |
| 153 | GroupAlertEntry groupAlertEntry = mGroupAlertEntries.get(mGroupManager.getGroupKey( |
| 154 | group.summary.notification)); |
| 155 | // Group is no longer suppressed. We should check if we need to transfer the alert |
| 156 | // back to the summary now that it's no longer suppressed. |
| 157 | if (groupAlertEntry.mAlertSummaryOnNextAddition) { |
Selim Cinek | c3fec68 | 2019-06-06 18:11:07 -0700 | [diff] [blame] | 158 | if (!mHeadsUpManager.isAlerting(group.summary.key)) { |
| 159 | alertNotificationWhenPossible(group.summary, mHeadsUpManager); |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 160 | } |
| 161 | groupAlertEntry.mAlertSummaryOnNextAddition = false; |
| 162 | } else { |
| 163 | checkShouldTransferBack(groupAlertEntry); |
| 164 | } |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 167 | }; |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 168 | |
| 169 | @Override |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 170 | public void onHeadsUpStateChanged(NotificationEntry entry, boolean isHeadsUp) { |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 171 | onAlertStateChanged(entry, isHeadsUp, mHeadsUpManager); |
| 172 | } |
| 173 | |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 174 | private void onAlertStateChanged(NotificationEntry entry, boolean isAlerting, |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 175 | AlertingNotificationManager alertManager) { |
| 176 | if (isAlerting && mGroupManager.isSummaryOfSuppressedGroup(entry.notification)) { |
| 177 | handleSuppressedSummaryAlerted(entry, alertManager); |
| 178 | } |
| 179 | } |
| 180 | |
Gus Prevas | d65c2db | 2018-12-18 17:13:38 -0500 | [diff] [blame] | 181 | private final NotificationEntryListener mNotificationEntryListener = |
| 182 | new NotificationEntryListener() { |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 183 | // Called when a new notification has been posted but is not inflated yet. We use this to |
| 184 | // see as early as we can if we need to abort a transfer. |
| 185 | @Override |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 186 | public void onPendingEntryAdded(NotificationEntry entry) { |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 187 | String groupKey = mGroupManager.getGroupKey(entry.notification); |
| 188 | GroupAlertEntry groupAlertEntry = mGroupAlertEntries.get(groupKey); |
| 189 | if (groupAlertEntry != null) { |
| 190 | checkShouldTransferBack(groupAlertEntry); |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 193 | |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 194 | // Called when the entry's reinflation has finished. If there is an alert pending, we |
| 195 | // then show the alert. |
| 196 | @Override |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 197 | public void onEntryReinflated(NotificationEntry entry) { |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 198 | PendingAlertInfo alertInfo = mPendingAlerts.remove(entry.key); |
| 199 | if (alertInfo != null) { |
| 200 | if (alertInfo.isStillValid()) { |
Selim Cinek | c3fec68 | 2019-06-06 18:11:07 -0700 | [diff] [blame] | 201 | alertNotificationWhenPossible(entry, mHeadsUpManager); |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 202 | } else { |
| 203 | // The transfer is no longer valid. Free the content. |
Selim Cinek | c3fec68 | 2019-06-06 18:11:07 -0700 | [diff] [blame] | 204 | entry.getRow().freeContentViewWhenSafe(mHeadsUpManager.getContentFlag()); |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 205 | } |
| 206 | } |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 207 | } |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 208 | |
| 209 | @Override |
Gus Prevas | f37435a | 2018-12-20 15:40:01 -0500 | [diff] [blame] | 210 | public void onEntryRemoved( |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 211 | @Nullable NotificationEntry entry, |
Gus Prevas | ca1b6f7 | 2018-12-28 10:53:11 -0500 | [diff] [blame] | 212 | NotificationVisibility visibility, |
Gus Prevas | dca2be5 | 2018-12-21 11:25:10 -0500 | [diff] [blame] | 213 | boolean removedByUser) { |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 214 | // Removes any alerts pending on this entry. Note that this will not stop any inflation |
| 215 | // tasks started by a transfer, so this should only be used as clean-up for when |
| 216 | // inflation is stopped and the pending alert no longer needs to happen. |
Ned Burns | ef2ef6c | 2019-01-02 16:48:08 -0500 | [diff] [blame] | 217 | mPendingAlerts.remove(entry.key); |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 218 | } |
| 219 | }; |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 220 | |
| 221 | /** |
| 222 | * Gets the number of new notifications pending inflation that will be added to the group |
| 223 | * but currently aren't and should not alert. |
| 224 | * |
| 225 | * @param group group to check |
| 226 | * @return the number of new notifications that will be added to the group |
| 227 | */ |
| 228 | private int getPendingChildrenNotAlerting(@NonNull NotificationGroup group) { |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 229 | if (mEntryManager == null) { |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 230 | return 0; |
| 231 | } |
| 232 | int number = 0; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 233 | Iterable<NotificationEntry> values = mEntryManager.getPendingNotificationsIterator(); |
| 234 | for (NotificationEntry entry : values) { |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 235 | if (isPendingNotificationInGroup(entry, group) && onlySummaryAlerts(entry)) { |
| 236 | number++; |
| 237 | } |
| 238 | } |
| 239 | return number; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Checks if the pending inflations will add children to this group. |
| 244 | * |
| 245 | * @param group group to check |
| 246 | * @return true if a pending notification will add to this group |
| 247 | */ |
| 248 | private boolean pendingInflationsWillAddChildren(@NonNull NotificationGroup group) { |
Ned Burns | 3d6b396 | 2018-12-07 21:26:00 -0500 | [diff] [blame] | 249 | if (mEntryManager == null) { |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 250 | return false; |
| 251 | } |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 252 | Iterable<NotificationEntry> values = mEntryManager.getPendingNotificationsIterator(); |
| 253 | for (NotificationEntry entry : values) { |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 254 | if (isPendingNotificationInGroup(entry, group)) { |
| 255 | return true; |
| 256 | } |
| 257 | } |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Checks if a new pending notification will be added to the group. |
| 263 | * |
| 264 | * @param entry pending notification |
| 265 | * @param group group to check |
| 266 | * @return true if the notification will add to the group, false o/w |
| 267 | */ |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 268 | private boolean isPendingNotificationInGroup(@NonNull NotificationEntry entry, |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 269 | @NonNull NotificationGroup group) { |
| 270 | String groupKey = mGroupManager.getGroupKey(group.summary.notification); |
| 271 | return mGroupManager.isGroupChild(entry.notification) |
| 272 | && Objects.equals(mGroupManager.getGroupKey(entry.notification), groupKey) |
| 273 | && !group.children.containsKey(entry.key); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Handles the scenario where a summary that has been suppressed is alerted. A suppressed |
| 278 | * summary should for all intents and purposes be invisible to the user and as a result should |
| 279 | * not alert. When this is the case, it is our responsibility to pass the alert to the |
| 280 | * appropriate child which will be the representative notification alerting for the group. |
| 281 | * |
| 282 | * @param summary the summary that is suppressed and alerting |
| 283 | * @param alertManager the alert manager that manages the alerting summary |
| 284 | */ |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 285 | private void handleSuppressedSummaryAlerted(@NonNull NotificationEntry summary, |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 286 | @NonNull AlertingNotificationManager alertManager) { |
| 287 | StatusBarNotification sbn = summary.notification; |
| 288 | GroupAlertEntry groupAlertEntry = |
| 289 | mGroupAlertEntries.get(mGroupManager.getGroupKey(sbn)); |
| 290 | if (!mGroupManager.isSummaryOfSuppressedGroup(summary.notification) |
| 291 | || !alertManager.isAlerting(sbn.getKey()) |
| 292 | || groupAlertEntry == null) { |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | if (pendingInflationsWillAddChildren(groupAlertEntry.mGroup)) { |
| 297 | // New children will actually be added to this group, let's not transfer the alert. |
| 298 | return; |
| 299 | } |
| 300 | |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 301 | NotificationEntry child = mGroupManager.getLogicalChildren(summary.notification).iterator().next(); |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 302 | if (child != null) { |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 303 | if (child.getRow().keepInParent() |
| 304 | || child.isRowRemoved() |
| 305 | || child.isRowDismissed()) { |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 306 | // The notification is actually already removed. No need to alert it. |
| 307 | return; |
| 308 | } |
| 309 | if (!alertManager.isAlerting(child.key) && onlySummaryAlerts(summary)) { |
| 310 | groupAlertEntry.mLastAlertTransferTime = SystemClock.elapsedRealtime(); |
| 311 | } |
| 312 | transferAlertState(summary, child, alertManager); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Transfers the alert state one entry to another. We remove the alert from the first entry |
| 318 | * immediately to have the incorrect one up as short as possible. The second should alert |
| 319 | * when possible. |
| 320 | * |
| 321 | * @param fromEntry entry to transfer alert from |
| 322 | * @param toEntry entry to transfer to |
| 323 | * @param alertManager alert manager for the alert type |
| 324 | */ |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 325 | private void transferAlertState(@NonNull NotificationEntry fromEntry, @NonNull NotificationEntry toEntry, |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 326 | @NonNull AlertingNotificationManager alertManager) { |
| 327 | alertManager.removeNotification(fromEntry.key, true /* releaseImmediately */); |
| 328 | alertNotificationWhenPossible(toEntry, alertManager); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Determines if we need to transfer the alert back to the summary from the child and does |
| 333 | * so if needed. |
| 334 | * |
| 335 | * This can happen since notification groups are not delivered as a whole unit and it is |
| 336 | * possible we erroneously transfer the alert from the summary to the child even though |
| 337 | * more children are coming. Thus, if a child is added within a certain timeframe after we |
| 338 | * transfer, we back out and alert the summary again. |
| 339 | * |
| 340 | * @param groupAlertEntry group alert entry to check |
| 341 | */ |
| 342 | private void checkShouldTransferBack(@NonNull GroupAlertEntry groupAlertEntry) { |
| 343 | if (SystemClock.elapsedRealtime() - groupAlertEntry.mLastAlertTransferTime |
| 344 | < ALERT_TRANSFER_TIMEOUT) { |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 345 | NotificationEntry summary = groupAlertEntry.mGroup.summary; |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 346 | |
| 347 | if (!onlySummaryAlerts(summary)) { |
| 348 | return; |
| 349 | } |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 350 | ArrayList<NotificationEntry> children = mGroupManager.getLogicalChildren(summary.notification); |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 351 | int numChildren = children.size(); |
| 352 | int numPendingChildren = getPendingChildrenNotAlerting(groupAlertEntry.mGroup); |
| 353 | numChildren += numPendingChildren; |
| 354 | if (numChildren <= 1) { |
| 355 | return; |
| 356 | } |
| 357 | boolean releasedChild = false; |
| 358 | for (int i = 0; i < children.size(); i++) { |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 359 | NotificationEntry entry = children.get(i); |
Selim Cinek | c3fec68 | 2019-06-06 18:11:07 -0700 | [diff] [blame] | 360 | if (onlySummaryAlerts(entry) && mHeadsUpManager.isAlerting(entry.key)) { |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 361 | releasedChild = true; |
Selim Cinek | c3fec68 | 2019-06-06 18:11:07 -0700 | [diff] [blame] | 362 | mHeadsUpManager.removeNotification(entry.key, true /* releaseImmediately */); |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 363 | } |
| 364 | if (mPendingAlerts.containsKey(entry.key)) { |
| 365 | // This is the child that would've been removed if it was inflated. |
| 366 | releasedChild = true; |
| 367 | mPendingAlerts.get(entry.key).mAbortOnInflation = true; |
| 368 | } |
| 369 | } |
Selim Cinek | c3fec68 | 2019-06-06 18:11:07 -0700 | [diff] [blame] | 370 | if (releasedChild && !mHeadsUpManager.isAlerting(summary.key)) { |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 371 | boolean notifyImmediately = (numChildren - numPendingChildren) > 1; |
| 372 | if (notifyImmediately) { |
Selim Cinek | c3fec68 | 2019-06-06 18:11:07 -0700 | [diff] [blame] | 373 | alertNotificationWhenPossible(summary, mHeadsUpManager); |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 374 | } else { |
| 375 | // Should wait until the pending child inflates before alerting. |
| 376 | groupAlertEntry.mAlertSummaryOnNextAddition = true; |
| 377 | } |
| 378 | groupAlertEntry.mLastAlertTransferTime = 0; |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Tries to alert the notification. If its content view is not inflated, we inflate and continue |
| 385 | * when the entry finishes inflating the view. |
| 386 | * |
| 387 | * @param entry entry to show |
| 388 | * @param alertManager alert manager for the alert type |
| 389 | */ |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 390 | private void alertNotificationWhenPossible(@NonNull NotificationEntry entry, |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 391 | @NonNull AlertingNotificationManager alertManager) { |
| 392 | @InflationFlag int contentFlag = alertManager.getContentFlag(); |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 393 | if (!entry.getRow().isInflationFlagSet(contentFlag)) { |
Selim Cinek | c3fec68 | 2019-06-06 18:11:07 -0700 | [diff] [blame] | 394 | mPendingAlerts.put(entry.key, new PendingAlertInfo(entry)); |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 395 | entry.getRow().updateInflationFlag(contentFlag, true /* shouldInflate */); |
| 396 | entry.getRow().inflateViews(); |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 397 | return; |
| 398 | } |
| 399 | if (alertManager.isAlerting(entry.key)) { |
| 400 | alertManager.updateNotification(entry.key, true /* alert */); |
| 401 | } else { |
| 402 | alertManager.showNotification(entry); |
| 403 | } |
| 404 | } |
| 405 | |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 406 | private boolean onlySummaryAlerts(NotificationEntry entry) { |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 407 | return entry.notification.getNotification().getGroupAlertBehavior() |
| 408 | == Notification.GROUP_ALERT_SUMMARY; |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Information about a pending alert used to determine if the alert is still needed when |
| 413 | * inflation completes. |
| 414 | */ |
| 415 | private class PendingAlertInfo { |
Kevin | 4b8bbda | 2018-11-19 14:36:31 -0800 | [diff] [blame] | 416 | |
| 417 | /** |
| 418 | * The original notification when the transfer is initiated. This is used to determine if |
| 419 | * the transfer is still valid if the notification is updated. |
| 420 | */ |
| 421 | final StatusBarNotification mOriginalNotification; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 422 | final NotificationEntry mEntry; |
Kevin | 4b8bbda | 2018-11-19 14:36:31 -0800 | [diff] [blame] | 423 | |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 424 | /** |
| 425 | * The notification is still pending inflation but we've decided that we no longer need |
| 426 | * the content view (e.g. suppression might have changed and we decided we need to transfer |
| 427 | * back). However, there is no way to abort just this inflation if other inflation requests |
| 428 | * have started (see {@link AsyncInflationTask#supersedeTask(InflationTask)}). So instead |
| 429 | * we just flag it as aborted and free when it's inflated. |
| 430 | */ |
| 431 | boolean mAbortOnInflation; |
| 432 | |
Selim Cinek | c3fec68 | 2019-06-06 18:11:07 -0700 | [diff] [blame] | 433 | PendingAlertInfo(NotificationEntry entry) { |
Kevin | 4b8bbda | 2018-11-19 14:36:31 -0800 | [diff] [blame] | 434 | mOriginalNotification = entry.notification; |
| 435 | mEntry = entry; |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Whether or not the pending alert is still valid and should still alert after inflation. |
| 440 | * |
| 441 | * @return true if the pending alert should still occur, false o/w |
| 442 | */ |
| 443 | private boolean isStillValid() { |
| 444 | if (mAbortOnInflation) { |
| 445 | // Notification is aborted due to the transfer being explicitly cancelled |
| 446 | return false; |
| 447 | } |
Kevin | 4b8bbda | 2018-11-19 14:36:31 -0800 | [diff] [blame] | 448 | if (mEntry.notification.getGroupKey() != mOriginalNotification.getGroupKey()) { |
| 449 | // Groups have changed |
| 450 | return false; |
| 451 | } |
| 452 | if (mEntry.notification.getNotification().isGroupSummary() |
| 453 | != mOriginalNotification.getNotification().isGroupSummary()) { |
| 454 | // Notification has changed from group summary to not or vice versa |
| 455 | return false; |
| 456 | } |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 457 | return true; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Contains alert metadata for the notification group used to determine when/how the alert |
| 463 | * should be transferred. |
| 464 | */ |
| 465 | private static class GroupAlertEntry { |
| 466 | /** |
| 467 | * The time when the last alert transfer from summary to child happened. |
| 468 | */ |
| 469 | long mLastAlertTransferTime; |
| 470 | boolean mAlertSummaryOnNextAddition; |
| 471 | final NotificationGroup mGroup; |
| 472 | |
| 473 | GroupAlertEntry(NotificationGroup group) { |
| 474 | this.mGroup = group; |
| 475 | } |
| 476 | } |
| 477 | } |