blob: 77337e95bb9589316c2c128b8b6b612342fe278f [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 Cinek25fd4e2b2015-02-20 17:46:07 +010020import android.service.notification.StatusBarNotification;
Kevin01a53cb2018-11-09 18:19:54 -080021import android.util.ArraySet;
Selim Cinek04be3892017-08-08 10:58:32 -070022import android.util.Log;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010023
Jason Monk1fd3fc32018-08-14 17:20:09 -040024import com.android.systemui.Dependency;
Mady Mellor740d85d2019-07-09 15:26:47 -070025import com.android.systemui.bubbles.BubbleController;
Beverly8fdb5332019-02-04 14:29:49 -050026import com.android.systemui.plugins.statusbar.StatusBarStateController;
27import com.android.systemui.plugins.statusbar.StatusBarStateController.StateListener;
Jason Monk1fd3fc32018-08-14 17:20:09 -040028import com.android.systemui.statusbar.StatusBarState;
Ned Burnsf81c4c42019-01-07 14:10:43 -050029import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Evan Laird878c8532018-10-15 15:54:29 -040030import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
Selim Cinekef5127e2015-12-21 16:55:58 -080031import com.android.systemui.statusbar.policy.HeadsUpManager;
Selim Cineka7d4f822016-12-06 14:34:47 -080032import com.android.systemui.statusbar.policy.OnHeadsUpChangedListener;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010033
Selim Cinek52941c52016-05-07 18:29:32 -040034import java.io.FileDescriptor;
35import java.io.PrintWriter;
Selim Cinekc0b14b02016-05-09 13:12:40 -040036import java.util.ArrayList;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010037import java.util.HashMap;
Selim Cinek52941c52016-05-07 18:29:32 -040038import java.util.Map;
Selim Cinekf93bf3e2018-05-08 14:43:21 -070039import java.util.Objects;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010040
Jason Monk27d01a622018-12-10 15:57:09 -050041import javax.inject.Inject;
42import javax.inject.Singleton;
43
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010044/**
45 * A class to handle notifications and their corresponding groups.
46 */
Jason Monk27d01a622018-12-10 15:57:09 -050047@Singleton
Selim Cinekc3fec682019-06-06 18:11:07 -070048public class NotificationGroupManager implements OnHeadsUpChangedListener, StateListener {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010049
Selim Cinek04be3892017-08-08 10:58:32 -070050 private static final String TAG = "NotificationGroupManager";
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010051 private final HashMap<String, NotificationGroup> mGroupMap = new HashMap<>();
Kevin01a53cb2018-11-09 18:19:54 -080052 private final ArraySet<OnGroupChangeListener> mListeners = new ArraySet<>();
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010053 private int mBarState = -1;
Selim Cineka6c0cef2016-03-18 11:42:25 -070054 private HashMap<String, StatusBarNotification> mIsolatedEntries = new HashMap<>();
Selim Cinek967ed2a2016-04-08 18:29:11 -070055 private HeadsUpManager mHeadsUpManager;
Selim Cinek80c44dd2016-08-15 19:39:55 -070056 private boolean mIsUpdatingUnchangedGroup;
Mady Mellor740d85d2019-07-09 15:26:47 -070057 @Nullable private BubbleController mBubbleController = null;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010058
Jason Monk27d01a622018-12-10 15:57:09 -050059 @Inject
Selim Cinekc3fec682019-06-06 18:11:07 -070060 public NotificationGroupManager(StatusBarStateController statusBarStateController) {
61 statusBarStateController.addCallback(this);
Jason Monk1fd3fc32018-08-14 17:20:09 -040062 }
63
Mady Mellor740d85d2019-07-09 15:26:47 -070064 private BubbleController getBubbleController() {
65 if (mBubbleController == null) {
66 mBubbleController = Dependency.get(BubbleController.class);
67 }
68 return mBubbleController;
69 }
70
Kevin01a53cb2018-11-09 18:19:54 -080071 /**
72 * Add a listener for changes to groups.
73 *
74 * @param listener listener to add
75 */
76 public void addOnGroupChangeListener(OnGroupChangeListener listener) {
77 mListeners.add(listener);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010078 }
79
80 public boolean isGroupExpanded(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -080081 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010082 if (group == null) {
83 return false;
84 }
85 return group.expanded;
86 }
87
88 public void setGroupExpanded(StatusBarNotification sbn, boolean expanded) {
Selim Cinekef5127e2015-12-21 16:55:58 -080089 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010090 if (group == null) {
91 return;
92 }
93 setGroupExpanded(group, expanded);
94 }
95
96 private void setGroupExpanded(NotificationGroup group, boolean expanded) {
97 group.expanded = expanded;
98 if (group.summary != null) {
Kevin01a53cb2018-11-09 18:19:54 -080099 for (OnGroupChangeListener listener : mListeners) {
Evan Laird94492852018-10-25 13:43:01 -0400100 listener.onGroupExpansionChanged(group.summary.getRow(), expanded);
Kevin01a53cb2018-11-09 18:19:54 -0800101 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100102 }
103 }
104
Ned Burnsf81c4c42019-01-07 14:10:43 -0500105 public void onEntryRemoved(NotificationEntry removed) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400106 onEntryRemovedInternal(removed, removed.getSbn());
107 mIsolatedEntries.remove(removed.getKey());
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100108 }
109
110 /**
111 * An entry was removed.
112 *
113 * @param removed the removed entry
114 * @param sbn the notification the entry has, which doesn't need to be the same as it's internal
115 * notification
116 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500117 private void onEntryRemovedInternal(NotificationEntry removed,
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100118 final StatusBarNotification sbn) {
Beverlyc5094122020-01-09 17:05:15 -0500119 onEntryRemovedInternal(removed, sbn.getGroupKey(), sbn.isGroup(),
120 sbn.getNotification().isGroupSummary());
121 }
122
123 private void onEntryRemovedInternal(NotificationEntry removed, String notifGroupKey, boolean
124 isGroup, boolean isGroupSummary) {
125 String groupKey = getGroupKey(removed.getKey(), notifGroupKey);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100126 final NotificationGroup group = mGroupMap.get(groupKey);
Selim Cinek0b4aeab2015-09-01 17:07:38 -0700127 if (group == null) {
128 // When an app posts 2 different notifications as summary of the same group, then a
129 // cancellation of the first notification removes this group.
130 // This situation is not supported and we will not allow such notifications anymore in
131 // the close future. See b/23676310 for reference.
132 return;
133 }
Beverlyc5094122020-01-09 17:05:15 -0500134 if (isGroupChild(removed.getKey(), isGroup, isGroupSummary)) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400135 group.children.remove(removed.getKey());
Selim Cineke73ad212015-11-03 19:11:08 -0800136 } else {
137 group.summary = null;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100138 }
Selim Cinek2a739342016-03-17 10:28:55 -0700139 updateSuppression(group);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100140 if (group.children.isEmpty()) {
141 if (group.summary == null) {
142 mGroupMap.remove(groupKey);
Kevin01a53cb2018-11-09 18:19:54 -0800143 for (OnGroupChangeListener listener : mListeners) {
144 listener.onGroupRemoved(group, groupKey);
145 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100146 }
147 }
148 }
149
Ned Burnsf81c4c42019-01-07 14:10:43 -0500150 public void onEntryAdded(final NotificationEntry added) {
Evan Laird94492852018-10-25 13:43:01 -0400151 if (added.isRowRemoved()) {
Selim Cinek04be3892017-08-08 10:58:32 -0700152 added.setDebugThrowable(new Throwable());
153 }
Ned Burns00b4b2d2019-10-17 22:09:27 -0400154 final StatusBarNotification sbn = added.getSbn();
Selim Cinekef5127e2015-12-21 16:55:58 -0800155 boolean isGroupChild = isGroupChild(sbn);
156 String groupKey = getGroupKey(sbn);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100157 NotificationGroup group = mGroupMap.get(groupKey);
158 if (group == null) {
159 group = new NotificationGroup();
160 mGroupMap.put(groupKey, group);
Kevin01a53cb2018-11-09 18:19:54 -0800161 for (OnGroupChangeListener listener : mListeners) {
162 listener.onGroupCreated(group, groupKey);
163 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100164 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800165 if (isGroupChild) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400166 NotificationEntry existing = group.children.get(added.getKey());
Selim Cinek04be3892017-08-08 10:58:32 -0700167 if (existing != null && existing != added) {
168 Throwable existingThrowable = existing.getDebugThrowable();
Ned Burns00b4b2d2019-10-17 22:09:27 -0400169 Log.wtf(TAG, "Inconsistent entries found with the same key " + added.getKey()
Evan Laird94492852018-10-25 13:43:01 -0400170 + "existing removed: " + existing.isRowRemoved()
Selim Cinek04be3892017-08-08 10:58:32 -0700171 + (existingThrowable != null
172 ? Log.getStackTraceString(existingThrowable) + "\n": "")
Evan Laird94492852018-10-25 13:43:01 -0400173 + " added removed" + added.isRowRemoved()
Selim Cinek04be3892017-08-08 10:58:32 -0700174 , new Throwable());
175 }
Ned Burns00b4b2d2019-10-17 22:09:27 -0400176 group.children.put(added.getKey(), added);
Selim Cinek2a739342016-03-17 10:28:55 -0700177 updateSuppression(group);
Selim Cineke73ad212015-11-03 19:11:08 -0800178 } else {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100179 group.summary = added;
Evan Laird94492852018-10-25 13:43:01 -0400180 group.expanded = added.areChildrenExpanded();
Selim Cinek2a739342016-03-17 10:28:55 -0700181 updateSuppression(group);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100182 if (!group.children.isEmpty()) {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500183 ArrayList<NotificationEntry> childrenCopy
Selim Cinek04be3892017-08-08 10:58:32 -0700184 = new ArrayList<>(group.children.values());
Ned Burnsf81c4c42019-01-07 14:10:43 -0500185 for (NotificationEntry child : childrenCopy) {
Selim Cinek50e74672016-05-05 15:58:10 -0400186 onEntryBecomingChild(child);
187 }
Kevin01a53cb2018-11-09 18:19:54 -0800188 for (OnGroupChangeListener listener : mListeners) {
189 listener.onGroupCreatedFromChildren(group);
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700190 }
191 }
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700192 }
193 }
194
Ned Burnsf81c4c42019-01-07 14:10:43 -0500195 private void onEntryBecomingChild(NotificationEntry entry) {
Kevina97ea052018-09-11 13:53:18 -0700196 if (shouldIsolate(entry)) {
197 isolateNotification(entry);
Selim Cinek50e74672016-05-05 15:58:10 -0400198 }
199 }
200
Selim Cinek2a739342016-03-17 10:28:55 -0700201 private void updateSuppression(NotificationGroup group) {
Selim Cinek80c44dd2016-08-15 19:39:55 -0700202 if (group == null) {
Selim Cinek23c80342016-03-17 18:27:36 -0700203 return;
204 }
Mady Mellor740d85d2019-07-09 15:26:47 -0700205 int childCount = 0;
206 boolean hasBubbles = false;
Beverlyed8aea22020-01-22 16:52:47 -0500207 for (NotificationEntry entry : group.children.values()) {
208 if (!getBubbleController().isBubbleNotificationSuppressedFromShade(entry)) {
Mady Mellor740d85d2019-07-09 15:26:47 -0700209 childCount++;
210 } else {
211 hasBubbles = true;
212 }
213 }
214
Selim Cinek2a739342016-03-17 10:28:55 -0700215 boolean prevSuppressed = group.suppressed;
Selim Cinek23c80342016-03-17 18:27:36 -0700216 group.suppressed = group.summary != null && !group.expanded
Mady Mellor740d85d2019-07-09 15:26:47 -0700217 && (childCount == 1
218 || (childCount == 0
Ned Burns00b4b2d2019-10-17 22:09:27 -0400219 && group.summary.getSbn().getNotification().isGroupSummary()
Mady Mellor740d85d2019-07-09 15:26:47 -0700220 && (hasIsolatedChildren(group) || hasBubbles)));
Selim Cinek2a739342016-03-17 10:28:55 -0700221 if (prevSuppressed != group.suppressed) {
Kevin01a53cb2018-11-09 18:19:54 -0800222 for (OnGroupChangeListener listener : mListeners) {
223 if (!mIsUpdatingUnchangedGroup) {
224 listener.onGroupSuppressionChanged(group, group.suppressed);
225 listener.onGroupsChanged();
Kevina97ea052018-09-11 13:53:18 -0700226 }
Selim Cinek967ed2a2016-04-08 18:29:11 -0700227 }
Selim Cinek2a739342016-03-17 10:28:55 -0700228 }
229 }
230
Selim Cinek23c80342016-03-17 18:27:36 -0700231 private boolean hasIsolatedChildren(NotificationGroup group) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400232 return getNumberOfIsolatedChildren(group.summary.getSbn().getGroupKey()) != 0;
Selim Cinek23c80342016-03-17 18:27:36 -0700233 }
234
235 private int getNumberOfIsolatedChildren(String groupKey) {
236 int count = 0;
Selim Cineka6c0cef2016-03-18 11:42:25 -0700237 for (StatusBarNotification sbn : mIsolatedEntries.values()) {
Beverlyc5094122020-01-09 17:05:15 -0500238 if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn.getKey())) {
Selim Cinek23c80342016-03-17 18:27:36 -0700239 count++;
240 }
241 }
242 return count;
243 }
244
Ned Burnsf81c4c42019-01-07 14:10:43 -0500245 private NotificationEntry getIsolatedChild(String groupKey) {
Selim Cinek967ed2a2016-04-08 18:29:11 -0700246 for (StatusBarNotification sbn : mIsolatedEntries.values()) {
Beverlyc5094122020-01-09 17:05:15 -0500247 if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn.getKey())) {
Selim Cinek967ed2a2016-04-08 18:29:11 -0700248 return mGroupMap.get(sbn.getKey()).summary;
249 }
250 }
251 return null;
252 }
253
Beverlyc5094122020-01-09 17:05:15 -0500254 /**
255 * Update an entry's group information
256 * @param entry notification entry to update
257 * @param oldNotification previous notification info before this update
258 */
259 public void onEntryUpdated(NotificationEntry entry, StatusBarNotification oldNotification) {
260 onEntryUpdated(entry, oldNotification.getGroupKey(), oldNotification.isGroup(),
261 oldNotification.getNotification().isGroupSummary());
262 }
263
264 /**
265 * Updates an entry's group information
266 * @param entry notification entry to update
267 * @param oldGroupKey the notification's previous group key before this update
268 * @param oldIsGroup whether this notification was a group before this update
269 * @param oldIsGroupSummary whether this notification was a group summary before this update
270 */
271 public void onEntryUpdated(NotificationEntry entry, String oldGroupKey, boolean oldIsGroup,
272 boolean oldIsGroupSummary) {
273 String newGroupKey = entry.getSbn().getGroupKey();
274 boolean groupKeysChanged = !oldGroupKey.equals(newGroupKey);
275 boolean wasGroupChild = isGroupChild(entry.getKey(), oldIsGroup, oldIsGroupSummary);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400276 boolean isGroupChild = isGroupChild(entry.getSbn());
Selim Cinek80c44dd2016-08-15 19:39:55 -0700277 mIsUpdatingUnchangedGroup = !groupKeysChanged && wasGroupChild == isGroupChild;
Beverlyc5094122020-01-09 17:05:15 -0500278 if (mGroupMap.get(getGroupKey(entry.getKey(), oldGroupKey)) != null) {
279 onEntryRemovedInternal(entry, oldGroupKey, oldIsGroup, oldIsGroupSummary);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100280 }
281 onEntryAdded(entry);
Selim Cinek80c44dd2016-08-15 19:39:55 -0700282 mIsUpdatingUnchangedGroup = false;
Beverlyc5094122020-01-09 17:05:15 -0500283 if (isIsolated(entry.getSbn().getKey())) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400284 mIsolatedEntries.put(entry.getKey(), entry.getSbn());
Selim Cinek68bdff12016-08-03 13:38:56 -0700285 if (groupKeysChanged) {
Beverlyc5094122020-01-09 17:05:15 -0500286 updateSuppression(mGroupMap.get(oldGroupKey));
287 updateSuppression(mGroupMap.get(newGroupKey));
Selim Cinek23c80342016-03-17 18:27:36 -0700288 }
Selim Cinek68bdff12016-08-03 13:38:56 -0700289 } else if (!wasGroupChild && isGroupChild) {
Selim Cinek50e74672016-05-05 15:58:10 -0400290 onEntryBecomingChild(entry);
Selim Cinek23c80342016-03-17 18:27:36 -0700291 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100292 }
293
Selim Cinek2a739342016-03-17 10:28:55 -0700294 public boolean isSummaryOfSuppressedGroup(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700295 return isGroupSuppressed(getGroupKey(sbn)) && sbn.getNotification().isGroupSummary();
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100296 }
297
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700298 private boolean isOnlyChild(StatusBarNotification sbn) {
Selim Cinek36b02232016-05-11 23:07:05 -0400299 return !sbn.getNotification().isGroupSummary()
Selim Cinek23c80342016-03-17 18:27:36 -0700300 && getTotalNumberOfChildren(sbn) == 1;
Selim Cinek2a739342016-03-17 10:28:55 -0700301 }
302
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700303 public boolean isOnlyChildInGroup(StatusBarNotification sbn) {
Selim Cinek88086e72016-06-17 21:01:32 -0700304 if (!isOnlyChild(sbn)) {
305 return false;
306 }
Ned Burnsf81c4c42019-01-07 14:10:43 -0500307 NotificationEntry logicalGroupSummary = getLogicalGroupSummary(sbn);
Selim Cinek88086e72016-06-17 21:01:32 -0700308 return logicalGroupSummary != null
Ned Burns00b4b2d2019-10-17 22:09:27 -0400309 && !logicalGroupSummary.getSbn().equals(sbn);
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700310 }
311
Selim Cinek23c80342016-03-17 18:27:36 -0700312 private int getTotalNumberOfChildren(StatusBarNotification sbn) {
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700313 int isolatedChildren = getNumberOfIsolatedChildren(sbn.getGroupKey());
314 NotificationGroup group = mGroupMap.get(sbn.getGroupKey());
315 int realChildren = group != null ? group.children.size() : 0;
316 return isolatedChildren + realChildren;
Selim Cinek23c80342016-03-17 18:27:36 -0700317 }
318
319 private boolean isGroupSuppressed(String groupKey) {
320 NotificationGroup group = mGroupMap.get(groupKey);
Selim Cinek2a739342016-03-17 10:28:55 -0700321 return group != null && group.suppressed;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100322 }
323
Jason Monk1fd3fc32018-08-14 17:20:09 -0400324 private void setStatusBarState(int newState) {
Selim Cinek9c4c4142015-12-04 16:44:56 -0800325 mBarState = newState;
326 if (mBarState == StatusBarState.KEYGUARD) {
Selim Cinek9184f9c2016-02-02 17:36:53 -0800327 collapseAllGroups();
328 }
329 }
330
331 public void collapseAllGroups() {
Selim Cinekc0b14b02016-05-09 13:12:40 -0400332 // Because notifications can become isolated when the group becomes suppressed it can
333 // lead to concurrent modifications while looping. We need to make a copy.
334 ArrayList<NotificationGroup> groupCopy = new ArrayList<>(mGroupMap.values());
335 int size = groupCopy.size();
336 for (int i = 0; i < size; i++) {
337 NotificationGroup group = groupCopy.get(i);
Selim Cinek9184f9c2016-02-02 17:36:53 -0800338 if (group.expanded) {
339 setGroupExpanded(group, false);
Selim Cinek9c4c4142015-12-04 16:44:56 -0800340 }
Selim Cinek2a739342016-03-17 10:28:55 -0700341 updateSuppression(group);
Selim Cinek9c4c4142015-12-04 16:44:56 -0800342 }
343 }
344
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100345 /**
346 * @return whether a given notification is a child in a group which has a summary
347 */
348 public boolean isChildInGroupWithSummary(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800349 if (!isGroupChild(sbn)) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100350 return false;
351 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800352 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek2a739342016-03-17 10:28:55 -0700353 if (group == null || group.summary == null || group.suppressed) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100354 return false;
355 }
Selim Cinek3f19f602016-05-02 18:01:56 -0700356 if (group.children.isEmpty()) {
357 // If the suppression of a group changes because the last child was removed, this can
358 // still be called temporarily because the child hasn't been fully removed yet. Let's
359 // make sure we still return false in that case.
360 return false;
361 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100362 return true;
363 }
364
Selim Cinek263398f2015-10-21 17:40:23 -0700365 /**
366 * @return whether a given notification is a summary in a group which has children
367 */
368 public boolean isSummaryOfGroup(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800369 if (!isGroupSummary(sbn)) {
Selim Cinek263398f2015-10-21 17:40:23 -0700370 return false;
371 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800372 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Kevin96eed642018-09-05 18:53:33 -0700373 if (group == null || group.summary == null) {
Selim Cinek263398f2015-10-21 17:40:23 -0700374 return false;
375 }
Ned Burns00b4b2d2019-10-17 22:09:27 -0400376 return !group.children.isEmpty() && Objects.equals(group.summary.getSbn(), sbn);
Selim Cinek263398f2015-10-21 17:40:23 -0700377 }
378
Selim Cinek23c80342016-03-17 18:27:36 -0700379 /**
380 * Get the summary of a specified status bar notification. For isolated notification this return
381 * itself.
382 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500383 public NotificationEntry getGroupSummary(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700384 return getGroupSummary(getGroupKey(sbn));
385 }
386
387 /**
388 * Similar to {@link #getGroupSummary(StatusBarNotification)} but doesn't get the visual summary
389 * but the logical summary, i.e when a child is isolated, it still returns the summary as if
390 * it wasn't isolated.
391 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500392 public NotificationEntry getLogicalGroupSummary(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700393 return getGroupSummary(sbn.getGroupKey());
394 }
395
396 @Nullable
Ned Burnsf81c4c42019-01-07 14:10:43 -0500397 private NotificationEntry getGroupSummary(String groupKey) {
Selim Cinek23c80342016-03-17 18:27:36 -0700398 NotificationGroup group = mGroupMap.get(groupKey);
Evan Laird94492852018-10-25 13:43:01 -0400399 //TODO: see if this can become an Entry
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100400 return group == null ? null
401 : group.summary == null ? null
Evan Laird94492852018-10-25 13:43:01 -0400402 : group.summary;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100403 }
404
Kevin01a53cb2018-11-09 18:19:54 -0800405 /**
406 * Get the children that are logically in the summary's group, whether or not they are isolated.
407 *
408 * @param summary summary of a group
409 * @return list of the children
410 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500411 public ArrayList<NotificationEntry> getLogicalChildren(StatusBarNotification summary) {
Kevin01a53cb2018-11-09 18:19:54 -0800412 NotificationGroup group = mGroupMap.get(summary.getGroupKey());
413 if (group == null) {
414 return null;
415 }
Ned Burnsf81c4c42019-01-07 14:10:43 -0500416 ArrayList<NotificationEntry> children = new ArrayList<>(group.children.values());
417 NotificationEntry isolatedChild = getIsolatedChild(summary.getGroupKey());
Kevin01a53cb2018-11-09 18:19:54 -0800418 if (isolatedChild != null) {
419 children.add(isolatedChild);
420 }
421 return children;
422 }
423
424 /**
Mady Mellor740d85d2019-07-09 15:26:47 -0700425 * If there is a {@link NotificationGroup} associated with the provided entry, this method
426 * will update the suppression of that group.
427 */
428 public void updateSuppression(NotificationEntry entry) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400429 NotificationGroup group = mGroupMap.get(getGroupKey(entry.getSbn()));
Mady Mellor740d85d2019-07-09 15:26:47 -0700430 if (group != null) {
431 updateSuppression(group);
432 }
433 }
434
435 /**
Kevin01a53cb2018-11-09 18:19:54 -0800436 * Get the group key. May differ from the one in the notification due to the notification
437 * being temporarily isolated.
438 *
439 * @param sbn notification to check
440 * @return the key of the notification
441 */
442 public String getGroupKey(StatusBarNotification sbn) {
Beverlyc5094122020-01-09 17:05:15 -0500443 return getGroupKey(sbn.getKey(), sbn.getGroupKey());
444 }
445
446 private String getGroupKey(String key, String groupKey) {
447 if (isIsolated(key)) {
448 return key;
Kevin01a53cb2018-11-09 18:19:54 -0800449 }
Beverlyc5094122020-01-09 17:05:15 -0500450 return groupKey;
Kevin01a53cb2018-11-09 18:19:54 -0800451 }
452
Chris Wren698b1702016-05-23 11:16:32 -0400453 /** @return group expansion state after toggling. */
454 public boolean toggleGroupExpansion(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800455 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek83bc7832015-10-22 13:26:54 -0700456 if (group == null) {
Chris Wren698b1702016-05-23 11:16:32 -0400457 return false;
Selim Cinek83bc7832015-10-22 13:26:54 -0700458 }
459 setGroupExpanded(group, !group.expanded);
Chris Wren698b1702016-05-23 11:16:32 -0400460 return group.expanded;
Selim Cinek83bc7832015-10-22 13:26:54 -0700461 }
462
Beverlyc5094122020-01-09 17:05:15 -0500463 private boolean isIsolated(String sbnKey) {
464 return mIsolatedEntries.containsKey(sbnKey);
Selim Cinekef5127e2015-12-21 16:55:58 -0800465 }
466
Kevin01a53cb2018-11-09 18:19:54 -0800467 /**
468 * Whether a notification is visually a group summary.
469 *
470 * @param sbn notification to check
471 * @return true if it is visually a group summary
472 */
473 public boolean isGroupSummary(StatusBarNotification sbn) {
Beverlyc5094122020-01-09 17:05:15 -0500474 if (isIsolated(sbn.getKey())) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800475 return true;
476 }
477 return sbn.getNotification().isGroupSummary();
478 }
Julia Reynoldse46bb372016-03-17 11:05:58 -0400479
Kevin01a53cb2018-11-09 18:19:54 -0800480 /**
481 * Whether a notification is visually a group child.
482 *
483 * @param sbn notification to check
484 * @return true if it is visually a group child
485 */
486 public boolean isGroupChild(StatusBarNotification sbn) {
Beverlyc5094122020-01-09 17:05:15 -0500487 return isGroupChild(sbn.getKey(), sbn.isGroup(), sbn.getNotification().isGroupSummary());
488 }
489
490 private boolean isGroupChild(String key, boolean isGroup, boolean isGroupSummary) {
491 if (isIsolated(key)) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800492 return false;
493 }
Beverlyc5094122020-01-09 17:05:15 -0500494 return isGroup && !isGroupSummary;
Selim Cinekef5127e2015-12-21 16:55:58 -0800495 }
496
Selim Cinekef5127e2015-12-21 16:55:58 -0800497 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500498 public void onHeadsUpStateChanged(NotificationEntry entry, boolean isHeadsUp) {
Kevin01a53cb2018-11-09 18:19:54 -0800499 onAlertStateChanged(entry, isHeadsUp);
Kevina97ea052018-09-11 13:53:18 -0700500 }
501
Ned Burnsf81c4c42019-01-07 14:10:43 -0500502 private void onAlertStateChanged(NotificationEntry entry, boolean isAlerting) {
Kevina97ea052018-09-11 13:53:18 -0700503 if (isAlerting) {
504 if (shouldIsolate(entry)) {
505 isolateNotification(entry);
Selim Cinekef5127e2015-12-21 16:55:58 -0800506 }
507 } else {
Kevina97ea052018-09-11 13:53:18 -0700508 stopIsolatingNotification(entry);
Selim Cinekef5127e2015-12-21 16:55:58 -0800509 }
510 }
511
Kevina97ea052018-09-11 13:53:18 -0700512 /**
Kevina97ea052018-09-11 13:53:18 -0700513 * Whether a notification that is normally part of a group should be temporarily isolated from
514 * the group and put in their own group visually. This generally happens when the notification
515 * is alerting.
516 *
517 * @param entry the notification to check
518 * @return true if the entry should be isolated
519 */
520
Ned Burnsf81c4c42019-01-07 14:10:43 -0500521 private boolean shouldIsolate(NotificationEntry entry) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400522 StatusBarNotification sbn = entry.getSbn();
Selim Cineka6c0cef2016-03-18 11:42:25 -0700523 NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey());
Kevina97ea052018-09-11 13:53:18 -0700524 if (!sbn.isGroup() || sbn.getNotification().isGroupSummary()) {
525 return false;
526 }
Ned Burns00b4b2d2019-10-17 22:09:27 -0400527 if (mHeadsUpManager != null && !mHeadsUpManager.isAlerting(entry.getKey())) {
Kevina97ea052018-09-11 13:53:18 -0700528 return false;
529 }
530 return (sbn.getNotification().fullScreenIntent != null
531 || notificationGroup == null
532 || !notificationGroup.expanded
533 || isGroupNotFullyVisible(notificationGroup));
534 }
535
536 /**
537 * Isolate a notification from its group so that it visually shows as its own group.
538 *
539 * @param entry the notification to isolate
540 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500541 private void isolateNotification(NotificationEntry entry) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400542 StatusBarNotification sbn = entry.getSbn();
Kevina97ea052018-09-11 13:53:18 -0700543
544 // We will be isolated now, so lets update the groups
Ned Burns00b4b2d2019-10-17 22:09:27 -0400545 onEntryRemovedInternal(entry, entry.getSbn());
Kevina97ea052018-09-11 13:53:18 -0700546
547 mIsolatedEntries.put(sbn.getKey(), sbn);
548
549 onEntryAdded(entry);
550 // We also need to update the suppression of the old group, because this call comes
551 // even before the groupManager knows about the notification at all.
552 // When the notification gets added afterwards it is already isolated and therefore
553 // it doesn't lead to an update.
Ned Burns00b4b2d2019-10-17 22:09:27 -0400554 updateSuppression(mGroupMap.get(entry.getSbn().getGroupKey()));
Kevin01a53cb2018-11-09 18:19:54 -0800555 for (OnGroupChangeListener listener : mListeners) {
556 listener.onGroupsChanged();
557 }
Kevina97ea052018-09-11 13:53:18 -0700558 }
559
560 /**
561 * Stop isolating a notification and re-group it with its original logical group.
562 *
563 * @param entry the notification to un-isolate
564 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500565 private void stopIsolatingNotification(NotificationEntry entry) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400566 StatusBarNotification sbn = entry.getSbn();
Kevina97ea052018-09-11 13:53:18 -0700567 if (mIsolatedEntries.containsKey(sbn.getKey())) {
568 // not isolated anymore, we need to update the groups
Ned Burns00b4b2d2019-10-17 22:09:27 -0400569 onEntryRemovedInternal(entry, entry.getSbn());
Kevina97ea052018-09-11 13:53:18 -0700570 mIsolatedEntries.remove(sbn.getKey());
571 onEntryAdded(entry);
Kevin01a53cb2018-11-09 18:19:54 -0800572 for (OnGroupChangeListener listener : mListeners) {
573 listener.onGroupsChanged();
574 }
Kevina97ea052018-09-11 13:53:18 -0700575 }
Selim Cineka6c0cef2016-03-18 11:42:25 -0700576 }
577
578 private boolean isGroupNotFullyVisible(NotificationGroup notificationGroup) {
579 return notificationGroup.summary == null
Evan Laird94492852018-10-25 13:43:01 -0400580 || notificationGroup.summary.isGroupNotFullyVisible();
Selim Cineka6c0cef2016-03-18 11:42:25 -0700581 }
582
Selim Cinek967ed2a2016-04-08 18:29:11 -0700583 public void setHeadsUpManager(HeadsUpManager headsUpManager) {
584 mHeadsUpManager = headsUpManager;
585 }
586
Selim Cinek52941c52016-05-07 18:29:32 -0400587 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
588 pw.println("GroupManager state:");
589 pw.println(" number of groups: " + mGroupMap.size());
590 for (Map.Entry<String, NotificationGroup> entry : mGroupMap.entrySet()) {
591 pw.println("\n key: " + entry.getKey()); pw.println(entry.getValue());
592 }
593 pw.println("\n isolated entries: " + mIsolatedEntries.size());
594 for (Map.Entry<String, StatusBarNotification> entry : mIsolatedEntries.entrySet()) {
595 pw.print(" "); pw.print(entry.getKey());
596 pw.print(", "); pw.println(entry.getValue());
597 }
598 }
599
Evan Laird878c8532018-10-15 15:54:29 -0400600 @Override
601 public void onStateChanged(int newState) {
602 setStatusBarState(newState);
603 }
604
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100605 public static class NotificationGroup {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500606 public final HashMap<String, NotificationEntry> children = new HashMap<>();
607 public NotificationEntry summary;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100608 public boolean expanded;
Selim Cinek2a739342016-03-17 10:28:55 -0700609 /**
610 * Is this notification group suppressed, i.e its summary is hidden
611 */
612 public boolean suppressed;
Selim Cinek52941c52016-05-07 18:29:32 -0400613
614 @Override
615 public String toString() {
Selim Cinek60ca7872016-05-12 11:24:07 -0700616 String result = " summary:\n "
Ned Burns00b4b2d2019-10-17 22:09:27 -0400617 + (summary != null ? summary.getSbn() : "null")
Selim Cinek04be3892017-08-08 10:58:32 -0700618 + (summary != null && summary.getDebugThrowable() != null
619 ? Log.getStackTraceString(summary.getDebugThrowable())
620 : "");
Selim Cinek52941c52016-05-07 18:29:32 -0400621 result += "\n children size: " + children.size();
Ned Burnsf81c4c42019-01-07 14:10:43 -0500622 for (NotificationEntry child : children.values()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400623 result += "\n " + child.getSbn()
Selim Cinek04be3892017-08-08 10:58:32 -0700624 + (child.getDebugThrowable() != null
625 ? Log.getStackTraceString(child.getDebugThrowable())
626 : "");
Selim Cinek52941c52016-05-07 18:29:32 -0400627 }
Mady Mellor740d85d2019-07-09 15:26:47 -0700628 result += "\n summary suppressed: " + suppressed;
Selim Cinek52941c52016-05-07 18:29:32 -0400629 return result;
630 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100631 }
632
633 public interface OnGroupChangeListener {
Kevin01a53cb2018-11-09 18:19:54 -0800634
635 /**
636 * A new group has been created.
637 *
638 * @param group the group that was created
639 * @param groupKey the group's key
640 */
641 default void onGroupCreated(NotificationGroup group, String groupKey) {}
642
643 /**
644 * A group has been removed.
645 *
646 * @param group the group that was removed
647 * @param groupKey the group's key
648 */
649 default void onGroupRemoved(NotificationGroup group, String groupKey) {}
650
651 /**
652 * The suppression of a group has changed.
653 *
654 * @param group the group that has changed
655 * @param suppressed true if the group is now suppressed, false o/w
656 */
657 default void onGroupSuppressionChanged(NotificationGroup group, boolean suppressed) {}
658
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100659 /**
660 * The expansion of a group has changed.
661 *
662 * @param changedRow the row for which the expansion has changed, which is also the summary
663 * @param expanded a boolean indicating the new expanded state
664 */
Kevin01a53cb2018-11-09 18:19:54 -0800665 default void onGroupExpansionChanged(ExpandableNotificationRow changedRow,
666 boolean expanded) {}
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100667
668 /**
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100669 * A group of children just received a summary notification and should therefore become
670 * children of it.
671 *
672 * @param group the group created
673 */
Kevin01a53cb2018-11-09 18:19:54 -0800674 default void onGroupCreatedFromChildren(NotificationGroup group) {}
Selim Cinekef5127e2015-12-21 16:55:58 -0800675
676 /**
Selim Cinek2a739342016-03-17 10:28:55 -0700677 * The groups have changed. This can happen if the isolation of a child has changes or if a
678 * group became suppressed / unsuppressed
Selim Cinekef5127e2015-12-21 16:55:58 -0800679 */
Kevin01a53cb2018-11-09 18:19:54 -0800680 default void onGroupsChanged() {}
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100681 }
682}