Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 19 | import android.annotation.NonNull; |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 20 | import android.app.Notification; |
| 21 | import android.os.SystemClock; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 22 | import android.service.notification.StatusBarNotification; |
Aurimas Liutikas | fd52c14 | 2018-04-17 09:50:46 -0700 | [diff] [blame] | 23 | import androidx.annotation.Nullable; |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 24 | import android.util.Log; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 25 | |
Jason Monk | 1fd3fc3 | 2018-08-14 17:20:09 -0400 | [diff] [blame] | 26 | import com.android.systemui.Dependency; |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 27 | import com.android.systemui.statusbar.AlertingNotificationManager; |
| 28 | import com.android.systemui.statusbar.AmbientPulseManager; |
| 29 | import com.android.systemui.statusbar.AmbientPulseManager.OnAmbientChangedListener; |
Jason Monk | 1fd3fc3 | 2018-08-14 17:20:09 -0400 | [diff] [blame] | 30 | import com.android.systemui.statusbar.StatusBarState; |
| 31 | import com.android.systemui.statusbar.StatusBarStateController.StateListener; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 32 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
| 33 | import com.android.systemui.statusbar.notification.NotificationData; |
Jason Monk | 1fd3fc3 | 2018-08-14 17:20:09 -0400 | [diff] [blame] | 34 | import com.android.systemui.statusbar.StatusBarStateController; |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 35 | import com.android.systemui.statusbar.policy.HeadsUpManager; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 36 | import com.android.systemui.statusbar.policy.OnHeadsUpChangedListener; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 37 | |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame] | 38 | import java.io.FileDescriptor; |
| 39 | import java.io.PrintWriter; |
Selim Cinek | c0b14b0 | 2016-05-09 13:12:40 -0400 | [diff] [blame] | 40 | import java.util.ArrayList; |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 41 | import java.util.Collection; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 42 | import java.util.HashMap; |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 43 | import java.util.Iterator; |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame] | 44 | import java.util.Map; |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 45 | import java.util.Objects; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * A class to handle notifications and their corresponding groups. |
| 49 | */ |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 50 | public class NotificationGroupManager implements OnHeadsUpChangedListener, |
| 51 | OnAmbientChangedListener { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 52 | |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 53 | private static final String TAG = "NotificationGroupManager"; |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 54 | private static final long ALERT_TRANSFER_TIMEOUT = 300; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 55 | private final HashMap<String, NotificationGroup> mGroupMap = new HashMap<>(); |
| 56 | private OnGroupChangeListener mListener; |
| 57 | private int mBarState = -1; |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 58 | private HashMap<String, StatusBarNotification> mIsolatedEntries = new HashMap<>(); |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 59 | private HeadsUpManager mHeadsUpManager; |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 60 | private AmbientPulseManager mAmbientPulseManager = Dependency.get(AmbientPulseManager.class); |
| 61 | private boolean mIsDozing; |
Selim Cinek | 80c44dd | 2016-08-15 19:39:55 -0700 | [diff] [blame] | 62 | private boolean mIsUpdatingUnchangedGroup; |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 63 | private HashMap<String, NotificationData.Entry> mPendingNotifications; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 64 | |
Jason Monk | 1fd3fc3 | 2018-08-14 17:20:09 -0400 | [diff] [blame] | 65 | private final StateListener mStateListener = this::setStatusBarState; |
| 66 | |
| 67 | public NotificationGroupManager() { |
| 68 | Dependency.get(StatusBarStateController.class).addListener(mStateListener); |
| 69 | } |
| 70 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 71 | public void setOnGroupChangeListener(OnGroupChangeListener listener) { |
| 72 | mListener = listener; |
| 73 | } |
| 74 | |
| 75 | public boolean isGroupExpanded(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 76 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 77 | if (group == null) { |
| 78 | return false; |
| 79 | } |
| 80 | return group.expanded; |
| 81 | } |
| 82 | |
| 83 | public void setGroupExpanded(StatusBarNotification sbn, boolean expanded) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 84 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 85 | if (group == null) { |
| 86 | return; |
| 87 | } |
| 88 | setGroupExpanded(group, expanded); |
| 89 | } |
| 90 | |
| 91 | private void setGroupExpanded(NotificationGroup group, boolean expanded) { |
| 92 | group.expanded = expanded; |
| 93 | if (group.summary != null) { |
| 94 | mListener.onGroupExpansionChanged(group.summary.row, expanded); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | public void onEntryRemoved(NotificationData.Entry removed) { |
| 99 | onEntryRemovedInternal(removed, removed.notification); |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame] | 100 | mIsolatedEntries.remove(removed.key); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * An entry was removed. |
| 105 | * |
| 106 | * @param removed the removed entry |
| 107 | * @param sbn the notification the entry has, which doesn't need to be the same as it's internal |
| 108 | * notification |
| 109 | */ |
| 110 | private void onEntryRemovedInternal(NotificationData.Entry removed, |
| 111 | final StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 112 | String groupKey = getGroupKey(sbn); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 113 | final NotificationGroup group = mGroupMap.get(groupKey); |
Selim Cinek | 0b4aeab | 2015-09-01 17:07:38 -0700 | [diff] [blame] | 114 | if (group == null) { |
| 115 | // When an app posts 2 different notifications as summary of the same group, then a |
| 116 | // cancellation of the first notification removes this group. |
| 117 | // This situation is not supported and we will not allow such notifications anymore in |
| 118 | // the close future. See b/23676310 for reference. |
| 119 | return; |
| 120 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 121 | if (isGroupChild(sbn)) { |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 122 | group.children.remove(removed.key); |
Selim Cinek | e73ad21 | 2015-11-03 19:11:08 -0800 | [diff] [blame] | 123 | } else { |
| 124 | group.summary = null; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 125 | } |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 126 | updateSuppression(group); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 127 | if (group.children.isEmpty()) { |
| 128 | if (group.summary == null) { |
| 129 | mGroupMap.remove(groupKey); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 134 | public void onEntryAdded(final NotificationData.Entry added) { |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 135 | if (added.row.isRemoved()) { |
| 136 | added.setDebugThrowable(new Throwable()); |
| 137 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 138 | final StatusBarNotification sbn = added.notification; |
| 139 | boolean isGroupChild = isGroupChild(sbn); |
| 140 | String groupKey = getGroupKey(sbn); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 141 | NotificationGroup group = mGroupMap.get(groupKey); |
| 142 | if (group == null) { |
| 143 | group = new NotificationGroup(); |
| 144 | mGroupMap.put(groupKey, group); |
| 145 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 146 | if (isGroupChild) { |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 147 | NotificationData.Entry existing = group.children.get(added.key); |
| 148 | if (existing != null && existing != added) { |
| 149 | Throwable existingThrowable = existing.getDebugThrowable(); |
| 150 | Log.wtf(TAG, "Inconsistent entries found with the same key " + added.key |
| 151 | + "existing removed: " + existing.row.isRemoved() |
| 152 | + (existingThrowable != null |
| 153 | ? Log.getStackTraceString(existingThrowable) + "\n": "") |
| 154 | + " added removed" + added.row.isRemoved() |
| 155 | , new Throwable()); |
| 156 | } |
| 157 | group.children.put(added.key, added); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 158 | updateSuppression(group); |
Selim Cinek | e73ad21 | 2015-11-03 19:11:08 -0800 | [diff] [blame] | 159 | } else { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 160 | group.summary = added; |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 161 | group.expanded = added.row.areChildrenExpanded(); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 162 | updateSuppression(group); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 163 | if (!group.children.isEmpty()) { |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 164 | ArrayList<NotificationData.Entry> childrenCopy |
| 165 | = new ArrayList<>(group.children.values()); |
Selim Cinek | 50e7467 | 2016-05-05 15:58:10 -0400 | [diff] [blame] | 166 | for (NotificationData.Entry child : childrenCopy) { |
| 167 | onEntryBecomingChild(child); |
| 168 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 169 | mListener.onGroupCreatedFromChildren(group); |
| 170 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 171 | } |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 172 | cleanUpAlertStatesOnAdd(group, false /* addIsPending */); |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | public void onPendingEntryAdded(NotificationData.Entry shadeEntry) { |
| 176 | String groupKey = getGroupKey(shadeEntry.notification); |
| 177 | NotificationGroup group = mGroupMap.get(groupKey); |
| 178 | if (group != null) { |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 179 | cleanUpAlertStatesOnAdd(group, true /* addIsPending */); |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | /** |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 184 | * Set whether or not the device is dozing. This allows the group manager to reset some |
| 185 | * specific alert state logic based off when the state changes. |
| 186 | * @param isDozing if the device is dozing. |
| 187 | */ |
| 188 | public void setDozing(boolean isDozing) { |
| 189 | if (mIsDozing != isDozing) { |
| 190 | for (NotificationGroup group : mGroupMap.values()) { |
| 191 | group.lastAlertTransfer = 0; |
| 192 | group.alertSummaryOnNextAddition = false; |
| 193 | } |
| 194 | } |
| 195 | mIsDozing = isDozing; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Clean up the alert states when a new child was added. |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 200 | * @param group The group where a view was added or will be added. |
| 201 | * @param addIsPending True if is the addition still pending or false has it already been added. |
| 202 | */ |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 203 | private void cleanUpAlertStatesOnAdd(NotificationGroup group, boolean addIsPending) { |
| 204 | |
| 205 | AlertingNotificationManager alertManager = |
| 206 | mIsDozing ? mAmbientPulseManager : mHeadsUpManager; |
| 207 | if (!addIsPending && group.alertSummaryOnNextAddition) { |
| 208 | if (!alertManager.isAlerting(group.summary.key)) { |
| 209 | alertManager.showNotification(group.summary); |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 210 | } |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 211 | group.alertSummaryOnNextAddition = false; |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 212 | } |
| 213 | // Because notification groups are not delivered as a whole unit, it may happen that a |
| 214 | // group child gets added quite a bit after the summary got posted. Our guidance is, that |
| 215 | // apps should always post the group summary as well and we'll hide it for them if the child |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 216 | // is the only child in a group. Because of this, we also have to transfer alert to the |
| 217 | // child, otherwise the invisible summary would be alerted. |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 218 | // This transfer to the child is not always correct in case the app has just posted another |
| 219 | // child in addition to the existing one, but it hasn't arrived in systemUI yet. In such |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 220 | // a scenario we would transfer the alert to the old child and the wrong notification |
| 221 | // would be alerted. In order to avoid this, we'll recover from this issue and alert the |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 222 | // summary again instead of the old child if it's within a certain timeout. |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 223 | if (SystemClock.elapsedRealtime() - group.lastAlertTransfer < ALERT_TRANSFER_TIMEOUT) { |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 224 | if (!onlySummaryAlerts(group.summary)) { |
| 225 | return; |
| 226 | } |
| 227 | int numChildren = group.children.size(); |
| 228 | NotificationData.Entry isolatedChild = getIsolatedChild(getGroupKey( |
| 229 | group.summary.notification)); |
| 230 | int numPendingChildren = getPendingChildrenNotAlerting(group); |
| 231 | numChildren += numPendingChildren; |
| 232 | if (isolatedChild != null) { |
| 233 | numChildren++; |
| 234 | } |
| 235 | if (numChildren <= 1) { |
| 236 | return; |
| 237 | } |
| 238 | boolean releasedChild = false; |
| 239 | ArrayList<NotificationData.Entry> children = new ArrayList<>(group.children.values()); |
| 240 | int size = children.size(); |
| 241 | for (int i = 0; i < size; i++) { |
| 242 | NotificationData.Entry entry = children.get(i); |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 243 | if (onlySummaryAlerts(entry) && alertManager.isAlerting(entry.key)) { |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 244 | releasedChild = true; |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 245 | alertManager.removeNotification(entry.key, true /* releaseImmediately */); |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | if (isolatedChild != null && onlySummaryAlerts(isolatedChild) |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 249 | && alertManager.isAlerting(isolatedChild.key)) { |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 250 | releasedChild = true; |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 251 | alertManager.removeNotification(isolatedChild.key, true /* releaseImmediately */); |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 252 | } |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 253 | if (releasedChild && !alertManager.isAlerting(group.summary.key)) { |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 254 | boolean notifyImmediately = (numChildren - numPendingChildren) > 1; |
| 255 | if (notifyImmediately) { |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 256 | alertManager.showNotification(group.summary); |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 257 | } else { |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 258 | group.alertSummaryOnNextAddition = true; |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 259 | } |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 260 | group.lastAlertTransfer = 0; |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | private int getPendingChildrenNotAlerting(NotificationGroup group) { |
| 266 | if (mPendingNotifications == null) { |
| 267 | return 0; |
| 268 | } |
| 269 | int number = 0; |
| 270 | String groupKey = getGroupKey(group.summary.notification); |
| 271 | Collection<NotificationData.Entry> values = mPendingNotifications.values(); |
| 272 | for (NotificationData.Entry entry : values) { |
| 273 | if (!isGroupChild(entry.notification)) { |
| 274 | continue; |
| 275 | } |
| 276 | if (!Objects.equals(getGroupKey(entry.notification), groupKey)) { |
| 277 | continue; |
| 278 | } |
| 279 | if (group.children.containsKey(entry.key)) { |
| 280 | continue; |
| 281 | } |
| 282 | if (onlySummaryAlerts(entry)) { |
| 283 | number++; |
| 284 | } |
| 285 | } |
| 286 | return number; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 287 | } |
| 288 | |
Selim Cinek | 50e7467 | 2016-05-05 15:58:10 -0400 | [diff] [blame] | 289 | private void onEntryBecomingChild(NotificationData.Entry entry) { |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 290 | if (shouldIsolate(entry)) { |
| 291 | isolateNotification(entry); |
Selim Cinek | 50e7467 | 2016-05-05 15:58:10 -0400 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 295 | private void updateSuppression(NotificationGroup group) { |
Selim Cinek | 80c44dd | 2016-08-15 19:39:55 -0700 | [diff] [blame] | 296 | if (group == null) { |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 297 | return; |
| 298 | } |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 299 | boolean prevSuppressed = group.suppressed; |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 300 | group.suppressed = group.summary != null && !group.expanded |
| 301 | && (group.children.size() == 1 |
| 302 | || (group.children.size() == 0 |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 303 | && group.summary.notification.getNotification().isGroupSummary() |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 304 | && hasIsolatedChildren(group))); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 305 | if (prevSuppressed != group.suppressed) { |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 306 | if (group.suppressed) { |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 307 | if (mHeadsUpManager.isAlerting(group.summary.key)) { |
| 308 | handleSuppressedSummaryAlerted(group.summary, mHeadsUpManager); |
| 309 | } else if (mAmbientPulseManager.isAlerting(group.summary.key)) { |
| 310 | handleSuppressedSummaryAlerted(group.summary, mAmbientPulseManager); |
| 311 | } |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 312 | } |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 313 | if (!mIsUpdatingUnchangedGroup && mListener != null) { |
Selim Cinek | 80c44dd | 2016-08-15 19:39:55 -0700 | [diff] [blame] | 314 | mListener.onGroupsChanged(); |
| 315 | } |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 319 | private boolean hasIsolatedChildren(NotificationGroup group) { |
| 320 | return getNumberOfIsolatedChildren(group.summary.notification.getGroupKey()) != 0; |
| 321 | } |
| 322 | |
| 323 | private int getNumberOfIsolatedChildren(String groupKey) { |
| 324 | int count = 0; |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 325 | for (StatusBarNotification sbn : mIsolatedEntries.values()) { |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 326 | if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) { |
| 327 | count++; |
| 328 | } |
| 329 | } |
| 330 | return count; |
| 331 | } |
| 332 | |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 333 | private NotificationData.Entry getIsolatedChild(String groupKey) { |
| 334 | for (StatusBarNotification sbn : mIsolatedEntries.values()) { |
| 335 | if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) { |
| 336 | return mGroupMap.get(sbn.getKey()).summary; |
| 337 | } |
| 338 | } |
| 339 | return null; |
| 340 | } |
| 341 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 342 | public void onEntryUpdated(NotificationData.Entry entry, |
| 343 | StatusBarNotification oldNotification) { |
Selim Cinek | 68bdff1 | 2016-08-03 13:38:56 -0700 | [diff] [blame] | 344 | String oldKey = oldNotification.getGroupKey(); |
| 345 | String newKey = entry.notification.getGroupKey(); |
| 346 | boolean groupKeysChanged = !oldKey.equals(newKey); |
| 347 | boolean wasGroupChild = isGroupChild(oldNotification); |
| 348 | boolean isGroupChild = isGroupChild(entry.notification); |
Selim Cinek | 80c44dd | 2016-08-15 19:39:55 -0700 | [diff] [blame] | 349 | mIsUpdatingUnchangedGroup = !groupKeysChanged && wasGroupChild == isGroupChild; |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 350 | if (mGroupMap.get(getGroupKey(oldNotification)) != null) { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 351 | onEntryRemovedInternal(entry, oldNotification); |
| 352 | } |
| 353 | onEntryAdded(entry); |
Selim Cinek | 80c44dd | 2016-08-15 19:39:55 -0700 | [diff] [blame] | 354 | mIsUpdatingUnchangedGroup = false; |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 355 | if (isIsolated(entry.notification)) { |
| 356 | mIsolatedEntries.put(entry.key, entry.notification); |
Selim Cinek | 68bdff1 | 2016-08-03 13:38:56 -0700 | [diff] [blame] | 357 | if (groupKeysChanged) { |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 358 | updateSuppression(mGroupMap.get(oldKey)); |
| 359 | updateSuppression(mGroupMap.get(newKey)); |
| 360 | } |
Selim Cinek | 68bdff1 | 2016-08-03 13:38:56 -0700 | [diff] [blame] | 361 | } else if (!wasGroupChild && isGroupChild) { |
Selim Cinek | 50e7467 | 2016-05-05 15:58:10 -0400 | [diff] [blame] | 362 | onEntryBecomingChild(entry); |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 363 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 364 | } |
| 365 | |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 366 | public boolean isSummaryOfSuppressedGroup(StatusBarNotification sbn) { |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 367 | return isGroupSuppressed(getGroupKey(sbn)) && sbn.getNotification().isGroupSummary(); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 368 | } |
| 369 | |
Selim Cinek | 53c4d3d | 2016-05-23 10:56:34 -0700 | [diff] [blame] | 370 | private boolean isOnlyChild(StatusBarNotification sbn) { |
Selim Cinek | 36b0223 | 2016-05-11 23:07:05 -0400 | [diff] [blame] | 371 | return !sbn.getNotification().isGroupSummary() |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 372 | && getTotalNumberOfChildren(sbn) == 1; |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Selim Cinek | 53c4d3d | 2016-05-23 10:56:34 -0700 | [diff] [blame] | 375 | public boolean isOnlyChildInGroup(StatusBarNotification sbn) { |
Selim Cinek | 88086e7 | 2016-06-17 21:01:32 -0700 | [diff] [blame] | 376 | if (!isOnlyChild(sbn)) { |
| 377 | return false; |
| 378 | } |
| 379 | ExpandableNotificationRow logicalGroupSummary = getLogicalGroupSummary(sbn); |
| 380 | return logicalGroupSummary != null |
| 381 | && !logicalGroupSummary.getStatusBarNotification().equals(sbn); |
Selim Cinek | 53c4d3d | 2016-05-23 10:56:34 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 384 | private int getTotalNumberOfChildren(StatusBarNotification sbn) { |
Selim Cinek | 53c4d3d | 2016-05-23 10:56:34 -0700 | [diff] [blame] | 385 | int isolatedChildren = getNumberOfIsolatedChildren(sbn.getGroupKey()); |
| 386 | NotificationGroup group = mGroupMap.get(sbn.getGroupKey()); |
| 387 | int realChildren = group != null ? group.children.size() : 0; |
| 388 | return isolatedChildren + realChildren; |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | private boolean isGroupSuppressed(String groupKey) { |
| 392 | NotificationGroup group = mGroupMap.get(groupKey); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 393 | return group != null && group.suppressed; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 394 | } |
| 395 | |
Jason Monk | 1fd3fc3 | 2018-08-14 17:20:09 -0400 | [diff] [blame] | 396 | private void setStatusBarState(int newState) { |
Selim Cinek | 9c4c414 | 2015-12-04 16:44:56 -0800 | [diff] [blame] | 397 | mBarState = newState; |
| 398 | if (mBarState == StatusBarState.KEYGUARD) { |
Selim Cinek | 9184f9c | 2016-02-02 17:36:53 -0800 | [diff] [blame] | 399 | collapseAllGroups(); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | public void collapseAllGroups() { |
Selim Cinek | c0b14b0 | 2016-05-09 13:12:40 -0400 | [diff] [blame] | 404 | // Because notifications can become isolated when the group becomes suppressed it can |
| 405 | // lead to concurrent modifications while looping. We need to make a copy. |
| 406 | ArrayList<NotificationGroup> groupCopy = new ArrayList<>(mGroupMap.values()); |
| 407 | int size = groupCopy.size(); |
| 408 | for (int i = 0; i < size; i++) { |
| 409 | NotificationGroup group = groupCopy.get(i); |
Selim Cinek | 9184f9c | 2016-02-02 17:36:53 -0800 | [diff] [blame] | 410 | if (group.expanded) { |
| 411 | setGroupExpanded(group, false); |
Selim Cinek | 9c4c414 | 2015-12-04 16:44:56 -0800 | [diff] [blame] | 412 | } |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 413 | updateSuppression(group); |
Selim Cinek | 9c4c414 | 2015-12-04 16:44:56 -0800 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 417 | /** |
| 418 | * @return whether a given notification is a child in a group which has a summary |
| 419 | */ |
| 420 | public boolean isChildInGroupWithSummary(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 421 | if (!isGroupChild(sbn)) { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 422 | return false; |
| 423 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 424 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 425 | if (group == null || group.summary == null || group.suppressed) { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 426 | return false; |
| 427 | } |
Selim Cinek | 3f19f60 | 2016-05-02 18:01:56 -0700 | [diff] [blame] | 428 | if (group.children.isEmpty()) { |
| 429 | // If the suppression of a group changes because the last child was removed, this can |
| 430 | // still be called temporarily because the child hasn't been fully removed yet. Let's |
| 431 | // make sure we still return false in that case. |
| 432 | return false; |
| 433 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 434 | return true; |
| 435 | } |
| 436 | |
Selim Cinek | 263398f | 2015-10-21 17:40:23 -0700 | [diff] [blame] | 437 | /** |
| 438 | * @return whether a given notification is a summary in a group which has children |
| 439 | */ |
| 440 | public boolean isSummaryOfGroup(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 441 | if (!isGroupSummary(sbn)) { |
Selim Cinek | 263398f | 2015-10-21 17:40:23 -0700 | [diff] [blame] | 442 | return false; |
| 443 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 444 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Kevin | 96eed64 | 2018-09-05 18:53:33 -0700 | [diff] [blame] | 445 | if (group == null || group.summary == null) { |
Selim Cinek | 263398f | 2015-10-21 17:40:23 -0700 | [diff] [blame] | 446 | return false; |
| 447 | } |
Kevin | 96eed64 | 2018-09-05 18:53:33 -0700 | [diff] [blame] | 448 | return !group.children.isEmpty() && Objects.equals(group.summary.notification, sbn); |
Selim Cinek | 263398f | 2015-10-21 17:40:23 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 451 | /** |
| 452 | * Get the summary of a specified status bar notification. For isolated notification this return |
| 453 | * itself. |
| 454 | */ |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 455 | public ExpandableNotificationRow getGroupSummary(StatusBarNotification sbn) { |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 456 | return getGroupSummary(getGroupKey(sbn)); |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Similar to {@link #getGroupSummary(StatusBarNotification)} but doesn't get the visual summary |
| 461 | * but the logical summary, i.e when a child is isolated, it still returns the summary as if |
| 462 | * it wasn't isolated. |
| 463 | */ |
| 464 | public ExpandableNotificationRow getLogicalGroupSummary( |
| 465 | StatusBarNotification sbn) { |
| 466 | return getGroupSummary(sbn.getGroupKey()); |
| 467 | } |
| 468 | |
| 469 | @Nullable |
| 470 | private ExpandableNotificationRow getGroupSummary(String groupKey) { |
| 471 | NotificationGroup group = mGroupMap.get(groupKey); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 472 | return group == null ? null |
| 473 | : group.summary == null ? null |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 474 | : group.summary.row; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 475 | } |
| 476 | |
Chris Wren | 698b170 | 2016-05-23 11:16:32 -0400 | [diff] [blame] | 477 | /** @return group expansion state after toggling. */ |
| 478 | public boolean toggleGroupExpansion(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 479 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 83bc783 | 2015-10-22 13:26:54 -0700 | [diff] [blame] | 480 | if (group == null) { |
Chris Wren | 698b170 | 2016-05-23 11:16:32 -0400 | [diff] [blame] | 481 | return false; |
Selim Cinek | 83bc783 | 2015-10-22 13:26:54 -0700 | [diff] [blame] | 482 | } |
| 483 | setGroupExpanded(group, !group.expanded); |
Chris Wren | 698b170 | 2016-05-23 11:16:32 -0400 | [diff] [blame] | 484 | return group.expanded; |
Selim Cinek | 83bc783 | 2015-10-22 13:26:54 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 487 | private boolean isIsolated(StatusBarNotification sbn) { |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 488 | return mIsolatedEntries.containsKey(sbn.getKey()); |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | private boolean isGroupSummary(StatusBarNotification sbn) { |
| 492 | if (isIsolated(sbn)) { |
| 493 | return true; |
| 494 | } |
| 495 | return sbn.getNotification().isGroupSummary(); |
| 496 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 497 | |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 498 | private boolean isGroupChild(StatusBarNotification sbn) { |
| 499 | if (isIsolated(sbn)) { |
| 500 | return false; |
| 501 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 502 | return sbn.isGroup() && !sbn.getNotification().isGroupSummary(); |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | private String getGroupKey(StatusBarNotification sbn) { |
| 506 | if (isIsolated(sbn)) { |
| 507 | return sbn.getKey(); |
| 508 | } |
| 509 | return sbn.getGroupKey(); |
| 510 | } |
| 511 | |
| 512 | @Override |
| 513 | public void onHeadsUpPinnedModeChanged(boolean inPinnedMode) { |
| 514 | } |
| 515 | |
| 516 | @Override |
| 517 | public void onHeadsUpPinned(ExpandableNotificationRow headsUp) { |
| 518 | } |
| 519 | |
| 520 | @Override |
| 521 | public void onHeadsUpUnPinned(ExpandableNotificationRow headsUp) { |
| 522 | } |
| 523 | |
| 524 | @Override |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 525 | public void onAmbientStateChanged(NotificationData.Entry entry, boolean isAmbient) { |
| 526 | onAlertStateChanged(entry, isAmbient, mAmbientPulseManager); |
| 527 | } |
| 528 | |
| 529 | @Override |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 530 | public void onHeadsUpStateChanged(NotificationData.Entry entry, boolean isHeadsUp) { |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 531 | onAlertStateChanged(entry, isHeadsUp, mHeadsUpManager); |
| 532 | } |
| 533 | |
| 534 | private void onAlertStateChanged(NotificationData.Entry entry, boolean isAlerting, |
| 535 | AlertingNotificationManager alertManager) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 536 | final StatusBarNotification sbn = entry.notification; |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 537 | if (isAlerting) { |
| 538 | if (shouldIsolate(entry)) { |
| 539 | isolateNotification(entry); |
| 540 | } else if (sbn.getNotification().isGroupSummary() |
| 541 | && isGroupSuppressed(sbn.getGroupKey())){ |
| 542 | handleSuppressedSummaryAlerted(entry, alertManager); |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 543 | } |
| 544 | } else { |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 545 | stopIsolatingNotification(entry); |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 549 | /** |
| 550 | * Handles the scenario where a summary that has been suppressed is alerted. A suppressed |
| 551 | * summary should for all intents and purposes be invisible to the user and as a result should |
| 552 | * not alert. When this is the case, it is our responsibility to pass the alert to the |
| 553 | * appropriate child which will be the representative notification alerting for the group. |
| 554 | * @param summary the summary that is suppressed and alerting |
| 555 | * @param alertManager the alert manager that manages the alerting summary |
| 556 | */ |
| 557 | private void handleSuppressedSummaryAlerted(@NonNull NotificationData.Entry summary, |
| 558 | @NonNull AlertingNotificationManager alertManager) { |
| 559 | StatusBarNotification sbn = summary.notification; |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 560 | if (!isGroupSuppressed(sbn.getGroupKey()) |
| 561 | || !sbn.getNotification().isGroupSummary() |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 562 | || !alertManager.isAlerting(sbn.getKey())) { |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 563 | return; |
| 564 | } |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 565 | |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 566 | // The parent of a suppressed group got alerted, lets alert the child! |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 567 | NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey()); |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 568 | |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 569 | if (notificationGroup != null) { |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 570 | if (pendingInflationsWillAddChildren(notificationGroup)) { |
| 571 | // New children will actually be added to this group, let's not transfer the alert. |
| 572 | return; |
| 573 | } |
| 574 | |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 575 | Iterator<NotificationData.Entry> iterator |
| 576 | = notificationGroup.children.values().iterator(); |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 577 | NotificationData.Entry child = iterator.hasNext() ? iterator.next() : null; |
| 578 | if (child == null) { |
| 579 | child = getIsolatedChild(sbn.getGroupKey()); |
| 580 | } |
| 581 | if (child != null) { |
Selim Cinek | 40f8876 | 2016-12-16 16:56:41 -0800 | [diff] [blame] | 582 | if (child.row.keepInParent() || child.row.isRemoved() || child.row.isDismissed()) { |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 583 | // the notification is actually already removed, no need to do alert on it. |
Selim Cinek | 40f8876 | 2016-12-16 16:56:41 -0800 | [diff] [blame] | 584 | return; |
| 585 | } |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 586 | transferAlertStateToChild(summary, child, alertManager); |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 587 | } |
| 588 | } |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 589 | } |
| 590 | |
| 591 | /** |
| 592 | * Transfers the alert state from a given summary notification to the specified child. The |
| 593 | * result is the child will now alert while the summary does not. |
| 594 | * |
| 595 | * @param summary the currently alerting summary notification |
| 596 | * @param child the child that should receive the alert |
| 597 | * @param alertManager the manager for the alert |
| 598 | */ |
| 599 | private void transferAlertStateToChild(@NonNull NotificationData.Entry summary, |
| 600 | @NonNull NotificationData.Entry child, |
| 601 | @NonNull AlertingNotificationManager alertManager) { |
| 602 | NotificationGroup notificationGroup = mGroupMap.get(summary.notification.getGroupKey()); |
| 603 | if (alertManager.isAlerting(child.key)) { |
| 604 | alertManager.updateNotification(child.key, true /* alert */); |
| 605 | } else { |
| 606 | if (onlySummaryAlerts(summary)) { |
| 607 | notificationGroup.lastAlertTransfer = SystemClock.elapsedRealtime(); |
| 608 | } |
| 609 | alertManager.showNotification(child); |
| 610 | } |
| 611 | alertManager.removeNotification(summary.key, true /* releaseImmediately */); |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 614 | private boolean onlySummaryAlerts(NotificationData.Entry entry) { |
| 615 | return entry.notification.getNotification().getGroupAlertBehavior() |
| 616 | == Notification.GROUP_ALERT_SUMMARY; |
| 617 | } |
| 618 | |
| 619 | /** |
| 620 | * Check if the pending inflations will add children to this group. |
| 621 | * @param group The group to check. |
| 622 | */ |
| 623 | private boolean pendingInflationsWillAddChildren(NotificationGroup group) { |
| 624 | if (mPendingNotifications == null) { |
| 625 | return false; |
| 626 | } |
| 627 | Collection<NotificationData.Entry> values = mPendingNotifications.values(); |
| 628 | String groupKey = getGroupKey(group.summary.notification); |
| 629 | for (NotificationData.Entry entry : values) { |
| 630 | if (!isGroupChild(entry.notification)) { |
| 631 | continue; |
| 632 | } |
| 633 | if (!Objects.equals(getGroupKey(entry.notification), groupKey)) { |
| 634 | continue; |
| 635 | } |
| 636 | if (!group.children.containsKey(entry.key)) { |
| 637 | return true; |
| 638 | } |
| 639 | } |
| 640 | return false; |
| 641 | } |
| 642 | |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 643 | /** |
| 644 | * Whether a notification that is normally part of a group should be temporarily isolated from |
| 645 | * the group and put in their own group visually. This generally happens when the notification |
| 646 | * is alerting. |
| 647 | * |
| 648 | * @param entry the notification to check |
| 649 | * @return true if the entry should be isolated |
| 650 | */ |
| 651 | |
| 652 | private boolean shouldIsolate(NotificationData.Entry entry) { |
| 653 | StatusBarNotification sbn = entry.notification; |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 654 | NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey()); |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 655 | if (!sbn.isGroup() || sbn.getNotification().isGroupSummary()) { |
| 656 | return false; |
| 657 | } |
| 658 | if (!mIsDozing && !mHeadsUpManager.isAlerting(entry.key)) { |
| 659 | return false; |
| 660 | } |
| 661 | if (mIsDozing && !mAmbientPulseManager.isAlerting(entry.key)) { |
| 662 | return false; |
| 663 | } |
| 664 | return (sbn.getNotification().fullScreenIntent != null |
| 665 | || notificationGroup == null |
| 666 | || !notificationGroup.expanded |
| 667 | || isGroupNotFullyVisible(notificationGroup)); |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * Isolate a notification from its group so that it visually shows as its own group. |
| 672 | * |
| 673 | * @param entry the notification to isolate |
| 674 | */ |
| 675 | private void isolateNotification(NotificationData.Entry entry) { |
| 676 | StatusBarNotification sbn = entry.notification; |
| 677 | |
| 678 | // We will be isolated now, so lets update the groups |
| 679 | onEntryRemovedInternal(entry, entry.notification); |
| 680 | |
| 681 | mIsolatedEntries.put(sbn.getKey(), sbn); |
| 682 | |
| 683 | onEntryAdded(entry); |
| 684 | // We also need to update the suppression of the old group, because this call comes |
| 685 | // even before the groupManager knows about the notification at all. |
| 686 | // When the notification gets added afterwards it is already isolated and therefore |
| 687 | // it doesn't lead to an update. |
| 688 | updateSuppression(mGroupMap.get(entry.notification.getGroupKey())); |
| 689 | mListener.onGroupsChanged(); |
| 690 | } |
| 691 | |
| 692 | /** |
| 693 | * Stop isolating a notification and re-group it with its original logical group. |
| 694 | * |
| 695 | * @param entry the notification to un-isolate |
| 696 | */ |
| 697 | private void stopIsolatingNotification(NotificationData.Entry entry) { |
| 698 | StatusBarNotification sbn = entry.notification; |
| 699 | if (mIsolatedEntries.containsKey(sbn.getKey())) { |
| 700 | // not isolated anymore, we need to update the groups |
| 701 | onEntryRemovedInternal(entry, entry.notification); |
| 702 | mIsolatedEntries.remove(sbn.getKey()); |
| 703 | onEntryAdded(entry); |
| 704 | mListener.onGroupsChanged(); |
| 705 | } |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | private boolean isGroupNotFullyVisible(NotificationGroup notificationGroup) { |
| 709 | return notificationGroup.summary == null |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 710 | || notificationGroup.summary.row.getClipTopAmount() > 0 |
| 711 | || notificationGroup.summary.row.getTranslationY() < 0; |
| 712 | } |
| 713 | |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 714 | public void setHeadsUpManager(HeadsUpManager headsUpManager) { |
| 715 | mHeadsUpManager = headsUpManager; |
| 716 | } |
| 717 | |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame] | 718 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 719 | pw.println("GroupManager state:"); |
| 720 | pw.println(" number of groups: " + mGroupMap.size()); |
| 721 | for (Map.Entry<String, NotificationGroup> entry : mGroupMap.entrySet()) { |
| 722 | pw.println("\n key: " + entry.getKey()); pw.println(entry.getValue()); |
| 723 | } |
| 724 | pw.println("\n isolated entries: " + mIsolatedEntries.size()); |
| 725 | for (Map.Entry<String, StatusBarNotification> entry : mIsolatedEntries.entrySet()) { |
| 726 | pw.print(" "); pw.print(entry.getKey()); |
| 727 | pw.print(", "); pw.println(entry.getValue()); |
| 728 | } |
| 729 | } |
| 730 | |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 731 | public void setPendingEntries(HashMap<String, NotificationData.Entry> pendingNotifications) { |
| 732 | mPendingNotifications = pendingNotifications; |
| 733 | } |
| 734 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 735 | public static class NotificationGroup { |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 736 | public final HashMap<String, NotificationData.Entry> children = new HashMap<>(); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 737 | public NotificationData.Entry summary; |
| 738 | public boolean expanded; |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 739 | /** |
| 740 | * Is this notification group suppressed, i.e its summary is hidden |
| 741 | */ |
| 742 | public boolean suppressed; |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 743 | /** |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 744 | * The time when the last alert transfer from group to child happened, while the summary |
| 745 | * has the flags to alert up on its own. |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 746 | */ |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame^] | 747 | public long lastAlertTransfer; |
| 748 | public boolean alertSummaryOnNextAddition; |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame] | 749 | |
| 750 | @Override |
| 751 | public String toString() { |
Selim Cinek | 60ca787 | 2016-05-12 11:24:07 -0700 | [diff] [blame] | 752 | String result = " summary:\n " |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 753 | + (summary != null ? summary.notification : "null") |
| 754 | + (summary != null && summary.getDebugThrowable() != null |
| 755 | ? Log.getStackTraceString(summary.getDebugThrowable()) |
| 756 | : ""); |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame] | 757 | result += "\n children size: " + children.size(); |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 758 | for (NotificationData.Entry child : children.values()) { |
| 759 | result += "\n " + child.notification |
| 760 | + (child.getDebugThrowable() != null |
| 761 | ? Log.getStackTraceString(child.getDebugThrowable()) |
| 762 | : ""); |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame] | 763 | } |
| 764 | return result; |
| 765 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | public interface OnGroupChangeListener { |
| 769 | /** |
| 770 | * The expansion of a group has changed. |
| 771 | * |
| 772 | * @param changedRow the row for which the expansion has changed, which is also the summary |
| 773 | * @param expanded a boolean indicating the new expanded state |
| 774 | */ |
| 775 | void onGroupExpansionChanged(ExpandableNotificationRow changedRow, boolean expanded); |
| 776 | |
| 777 | /** |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 778 | * A group of children just received a summary notification and should therefore become |
| 779 | * children of it. |
| 780 | * |
| 781 | * @param group the group created |
| 782 | */ |
| 783 | void onGroupCreatedFromChildren(NotificationGroup group); |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 784 | |
| 785 | /** |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 786 | * The groups have changed. This can happen if the isolation of a child has changes or if a |
| 787 | * group became suppressed / unsuppressed |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 788 | */ |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 789 | void onGroupsChanged(); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 790 | } |
| 791 | } |