blob: 6b12dd9519e438840326a1647962392afcec2dce [file] [log] [blame]
Selim Cinek25fd4e2b2015-02-20 17:46:07 +01001/*
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
17package com.android.systemui.statusbar.phone;
18
Selim Cinekf93bf3e2018-05-08 14:43:21 -070019import android.app.Notification;
20import android.os.SystemClock;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010021import android.service.notification.StatusBarNotification;
Selim Cinek04be3892017-08-08 10:58:32 -070022import android.util.Log;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010023
Evan Laird878c8532018-10-15 15:54:29 -040024import androidx.annotation.NonNull;
25import androidx.annotation.Nullable;
26
27import com.android.internal.annotations.VisibleForTesting;
Jason Monk1fd3fc32018-08-14 17:20:09 -040028import com.android.systemui.Dependency;
Kevina97ea052018-09-11 13:53:18 -070029import com.android.systemui.statusbar.AlertingNotificationManager;
30import com.android.systemui.statusbar.AmbientPulseManager;
31import com.android.systemui.statusbar.AmbientPulseManager.OnAmbientChangedListener;
Jason Monk1fd3fc32018-08-14 17:20:09 -040032import com.android.systemui.statusbar.StatusBarState;
Jason Monk1fd3fc32018-08-14 17:20:09 -040033import com.android.systemui.statusbar.StatusBarStateController;
Evan Laird878c8532018-10-15 15:54:29 -040034import com.android.systemui.statusbar.StatusBarStateController.StateListener;
35import com.android.systemui.statusbar.notification.NotificationData;
36import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
Selim Cinekef5127e2015-12-21 16:55:58 -080037import com.android.systemui.statusbar.policy.HeadsUpManager;
Selim Cineka7d4f822016-12-06 14:34:47 -080038import com.android.systemui.statusbar.policy.OnHeadsUpChangedListener;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010039
Selim Cinek52941c52016-05-07 18:29:32 -040040import java.io.FileDescriptor;
41import java.io.PrintWriter;
Selim Cinekc0b14b02016-05-09 13:12:40 -040042import java.util.ArrayList;
Selim Cinekf93bf3e2018-05-08 14:43:21 -070043import java.util.Collection;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010044import java.util.HashMap;
Selim Cinek967ed2a2016-04-08 18:29:11 -070045import java.util.Iterator;
Selim Cinek52941c52016-05-07 18:29:32 -040046import java.util.Map;
Selim Cinekf93bf3e2018-05-08 14:43:21 -070047import java.util.Objects;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010048
49/**
50 * A class to handle notifications and their corresponding groups.
51 */
Kevina97ea052018-09-11 13:53:18 -070052public class NotificationGroupManager implements OnHeadsUpChangedListener,
Evan Laird878c8532018-10-15 15:54:29 -040053 OnAmbientChangedListener, StateListener {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010054
Selim Cinek04be3892017-08-08 10:58:32 -070055 private static final String TAG = "NotificationGroupManager";
Kevina97ea052018-09-11 13:53:18 -070056 private static final long ALERT_TRANSFER_TIMEOUT = 300;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010057 private final HashMap<String, NotificationGroup> mGroupMap = new HashMap<>();
58 private OnGroupChangeListener mListener;
59 private int mBarState = -1;
Selim Cineka6c0cef2016-03-18 11:42:25 -070060 private HashMap<String, StatusBarNotification> mIsolatedEntries = new HashMap<>();
Selim Cinek967ed2a2016-04-08 18:29:11 -070061 private HeadsUpManager mHeadsUpManager;
Kevina97ea052018-09-11 13:53:18 -070062 private AmbientPulseManager mAmbientPulseManager = Dependency.get(AmbientPulseManager.class);
63 private boolean mIsDozing;
Selim Cinek80c44dd2016-08-15 19:39:55 -070064 private boolean mIsUpdatingUnchangedGroup;
Selim Cinekf93bf3e2018-05-08 14:43:21 -070065 private HashMap<String, NotificationData.Entry> mPendingNotifications;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010066
Jason Monk1fd3fc32018-08-14 17:20:09 -040067 public NotificationGroupManager() {
Evan Laird878c8532018-10-15 15:54:29 -040068 Dependency.get(StatusBarStateController.class).addListener(this);
Jason Monk1fd3fc32018-08-14 17:20:09 -040069 }
70
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010071 public void setOnGroupChangeListener(OnGroupChangeListener listener) {
72 mListener = listener;
73 }
74
75 public boolean isGroupExpanded(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -080076 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010077 if (group == null) {
78 return false;
79 }
80 return group.expanded;
81 }
82
83 public void setGroupExpanded(StatusBarNotification sbn, boolean expanded) {
Selim Cinekef5127e2015-12-21 16:55:58 -080084 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010085 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 Cinek52941c52016-05-07 18:29:32 -0400100 mIsolatedEntries.remove(removed.key);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100101 }
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 Cinekef5127e2015-12-21 16:55:58 -0800112 String groupKey = getGroupKey(sbn);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100113 final NotificationGroup group = mGroupMap.get(groupKey);
Selim Cinek0b4aeab2015-09-01 17:07:38 -0700114 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 Cinekef5127e2015-12-21 16:55:58 -0800121 if (isGroupChild(sbn)) {
Selim Cinek04be3892017-08-08 10:58:32 -0700122 group.children.remove(removed.key);
Selim Cineke73ad212015-11-03 19:11:08 -0800123 } else {
124 group.summary = null;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100125 }
Selim Cinek2a739342016-03-17 10:28:55 -0700126 updateSuppression(group);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100127 if (group.children.isEmpty()) {
128 if (group.summary == null) {
129 mGroupMap.remove(groupKey);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100130 }
131 }
132 }
133
Selim Cinekef5127e2015-12-21 16:55:58 -0800134 public void onEntryAdded(final NotificationData.Entry added) {
Selim Cinek04be3892017-08-08 10:58:32 -0700135 if (added.row.isRemoved()) {
136 added.setDebugThrowable(new Throwable());
137 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800138 final StatusBarNotification sbn = added.notification;
139 boolean isGroupChild = isGroupChild(sbn);
140 String groupKey = getGroupKey(sbn);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100141 NotificationGroup group = mGroupMap.get(groupKey);
142 if (group == null) {
143 group = new NotificationGroup();
144 mGroupMap.put(groupKey, group);
145 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800146 if (isGroupChild) {
Selim Cinek04be3892017-08-08 10:58:32 -0700147 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 Cinek2a739342016-03-17 10:28:55 -0700158 updateSuppression(group);
Selim Cineke73ad212015-11-03 19:11:08 -0800159 } else {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100160 group.summary = added;
Selim Cinekb5605e52015-02-20 18:21:41 +0100161 group.expanded = added.row.areChildrenExpanded();
Selim Cinek2a739342016-03-17 10:28:55 -0700162 updateSuppression(group);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100163 if (!group.children.isEmpty()) {
Selim Cinek04be3892017-08-08 10:58:32 -0700164 ArrayList<NotificationData.Entry> childrenCopy
165 = new ArrayList<>(group.children.values());
Selim Cinek50e74672016-05-05 15:58:10 -0400166 for (NotificationData.Entry child : childrenCopy) {
167 onEntryBecomingChild(child);
168 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100169 mListener.onGroupCreatedFromChildren(group);
170 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100171 }
Kevina97ea052018-09-11 13:53:18 -0700172 cleanUpAlertStatesOnAdd(group, false /* addIsPending */);
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700173 }
174
175 public void onPendingEntryAdded(NotificationData.Entry shadeEntry) {
176 String groupKey = getGroupKey(shadeEntry.notification);
177 NotificationGroup group = mGroupMap.get(groupKey);
178 if (group != null) {
Kevina97ea052018-09-11 13:53:18 -0700179 cleanUpAlertStatesOnAdd(group, true /* addIsPending */);
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700180 }
181 }
182
183 /**
Kevina97ea052018-09-11 13:53:18 -0700184 * 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 */
Evan Laird878c8532018-10-15 15:54:29 -0400188 @VisibleForTesting
Kevina97ea052018-09-11 13:53:18 -0700189 public void setDozing(boolean isDozing) {
190 if (mIsDozing != isDozing) {
191 for (NotificationGroup group : mGroupMap.values()) {
192 group.lastAlertTransfer = 0;
193 group.alertSummaryOnNextAddition = false;
194 }
195 }
196 mIsDozing = isDozing;
197 }
198
199 /**
200 * Clean up the alert states when a new child was added.
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700201 * @param group The group where a view was added or will be added.
202 * @param addIsPending True if is the addition still pending or false has it already been added.
203 */
Kevina97ea052018-09-11 13:53:18 -0700204 private void cleanUpAlertStatesOnAdd(NotificationGroup group, boolean addIsPending) {
205
206 AlertingNotificationManager alertManager =
207 mIsDozing ? mAmbientPulseManager : mHeadsUpManager;
208 if (!addIsPending && group.alertSummaryOnNextAddition) {
209 if (!alertManager.isAlerting(group.summary.key)) {
210 alertManager.showNotification(group.summary);
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700211 }
Kevina97ea052018-09-11 13:53:18 -0700212 group.alertSummaryOnNextAddition = false;
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700213 }
214 // Because notification groups are not delivered as a whole unit, it may happen that a
215 // group child gets added quite a bit after the summary got posted. Our guidance is, that
216 // apps should always post the group summary as well and we'll hide it for them if the child
Kevina97ea052018-09-11 13:53:18 -0700217 // is the only child in a group. Because of this, we also have to transfer alert to the
218 // child, otherwise the invisible summary would be alerted.
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700219 // This transfer to the child is not always correct in case the app has just posted another
220 // child in addition to the existing one, but it hasn't arrived in systemUI yet. In such
Kevina97ea052018-09-11 13:53:18 -0700221 // a scenario we would transfer the alert to the old child and the wrong notification
222 // would be alerted. In order to avoid this, we'll recover from this issue and alert the
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700223 // summary again instead of the old child if it's within a certain timeout.
Kevina97ea052018-09-11 13:53:18 -0700224 if (SystemClock.elapsedRealtime() - group.lastAlertTransfer < ALERT_TRANSFER_TIMEOUT) {
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700225 if (!onlySummaryAlerts(group.summary)) {
226 return;
227 }
228 int numChildren = group.children.size();
229 NotificationData.Entry isolatedChild = getIsolatedChild(getGroupKey(
230 group.summary.notification));
231 int numPendingChildren = getPendingChildrenNotAlerting(group);
232 numChildren += numPendingChildren;
233 if (isolatedChild != null) {
234 numChildren++;
235 }
236 if (numChildren <= 1) {
237 return;
238 }
239 boolean releasedChild = false;
240 ArrayList<NotificationData.Entry> children = new ArrayList<>(group.children.values());
241 int size = children.size();
242 for (int i = 0; i < size; i++) {
243 NotificationData.Entry entry = children.get(i);
Kevina97ea052018-09-11 13:53:18 -0700244 if (onlySummaryAlerts(entry) && alertManager.isAlerting(entry.key)) {
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700245 releasedChild = true;
Kevina97ea052018-09-11 13:53:18 -0700246 alertManager.removeNotification(entry.key, true /* releaseImmediately */);
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700247 }
248 }
249 if (isolatedChild != null && onlySummaryAlerts(isolatedChild)
Kevina97ea052018-09-11 13:53:18 -0700250 && alertManager.isAlerting(isolatedChild.key)) {
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700251 releasedChild = true;
Kevina97ea052018-09-11 13:53:18 -0700252 alertManager.removeNotification(isolatedChild.key, true /* releaseImmediately */);
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700253 }
Kevina97ea052018-09-11 13:53:18 -0700254 if (releasedChild && !alertManager.isAlerting(group.summary.key)) {
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700255 boolean notifyImmediately = (numChildren - numPendingChildren) > 1;
256 if (notifyImmediately) {
Kevina97ea052018-09-11 13:53:18 -0700257 alertManager.showNotification(group.summary);
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700258 } else {
Kevina97ea052018-09-11 13:53:18 -0700259 group.alertSummaryOnNextAddition = true;
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700260 }
Kevina97ea052018-09-11 13:53:18 -0700261 group.lastAlertTransfer = 0;
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700262 }
263 }
264 }
265
266 private int getPendingChildrenNotAlerting(NotificationGroup group) {
267 if (mPendingNotifications == null) {
268 return 0;
269 }
270 int number = 0;
271 String groupKey = getGroupKey(group.summary.notification);
272 Collection<NotificationData.Entry> values = mPendingNotifications.values();
273 for (NotificationData.Entry entry : values) {
274 if (!isGroupChild(entry.notification)) {
275 continue;
276 }
277 if (!Objects.equals(getGroupKey(entry.notification), groupKey)) {
278 continue;
279 }
280 if (group.children.containsKey(entry.key)) {
281 continue;
282 }
283 if (onlySummaryAlerts(entry)) {
284 number++;
285 }
286 }
287 return number;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100288 }
289
Selim Cinek50e74672016-05-05 15:58:10 -0400290 private void onEntryBecomingChild(NotificationData.Entry entry) {
Kevina97ea052018-09-11 13:53:18 -0700291 if (shouldIsolate(entry)) {
292 isolateNotification(entry);
Selim Cinek50e74672016-05-05 15:58:10 -0400293 }
294 }
295
Selim Cinek2a739342016-03-17 10:28:55 -0700296 private void updateSuppression(NotificationGroup group) {
Selim Cinek80c44dd2016-08-15 19:39:55 -0700297 if (group == null) {
Selim Cinek23c80342016-03-17 18:27:36 -0700298 return;
299 }
Selim Cinek2a739342016-03-17 10:28:55 -0700300 boolean prevSuppressed = group.suppressed;
Selim Cinek23c80342016-03-17 18:27:36 -0700301 group.suppressed = group.summary != null && !group.expanded
302 && (group.children.size() == 1
303 || (group.children.size() == 0
Julia Reynoldse46bb372016-03-17 11:05:58 -0400304 && group.summary.notification.getNotification().isGroupSummary()
Selim Cinek23c80342016-03-17 18:27:36 -0700305 && hasIsolatedChildren(group)));
Selim Cinek2a739342016-03-17 10:28:55 -0700306 if (prevSuppressed != group.suppressed) {
Selim Cinek967ed2a2016-04-08 18:29:11 -0700307 if (group.suppressed) {
Kevina97ea052018-09-11 13:53:18 -0700308 if (mHeadsUpManager.isAlerting(group.summary.key)) {
309 handleSuppressedSummaryAlerted(group.summary, mHeadsUpManager);
310 } else if (mAmbientPulseManager.isAlerting(group.summary.key)) {
311 handleSuppressedSummaryAlerted(group.summary, mAmbientPulseManager);
312 }
Selim Cinek967ed2a2016-04-08 18:29:11 -0700313 }
Rohan Shah524cf7b2018-03-15 14:40:02 -0700314 if (!mIsUpdatingUnchangedGroup && mListener != null) {
Selim Cinek80c44dd2016-08-15 19:39:55 -0700315 mListener.onGroupsChanged();
316 }
Selim Cinek2a739342016-03-17 10:28:55 -0700317 }
318 }
319
Selim Cinek23c80342016-03-17 18:27:36 -0700320 private boolean hasIsolatedChildren(NotificationGroup group) {
321 return getNumberOfIsolatedChildren(group.summary.notification.getGroupKey()) != 0;
322 }
323
324 private int getNumberOfIsolatedChildren(String groupKey) {
325 int count = 0;
Selim Cineka6c0cef2016-03-18 11:42:25 -0700326 for (StatusBarNotification sbn : mIsolatedEntries.values()) {
Selim Cinek23c80342016-03-17 18:27:36 -0700327 if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) {
328 count++;
329 }
330 }
331 return count;
332 }
333
Selim Cinek967ed2a2016-04-08 18:29:11 -0700334 private NotificationData.Entry getIsolatedChild(String groupKey) {
335 for (StatusBarNotification sbn : mIsolatedEntries.values()) {
336 if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) {
337 return mGroupMap.get(sbn.getKey()).summary;
338 }
339 }
340 return null;
341 }
342
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100343 public void onEntryUpdated(NotificationData.Entry entry,
344 StatusBarNotification oldNotification) {
Selim Cinek68bdff12016-08-03 13:38:56 -0700345 String oldKey = oldNotification.getGroupKey();
346 String newKey = entry.notification.getGroupKey();
347 boolean groupKeysChanged = !oldKey.equals(newKey);
348 boolean wasGroupChild = isGroupChild(oldNotification);
349 boolean isGroupChild = isGroupChild(entry.notification);
Selim Cinek80c44dd2016-08-15 19:39:55 -0700350 mIsUpdatingUnchangedGroup = !groupKeysChanged && wasGroupChild == isGroupChild;
Selim Cinekef5127e2015-12-21 16:55:58 -0800351 if (mGroupMap.get(getGroupKey(oldNotification)) != null) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100352 onEntryRemovedInternal(entry, oldNotification);
353 }
354 onEntryAdded(entry);
Selim Cinek80c44dd2016-08-15 19:39:55 -0700355 mIsUpdatingUnchangedGroup = false;
Selim Cineka6c0cef2016-03-18 11:42:25 -0700356 if (isIsolated(entry.notification)) {
357 mIsolatedEntries.put(entry.key, entry.notification);
Selim Cinek68bdff12016-08-03 13:38:56 -0700358 if (groupKeysChanged) {
Selim Cinek23c80342016-03-17 18:27:36 -0700359 updateSuppression(mGroupMap.get(oldKey));
360 updateSuppression(mGroupMap.get(newKey));
361 }
Selim Cinek68bdff12016-08-03 13:38:56 -0700362 } else if (!wasGroupChild && isGroupChild) {
Selim Cinek50e74672016-05-05 15:58:10 -0400363 onEntryBecomingChild(entry);
Selim Cinek23c80342016-03-17 18:27:36 -0700364 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100365 }
366
Selim Cinek2a739342016-03-17 10:28:55 -0700367 public boolean isSummaryOfSuppressedGroup(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700368 return isGroupSuppressed(getGroupKey(sbn)) && sbn.getNotification().isGroupSummary();
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100369 }
370
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700371 private boolean isOnlyChild(StatusBarNotification sbn) {
Selim Cinek36b02232016-05-11 23:07:05 -0400372 return !sbn.getNotification().isGroupSummary()
Selim Cinek23c80342016-03-17 18:27:36 -0700373 && getTotalNumberOfChildren(sbn) == 1;
Selim Cinek2a739342016-03-17 10:28:55 -0700374 }
375
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700376 public boolean isOnlyChildInGroup(StatusBarNotification sbn) {
Selim Cinek88086e72016-06-17 21:01:32 -0700377 if (!isOnlyChild(sbn)) {
378 return false;
379 }
380 ExpandableNotificationRow logicalGroupSummary = getLogicalGroupSummary(sbn);
381 return logicalGroupSummary != null
382 && !logicalGroupSummary.getStatusBarNotification().equals(sbn);
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700383 }
384
Selim Cinek23c80342016-03-17 18:27:36 -0700385 private int getTotalNumberOfChildren(StatusBarNotification sbn) {
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700386 int isolatedChildren = getNumberOfIsolatedChildren(sbn.getGroupKey());
387 NotificationGroup group = mGroupMap.get(sbn.getGroupKey());
388 int realChildren = group != null ? group.children.size() : 0;
389 return isolatedChildren + realChildren;
Selim Cinek23c80342016-03-17 18:27:36 -0700390 }
391
392 private boolean isGroupSuppressed(String groupKey) {
393 NotificationGroup group = mGroupMap.get(groupKey);
Selim Cinek2a739342016-03-17 10:28:55 -0700394 return group != null && group.suppressed;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100395 }
396
Jason Monk1fd3fc32018-08-14 17:20:09 -0400397 private void setStatusBarState(int newState) {
Selim Cinek9c4c4142015-12-04 16:44:56 -0800398 mBarState = newState;
399 if (mBarState == StatusBarState.KEYGUARD) {
Selim Cinek9184f9c2016-02-02 17:36:53 -0800400 collapseAllGroups();
401 }
402 }
403
404 public void collapseAllGroups() {
Selim Cinekc0b14b02016-05-09 13:12:40 -0400405 // Because notifications can become isolated when the group becomes suppressed it can
406 // lead to concurrent modifications while looping. We need to make a copy.
407 ArrayList<NotificationGroup> groupCopy = new ArrayList<>(mGroupMap.values());
408 int size = groupCopy.size();
409 for (int i = 0; i < size; i++) {
410 NotificationGroup group = groupCopy.get(i);
Selim Cinek9184f9c2016-02-02 17:36:53 -0800411 if (group.expanded) {
412 setGroupExpanded(group, false);
Selim Cinek9c4c4142015-12-04 16:44:56 -0800413 }
Selim Cinek2a739342016-03-17 10:28:55 -0700414 updateSuppression(group);
Selim Cinek9c4c4142015-12-04 16:44:56 -0800415 }
416 }
417
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100418 /**
419 * @return whether a given notification is a child in a group which has a summary
420 */
421 public boolean isChildInGroupWithSummary(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800422 if (!isGroupChild(sbn)) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100423 return false;
424 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800425 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek2a739342016-03-17 10:28:55 -0700426 if (group == null || group.summary == null || group.suppressed) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100427 return false;
428 }
Selim Cinek3f19f602016-05-02 18:01:56 -0700429 if (group.children.isEmpty()) {
430 // If the suppression of a group changes because the last child was removed, this can
431 // still be called temporarily because the child hasn't been fully removed yet. Let's
432 // make sure we still return false in that case.
433 return false;
434 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100435 return true;
436 }
437
Selim Cinek263398f2015-10-21 17:40:23 -0700438 /**
439 * @return whether a given notification is a summary in a group which has children
440 */
441 public boolean isSummaryOfGroup(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800442 if (!isGroupSummary(sbn)) {
Selim Cinek263398f2015-10-21 17:40:23 -0700443 return false;
444 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800445 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Kevin96eed642018-09-05 18:53:33 -0700446 if (group == null || group.summary == null) {
Selim Cinek263398f2015-10-21 17:40:23 -0700447 return false;
448 }
Kevin96eed642018-09-05 18:53:33 -0700449 return !group.children.isEmpty() && Objects.equals(group.summary.notification, sbn);
Selim Cinek263398f2015-10-21 17:40:23 -0700450 }
451
Selim Cinek23c80342016-03-17 18:27:36 -0700452 /**
453 * Get the summary of a specified status bar notification. For isolated notification this return
454 * itself.
455 */
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100456 public ExpandableNotificationRow getGroupSummary(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700457 return getGroupSummary(getGroupKey(sbn));
458 }
459
460 /**
461 * Similar to {@link #getGroupSummary(StatusBarNotification)} but doesn't get the visual summary
462 * but the logical summary, i.e when a child is isolated, it still returns the summary as if
463 * it wasn't isolated.
464 */
465 public ExpandableNotificationRow getLogicalGroupSummary(
466 StatusBarNotification sbn) {
467 return getGroupSummary(sbn.getGroupKey());
468 }
469
470 @Nullable
471 private ExpandableNotificationRow getGroupSummary(String groupKey) {
472 NotificationGroup group = mGroupMap.get(groupKey);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100473 return group == null ? null
474 : group.summary == null ? null
Selim Cinek23c80342016-03-17 18:27:36 -0700475 : group.summary.row;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100476 }
477
Chris Wren698b1702016-05-23 11:16:32 -0400478 /** @return group expansion state after toggling. */
479 public boolean toggleGroupExpansion(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800480 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek83bc7832015-10-22 13:26:54 -0700481 if (group == null) {
Chris Wren698b1702016-05-23 11:16:32 -0400482 return false;
Selim Cinek83bc7832015-10-22 13:26:54 -0700483 }
484 setGroupExpanded(group, !group.expanded);
Chris Wren698b1702016-05-23 11:16:32 -0400485 return group.expanded;
Selim Cinek83bc7832015-10-22 13:26:54 -0700486 }
487
Selim Cinekef5127e2015-12-21 16:55:58 -0800488 private boolean isIsolated(StatusBarNotification sbn) {
Selim Cineka6c0cef2016-03-18 11:42:25 -0700489 return mIsolatedEntries.containsKey(sbn.getKey());
Selim Cinekef5127e2015-12-21 16:55:58 -0800490 }
491
492 private boolean isGroupSummary(StatusBarNotification sbn) {
493 if (isIsolated(sbn)) {
494 return true;
495 }
496 return sbn.getNotification().isGroupSummary();
497 }
Julia Reynoldse46bb372016-03-17 11:05:58 -0400498
Selim Cinekef5127e2015-12-21 16:55:58 -0800499 private boolean isGroupChild(StatusBarNotification sbn) {
500 if (isIsolated(sbn)) {
501 return false;
502 }
Julia Reynoldse46bb372016-03-17 11:05:58 -0400503 return sbn.isGroup() && !sbn.getNotification().isGroupSummary();
Selim Cinekef5127e2015-12-21 16:55:58 -0800504 }
505
506 private String getGroupKey(StatusBarNotification sbn) {
507 if (isIsolated(sbn)) {
508 return sbn.getKey();
509 }
510 return sbn.getGroupKey();
511 }
512
513 @Override
514 public void onHeadsUpPinnedModeChanged(boolean inPinnedMode) {
515 }
516
517 @Override
518 public void onHeadsUpPinned(ExpandableNotificationRow headsUp) {
519 }
520
521 @Override
522 public void onHeadsUpUnPinned(ExpandableNotificationRow headsUp) {
523 }
524
525 @Override
Kevina97ea052018-09-11 13:53:18 -0700526 public void onAmbientStateChanged(NotificationData.Entry entry, boolean isAmbient) {
527 onAlertStateChanged(entry, isAmbient, mAmbientPulseManager);
528 }
529
530 @Override
Selim Cinekef5127e2015-12-21 16:55:58 -0800531 public void onHeadsUpStateChanged(NotificationData.Entry entry, boolean isHeadsUp) {
Kevina97ea052018-09-11 13:53:18 -0700532 onAlertStateChanged(entry, isHeadsUp, mHeadsUpManager);
533 }
534
535 private void onAlertStateChanged(NotificationData.Entry entry, boolean isAlerting,
536 AlertingNotificationManager alertManager) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800537 final StatusBarNotification sbn = entry.notification;
Kevina97ea052018-09-11 13:53:18 -0700538 if (isAlerting) {
539 if (shouldIsolate(entry)) {
540 isolateNotification(entry);
541 } else if (sbn.getNotification().isGroupSummary()
542 && isGroupSuppressed(sbn.getGroupKey())){
543 handleSuppressedSummaryAlerted(entry, alertManager);
Selim Cinekef5127e2015-12-21 16:55:58 -0800544 }
545 } else {
Kevina97ea052018-09-11 13:53:18 -0700546 stopIsolatingNotification(entry);
Selim Cinekef5127e2015-12-21 16:55:58 -0800547 }
548 }
549
Kevina97ea052018-09-11 13:53:18 -0700550 /**
551 * Handles the scenario where a summary that has been suppressed is alerted. A suppressed
552 * summary should for all intents and purposes be invisible to the user and as a result should
553 * not alert. When this is the case, it is our responsibility to pass the alert to the
554 * appropriate child which will be the representative notification alerting for the group.
555 * @param summary the summary that is suppressed and alerting
556 * @param alertManager the alert manager that manages the alerting summary
557 */
558 private void handleSuppressedSummaryAlerted(@NonNull NotificationData.Entry summary,
559 @NonNull AlertingNotificationManager alertManager) {
560 StatusBarNotification sbn = summary.notification;
Selim Cinek967ed2a2016-04-08 18:29:11 -0700561 if (!isGroupSuppressed(sbn.getGroupKey())
562 || !sbn.getNotification().isGroupSummary()
Kevina97ea052018-09-11 13:53:18 -0700563 || !alertManager.isAlerting(sbn.getKey())) {
Selim Cinek967ed2a2016-04-08 18:29:11 -0700564 return;
565 }
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700566
Kevina97ea052018-09-11 13:53:18 -0700567 // The parent of a suppressed group got alerted, lets alert the child!
Selim Cinek967ed2a2016-04-08 18:29:11 -0700568 NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey());
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700569
Selim Cinek967ed2a2016-04-08 18:29:11 -0700570 if (notificationGroup != null) {
Kevina97ea052018-09-11 13:53:18 -0700571 if (pendingInflationsWillAddChildren(notificationGroup)) {
572 // New children will actually be added to this group, let's not transfer the alert.
573 return;
574 }
575
Selim Cinek04be3892017-08-08 10:58:32 -0700576 Iterator<NotificationData.Entry> iterator
577 = notificationGroup.children.values().iterator();
Selim Cinek967ed2a2016-04-08 18:29:11 -0700578 NotificationData.Entry child = iterator.hasNext() ? iterator.next() : null;
579 if (child == null) {
580 child = getIsolatedChild(sbn.getGroupKey());
581 }
582 if (child != null) {
Selim Cinek40f88762016-12-16 16:56:41 -0800583 if (child.row.keepInParent() || child.row.isRemoved() || child.row.isDismissed()) {
Kevina97ea052018-09-11 13:53:18 -0700584 // the notification is actually already removed, no need to do alert on it.
Selim Cinek40f88762016-12-16 16:56:41 -0800585 return;
586 }
Kevina97ea052018-09-11 13:53:18 -0700587 transferAlertStateToChild(summary, child, alertManager);
Selim Cinek967ed2a2016-04-08 18:29:11 -0700588 }
589 }
Kevina97ea052018-09-11 13:53:18 -0700590 }
591
592 /**
593 * Transfers the alert state from a given summary notification to the specified child. The
594 * result is the child will now alert while the summary does not.
595 *
596 * @param summary the currently alerting summary notification
597 * @param child the child that should receive the alert
598 * @param alertManager the manager for the alert
599 */
600 private void transferAlertStateToChild(@NonNull NotificationData.Entry summary,
601 @NonNull NotificationData.Entry child,
602 @NonNull AlertingNotificationManager alertManager) {
603 NotificationGroup notificationGroup = mGroupMap.get(summary.notification.getGroupKey());
604 if (alertManager.isAlerting(child.key)) {
605 alertManager.updateNotification(child.key, true /* alert */);
606 } else {
607 if (onlySummaryAlerts(summary)) {
608 notificationGroup.lastAlertTransfer = SystemClock.elapsedRealtime();
609 }
610 alertManager.showNotification(child);
611 }
612 alertManager.removeNotification(summary.key, true /* releaseImmediately */);
Selim Cinek967ed2a2016-04-08 18:29:11 -0700613 }
614
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700615 private boolean onlySummaryAlerts(NotificationData.Entry entry) {
616 return entry.notification.getNotification().getGroupAlertBehavior()
617 == Notification.GROUP_ALERT_SUMMARY;
618 }
619
620 /**
621 * Check if the pending inflations will add children to this group.
622 * @param group The group to check.
623 */
624 private boolean pendingInflationsWillAddChildren(NotificationGroup group) {
625 if (mPendingNotifications == null) {
626 return false;
627 }
628 Collection<NotificationData.Entry> values = mPendingNotifications.values();
629 String groupKey = getGroupKey(group.summary.notification);
630 for (NotificationData.Entry entry : values) {
631 if (!isGroupChild(entry.notification)) {
632 continue;
633 }
634 if (!Objects.equals(getGroupKey(entry.notification), groupKey)) {
635 continue;
636 }
637 if (!group.children.containsKey(entry.key)) {
638 return true;
639 }
640 }
641 return false;
642 }
643
Kevina97ea052018-09-11 13:53:18 -0700644 /**
645 * Whether a notification that is normally part of a group should be temporarily isolated from
646 * the group and put in their own group visually. This generally happens when the notification
647 * is alerting.
648 *
649 * @param entry the notification to check
650 * @return true if the entry should be isolated
651 */
652
653 private boolean shouldIsolate(NotificationData.Entry entry) {
654 StatusBarNotification sbn = entry.notification;
Selim Cineka6c0cef2016-03-18 11:42:25 -0700655 NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey());
Kevina97ea052018-09-11 13:53:18 -0700656 if (!sbn.isGroup() || sbn.getNotification().isGroupSummary()) {
657 return false;
658 }
659 if (!mIsDozing && !mHeadsUpManager.isAlerting(entry.key)) {
660 return false;
661 }
662 if (mIsDozing && !mAmbientPulseManager.isAlerting(entry.key)) {
663 return false;
664 }
665 return (sbn.getNotification().fullScreenIntent != null
666 || notificationGroup == null
667 || !notificationGroup.expanded
668 || isGroupNotFullyVisible(notificationGroup));
669 }
670
671 /**
672 * Isolate a notification from its group so that it visually shows as its own group.
673 *
674 * @param entry the notification to isolate
675 */
676 private void isolateNotification(NotificationData.Entry entry) {
677 StatusBarNotification sbn = entry.notification;
678
679 // We will be isolated now, so lets update the groups
680 onEntryRemovedInternal(entry, entry.notification);
681
682 mIsolatedEntries.put(sbn.getKey(), sbn);
683
684 onEntryAdded(entry);
685 // We also need to update the suppression of the old group, because this call comes
686 // even before the groupManager knows about the notification at all.
687 // When the notification gets added afterwards it is already isolated and therefore
688 // it doesn't lead to an update.
689 updateSuppression(mGroupMap.get(entry.notification.getGroupKey()));
690 mListener.onGroupsChanged();
691 }
692
693 /**
694 * Stop isolating a notification and re-group it with its original logical group.
695 *
696 * @param entry the notification to un-isolate
697 */
698 private void stopIsolatingNotification(NotificationData.Entry entry) {
699 StatusBarNotification sbn = entry.notification;
700 if (mIsolatedEntries.containsKey(sbn.getKey())) {
701 // not isolated anymore, we need to update the groups
702 onEntryRemovedInternal(entry, entry.notification);
703 mIsolatedEntries.remove(sbn.getKey());
704 onEntryAdded(entry);
705 mListener.onGroupsChanged();
706 }
Selim Cineka6c0cef2016-03-18 11:42:25 -0700707 }
708
709 private boolean isGroupNotFullyVisible(NotificationGroup notificationGroup) {
710 return notificationGroup.summary == null
Selim Cineka6c0cef2016-03-18 11:42:25 -0700711 || notificationGroup.summary.row.getClipTopAmount() > 0
712 || notificationGroup.summary.row.getTranslationY() < 0;
713 }
714
Selim Cinek967ed2a2016-04-08 18:29:11 -0700715 public void setHeadsUpManager(HeadsUpManager headsUpManager) {
716 mHeadsUpManager = headsUpManager;
717 }
718
Selim Cinek52941c52016-05-07 18:29:32 -0400719 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
720 pw.println("GroupManager state:");
721 pw.println(" number of groups: " + mGroupMap.size());
722 for (Map.Entry<String, NotificationGroup> entry : mGroupMap.entrySet()) {
723 pw.println("\n key: " + entry.getKey()); pw.println(entry.getValue());
724 }
725 pw.println("\n isolated entries: " + mIsolatedEntries.size());
726 for (Map.Entry<String, StatusBarNotification> entry : mIsolatedEntries.entrySet()) {
727 pw.print(" "); pw.print(entry.getKey());
728 pw.print(", "); pw.println(entry.getValue());
729 }
730 }
731
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700732 public void setPendingEntries(HashMap<String, NotificationData.Entry> pendingNotifications) {
733 mPendingNotifications = pendingNotifications;
734 }
735
Evan Laird878c8532018-10-15 15:54:29 -0400736 @Override
737 public void onStateChanged(int newState) {
738 setStatusBarState(newState);
739 }
740
741 @Override
742 public void onDozingChanged(boolean isDozing) {
743 setDozing(isDozing);
744 }
745
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100746 public static class NotificationGroup {
Selim Cinek04be3892017-08-08 10:58:32 -0700747 public final HashMap<String, NotificationData.Entry> children = new HashMap<>();
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100748 public NotificationData.Entry summary;
749 public boolean expanded;
Selim Cinek2a739342016-03-17 10:28:55 -0700750 /**
751 * Is this notification group suppressed, i.e its summary is hidden
752 */
753 public boolean suppressed;
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700754 /**
Kevina97ea052018-09-11 13:53:18 -0700755 * The time when the last alert transfer from group to child happened, while the summary
756 * has the flags to alert up on its own.
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700757 */
Kevina97ea052018-09-11 13:53:18 -0700758 public long lastAlertTransfer;
759 public boolean alertSummaryOnNextAddition;
Selim Cinek52941c52016-05-07 18:29:32 -0400760
761 @Override
762 public String toString() {
Selim Cinek60ca7872016-05-12 11:24:07 -0700763 String result = " summary:\n "
Selim Cinek04be3892017-08-08 10:58:32 -0700764 + (summary != null ? summary.notification : "null")
765 + (summary != null && summary.getDebugThrowable() != null
766 ? Log.getStackTraceString(summary.getDebugThrowable())
767 : "");
Selim Cinek52941c52016-05-07 18:29:32 -0400768 result += "\n children size: " + children.size();
Selim Cinek04be3892017-08-08 10:58:32 -0700769 for (NotificationData.Entry child : children.values()) {
770 result += "\n " + child.notification
771 + (child.getDebugThrowable() != null
772 ? Log.getStackTraceString(child.getDebugThrowable())
773 : "");
Selim Cinek52941c52016-05-07 18:29:32 -0400774 }
775 return result;
776 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100777 }
778
779 public interface OnGroupChangeListener {
780 /**
781 * The expansion of a group has changed.
782 *
783 * @param changedRow the row for which the expansion has changed, which is also the summary
784 * @param expanded a boolean indicating the new expanded state
785 */
786 void onGroupExpansionChanged(ExpandableNotificationRow changedRow, boolean expanded);
787
788 /**
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100789 * A group of children just received a summary notification and should therefore become
790 * children of it.
791 *
792 * @param group the group created
793 */
794 void onGroupCreatedFromChildren(NotificationGroup group);
Selim Cinekef5127e2015-12-21 16:55:58 -0800795
796 /**
Selim Cinek2a739342016-03-17 10:28:55 -0700797 * The groups have changed. This can happen if the isolation of a child has changes or if a
798 * group became suppressed / unsuppressed
Selim Cinekef5127e2015-12-21 16:55:58 -0800799 */
Selim Cinek2a739342016-03-17 10:28:55 -0700800 void onGroupsChanged();
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100801 }
802}