blob: 84dd48b6eb6b34c41d4888ec12fd739387cb529a [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
Kevin01a53cb2018-11-09 18:19:54 -080019import android.annotation.Nullable;
Selim Cinekba069ae2020-04-01 19:45:16 -070020import android.app.NotificationChannel;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010021import android.service.notification.StatusBarNotification;
Kevin01a53cb2018-11-09 18:19:54 -080022import android.util.ArraySet;
Selim Cinek04be3892017-08-08 10:58:32 -070023import android.util.Log;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010024
Jason Monk1fd3fc32018-08-14 17:20:09 -040025import com.android.systemui.Dependency;
Mady Mellor740d85d2019-07-09 15:26:47 -070026import com.android.systemui.bubbles.BubbleController;
Beverly8fdb5332019-02-04 14:29:49 -050027import com.android.systemui.plugins.statusbar.StatusBarStateController;
28import com.android.systemui.plugins.statusbar.StatusBarStateController.StateListener;
Jason Monk1fd3fc32018-08-14 17:20:09 -040029import com.android.systemui.statusbar.StatusBarState;
Ned Burnsf81c4c42019-01-07 14:10:43 -050030import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Evan Laird878c8532018-10-15 15:54:29 -040031import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
Selim Cinekef5127e2015-12-21 16:55:58 -080032import com.android.systemui.statusbar.policy.HeadsUpManager;
Selim Cineka7d4f822016-12-06 14:34:47 -080033import com.android.systemui.statusbar.policy.OnHeadsUpChangedListener;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010034
Selim Cinek52941c52016-05-07 18:29:32 -040035import java.io.FileDescriptor;
36import java.io.PrintWriter;
Selim Cinekc0b14b02016-05-09 13:12:40 -040037import java.util.ArrayList;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010038import java.util.HashMap;
Selim Cinek52941c52016-05-07 18:29:32 -040039import java.util.Map;
Selim Cinekf93bf3e2018-05-08 14:43:21 -070040import java.util.Objects;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010041
Jason Monk27d01a622018-12-10 15:57:09 -050042import javax.inject.Inject;
43import javax.inject.Singleton;
44
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010045/**
46 * A class to handle notifications and their corresponding groups.
47 */
Jason Monk27d01a622018-12-10 15:57:09 -050048@Singleton
Selim Cinekc3fec682019-06-06 18:11:07 -070049public class NotificationGroupManager implements OnHeadsUpChangedListener, StateListener {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010050
Selim Cinek04be3892017-08-08 10:58:32 -070051 private static final String TAG = "NotificationGroupManager";
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010052 private final HashMap<String, NotificationGroup> mGroupMap = new HashMap<>();
Kevin01a53cb2018-11-09 18:19:54 -080053 private final ArraySet<OnGroupChangeListener> mListeners = new ArraySet<>();
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010054 private int mBarState = -1;
Selim Cineka6c0cef2016-03-18 11:42:25 -070055 private HashMap<String, StatusBarNotification> mIsolatedEntries = new HashMap<>();
Selim Cinek967ed2a2016-04-08 18:29:11 -070056 private HeadsUpManager mHeadsUpManager;
Selim Cinek80c44dd2016-08-15 19:39:55 -070057 private boolean mIsUpdatingUnchangedGroup;
Mady Mellor740d85d2019-07-09 15:26:47 -070058 @Nullable private BubbleController mBubbleController = null;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010059
Jason Monk27d01a622018-12-10 15:57:09 -050060 @Inject
Selim Cinekc3fec682019-06-06 18:11:07 -070061 public NotificationGroupManager(StatusBarStateController statusBarStateController) {
62 statusBarStateController.addCallback(this);
Jason Monk1fd3fc32018-08-14 17:20:09 -040063 }
64
Mady Mellor740d85d2019-07-09 15:26:47 -070065 private BubbleController getBubbleController() {
66 if (mBubbleController == null) {
67 mBubbleController = Dependency.get(BubbleController.class);
68 }
69 return mBubbleController;
70 }
71
Kevin01a53cb2018-11-09 18:19:54 -080072 /**
73 * Add a listener for changes to groups.
74 *
75 * @param listener listener to add
76 */
77 public void addOnGroupChangeListener(OnGroupChangeListener listener) {
78 mListeners.add(listener);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010079 }
80
81 public boolean isGroupExpanded(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -080082 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010083 if (group == null) {
84 return false;
85 }
86 return group.expanded;
87 }
88
Selim Cinekba069ae2020-04-01 19:45:16 -070089 /**
90 * @return if the group that this notification is associated with logically is expanded
91 */
92 public boolean isLogicalGroupExpanded(StatusBarNotification sbn) {
93 NotificationGroup group = mGroupMap.get(sbn.getGroupKey());
94 if (group == null) {
95 return false;
96 }
97 return group.expanded;
98 }
99
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100100 public void setGroupExpanded(StatusBarNotification sbn, boolean expanded) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800101 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100102 if (group == null) {
103 return;
104 }
105 setGroupExpanded(group, expanded);
106 }
107
108 private void setGroupExpanded(NotificationGroup group, boolean expanded) {
109 group.expanded = expanded;
110 if (group.summary != null) {
Kevin01a53cb2018-11-09 18:19:54 -0800111 for (OnGroupChangeListener listener : mListeners) {
Evan Laird94492852018-10-25 13:43:01 -0400112 listener.onGroupExpansionChanged(group.summary.getRow(), expanded);
Kevin01a53cb2018-11-09 18:19:54 -0800113 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100114 }
115 }
116
Ned Burnsf81c4c42019-01-07 14:10:43 -0500117 public void onEntryRemoved(NotificationEntry removed) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400118 onEntryRemovedInternal(removed, removed.getSbn());
119 mIsolatedEntries.remove(removed.getKey());
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100120 }
121
122 /**
123 * An entry was removed.
124 *
125 * @param removed the removed entry
126 * @param sbn the notification the entry has, which doesn't need to be the same as it's internal
127 * notification
128 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500129 private void onEntryRemovedInternal(NotificationEntry removed,
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100130 final StatusBarNotification sbn) {
Beverlyc5094122020-01-09 17:05:15 -0500131 onEntryRemovedInternal(removed, sbn.getGroupKey(), sbn.isGroup(),
132 sbn.getNotification().isGroupSummary());
133 }
134
135 private void onEntryRemovedInternal(NotificationEntry removed, String notifGroupKey, boolean
136 isGroup, boolean isGroupSummary) {
137 String groupKey = getGroupKey(removed.getKey(), notifGroupKey);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100138 final NotificationGroup group = mGroupMap.get(groupKey);
Selim Cinek0b4aeab2015-09-01 17:07:38 -0700139 if (group == null) {
140 // When an app posts 2 different notifications as summary of the same group, then a
141 // cancellation of the first notification removes this group.
142 // This situation is not supported and we will not allow such notifications anymore in
143 // the close future. See b/23676310 for reference.
144 return;
145 }
Beverlyc5094122020-01-09 17:05:15 -0500146 if (isGroupChild(removed.getKey(), isGroup, isGroupSummary)) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400147 group.children.remove(removed.getKey());
Selim Cineke73ad212015-11-03 19:11:08 -0800148 } else {
149 group.summary = null;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100150 }
Selim Cinek2a739342016-03-17 10:28:55 -0700151 updateSuppression(group);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100152 if (group.children.isEmpty()) {
153 if (group.summary == null) {
154 mGroupMap.remove(groupKey);
Kevin01a53cb2018-11-09 18:19:54 -0800155 for (OnGroupChangeListener listener : mListeners) {
156 listener.onGroupRemoved(group, groupKey);
157 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100158 }
159 }
160 }
161
Selim Cinekba069ae2020-04-01 19:45:16 -0700162 /**
163 * Notify the group manager that a new entry was added
164 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500165 public void onEntryAdded(final NotificationEntry added) {
Selim Cinekba069ae2020-04-01 19:45:16 -0700166 updateIsolation(added);
167 onEntryAddedInternal(added);
168 }
169
170 private void onEntryAddedInternal(final NotificationEntry added) {
Evan Laird94492852018-10-25 13:43:01 -0400171 if (added.isRowRemoved()) {
Selim Cinek04be3892017-08-08 10:58:32 -0700172 added.setDebugThrowable(new Throwable());
173 }
Ned Burns00b4b2d2019-10-17 22:09:27 -0400174 final StatusBarNotification sbn = added.getSbn();
Selim Cinekef5127e2015-12-21 16:55:58 -0800175 boolean isGroupChild = isGroupChild(sbn);
176 String groupKey = getGroupKey(sbn);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100177 NotificationGroup group = mGroupMap.get(groupKey);
178 if (group == null) {
179 group = new NotificationGroup();
180 mGroupMap.put(groupKey, group);
Kevin01a53cb2018-11-09 18:19:54 -0800181 for (OnGroupChangeListener listener : mListeners) {
182 listener.onGroupCreated(group, groupKey);
183 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100184 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800185 if (isGroupChild) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400186 NotificationEntry existing = group.children.get(added.getKey());
Selim Cinek04be3892017-08-08 10:58:32 -0700187 if (existing != null && existing != added) {
188 Throwable existingThrowable = existing.getDebugThrowable();
Ned Burns00b4b2d2019-10-17 22:09:27 -0400189 Log.wtf(TAG, "Inconsistent entries found with the same key " + added.getKey()
Evan Laird94492852018-10-25 13:43:01 -0400190 + "existing removed: " + existing.isRowRemoved()
Selim Cinek04be3892017-08-08 10:58:32 -0700191 + (existingThrowable != null
192 ? Log.getStackTraceString(existingThrowable) + "\n": "")
Evan Laird94492852018-10-25 13:43:01 -0400193 + " added removed" + added.isRowRemoved()
Selim Cinek04be3892017-08-08 10:58:32 -0700194 , new Throwable());
195 }
Ned Burns00b4b2d2019-10-17 22:09:27 -0400196 group.children.put(added.getKey(), added);
Selim Cinek2a739342016-03-17 10:28:55 -0700197 updateSuppression(group);
Selim Cineke73ad212015-11-03 19:11:08 -0800198 } else {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100199 group.summary = added;
Evan Laird94492852018-10-25 13:43:01 -0400200 group.expanded = added.areChildrenExpanded();
Selim Cinek2a739342016-03-17 10:28:55 -0700201 updateSuppression(group);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100202 if (!group.children.isEmpty()) {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500203 ArrayList<NotificationEntry> childrenCopy
Selim Cinek04be3892017-08-08 10:58:32 -0700204 = new ArrayList<>(group.children.values());
Ned Burnsf81c4c42019-01-07 14:10:43 -0500205 for (NotificationEntry child : childrenCopy) {
Selim Cinek50e74672016-05-05 15:58:10 -0400206 onEntryBecomingChild(child);
207 }
Kevin01a53cb2018-11-09 18:19:54 -0800208 for (OnGroupChangeListener listener : mListeners) {
209 listener.onGroupCreatedFromChildren(group);
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700210 }
211 }
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700212 }
213 }
214
Ned Burnsf81c4c42019-01-07 14:10:43 -0500215 private void onEntryBecomingChild(NotificationEntry entry) {
Selim Cinekba069ae2020-04-01 19:45:16 -0700216 updateIsolation(entry);
Selim Cinek50e74672016-05-05 15:58:10 -0400217 }
218
Selim Cinek2a739342016-03-17 10:28:55 -0700219 private void updateSuppression(NotificationGroup group) {
Selim Cinek80c44dd2016-08-15 19:39:55 -0700220 if (group == null) {
Selim Cinek23c80342016-03-17 18:27:36 -0700221 return;
222 }
Mady Mellor740d85d2019-07-09 15:26:47 -0700223 int childCount = 0;
224 boolean hasBubbles = false;
Beverlyed8aea22020-01-22 16:52:47 -0500225 for (NotificationEntry entry : group.children.values()) {
226 if (!getBubbleController().isBubbleNotificationSuppressedFromShade(entry)) {
Mady Mellor740d85d2019-07-09 15:26:47 -0700227 childCount++;
228 } else {
229 hasBubbles = true;
230 }
231 }
232
Selim Cinek2a739342016-03-17 10:28:55 -0700233 boolean prevSuppressed = group.suppressed;
Selim Cinek23c80342016-03-17 18:27:36 -0700234 group.suppressed = group.summary != null && !group.expanded
Mady Mellor740d85d2019-07-09 15:26:47 -0700235 && (childCount == 1
236 || (childCount == 0
Ned Burns00b4b2d2019-10-17 22:09:27 -0400237 && group.summary.getSbn().getNotification().isGroupSummary()
Mady Mellor740d85d2019-07-09 15:26:47 -0700238 && (hasIsolatedChildren(group) || hasBubbles)));
Selim Cinek2a739342016-03-17 10:28:55 -0700239 if (prevSuppressed != group.suppressed) {
Kevin01a53cb2018-11-09 18:19:54 -0800240 for (OnGroupChangeListener listener : mListeners) {
241 if (!mIsUpdatingUnchangedGroup) {
242 listener.onGroupSuppressionChanged(group, group.suppressed);
243 listener.onGroupsChanged();
Kevina97ea052018-09-11 13:53:18 -0700244 }
Selim Cinek967ed2a2016-04-08 18:29:11 -0700245 }
Selim Cinek2a739342016-03-17 10:28:55 -0700246 }
247 }
248
Selim Cinek23c80342016-03-17 18:27:36 -0700249 private boolean hasIsolatedChildren(NotificationGroup group) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400250 return getNumberOfIsolatedChildren(group.summary.getSbn().getGroupKey()) != 0;
Selim Cinek23c80342016-03-17 18:27:36 -0700251 }
252
253 private int getNumberOfIsolatedChildren(String groupKey) {
254 int count = 0;
Selim Cineka6c0cef2016-03-18 11:42:25 -0700255 for (StatusBarNotification sbn : mIsolatedEntries.values()) {
Beverlyc5094122020-01-09 17:05:15 -0500256 if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn.getKey())) {
Selim Cinek23c80342016-03-17 18:27:36 -0700257 count++;
258 }
259 }
260 return count;
261 }
262
Beverlyc5094122020-01-09 17:05:15 -0500263 /**
264 * Update an entry's group information
265 * @param entry notification entry to update
266 * @param oldNotification previous notification info before this update
267 */
268 public void onEntryUpdated(NotificationEntry entry, StatusBarNotification oldNotification) {
269 onEntryUpdated(entry, oldNotification.getGroupKey(), oldNotification.isGroup(),
270 oldNotification.getNotification().isGroupSummary());
271 }
272
273 /**
274 * Updates an entry's group information
275 * @param entry notification entry to update
276 * @param oldGroupKey the notification's previous group key before this update
277 * @param oldIsGroup whether this notification was a group before this update
278 * @param oldIsGroupSummary whether this notification was a group summary before this update
279 */
280 public void onEntryUpdated(NotificationEntry entry, String oldGroupKey, boolean oldIsGroup,
281 boolean oldIsGroupSummary) {
282 String newGroupKey = entry.getSbn().getGroupKey();
283 boolean groupKeysChanged = !oldGroupKey.equals(newGroupKey);
284 boolean wasGroupChild = isGroupChild(entry.getKey(), oldIsGroup, oldIsGroupSummary);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400285 boolean isGroupChild = isGroupChild(entry.getSbn());
Selim Cinek80c44dd2016-08-15 19:39:55 -0700286 mIsUpdatingUnchangedGroup = !groupKeysChanged && wasGroupChild == isGroupChild;
Beverlyc5094122020-01-09 17:05:15 -0500287 if (mGroupMap.get(getGroupKey(entry.getKey(), oldGroupKey)) != null) {
288 onEntryRemovedInternal(entry, oldGroupKey, oldIsGroup, oldIsGroupSummary);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100289 }
Selim Cinekba069ae2020-04-01 19:45:16 -0700290 onEntryAddedInternal(entry);
Selim Cinek80c44dd2016-08-15 19:39:55 -0700291 mIsUpdatingUnchangedGroup = false;
Beverlyc5094122020-01-09 17:05:15 -0500292 if (isIsolated(entry.getSbn().getKey())) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400293 mIsolatedEntries.put(entry.getKey(), entry.getSbn());
Selim Cinek68bdff12016-08-03 13:38:56 -0700294 if (groupKeysChanged) {
Beverlyc5094122020-01-09 17:05:15 -0500295 updateSuppression(mGroupMap.get(oldGroupKey));
296 updateSuppression(mGroupMap.get(newGroupKey));
Selim Cinek23c80342016-03-17 18:27:36 -0700297 }
Selim Cinek68bdff12016-08-03 13:38:56 -0700298 } else if (!wasGroupChild && isGroupChild) {
Selim Cinek50e74672016-05-05 15:58:10 -0400299 onEntryBecomingChild(entry);
Selim Cinek23c80342016-03-17 18:27:36 -0700300 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100301 }
302
Selim Cinek2a739342016-03-17 10:28:55 -0700303 public boolean isSummaryOfSuppressedGroup(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700304 return isGroupSuppressed(getGroupKey(sbn)) && sbn.getNotification().isGroupSummary();
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100305 }
306
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700307 private boolean isOnlyChild(StatusBarNotification sbn) {
Selim Cinek36b02232016-05-11 23:07:05 -0400308 return !sbn.getNotification().isGroupSummary()
Selim Cinek23c80342016-03-17 18:27:36 -0700309 && getTotalNumberOfChildren(sbn) == 1;
Selim Cinek2a739342016-03-17 10:28:55 -0700310 }
311
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700312 public boolean isOnlyChildInGroup(StatusBarNotification sbn) {
Selim Cinek88086e72016-06-17 21:01:32 -0700313 if (!isOnlyChild(sbn)) {
314 return false;
315 }
Ned Burnsf81c4c42019-01-07 14:10:43 -0500316 NotificationEntry logicalGroupSummary = getLogicalGroupSummary(sbn);
Selim Cinek88086e72016-06-17 21:01:32 -0700317 return logicalGroupSummary != null
Ned Burns00b4b2d2019-10-17 22:09:27 -0400318 && !logicalGroupSummary.getSbn().equals(sbn);
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700319 }
320
Selim Cinek23c80342016-03-17 18:27:36 -0700321 private int getTotalNumberOfChildren(StatusBarNotification sbn) {
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700322 int isolatedChildren = getNumberOfIsolatedChildren(sbn.getGroupKey());
323 NotificationGroup group = mGroupMap.get(sbn.getGroupKey());
324 int realChildren = group != null ? group.children.size() : 0;
325 return isolatedChildren + realChildren;
Selim Cinek23c80342016-03-17 18:27:36 -0700326 }
327
328 private boolean isGroupSuppressed(String groupKey) {
329 NotificationGroup group = mGroupMap.get(groupKey);
Selim Cinek2a739342016-03-17 10:28:55 -0700330 return group != null && group.suppressed;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100331 }
332
Jason Monk1fd3fc32018-08-14 17:20:09 -0400333 private void setStatusBarState(int newState) {
Selim Cinek9c4c4142015-12-04 16:44:56 -0800334 mBarState = newState;
335 if (mBarState == StatusBarState.KEYGUARD) {
Selim Cinek9184f9c2016-02-02 17:36:53 -0800336 collapseAllGroups();
337 }
338 }
339
340 public void collapseAllGroups() {
Selim Cinekc0b14b02016-05-09 13:12:40 -0400341 // Because notifications can become isolated when the group becomes suppressed it can
342 // lead to concurrent modifications while looping. We need to make a copy.
343 ArrayList<NotificationGroup> groupCopy = new ArrayList<>(mGroupMap.values());
344 int size = groupCopy.size();
345 for (int i = 0; i < size; i++) {
346 NotificationGroup group = groupCopy.get(i);
Selim Cinek9184f9c2016-02-02 17:36:53 -0800347 if (group.expanded) {
348 setGroupExpanded(group, false);
Selim Cinek9c4c4142015-12-04 16:44:56 -0800349 }
Selim Cinek2a739342016-03-17 10:28:55 -0700350 updateSuppression(group);
Selim Cinek9c4c4142015-12-04 16:44:56 -0800351 }
352 }
353
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100354 /**
355 * @return whether a given notification is a child in a group which has a summary
356 */
357 public boolean isChildInGroupWithSummary(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800358 if (!isGroupChild(sbn)) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100359 return false;
360 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800361 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek2a739342016-03-17 10:28:55 -0700362 if (group == null || group.summary == null || group.suppressed) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100363 return false;
364 }
Selim Cinek3f19f602016-05-02 18:01:56 -0700365 if (group.children.isEmpty()) {
366 // If the suppression of a group changes because the last child was removed, this can
367 // still be called temporarily because the child hasn't been fully removed yet. Let's
368 // make sure we still return false in that case.
369 return false;
370 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100371 return true;
372 }
373
Selim Cinek263398f2015-10-21 17:40:23 -0700374 /**
375 * @return whether a given notification is a summary in a group which has children
376 */
377 public boolean isSummaryOfGroup(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800378 if (!isGroupSummary(sbn)) {
Selim Cinek263398f2015-10-21 17:40:23 -0700379 return false;
380 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800381 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Kevin96eed642018-09-05 18:53:33 -0700382 if (group == null || group.summary == null) {
Selim Cinek263398f2015-10-21 17:40:23 -0700383 return false;
384 }
Ned Burns00b4b2d2019-10-17 22:09:27 -0400385 return !group.children.isEmpty() && Objects.equals(group.summary.getSbn(), sbn);
Selim Cinek263398f2015-10-21 17:40:23 -0700386 }
387
Selim Cinek23c80342016-03-17 18:27:36 -0700388 /**
389 * Get the summary of a specified status bar notification. For isolated notification this return
390 * itself.
391 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500392 public NotificationEntry getGroupSummary(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700393 return getGroupSummary(getGroupKey(sbn));
394 }
395
396 /**
397 * Similar to {@link #getGroupSummary(StatusBarNotification)} but doesn't get the visual summary
398 * but the logical summary, i.e when a child is isolated, it still returns the summary as if
399 * it wasn't isolated.
400 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500401 public NotificationEntry getLogicalGroupSummary(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700402 return getGroupSummary(sbn.getGroupKey());
403 }
404
405 @Nullable
Ned Burnsf81c4c42019-01-07 14:10:43 -0500406 private NotificationEntry getGroupSummary(String groupKey) {
Selim Cinek23c80342016-03-17 18:27:36 -0700407 NotificationGroup group = mGroupMap.get(groupKey);
Evan Laird94492852018-10-25 13:43:01 -0400408 //TODO: see if this can become an Entry
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100409 return group == null ? null
Evan Laird9a9a6192019-12-19 10:44:21 -0500410 : group.summary;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100411 }
412
Kevin01a53cb2018-11-09 18:19:54 -0800413 /**
414 * Get the children that are logically in the summary's group, whether or not they are isolated.
415 *
416 * @param summary summary of a group
417 * @return list of the children
418 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500419 public ArrayList<NotificationEntry> getLogicalChildren(StatusBarNotification summary) {
Kevin01a53cb2018-11-09 18:19:54 -0800420 NotificationGroup group = mGroupMap.get(summary.getGroupKey());
421 if (group == null) {
422 return null;
423 }
Ned Burnsf81c4c42019-01-07 14:10:43 -0500424 ArrayList<NotificationEntry> children = new ArrayList<>(group.children.values());
Selim Cinekba069ae2020-04-01 19:45:16 -0700425 for (StatusBarNotification sbn : mIsolatedEntries.values()) {
426 if (sbn.getGroupKey().equals(summary.getGroupKey())) {
427 children.add(mGroupMap.get(sbn.getKey()).summary);
428 }
Kevin01a53cb2018-11-09 18:19:54 -0800429 }
430 return children;
431 }
432
433 /**
Selim Cinekba069ae2020-04-01 19:45:16 -0700434 * Get the children that are in the summary's group, not including those isolated.
435 *
436 * @param summary summary of a group
437 * @return list of the children
438 */
439 public @Nullable ArrayList<NotificationEntry> getChildren(StatusBarNotification summary) {
440 NotificationGroup group = mGroupMap.get(summary.getGroupKey());
441 if (group == null) {
442 return null;
443 }
444 return new ArrayList<>(group.children.values());
445 }
446
447 /**
Mady Mellor740d85d2019-07-09 15:26:47 -0700448 * If there is a {@link NotificationGroup} associated with the provided entry, this method
449 * will update the suppression of that group.
450 */
451 public void updateSuppression(NotificationEntry entry) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400452 NotificationGroup group = mGroupMap.get(getGroupKey(entry.getSbn()));
Mady Mellor740d85d2019-07-09 15:26:47 -0700453 if (group != null) {
454 updateSuppression(group);
455 }
456 }
457
458 /**
Kevin01a53cb2018-11-09 18:19:54 -0800459 * Get the group key. May differ from the one in the notification due to the notification
460 * being temporarily isolated.
461 *
462 * @param sbn notification to check
463 * @return the key of the notification
464 */
465 public String getGroupKey(StatusBarNotification sbn) {
Beverlyc5094122020-01-09 17:05:15 -0500466 return getGroupKey(sbn.getKey(), sbn.getGroupKey());
467 }
468
469 private String getGroupKey(String key, String groupKey) {
470 if (isIsolated(key)) {
471 return key;
Kevin01a53cb2018-11-09 18:19:54 -0800472 }
Beverlyc5094122020-01-09 17:05:15 -0500473 return groupKey;
Kevin01a53cb2018-11-09 18:19:54 -0800474 }
475
Chris Wren698b1702016-05-23 11:16:32 -0400476 /** @return group expansion state after toggling. */
477 public boolean toggleGroupExpansion(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800478 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek83bc7832015-10-22 13:26:54 -0700479 if (group == null) {
Chris Wren698b1702016-05-23 11:16:32 -0400480 return false;
Selim Cinek83bc7832015-10-22 13:26:54 -0700481 }
482 setGroupExpanded(group, !group.expanded);
Chris Wren698b1702016-05-23 11:16:32 -0400483 return group.expanded;
Selim Cinek83bc7832015-10-22 13:26:54 -0700484 }
485
Beverlyc5094122020-01-09 17:05:15 -0500486 private boolean isIsolated(String sbnKey) {
487 return mIsolatedEntries.containsKey(sbnKey);
Selim Cinekef5127e2015-12-21 16:55:58 -0800488 }
489
Kevin01a53cb2018-11-09 18:19:54 -0800490 /**
491 * Whether a notification is visually a group summary.
492 *
493 * @param sbn notification to check
494 * @return true if it is visually a group summary
495 */
496 public boolean isGroupSummary(StatusBarNotification sbn) {
Beverlyc5094122020-01-09 17:05:15 -0500497 if (isIsolated(sbn.getKey())) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800498 return true;
499 }
500 return sbn.getNotification().isGroupSummary();
501 }
Julia Reynoldse46bb372016-03-17 11:05:58 -0400502
Kevin01a53cb2018-11-09 18:19:54 -0800503 /**
504 * Whether a notification is visually a group child.
505 *
506 * @param sbn notification to check
507 * @return true if it is visually a group child
508 */
509 public boolean isGroupChild(StatusBarNotification sbn) {
Beverlyc5094122020-01-09 17:05:15 -0500510 return isGroupChild(sbn.getKey(), sbn.isGroup(), sbn.getNotification().isGroupSummary());
511 }
512
513 private boolean isGroupChild(String key, boolean isGroup, boolean isGroupSummary) {
514 if (isIsolated(key)) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800515 return false;
516 }
Beverlyc5094122020-01-09 17:05:15 -0500517 return isGroup && !isGroupSummary;
Selim Cinekef5127e2015-12-21 16:55:58 -0800518 }
519
Selim Cinekef5127e2015-12-21 16:55:58 -0800520 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500521 public void onHeadsUpStateChanged(NotificationEntry entry, boolean isHeadsUp) {
Selim Cinekba069ae2020-04-01 19:45:16 -0700522 updateIsolation(entry);
Selim Cinekef5127e2015-12-21 16:55:58 -0800523 }
524
Kevina97ea052018-09-11 13:53:18 -0700525 /**
Kevina97ea052018-09-11 13:53:18 -0700526 * Whether a notification that is normally part of a group should be temporarily isolated from
527 * the group and put in their own group visually. This generally happens when the notification
528 * is alerting.
529 *
530 * @param entry the notification to check
531 * @return true if the entry should be isolated
532 */
533
Ned Burnsf81c4c42019-01-07 14:10:43 -0500534 private boolean shouldIsolate(NotificationEntry entry) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400535 StatusBarNotification sbn = entry.getSbn();
Kevina97ea052018-09-11 13:53:18 -0700536 if (!sbn.isGroup() || sbn.getNotification().isGroupSummary()) {
537 return false;
538 }
Selim Cinekba069ae2020-04-01 19:45:16 -0700539 NotificationChannel channel = entry.getChannel();
540 if (channel != null && channel.isImportantConversation()) {
541 return true;
542 }
Ned Burns00b4b2d2019-10-17 22:09:27 -0400543 if (mHeadsUpManager != null && !mHeadsUpManager.isAlerting(entry.getKey())) {
Kevina97ea052018-09-11 13:53:18 -0700544 return false;
545 }
Selim Cinekba069ae2020-04-01 19:45:16 -0700546 NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey());
Kevina97ea052018-09-11 13:53:18 -0700547 return (sbn.getNotification().fullScreenIntent != null
548 || notificationGroup == null
549 || !notificationGroup.expanded
550 || isGroupNotFullyVisible(notificationGroup));
551 }
552
553 /**
554 * Isolate a notification from its group so that it visually shows as its own group.
555 *
556 * @param entry the notification to isolate
557 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500558 private void isolateNotification(NotificationEntry entry) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400559 StatusBarNotification sbn = entry.getSbn();
Kevina97ea052018-09-11 13:53:18 -0700560
561 // We will be isolated now, so lets update the groups
Ned Burns00b4b2d2019-10-17 22:09:27 -0400562 onEntryRemovedInternal(entry, entry.getSbn());
Kevina97ea052018-09-11 13:53:18 -0700563
564 mIsolatedEntries.put(sbn.getKey(), sbn);
565
Selim Cinekba069ae2020-04-01 19:45:16 -0700566 onEntryAddedInternal(entry);
Kevina97ea052018-09-11 13:53:18 -0700567 // We also need to update the suppression of the old group, because this call comes
568 // even before the groupManager knows about the notification at all.
569 // When the notification gets added afterwards it is already isolated and therefore
570 // it doesn't lead to an update.
Ned Burns00b4b2d2019-10-17 22:09:27 -0400571 updateSuppression(mGroupMap.get(entry.getSbn().getGroupKey()));
Kevin01a53cb2018-11-09 18:19:54 -0800572 for (OnGroupChangeListener listener : mListeners) {
573 listener.onGroupsChanged();
574 }
Kevina97ea052018-09-11 13:53:18 -0700575 }
576
577 /**
Selim Cinekba069ae2020-04-01 19:45:16 -0700578 * Update the isolation of an entry, splitting it from the group.
579 */
580 public void updateIsolation(NotificationEntry entry) {
581 boolean isIsolated = isIsolated(entry.getSbn().getKey());
582 if (shouldIsolate(entry)) {
583 if (!isIsolated) {
584 isolateNotification(entry);
585 }
586 } else if (isIsolated) {
587 stopIsolatingNotification(entry);
588 }
589 }
590
591 /**
Kevina97ea052018-09-11 13:53:18 -0700592 * Stop isolating a notification and re-group it with its original logical group.
593 *
594 * @param entry the notification to un-isolate
595 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500596 private void stopIsolatingNotification(NotificationEntry entry) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400597 StatusBarNotification sbn = entry.getSbn();
Selim Cinekba069ae2020-04-01 19:45:16 -0700598 if (isIsolated(sbn.getKey())) {
Kevina97ea052018-09-11 13:53:18 -0700599 // not isolated anymore, we need to update the groups
Ned Burns00b4b2d2019-10-17 22:09:27 -0400600 onEntryRemovedInternal(entry, entry.getSbn());
Kevina97ea052018-09-11 13:53:18 -0700601 mIsolatedEntries.remove(sbn.getKey());
Selim Cinekba069ae2020-04-01 19:45:16 -0700602 onEntryAddedInternal(entry);
Kevin01a53cb2018-11-09 18:19:54 -0800603 for (OnGroupChangeListener listener : mListeners) {
604 listener.onGroupsChanged();
605 }
Kevina97ea052018-09-11 13:53:18 -0700606 }
Selim Cineka6c0cef2016-03-18 11:42:25 -0700607 }
608
609 private boolean isGroupNotFullyVisible(NotificationGroup notificationGroup) {
610 return notificationGroup.summary == null
Evan Laird94492852018-10-25 13:43:01 -0400611 || notificationGroup.summary.isGroupNotFullyVisible();
Selim Cineka6c0cef2016-03-18 11:42:25 -0700612 }
613
Selim Cinek967ed2a2016-04-08 18:29:11 -0700614 public void setHeadsUpManager(HeadsUpManager headsUpManager) {
615 mHeadsUpManager = headsUpManager;
616 }
617
Selim Cinek52941c52016-05-07 18:29:32 -0400618 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
619 pw.println("GroupManager state:");
620 pw.println(" number of groups: " + mGroupMap.size());
621 for (Map.Entry<String, NotificationGroup> entry : mGroupMap.entrySet()) {
622 pw.println("\n key: " + entry.getKey()); pw.println(entry.getValue());
623 }
624 pw.println("\n isolated entries: " + mIsolatedEntries.size());
625 for (Map.Entry<String, StatusBarNotification> entry : mIsolatedEntries.entrySet()) {
626 pw.print(" "); pw.print(entry.getKey());
627 pw.print(", "); pw.println(entry.getValue());
628 }
629 }
630
Evan Laird878c8532018-10-15 15:54:29 -0400631 @Override
632 public void onStateChanged(int newState) {
633 setStatusBarState(newState);
634 }
635
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100636 public static class NotificationGroup {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500637 public final HashMap<String, NotificationEntry> children = new HashMap<>();
638 public NotificationEntry summary;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100639 public boolean expanded;
Selim Cinek2a739342016-03-17 10:28:55 -0700640 /**
641 * Is this notification group suppressed, i.e its summary is hidden
642 */
643 public boolean suppressed;
Selim Cinek52941c52016-05-07 18:29:32 -0400644
645 @Override
646 public String toString() {
Selim Cinek60ca7872016-05-12 11:24:07 -0700647 String result = " summary:\n "
Ned Burns00b4b2d2019-10-17 22:09:27 -0400648 + (summary != null ? summary.getSbn() : "null")
Selim Cinek04be3892017-08-08 10:58:32 -0700649 + (summary != null && summary.getDebugThrowable() != null
650 ? Log.getStackTraceString(summary.getDebugThrowable())
651 : "");
Selim Cinek52941c52016-05-07 18:29:32 -0400652 result += "\n children size: " + children.size();
Ned Burnsf81c4c42019-01-07 14:10:43 -0500653 for (NotificationEntry child : children.values()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400654 result += "\n " + child.getSbn()
Selim Cinek04be3892017-08-08 10:58:32 -0700655 + (child.getDebugThrowable() != null
656 ? Log.getStackTraceString(child.getDebugThrowable())
657 : "");
Selim Cinek52941c52016-05-07 18:29:32 -0400658 }
Mady Mellor740d85d2019-07-09 15:26:47 -0700659 result += "\n summary suppressed: " + suppressed;
Selim Cinek52941c52016-05-07 18:29:32 -0400660 return result;
661 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100662 }
663
664 public interface OnGroupChangeListener {
Kevin01a53cb2018-11-09 18:19:54 -0800665
666 /**
667 * A new group has been created.
668 *
669 * @param group the group that was created
670 * @param groupKey the group's key
671 */
672 default void onGroupCreated(NotificationGroup group, String groupKey) {}
673
674 /**
675 * A group has been removed.
676 *
677 * @param group the group that was removed
678 * @param groupKey the group's key
679 */
680 default void onGroupRemoved(NotificationGroup group, String groupKey) {}
681
682 /**
683 * The suppression of a group has changed.
684 *
685 * @param group the group that has changed
686 * @param suppressed true if the group is now suppressed, false o/w
687 */
688 default void onGroupSuppressionChanged(NotificationGroup group, boolean suppressed) {}
689
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100690 /**
691 * The expansion of a group has changed.
692 *
693 * @param changedRow the row for which the expansion has changed, which is also the summary
694 * @param expanded a boolean indicating the new expanded state
695 */
Kevin01a53cb2018-11-09 18:19:54 -0800696 default void onGroupExpansionChanged(ExpandableNotificationRow changedRow,
697 boolean expanded) {}
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100698
699 /**
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100700 * A group of children just received a summary notification and should therefore become
701 * children of it.
702 *
703 * @param group the group created
704 */
Kevin01a53cb2018-11-09 18:19:54 -0800705 default void onGroupCreatedFromChildren(NotificationGroup group) {}
Selim Cinekef5127e2015-12-21 16:55:58 -0800706
707 /**
Selim Cinek2a739342016-03-17 10:28:55 -0700708 * The groups have changed. This can happen if the isolation of a child has changes or if a
709 * group became suppressed / unsuppressed
Selim Cinekef5127e2015-12-21 16:55:58 -0800710 */
Kevin01a53cb2018-11-09 18:19:54 -0800711 default void onGroupsChanged() {}
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100712 }
713}