blob: adaea9379c71c8081d9fdb5a0e8d6f465b58c1ff [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) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100106 onEntryRemovedInternal(removed, removed.notification);
Selim Cinek52941c52016-05-07 18:29:32 -0400107 mIsolatedEntries.remove(removed.key);
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) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800119 String groupKey = getGroupKey(sbn);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100120 final NotificationGroup group = mGroupMap.get(groupKey);
Selim Cinek0b4aeab2015-09-01 17:07:38 -0700121 if (group == null) {
122 // When an app posts 2 different notifications as summary of the same group, then a
123 // cancellation of the first notification removes this group.
124 // This situation is not supported and we will not allow such notifications anymore in
125 // the close future. See b/23676310 for reference.
126 return;
127 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800128 if (isGroupChild(sbn)) {
Selim Cinek04be3892017-08-08 10:58:32 -0700129 group.children.remove(removed.key);
Selim Cineke73ad212015-11-03 19:11:08 -0800130 } else {
131 group.summary = null;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100132 }
Selim Cinek2a739342016-03-17 10:28:55 -0700133 updateSuppression(group);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100134 if (group.children.isEmpty()) {
135 if (group.summary == null) {
136 mGroupMap.remove(groupKey);
Kevin01a53cb2018-11-09 18:19:54 -0800137 for (OnGroupChangeListener listener : mListeners) {
138 listener.onGroupRemoved(group, groupKey);
139 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100140 }
141 }
142 }
143
Ned Burnsf81c4c42019-01-07 14:10:43 -0500144 public void onEntryAdded(final NotificationEntry added) {
Evan Laird94492852018-10-25 13:43:01 -0400145 if (added.isRowRemoved()) {
Selim Cinek04be3892017-08-08 10:58:32 -0700146 added.setDebugThrowable(new Throwable());
147 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800148 final StatusBarNotification sbn = added.notification;
149 boolean isGroupChild = isGroupChild(sbn);
150 String groupKey = getGroupKey(sbn);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100151 NotificationGroup group = mGroupMap.get(groupKey);
152 if (group == null) {
153 group = new NotificationGroup();
154 mGroupMap.put(groupKey, group);
Kevin01a53cb2018-11-09 18:19:54 -0800155 for (OnGroupChangeListener listener : mListeners) {
156 listener.onGroupCreated(group, groupKey);
157 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100158 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800159 if (isGroupChild) {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500160 NotificationEntry existing = group.children.get(added.key);
Selim Cinek04be3892017-08-08 10:58:32 -0700161 if (existing != null && existing != added) {
162 Throwable existingThrowable = existing.getDebugThrowable();
163 Log.wtf(TAG, "Inconsistent entries found with the same key " + added.key
Evan Laird94492852018-10-25 13:43:01 -0400164 + "existing removed: " + existing.isRowRemoved()
Selim Cinek04be3892017-08-08 10:58:32 -0700165 + (existingThrowable != null
166 ? Log.getStackTraceString(existingThrowable) + "\n": "")
Evan Laird94492852018-10-25 13:43:01 -0400167 + " added removed" + added.isRowRemoved()
Selim Cinek04be3892017-08-08 10:58:32 -0700168 , new Throwable());
169 }
170 group.children.put(added.key, added);
Selim Cinek2a739342016-03-17 10:28:55 -0700171 updateSuppression(group);
Selim Cineke73ad212015-11-03 19:11:08 -0800172 } else {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100173 group.summary = added;
Evan Laird94492852018-10-25 13:43:01 -0400174 group.expanded = added.areChildrenExpanded();
Selim Cinek2a739342016-03-17 10:28:55 -0700175 updateSuppression(group);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100176 if (!group.children.isEmpty()) {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500177 ArrayList<NotificationEntry> childrenCopy
Selim Cinek04be3892017-08-08 10:58:32 -0700178 = new ArrayList<>(group.children.values());
Ned Burnsf81c4c42019-01-07 14:10:43 -0500179 for (NotificationEntry child : childrenCopy) {
Selim Cinek50e74672016-05-05 15:58:10 -0400180 onEntryBecomingChild(child);
181 }
Kevin01a53cb2018-11-09 18:19:54 -0800182 for (OnGroupChangeListener listener : mListeners) {
183 listener.onGroupCreatedFromChildren(group);
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700184 }
185 }
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700186 }
187 }
188
Ned Burnsf81c4c42019-01-07 14:10:43 -0500189 private void onEntryBecomingChild(NotificationEntry entry) {
Kevina97ea052018-09-11 13:53:18 -0700190 if (shouldIsolate(entry)) {
191 isolateNotification(entry);
Selim Cinek50e74672016-05-05 15:58:10 -0400192 }
193 }
194
Selim Cinek2a739342016-03-17 10:28:55 -0700195 private void updateSuppression(NotificationGroup group) {
Selim Cinek80c44dd2016-08-15 19:39:55 -0700196 if (group == null) {
Selim Cinek23c80342016-03-17 18:27:36 -0700197 return;
198 }
Mady Mellor740d85d2019-07-09 15:26:47 -0700199 int childCount = 0;
200 boolean hasBubbles = false;
201 for (String key : group.children.keySet()) {
202 if (!getBubbleController().isBubbleNotificationSuppressedFromShade(key)) {
203 childCount++;
204 } else {
205 hasBubbles = true;
206 }
207 }
208
Selim Cinek2a739342016-03-17 10:28:55 -0700209 boolean prevSuppressed = group.suppressed;
Selim Cinek23c80342016-03-17 18:27:36 -0700210 group.suppressed = group.summary != null && !group.expanded
Mady Mellor740d85d2019-07-09 15:26:47 -0700211 && (childCount == 1
212 || (childCount == 0
Julia Reynoldse46bb372016-03-17 11:05:58 -0400213 && group.summary.notification.getNotification().isGroupSummary()
Mady Mellor740d85d2019-07-09 15:26:47 -0700214 && (hasIsolatedChildren(group) || hasBubbles)));
Selim Cinek2a739342016-03-17 10:28:55 -0700215 if (prevSuppressed != group.suppressed) {
Kevin01a53cb2018-11-09 18:19:54 -0800216 for (OnGroupChangeListener listener : mListeners) {
217 if (!mIsUpdatingUnchangedGroup) {
218 listener.onGroupSuppressionChanged(group, group.suppressed);
219 listener.onGroupsChanged();
Kevina97ea052018-09-11 13:53:18 -0700220 }
Selim Cinek967ed2a2016-04-08 18:29:11 -0700221 }
Selim Cinek2a739342016-03-17 10:28:55 -0700222 }
223 }
224
Selim Cinek23c80342016-03-17 18:27:36 -0700225 private boolean hasIsolatedChildren(NotificationGroup group) {
226 return getNumberOfIsolatedChildren(group.summary.notification.getGroupKey()) != 0;
227 }
228
229 private int getNumberOfIsolatedChildren(String groupKey) {
230 int count = 0;
Selim Cineka6c0cef2016-03-18 11:42:25 -0700231 for (StatusBarNotification sbn : mIsolatedEntries.values()) {
Selim Cinek23c80342016-03-17 18:27:36 -0700232 if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) {
233 count++;
234 }
235 }
236 return count;
237 }
238
Ned Burnsf81c4c42019-01-07 14:10:43 -0500239 private NotificationEntry getIsolatedChild(String groupKey) {
Selim Cinek967ed2a2016-04-08 18:29:11 -0700240 for (StatusBarNotification sbn : mIsolatedEntries.values()) {
241 if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) {
242 return mGroupMap.get(sbn.getKey()).summary;
243 }
244 }
245 return null;
246 }
247
Ned Burnsf81c4c42019-01-07 14:10:43 -0500248 public void onEntryUpdated(NotificationEntry entry,
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100249 StatusBarNotification oldNotification) {
Selim Cinek68bdff12016-08-03 13:38:56 -0700250 String oldKey = oldNotification.getGroupKey();
251 String newKey = entry.notification.getGroupKey();
252 boolean groupKeysChanged = !oldKey.equals(newKey);
253 boolean wasGroupChild = isGroupChild(oldNotification);
254 boolean isGroupChild = isGroupChild(entry.notification);
Selim Cinek80c44dd2016-08-15 19:39:55 -0700255 mIsUpdatingUnchangedGroup = !groupKeysChanged && wasGroupChild == isGroupChild;
Selim Cinekef5127e2015-12-21 16:55:58 -0800256 if (mGroupMap.get(getGroupKey(oldNotification)) != null) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100257 onEntryRemovedInternal(entry, oldNotification);
258 }
259 onEntryAdded(entry);
Selim Cinek80c44dd2016-08-15 19:39:55 -0700260 mIsUpdatingUnchangedGroup = false;
Selim Cineka6c0cef2016-03-18 11:42:25 -0700261 if (isIsolated(entry.notification)) {
262 mIsolatedEntries.put(entry.key, entry.notification);
Selim Cinek68bdff12016-08-03 13:38:56 -0700263 if (groupKeysChanged) {
Selim Cinek23c80342016-03-17 18:27:36 -0700264 updateSuppression(mGroupMap.get(oldKey));
265 updateSuppression(mGroupMap.get(newKey));
266 }
Selim Cinek68bdff12016-08-03 13:38:56 -0700267 } else if (!wasGroupChild && isGroupChild) {
Selim Cinek50e74672016-05-05 15:58:10 -0400268 onEntryBecomingChild(entry);
Selim Cinek23c80342016-03-17 18:27:36 -0700269 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100270 }
271
Selim Cinek2a739342016-03-17 10:28:55 -0700272 public boolean isSummaryOfSuppressedGroup(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700273 return isGroupSuppressed(getGroupKey(sbn)) && sbn.getNotification().isGroupSummary();
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100274 }
275
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700276 private boolean isOnlyChild(StatusBarNotification sbn) {
Selim Cinek36b02232016-05-11 23:07:05 -0400277 return !sbn.getNotification().isGroupSummary()
Selim Cinek23c80342016-03-17 18:27:36 -0700278 && getTotalNumberOfChildren(sbn) == 1;
Selim Cinek2a739342016-03-17 10:28:55 -0700279 }
280
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700281 public boolean isOnlyChildInGroup(StatusBarNotification sbn) {
Selim Cinek88086e72016-06-17 21:01:32 -0700282 if (!isOnlyChild(sbn)) {
283 return false;
284 }
Ned Burnsf81c4c42019-01-07 14:10:43 -0500285 NotificationEntry logicalGroupSummary = getLogicalGroupSummary(sbn);
Selim Cinek88086e72016-06-17 21:01:32 -0700286 return logicalGroupSummary != null
Evan Laird94492852018-10-25 13:43:01 -0400287 && !logicalGroupSummary.notification.equals(sbn);
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700288 }
289
Selim Cinek23c80342016-03-17 18:27:36 -0700290 private int getTotalNumberOfChildren(StatusBarNotification sbn) {
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700291 int isolatedChildren = getNumberOfIsolatedChildren(sbn.getGroupKey());
292 NotificationGroup group = mGroupMap.get(sbn.getGroupKey());
293 int realChildren = group != null ? group.children.size() : 0;
294 return isolatedChildren + realChildren;
Selim Cinek23c80342016-03-17 18:27:36 -0700295 }
296
297 private boolean isGroupSuppressed(String groupKey) {
298 NotificationGroup group = mGroupMap.get(groupKey);
Selim Cinek2a739342016-03-17 10:28:55 -0700299 return group != null && group.suppressed;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100300 }
301
Jason Monk1fd3fc32018-08-14 17:20:09 -0400302 private void setStatusBarState(int newState) {
Selim Cinek9c4c4142015-12-04 16:44:56 -0800303 mBarState = newState;
304 if (mBarState == StatusBarState.KEYGUARD) {
Selim Cinek9184f9c2016-02-02 17:36:53 -0800305 collapseAllGroups();
306 }
307 }
308
309 public void collapseAllGroups() {
Selim Cinekc0b14b02016-05-09 13:12:40 -0400310 // Because notifications can become isolated when the group becomes suppressed it can
311 // lead to concurrent modifications while looping. We need to make a copy.
312 ArrayList<NotificationGroup> groupCopy = new ArrayList<>(mGroupMap.values());
313 int size = groupCopy.size();
314 for (int i = 0; i < size; i++) {
315 NotificationGroup group = groupCopy.get(i);
Selim Cinek9184f9c2016-02-02 17:36:53 -0800316 if (group.expanded) {
317 setGroupExpanded(group, false);
Selim Cinek9c4c4142015-12-04 16:44:56 -0800318 }
Selim Cinek2a739342016-03-17 10:28:55 -0700319 updateSuppression(group);
Selim Cinek9c4c4142015-12-04 16:44:56 -0800320 }
321 }
322
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100323 /**
324 * @return whether a given notification is a child in a group which has a summary
325 */
326 public boolean isChildInGroupWithSummary(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800327 if (!isGroupChild(sbn)) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100328 return false;
329 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800330 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek2a739342016-03-17 10:28:55 -0700331 if (group == null || group.summary == null || group.suppressed) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100332 return false;
333 }
Selim Cinek3f19f602016-05-02 18:01:56 -0700334 if (group.children.isEmpty()) {
335 // If the suppression of a group changes because the last child was removed, this can
336 // still be called temporarily because the child hasn't been fully removed yet. Let's
337 // make sure we still return false in that case.
338 return false;
339 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100340 return true;
341 }
342
Selim Cinek263398f2015-10-21 17:40:23 -0700343 /**
344 * @return whether a given notification is a summary in a group which has children
345 */
346 public boolean isSummaryOfGroup(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800347 if (!isGroupSummary(sbn)) {
Selim Cinek263398f2015-10-21 17:40:23 -0700348 return false;
349 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800350 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Kevin96eed642018-09-05 18:53:33 -0700351 if (group == null || group.summary == null) {
Selim Cinek263398f2015-10-21 17:40:23 -0700352 return false;
353 }
Kevin96eed642018-09-05 18:53:33 -0700354 return !group.children.isEmpty() && Objects.equals(group.summary.notification, sbn);
Selim Cinek263398f2015-10-21 17:40:23 -0700355 }
356
Selim Cinek23c80342016-03-17 18:27:36 -0700357 /**
358 * Get the summary of a specified status bar notification. For isolated notification this return
359 * itself.
360 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500361 public NotificationEntry getGroupSummary(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700362 return getGroupSummary(getGroupKey(sbn));
363 }
364
365 /**
366 * Similar to {@link #getGroupSummary(StatusBarNotification)} but doesn't get the visual summary
367 * but the logical summary, i.e when a child is isolated, it still returns the summary as if
368 * it wasn't isolated.
369 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500370 public NotificationEntry getLogicalGroupSummary(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700371 return getGroupSummary(sbn.getGroupKey());
372 }
373
374 @Nullable
Ned Burnsf81c4c42019-01-07 14:10:43 -0500375 private NotificationEntry getGroupSummary(String groupKey) {
Selim Cinek23c80342016-03-17 18:27:36 -0700376 NotificationGroup group = mGroupMap.get(groupKey);
Evan Laird94492852018-10-25 13:43:01 -0400377 //TODO: see if this can become an Entry
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100378 return group == null ? null
379 : group.summary == null ? null
Evan Laird94492852018-10-25 13:43:01 -0400380 : group.summary;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100381 }
382
Kevin01a53cb2018-11-09 18:19:54 -0800383 /**
384 * Get the children that are logically in the summary's group, whether or not they are isolated.
385 *
386 * @param summary summary of a group
387 * @return list of the children
388 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500389 public ArrayList<NotificationEntry> getLogicalChildren(StatusBarNotification summary) {
Kevin01a53cb2018-11-09 18:19:54 -0800390 NotificationGroup group = mGroupMap.get(summary.getGroupKey());
391 if (group == null) {
392 return null;
393 }
Ned Burnsf81c4c42019-01-07 14:10:43 -0500394 ArrayList<NotificationEntry> children = new ArrayList<>(group.children.values());
395 NotificationEntry isolatedChild = getIsolatedChild(summary.getGroupKey());
Kevin01a53cb2018-11-09 18:19:54 -0800396 if (isolatedChild != null) {
397 children.add(isolatedChild);
398 }
399 return children;
400 }
401
402 /**
Mady Mellor740d85d2019-07-09 15:26:47 -0700403 * If there is a {@link NotificationGroup} associated with the provided entry, this method
404 * will update the suppression of that group.
405 */
406 public void updateSuppression(NotificationEntry entry) {
407 NotificationGroup group = mGroupMap.get(getGroupKey(entry.notification));
408 if (group != null) {
409 updateSuppression(group);
410 }
411 }
412
413 /**
Kevin01a53cb2018-11-09 18:19:54 -0800414 * Get the group key. May differ from the one in the notification due to the notification
415 * being temporarily isolated.
416 *
417 * @param sbn notification to check
418 * @return the key of the notification
419 */
420 public String getGroupKey(StatusBarNotification sbn) {
421 if (isIsolated(sbn)) {
422 return sbn.getKey();
423 }
424 return sbn.getGroupKey();
425 }
426
Chris Wren698b1702016-05-23 11:16:32 -0400427 /** @return group expansion state after toggling. */
428 public boolean toggleGroupExpansion(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800429 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek83bc7832015-10-22 13:26:54 -0700430 if (group == null) {
Chris Wren698b1702016-05-23 11:16:32 -0400431 return false;
Selim Cinek83bc7832015-10-22 13:26:54 -0700432 }
433 setGroupExpanded(group, !group.expanded);
Chris Wren698b1702016-05-23 11:16:32 -0400434 return group.expanded;
Selim Cinek83bc7832015-10-22 13:26:54 -0700435 }
436
Selim Cinekef5127e2015-12-21 16:55:58 -0800437 private boolean isIsolated(StatusBarNotification sbn) {
Selim Cineka6c0cef2016-03-18 11:42:25 -0700438 return mIsolatedEntries.containsKey(sbn.getKey());
Selim Cinekef5127e2015-12-21 16:55:58 -0800439 }
440
Kevin01a53cb2018-11-09 18:19:54 -0800441 /**
442 * Whether a notification is visually a group summary.
443 *
444 * @param sbn notification to check
445 * @return true if it is visually a group summary
446 */
447 public boolean isGroupSummary(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800448 if (isIsolated(sbn)) {
449 return true;
450 }
451 return sbn.getNotification().isGroupSummary();
452 }
Julia Reynoldse46bb372016-03-17 11:05:58 -0400453
Kevin01a53cb2018-11-09 18:19:54 -0800454 /**
455 * Whether a notification is visually a group child.
456 *
457 * @param sbn notification to check
458 * @return true if it is visually a group child
459 */
460 public boolean isGroupChild(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800461 if (isIsolated(sbn)) {
462 return false;
463 }
Julia Reynoldse46bb372016-03-17 11:05:58 -0400464 return sbn.isGroup() && !sbn.getNotification().isGroupSummary();
Selim Cinekef5127e2015-12-21 16:55:58 -0800465 }
466
Selim Cinekef5127e2015-12-21 16:55:58 -0800467 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500468 public void onHeadsUpStateChanged(NotificationEntry entry, boolean isHeadsUp) {
Kevin01a53cb2018-11-09 18:19:54 -0800469 onAlertStateChanged(entry, isHeadsUp);
Kevina97ea052018-09-11 13:53:18 -0700470 }
471
Ned Burnsf81c4c42019-01-07 14:10:43 -0500472 private void onAlertStateChanged(NotificationEntry entry, boolean isAlerting) {
Kevina97ea052018-09-11 13:53:18 -0700473 if (isAlerting) {
474 if (shouldIsolate(entry)) {
475 isolateNotification(entry);
Selim Cinekef5127e2015-12-21 16:55:58 -0800476 }
477 } else {
Kevina97ea052018-09-11 13:53:18 -0700478 stopIsolatingNotification(entry);
Selim Cinekef5127e2015-12-21 16:55:58 -0800479 }
480 }
481
Kevina97ea052018-09-11 13:53:18 -0700482 /**
Kevina97ea052018-09-11 13:53:18 -0700483 * Whether a notification that is normally part of a group should be temporarily isolated from
484 * the group and put in their own group visually. This generally happens when the notification
485 * is alerting.
486 *
487 * @param entry the notification to check
488 * @return true if the entry should be isolated
489 */
490
Ned Burnsf81c4c42019-01-07 14:10:43 -0500491 private boolean shouldIsolate(NotificationEntry entry) {
Kevina97ea052018-09-11 13:53:18 -0700492 StatusBarNotification sbn = entry.notification;
Selim Cineka6c0cef2016-03-18 11:42:25 -0700493 NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey());
Kevina97ea052018-09-11 13:53:18 -0700494 if (!sbn.isGroup() || sbn.getNotification().isGroupSummary()) {
495 return false;
496 }
jorgegil@google.com13fc8782019-07-15 10:27:35 -0700497 if (mHeadsUpManager != null && !mHeadsUpManager.isAlerting(entry.key)) {
Kevina97ea052018-09-11 13:53:18 -0700498 return false;
499 }
500 return (sbn.getNotification().fullScreenIntent != null
501 || notificationGroup == null
502 || !notificationGroup.expanded
503 || isGroupNotFullyVisible(notificationGroup));
504 }
505
506 /**
507 * Isolate a notification from its group so that it visually shows as its own group.
508 *
509 * @param entry the notification to isolate
510 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500511 private void isolateNotification(NotificationEntry entry) {
Kevina97ea052018-09-11 13:53:18 -0700512 StatusBarNotification sbn = entry.notification;
513
514 // We will be isolated now, so lets update the groups
515 onEntryRemovedInternal(entry, entry.notification);
516
517 mIsolatedEntries.put(sbn.getKey(), sbn);
518
519 onEntryAdded(entry);
520 // We also need to update the suppression of the old group, because this call comes
521 // even before the groupManager knows about the notification at all.
522 // When the notification gets added afterwards it is already isolated and therefore
523 // it doesn't lead to an update.
524 updateSuppression(mGroupMap.get(entry.notification.getGroupKey()));
Kevin01a53cb2018-11-09 18:19:54 -0800525 for (OnGroupChangeListener listener : mListeners) {
526 listener.onGroupsChanged();
527 }
Kevina97ea052018-09-11 13:53:18 -0700528 }
529
530 /**
531 * Stop isolating a notification and re-group it with its original logical group.
532 *
533 * @param entry the notification to un-isolate
534 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500535 private void stopIsolatingNotification(NotificationEntry entry) {
Kevina97ea052018-09-11 13:53:18 -0700536 StatusBarNotification sbn = entry.notification;
537 if (mIsolatedEntries.containsKey(sbn.getKey())) {
538 // not isolated anymore, we need to update the groups
539 onEntryRemovedInternal(entry, entry.notification);
540 mIsolatedEntries.remove(sbn.getKey());
541 onEntryAdded(entry);
Kevin01a53cb2018-11-09 18:19:54 -0800542 for (OnGroupChangeListener listener : mListeners) {
543 listener.onGroupsChanged();
544 }
Kevina97ea052018-09-11 13:53:18 -0700545 }
Selim Cineka6c0cef2016-03-18 11:42:25 -0700546 }
547
548 private boolean isGroupNotFullyVisible(NotificationGroup notificationGroup) {
549 return notificationGroup.summary == null
Evan Laird94492852018-10-25 13:43:01 -0400550 || notificationGroup.summary.isGroupNotFullyVisible();
Selim Cineka6c0cef2016-03-18 11:42:25 -0700551 }
552
Selim Cinek967ed2a2016-04-08 18:29:11 -0700553 public void setHeadsUpManager(HeadsUpManager headsUpManager) {
554 mHeadsUpManager = headsUpManager;
555 }
556
Selim Cinek52941c52016-05-07 18:29:32 -0400557 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
558 pw.println("GroupManager state:");
559 pw.println(" number of groups: " + mGroupMap.size());
560 for (Map.Entry<String, NotificationGroup> entry : mGroupMap.entrySet()) {
561 pw.println("\n key: " + entry.getKey()); pw.println(entry.getValue());
562 }
563 pw.println("\n isolated entries: " + mIsolatedEntries.size());
564 for (Map.Entry<String, StatusBarNotification> entry : mIsolatedEntries.entrySet()) {
565 pw.print(" "); pw.print(entry.getKey());
566 pw.print(", "); pw.println(entry.getValue());
567 }
568 }
569
Evan Laird878c8532018-10-15 15:54:29 -0400570 @Override
571 public void onStateChanged(int newState) {
572 setStatusBarState(newState);
573 }
574
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100575 public static class NotificationGroup {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500576 public final HashMap<String, NotificationEntry> children = new HashMap<>();
577 public NotificationEntry summary;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100578 public boolean expanded;
Selim Cinek2a739342016-03-17 10:28:55 -0700579 /**
580 * Is this notification group suppressed, i.e its summary is hidden
581 */
582 public boolean suppressed;
Selim Cinek52941c52016-05-07 18:29:32 -0400583
584 @Override
585 public String toString() {
Selim Cinek60ca7872016-05-12 11:24:07 -0700586 String result = " summary:\n "
Selim Cinek04be3892017-08-08 10:58:32 -0700587 + (summary != null ? summary.notification : "null")
588 + (summary != null && summary.getDebugThrowable() != null
589 ? Log.getStackTraceString(summary.getDebugThrowable())
590 : "");
Selim Cinek52941c52016-05-07 18:29:32 -0400591 result += "\n children size: " + children.size();
Ned Burnsf81c4c42019-01-07 14:10:43 -0500592 for (NotificationEntry child : children.values()) {
Selim Cinek04be3892017-08-08 10:58:32 -0700593 result += "\n " + child.notification
594 + (child.getDebugThrowable() != null
595 ? Log.getStackTraceString(child.getDebugThrowable())
596 : "");
Selim Cinek52941c52016-05-07 18:29:32 -0400597 }
Mady Mellor740d85d2019-07-09 15:26:47 -0700598 result += "\n summary suppressed: " + suppressed;
Selim Cinek52941c52016-05-07 18:29:32 -0400599 return result;
600 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100601 }
602
603 public interface OnGroupChangeListener {
Kevin01a53cb2018-11-09 18:19:54 -0800604
605 /**
606 * A new group has been created.
607 *
608 * @param group the group that was created
609 * @param groupKey the group's key
610 */
611 default void onGroupCreated(NotificationGroup group, String groupKey) {}
612
613 /**
614 * A group has been removed.
615 *
616 * @param group the group that was removed
617 * @param groupKey the group's key
618 */
619 default void onGroupRemoved(NotificationGroup group, String groupKey) {}
620
621 /**
622 * The suppression of a group has changed.
623 *
624 * @param group the group that has changed
625 * @param suppressed true if the group is now suppressed, false o/w
626 */
627 default void onGroupSuppressionChanged(NotificationGroup group, boolean suppressed) {}
628
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100629 /**
630 * The expansion of a group has changed.
631 *
632 * @param changedRow the row for which the expansion has changed, which is also the summary
633 * @param expanded a boolean indicating the new expanded state
634 */
Kevin01a53cb2018-11-09 18:19:54 -0800635 default void onGroupExpansionChanged(ExpandableNotificationRow changedRow,
636 boolean expanded) {}
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100637
638 /**
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100639 * A group of children just received a summary notification and should therefore become
640 * children of it.
641 *
642 * @param group the group created
643 */
Kevin01a53cb2018-11-09 18:19:54 -0800644 default void onGroupCreatedFromChildren(NotificationGroup group) {}
Selim Cinekef5127e2015-12-21 16:55:58 -0800645
646 /**
Selim Cinek2a739342016-03-17 10:28:55 -0700647 * The groups have changed. This can happen if the isolation of a child has changes or if a
648 * group became suppressed / unsuppressed
Selim Cinekef5127e2015-12-21 16:55:58 -0800649 */
Kevin01a53cb2018-11-09 18:19:54 -0800650 default void onGroupsChanged() {}
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100651 }
652}