2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 package com.android.systemui.statusbar.phone;
19 import android.app.Notification;
20 import android.os.SystemClock;
21 import android.service.notification.StatusBarNotification;
22 import androidx.annotation.Nullable;
23 import android.util.Log;
25 import com.android.systemui.statusbar.ExpandableNotificationRow;
26 import com.android.systemui.statusbar.NotificationData;
27 import com.android.systemui.statusbar.StatusBarState;
28 import com.android.systemui.statusbar.policy.HeadsUpManager;
29 import com.android.systemui.statusbar.policy.OnHeadsUpChangedListener;
31 import java.io.FileDescriptor;
32 import java.io.PrintWriter;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.HashMap;
36 import java.util.Iterator;
38 import java.util.Objects;
41 * A class to handle notifications and their corresponding groups.
43 public class NotificationGroupManager implements OnHeadsUpChangedListener {
45 private static final String TAG = "NotificationGroupManager";
46 private static final long HEADS_UP_TRANSFER_TIMEOUT = 300;
47 private final HashMap<String, NotificationGroup> mGroupMap = new HashMap<>();
48 private OnGroupChangeListener mListener;
49 private int mBarState = -1;
50 private HashMap<String, StatusBarNotification> mIsolatedEntries = new HashMap<>();
51 private HeadsUpManager mHeadsUpManager;
52 private boolean mIsUpdatingUnchangedGroup;
53 private HashMap<String, NotificationData.Entry> mPendingNotifications;
55 public void setOnGroupChangeListener(OnGroupChangeListener listener) {
59 public boolean isGroupExpanded(StatusBarNotification sbn) {
60 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
64 return group.expanded;
67 public void setGroupExpanded(StatusBarNotification sbn, boolean expanded) {
68 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
72 setGroupExpanded(group, expanded);
75 private void setGroupExpanded(NotificationGroup group, boolean expanded) {
76 group.expanded = expanded;
77 if (group.summary != null) {
78 mListener.onGroupExpansionChanged(group.summary.row, expanded);
82 public void onEntryRemoved(NotificationData.Entry removed) {
83 onEntryRemovedInternal(removed, removed.notification);
84 mIsolatedEntries.remove(removed.key);
88 * An entry was removed.
90 * @param removed the removed entry
91 * @param sbn the notification the entry has, which doesn't need to be the same as it's internal
94 private void onEntryRemovedInternal(NotificationData.Entry removed,
95 final StatusBarNotification sbn) {
96 String groupKey = getGroupKey(sbn);
97 final NotificationGroup group = mGroupMap.get(groupKey);
99 // When an app posts 2 different notifications as summary of the same group, then a
100 // cancellation of the first notification removes this group.
101 // This situation is not supported and we will not allow such notifications anymore in
102 // the close future. See b/23676310 for reference.
105 if (isGroupChild(sbn)) {
106 group.children.remove(removed.key);
108 group.summary = null;
110 updateSuppression(group);
111 if (group.children.isEmpty()) {
112 if (group.summary == null) {
113 mGroupMap.remove(groupKey);
118 public void onEntryAdded(final NotificationData.Entry added) {
119 if (added.row.isRemoved()) {
120 added.setDebugThrowable(new Throwable());
122 final StatusBarNotification sbn = added.notification;
123 boolean isGroupChild = isGroupChild(sbn);
124 String groupKey = getGroupKey(sbn);
125 NotificationGroup group = mGroupMap.get(groupKey);
127 group = new NotificationGroup();
128 mGroupMap.put(groupKey, group);
131 NotificationData.Entry existing = group.children.get(added.key);
132 if (existing != null && existing != added) {
133 Throwable existingThrowable = existing.getDebugThrowable();
134 Log.wtf(TAG, "Inconsistent entries found with the same key " + added.key
135 + "existing removed: " + existing.row.isRemoved()
136 + (existingThrowable != null
137 ? Log.getStackTraceString(existingThrowable) + "\n": "")
138 + " added removed" + added.row.isRemoved()
141 group.children.put(added.key, added);
142 updateSuppression(group);
144 group.summary = added;
145 group.expanded = added.row.areChildrenExpanded();
146 updateSuppression(group);
147 if (!group.children.isEmpty()) {
148 ArrayList<NotificationData.Entry> childrenCopy
149 = new ArrayList<>(group.children.values());
150 for (NotificationData.Entry child : childrenCopy) {
151 onEntryBecomingChild(child);
153 mListener.onGroupCreatedFromChildren(group);
156 cleanUpHeadsUpStatesOnAdd(group, false /* addIsPending */);
159 public void onPendingEntryAdded(NotificationData.Entry shadeEntry) {
160 String groupKey = getGroupKey(shadeEntry.notification);
161 NotificationGroup group = mGroupMap.get(groupKey);
163 cleanUpHeadsUpStatesOnAdd(group, true /* addIsPending */);
168 * Clean up the heads up states when a new child was added.
169 * @param group The group where a view was added or will be added.
170 * @param addIsPending True if is the addition still pending or false has it already been added.
172 private void cleanUpHeadsUpStatesOnAdd(NotificationGroup group, boolean addIsPending) {
173 if (!addIsPending && group.hunSummaryOnNextAddition) {
174 if (!mHeadsUpManager.isHeadsUp(group.summary.key)) {
175 mHeadsUpManager.showNotification(group.summary);
177 group.hunSummaryOnNextAddition = false;
179 // Because notification groups are not delivered as a whole unit, it may happen that a
180 // group child gets added quite a bit after the summary got posted. Our guidance is, that
181 // apps should always post the group summary as well and we'll hide it for them if the child
182 // is the only child in a group. Because of this, we also have to transfer heads up to the
183 // child, otherwise the invisible summary would be heads-upped.
184 // This transfer to the child is not always correct in case the app has just posted another
185 // child in addition to the existing one, but it hasn't arrived in systemUI yet. In such
186 // a scenario we would transfer the heads up to the old child and the wrong notification
187 // would be heads-upped. In oder to avoid this, we'll recover from this issue and hun the
188 // summary again instead of the old child if it's within a certain timeout.
189 if (SystemClock.elapsedRealtime() - group.lastHeadsUpTransfer < HEADS_UP_TRANSFER_TIMEOUT) {
190 if (!onlySummaryAlerts(group.summary)) {
193 int numChildren = group.children.size();
194 NotificationData.Entry isolatedChild = getIsolatedChild(getGroupKey(
195 group.summary.notification));
196 int numPendingChildren = getPendingChildrenNotAlerting(group);
197 numChildren += numPendingChildren;
198 if (isolatedChild != null) {
201 if (numChildren <= 1) {
204 boolean releasedChild = false;
205 ArrayList<NotificationData.Entry> children = new ArrayList<>(group.children.values());
206 int size = children.size();
207 for (int i = 0; i < size; i++) {
208 NotificationData.Entry entry = children.get(i);
209 if (onlySummaryAlerts(entry) && entry.row.isHeadsUp()) {
210 releasedChild = true;
211 mHeadsUpManager.releaseImmediately(entry.key);
214 if (isolatedChild != null && onlySummaryAlerts(isolatedChild)
215 && isolatedChild.row.isHeadsUp()) {
216 releasedChild = true;
217 mHeadsUpManager.releaseImmediately(isolatedChild.key);
219 if (releasedChild && !mHeadsUpManager.isHeadsUp(group.summary.key)) {
220 boolean notifyImmediately = (numChildren - numPendingChildren) > 1;
221 if (notifyImmediately) {
222 mHeadsUpManager.showNotification(group.summary);
224 group.hunSummaryOnNextAddition = true;
226 group.lastHeadsUpTransfer = 0;
231 private int getPendingChildrenNotAlerting(NotificationGroup group) {
232 if (mPendingNotifications == null) {
236 String groupKey = getGroupKey(group.summary.notification);
237 Collection<NotificationData.Entry> values = mPendingNotifications.values();
238 for (NotificationData.Entry entry : values) {
239 if (!isGroupChild(entry.notification)) {
242 if (!Objects.equals(getGroupKey(entry.notification), groupKey)) {
245 if (group.children.containsKey(entry.key)) {
248 if (onlySummaryAlerts(entry)) {
255 private void onEntryBecomingChild(NotificationData.Entry entry) {
256 if (entry.row.isHeadsUp()) {
257 onHeadsUpStateChanged(entry, true);
261 private void updateSuppression(NotificationGroup group) {
265 boolean prevSuppressed = group.suppressed;
266 group.suppressed = group.summary != null && !group.expanded
267 && (group.children.size() == 1
268 || (group.children.size() == 0
269 && group.summary.notification.getNotification().isGroupSummary()
270 && hasIsolatedChildren(group)));
271 if (prevSuppressed != group.suppressed) {
272 if (group.suppressed) {
273 handleSuppressedSummaryHeadsUpped(group.summary);
275 if (!mIsUpdatingUnchangedGroup && mListener != null) {
276 mListener.onGroupsChanged();
281 private boolean hasIsolatedChildren(NotificationGroup group) {
282 return getNumberOfIsolatedChildren(group.summary.notification.getGroupKey()) != 0;
285 private int getNumberOfIsolatedChildren(String groupKey) {
287 for (StatusBarNotification sbn : mIsolatedEntries.values()) {
288 if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) {
295 private NotificationData.Entry getIsolatedChild(String groupKey) {
296 for (StatusBarNotification sbn : mIsolatedEntries.values()) {
297 if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) {
298 return mGroupMap.get(sbn.getKey()).summary;
304 public void onEntryUpdated(NotificationData.Entry entry,
305 StatusBarNotification oldNotification) {
306 String oldKey = oldNotification.getGroupKey();
307 String newKey = entry.notification.getGroupKey();
308 boolean groupKeysChanged = !oldKey.equals(newKey);
309 boolean wasGroupChild = isGroupChild(oldNotification);
310 boolean isGroupChild = isGroupChild(entry.notification);
311 mIsUpdatingUnchangedGroup = !groupKeysChanged && wasGroupChild == isGroupChild;
312 if (mGroupMap.get(getGroupKey(oldNotification)) != null) {
313 onEntryRemovedInternal(entry, oldNotification);
316 mIsUpdatingUnchangedGroup = false;
317 if (isIsolated(entry.notification)) {
318 mIsolatedEntries.put(entry.key, entry.notification);
319 if (groupKeysChanged) {
320 updateSuppression(mGroupMap.get(oldKey));
321 updateSuppression(mGroupMap.get(newKey));
323 } else if (!wasGroupChild && isGroupChild) {
324 onEntryBecomingChild(entry);
328 public boolean isSummaryOfSuppressedGroup(StatusBarNotification sbn) {
329 return isGroupSuppressed(getGroupKey(sbn)) && sbn.getNotification().isGroupSummary();
332 private boolean isOnlyChild(StatusBarNotification sbn) {
333 return !sbn.getNotification().isGroupSummary()
334 && getTotalNumberOfChildren(sbn) == 1;
337 public boolean isOnlyChildInGroup(StatusBarNotification sbn) {
338 if (!isOnlyChild(sbn)) {
341 ExpandableNotificationRow logicalGroupSummary = getLogicalGroupSummary(sbn);
342 return logicalGroupSummary != null
343 && !logicalGroupSummary.getStatusBarNotification().equals(sbn);
346 private int getTotalNumberOfChildren(StatusBarNotification sbn) {
347 int isolatedChildren = getNumberOfIsolatedChildren(sbn.getGroupKey());
348 NotificationGroup group = mGroupMap.get(sbn.getGroupKey());
349 int realChildren = group != null ? group.children.size() : 0;
350 return isolatedChildren + realChildren;
353 private boolean isGroupSuppressed(String groupKey) {
354 NotificationGroup group = mGroupMap.get(groupKey);
355 return group != null && group.suppressed;
358 public void setStatusBarState(int newState) {
359 if (mBarState == newState) {
362 mBarState = newState;
363 if (mBarState == StatusBarState.KEYGUARD) {
368 public void collapseAllGroups() {
369 // Because notifications can become isolated when the group becomes suppressed it can
370 // lead to concurrent modifications while looping. We need to make a copy.
371 ArrayList<NotificationGroup> groupCopy = new ArrayList<>(mGroupMap.values());
372 int size = groupCopy.size();
373 for (int i = 0; i < size; i++) {
374 NotificationGroup group = groupCopy.get(i);
375 if (group.expanded) {
376 setGroupExpanded(group, false);
378 updateSuppression(group);
383 * @return whether a given notification is a child in a group which has a summary
385 public boolean isChildInGroupWithSummary(StatusBarNotification sbn) {
386 if (!isGroupChild(sbn)) {
389 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
390 if (group == null || group.summary == null || group.suppressed) {
393 if (group.children.isEmpty()) {
394 // If the suppression of a group changes because the last child was removed, this can
395 // still be called temporarily because the child hasn't been fully removed yet. Let's
396 // make sure we still return false in that case.
403 * @return whether a given notification is a summary in a group which has children
405 public boolean isSummaryOfGroup(StatusBarNotification sbn) {
406 if (!isGroupSummary(sbn)) {
409 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
413 return !group.children.isEmpty();
417 * Get the summary of a specified status bar notification. For isolated notification this return
420 public ExpandableNotificationRow getGroupSummary(StatusBarNotification sbn) {
421 return getGroupSummary(getGroupKey(sbn));
425 * Similar to {@link #getGroupSummary(StatusBarNotification)} but doesn't get the visual summary
426 * but the logical summary, i.e when a child is isolated, it still returns the summary as if
427 * it wasn't isolated.
429 public ExpandableNotificationRow getLogicalGroupSummary(
430 StatusBarNotification sbn) {
431 return getGroupSummary(sbn.getGroupKey());
435 private ExpandableNotificationRow getGroupSummary(String groupKey) {
436 NotificationGroup group = mGroupMap.get(groupKey);
437 return group == null ? null
438 : group.summary == null ? null
442 /** @return group expansion state after toggling. */
443 public boolean toggleGroupExpansion(StatusBarNotification sbn) {
444 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
448 setGroupExpanded(group, !group.expanded);
449 return group.expanded;
452 private boolean isIsolated(StatusBarNotification sbn) {
453 return mIsolatedEntries.containsKey(sbn.getKey());
456 private boolean isGroupSummary(StatusBarNotification sbn) {
457 if (isIsolated(sbn)) {
460 return sbn.getNotification().isGroupSummary();
463 private boolean isGroupChild(StatusBarNotification sbn) {
464 if (isIsolated(sbn)) {
467 return sbn.isGroup() && !sbn.getNotification().isGroupSummary();
470 private String getGroupKey(StatusBarNotification sbn) {
471 if (isIsolated(sbn)) {
474 return sbn.getGroupKey();
478 public void onHeadsUpPinnedModeChanged(boolean inPinnedMode) {
482 public void onHeadsUpPinned(ExpandableNotificationRow headsUp) {
486 public void onHeadsUpUnPinned(ExpandableNotificationRow headsUp) {
490 public void onHeadsUpStateChanged(NotificationData.Entry entry, boolean isHeadsUp) {
491 final StatusBarNotification sbn = entry.notification;
492 if (entry.row.isHeadsUp()) {
493 if (shouldIsolate(sbn)) {
494 // We will be isolated now, so lets update the groups
495 onEntryRemovedInternal(entry, entry.notification);
497 mIsolatedEntries.put(sbn.getKey(), sbn);
500 // We also need to update the suppression of the old group, because this call comes
501 // even before the groupManager knows about the notification at all.
502 // When the notification gets added afterwards it is already isolated and therefore
503 // it doesn't lead to an update.
504 updateSuppression(mGroupMap.get(entry.notification.getGroupKey()));
505 mListener.onGroupsChanged();
507 handleSuppressedSummaryHeadsUpped(entry);
510 if (mIsolatedEntries.containsKey(sbn.getKey())) {
511 // not isolated anymore, we need to update the groups
512 onEntryRemovedInternal(entry, entry.notification);
513 mIsolatedEntries.remove(sbn.getKey());
515 mListener.onGroupsChanged();
520 private void handleSuppressedSummaryHeadsUpped(NotificationData.Entry entry) {
521 StatusBarNotification sbn = entry.notification;
522 if (!isGroupSuppressed(sbn.getGroupKey())
523 || !sbn.getNotification().isGroupSummary()
524 || !entry.row.isHeadsUp()) {
528 // The parent of a suppressed group got huned, lets hun the child!
529 NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey());
531 if (pendingInflationsWillAddChildren(notificationGroup)) {
532 // New children will actually be added to this group, let's not transfer the heads
537 if (notificationGroup != null) {
538 Iterator<NotificationData.Entry> iterator
539 = notificationGroup.children.values().iterator();
540 NotificationData.Entry child = iterator.hasNext() ? iterator.next() : null;
542 child = getIsolatedChild(sbn.getGroupKey());
545 if (child.row.keepInParent() || child.row.isRemoved() || child.row.isDismissed()) {
546 // the notification is actually already removed, no need to do heads-up on it.
549 if (mHeadsUpManager.isHeadsUp(child.key)) {
550 mHeadsUpManager.updateNotification(child, true);
552 if (onlySummaryAlerts(entry)) {
553 notificationGroup.lastHeadsUpTransfer = SystemClock.elapsedRealtime();
555 mHeadsUpManager.showNotification(child);
559 mHeadsUpManager.releaseImmediately(entry.key);
562 private boolean onlySummaryAlerts(NotificationData.Entry entry) {
563 return entry.notification.getNotification().getGroupAlertBehavior()
564 == Notification.GROUP_ALERT_SUMMARY;
568 * Check if the pending inflations will add children to this group.
569 * @param group The group to check.
571 private boolean pendingInflationsWillAddChildren(NotificationGroup group) {
572 if (mPendingNotifications == null) {
575 Collection<NotificationData.Entry> values = mPendingNotifications.values();
576 String groupKey = getGroupKey(group.summary.notification);
577 for (NotificationData.Entry entry : values) {
578 if (!isGroupChild(entry.notification)) {
581 if (!Objects.equals(getGroupKey(entry.notification), groupKey)) {
584 if (!group.children.containsKey(entry.key)) {
591 private boolean shouldIsolate(StatusBarNotification sbn) {
592 NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey());
593 return (sbn.isGroup() && !sbn.getNotification().isGroupSummary())
594 && (sbn.getNotification().fullScreenIntent != null
595 || notificationGroup == null
596 || !notificationGroup.expanded
597 || isGroupNotFullyVisible(notificationGroup));
600 private boolean isGroupNotFullyVisible(NotificationGroup notificationGroup) {
601 return notificationGroup.summary == null
602 || notificationGroup.summary.row.getClipTopAmount() > 0
603 || notificationGroup.summary.row.getTranslationY() < 0;
606 public void setHeadsUpManager(HeadsUpManager headsUpManager) {
607 mHeadsUpManager = headsUpManager;
610 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
611 pw.println("GroupManager state:");
612 pw.println(" number of groups: " + mGroupMap.size());
613 for (Map.Entry<String, NotificationGroup> entry : mGroupMap.entrySet()) {
614 pw.println("\n key: " + entry.getKey()); pw.println(entry.getValue());
616 pw.println("\n isolated entries: " + mIsolatedEntries.size());
617 for (Map.Entry<String, StatusBarNotification> entry : mIsolatedEntries.entrySet()) {
618 pw.print(" "); pw.print(entry.getKey());
619 pw.print(", "); pw.println(entry.getValue());
623 public void setPendingEntries(HashMap<String, NotificationData.Entry> pendingNotifications) {
624 mPendingNotifications = pendingNotifications;
627 public static class NotificationGroup {
628 public final HashMap<String, NotificationData.Entry> children = new HashMap<>();
629 public NotificationData.Entry summary;
630 public boolean expanded;
632 * Is this notification group suppressed, i.e its summary is hidden
634 public boolean suppressed;
636 * The time when the last heads transfer from group to child happened, while the summary
637 * has the flags to heads up on its own.
639 public long lastHeadsUpTransfer;
640 public boolean hunSummaryOnNextAddition;
643 public String toString() {
644 String result = " summary:\n "
645 + (summary != null ? summary.notification : "null")
646 + (summary != null && summary.getDebugThrowable() != null
647 ? Log.getStackTraceString(summary.getDebugThrowable())
649 result += "\n children size: " + children.size();
650 for (NotificationData.Entry child : children.values()) {
651 result += "\n " + child.notification
652 + (child.getDebugThrowable() != null
653 ? Log.getStackTraceString(child.getDebugThrowable())
660 public interface OnGroupChangeListener {
662 * The expansion of a group has changed.
664 * @param changedRow the row for which the expansion has changed, which is also the summary
665 * @param expanded a boolean indicating the new expanded state
667 void onGroupExpansionChanged(ExpandableNotificationRow changedRow, boolean expanded);
670 * A group of children just received a summary notification and should therefore become
673 * @param group the group created
675 void onGroupCreatedFromChildren(NotificationGroup group);
678 * The groups have changed. This can happen if the isolation of a child has changes or if a
679 * group became suppressed / unsuppressed
681 void onGroupsChanged();