blob: d0a872eb112fc25b975c2792890cc940a21d0486 [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
Pinyao Ting293b83d2020-05-06 17:10:56 -070019import android.annotation.NonNull;
Kevin01a53cb2018-11-09 18:19:54 -080020import android.annotation.Nullable;
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;
Steve Elliottd38a70f2020-05-11 14:01:08 -040031import com.android.systemui.statusbar.notification.people.PeopleNotificationIdentifier;
Evan Laird878c8532018-10-15 15:54:29 -040032import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
Selim Cinekef5127e2015-12-21 16:55:58 -080033import com.android.systemui.statusbar.policy.HeadsUpManager;
Selim Cineka7d4f822016-12-06 14:34:47 -080034import com.android.systemui.statusbar.policy.OnHeadsUpChangedListener;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010035
Selim Cinek52941c52016-05-07 18:29:32 -040036import java.io.FileDescriptor;
37import java.io.PrintWriter;
Selim Cinekc0b14b02016-05-09 13:12:40 -040038import java.util.ArrayList;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010039import java.util.HashMap;
Selim Cinek52941c52016-05-07 18:29:32 -040040import java.util.Map;
Selim Cinekf93bf3e2018-05-08 14:43:21 -070041import java.util.Objects;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010042
Jason Monk27d01a622018-12-10 15:57:09 -050043import javax.inject.Inject;
44import javax.inject.Singleton;
45
Steve Elliottd38a70f2020-05-11 14:01:08 -040046import dagger.Lazy;
47
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010048/**
49 * A class to handle notifications and their corresponding groups.
50 */
Jason Monk27d01a622018-12-10 15:57:09 -050051@Singleton
Selim Cinekc3fec682019-06-06 18:11:07 -070052public class NotificationGroupManager implements OnHeadsUpChangedListener, StateListener {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010053
Selim Cinek04be3892017-08-08 10:58:32 -070054 private static final String TAG = "NotificationGroupManager";
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010055 private final HashMap<String, NotificationGroup> mGroupMap = new HashMap<>();
Kevin01a53cb2018-11-09 18:19:54 -080056 private final ArraySet<OnGroupChangeListener> mListeners = new ArraySet<>();
Steve Elliottd38a70f2020-05-11 14:01:08 -040057 private final Lazy<PeopleNotificationIdentifier> mPeopleNotificationIdentifier;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010058 private int mBarState = -1;
Selim Cineka6c0cef2016-03-18 11:42:25 -070059 private HashMap<String, StatusBarNotification> mIsolatedEntries = new HashMap<>();
Selim Cinek967ed2a2016-04-08 18:29:11 -070060 private HeadsUpManager mHeadsUpManager;
Selim Cinek80c44dd2016-08-15 19:39:55 -070061 private boolean mIsUpdatingUnchangedGroup;
Mady Mellor740d85d2019-07-09 15:26:47 -070062 @Nullable private BubbleController mBubbleController = null;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010063
Jason Monk27d01a622018-12-10 15:57:09 -050064 @Inject
Steve Elliottd38a70f2020-05-11 14:01:08 -040065 public NotificationGroupManager(
66 StatusBarStateController statusBarStateController,
67 Lazy<PeopleNotificationIdentifier> peopleNotificationIdentifier) {
Selim Cinekc3fec682019-06-06 18:11:07 -070068 statusBarStateController.addCallback(this);
Steve Elliottd38a70f2020-05-11 14:01:08 -040069 mPeopleNotificationIdentifier = peopleNotificationIdentifier;
Jason Monk1fd3fc32018-08-14 17:20:09 -040070 }
71
Mady Mellor740d85d2019-07-09 15:26:47 -070072 private BubbleController getBubbleController() {
73 if (mBubbleController == null) {
74 mBubbleController = Dependency.get(BubbleController.class);
75 }
76 return mBubbleController;
77 }
78
Kevin01a53cb2018-11-09 18:19:54 -080079 /**
80 * Add a listener for changes to groups.
81 *
82 * @param listener listener to add
83 */
84 public void addOnGroupChangeListener(OnGroupChangeListener listener) {
85 mListeners.add(listener);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010086 }
87
88 public boolean isGroupExpanded(StatusBarNotification sbn) {
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 false;
92 }
93 return group.expanded;
94 }
95
Selim Cinekba069ae2020-04-01 19:45:16 -070096 /**
97 * @return if the group that this notification is associated with logically is expanded
98 */
99 public boolean isLogicalGroupExpanded(StatusBarNotification sbn) {
100 NotificationGroup group = mGroupMap.get(sbn.getGroupKey());
101 if (group == null) {
102 return false;
103 }
104 return group.expanded;
105 }
106
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100107 public void setGroupExpanded(StatusBarNotification sbn, boolean expanded) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800108 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100109 if (group == null) {
110 return;
111 }
112 setGroupExpanded(group, expanded);
113 }
114
115 private void setGroupExpanded(NotificationGroup group, boolean expanded) {
116 group.expanded = expanded;
117 if (group.summary != null) {
Kevin01a53cb2018-11-09 18:19:54 -0800118 for (OnGroupChangeListener listener : mListeners) {
Evan Laird94492852018-10-25 13:43:01 -0400119 listener.onGroupExpansionChanged(group.summary.getRow(), expanded);
Kevin01a53cb2018-11-09 18:19:54 -0800120 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100121 }
122 }
123
Ned Burnsf81c4c42019-01-07 14:10:43 -0500124 public void onEntryRemoved(NotificationEntry removed) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400125 onEntryRemovedInternal(removed, removed.getSbn());
126 mIsolatedEntries.remove(removed.getKey());
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100127 }
128
129 /**
130 * An entry was removed.
131 *
132 * @param removed the removed entry
133 * @param sbn the notification the entry has, which doesn't need to be the same as it's internal
134 * notification
135 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500136 private void onEntryRemovedInternal(NotificationEntry removed,
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100137 final StatusBarNotification sbn) {
Beverlyc5094122020-01-09 17:05:15 -0500138 onEntryRemovedInternal(removed, sbn.getGroupKey(), sbn.isGroup(),
139 sbn.getNotification().isGroupSummary());
140 }
141
142 private void onEntryRemovedInternal(NotificationEntry removed, String notifGroupKey, boolean
143 isGroup, boolean isGroupSummary) {
144 String groupKey = getGroupKey(removed.getKey(), notifGroupKey);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100145 final NotificationGroup group = mGroupMap.get(groupKey);
Selim Cinek0b4aeab2015-09-01 17:07:38 -0700146 if (group == null) {
147 // When an app posts 2 different notifications as summary of the same group, then a
148 // cancellation of the first notification removes this group.
149 // This situation is not supported and we will not allow such notifications anymore in
150 // the close future. See b/23676310 for reference.
151 return;
152 }
Beverlyc5094122020-01-09 17:05:15 -0500153 if (isGroupChild(removed.getKey(), isGroup, isGroupSummary)) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400154 group.children.remove(removed.getKey());
Selim Cineke73ad212015-11-03 19:11:08 -0800155 } else {
156 group.summary = null;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100157 }
Selim Cinek2a739342016-03-17 10:28:55 -0700158 updateSuppression(group);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100159 if (group.children.isEmpty()) {
160 if (group.summary == null) {
161 mGroupMap.remove(groupKey);
Kevin01a53cb2018-11-09 18:19:54 -0800162 for (OnGroupChangeListener listener : mListeners) {
163 listener.onGroupRemoved(group, groupKey);
164 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100165 }
166 }
167 }
168
Selim Cinekba069ae2020-04-01 19:45:16 -0700169 /**
170 * Notify the group manager that a new entry was added
171 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500172 public void onEntryAdded(final NotificationEntry added) {
Selim Cinekba069ae2020-04-01 19:45:16 -0700173 updateIsolation(added);
174 onEntryAddedInternal(added);
175 }
176
177 private void onEntryAddedInternal(final NotificationEntry added) {
Evan Laird94492852018-10-25 13:43:01 -0400178 if (added.isRowRemoved()) {
Selim Cinek04be3892017-08-08 10:58:32 -0700179 added.setDebugThrowable(new Throwable());
180 }
Ned Burns00b4b2d2019-10-17 22:09:27 -0400181 final StatusBarNotification sbn = added.getSbn();
Selim Cinekef5127e2015-12-21 16:55:58 -0800182 boolean isGroupChild = isGroupChild(sbn);
183 String groupKey = getGroupKey(sbn);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100184 NotificationGroup group = mGroupMap.get(groupKey);
185 if (group == null) {
186 group = new NotificationGroup();
187 mGroupMap.put(groupKey, group);
Kevin01a53cb2018-11-09 18:19:54 -0800188 for (OnGroupChangeListener listener : mListeners) {
189 listener.onGroupCreated(group, groupKey);
190 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100191 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800192 if (isGroupChild) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400193 NotificationEntry existing = group.children.get(added.getKey());
Selim Cinek04be3892017-08-08 10:58:32 -0700194 if (existing != null && existing != added) {
195 Throwable existingThrowable = existing.getDebugThrowable();
Ned Burns00b4b2d2019-10-17 22:09:27 -0400196 Log.wtf(TAG, "Inconsistent entries found with the same key " + added.getKey()
Evan Laird94492852018-10-25 13:43:01 -0400197 + "existing removed: " + existing.isRowRemoved()
Selim Cinek04be3892017-08-08 10:58:32 -0700198 + (existingThrowable != null
199 ? Log.getStackTraceString(existingThrowable) + "\n": "")
Evan Laird94492852018-10-25 13:43:01 -0400200 + " added removed" + added.isRowRemoved()
Selim Cinek04be3892017-08-08 10:58:32 -0700201 , new Throwable());
202 }
Ned Burns00b4b2d2019-10-17 22:09:27 -0400203 group.children.put(added.getKey(), added);
Selim Cinek2a739342016-03-17 10:28:55 -0700204 updateSuppression(group);
Selim Cineke73ad212015-11-03 19:11:08 -0800205 } else {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100206 group.summary = added;
Evan Laird94492852018-10-25 13:43:01 -0400207 group.expanded = added.areChildrenExpanded();
Selim Cinek2a739342016-03-17 10:28:55 -0700208 updateSuppression(group);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100209 if (!group.children.isEmpty()) {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500210 ArrayList<NotificationEntry> childrenCopy
Selim Cinek04be3892017-08-08 10:58:32 -0700211 = new ArrayList<>(group.children.values());
Ned Burnsf81c4c42019-01-07 14:10:43 -0500212 for (NotificationEntry child : childrenCopy) {
Selim Cinek50e74672016-05-05 15:58:10 -0400213 onEntryBecomingChild(child);
214 }
Kevin01a53cb2018-11-09 18:19:54 -0800215 for (OnGroupChangeListener listener : mListeners) {
216 listener.onGroupCreatedFromChildren(group);
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700217 }
218 }
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700219 }
220 }
221
Ned Burnsf81c4c42019-01-07 14:10:43 -0500222 private void onEntryBecomingChild(NotificationEntry entry) {
Selim Cinekba069ae2020-04-01 19:45:16 -0700223 updateIsolation(entry);
Selim Cinek50e74672016-05-05 15:58:10 -0400224 }
225
Selim Cinek2a739342016-03-17 10:28:55 -0700226 private void updateSuppression(NotificationGroup group) {
Selim Cinek80c44dd2016-08-15 19:39:55 -0700227 if (group == null) {
Selim Cinek23c80342016-03-17 18:27:36 -0700228 return;
229 }
Mady Mellor740d85d2019-07-09 15:26:47 -0700230 int childCount = 0;
231 boolean hasBubbles = false;
Beverlyed8aea22020-01-22 16:52:47 -0500232 for (NotificationEntry entry : group.children.values()) {
233 if (!getBubbleController().isBubbleNotificationSuppressedFromShade(entry)) {
Mady Mellor740d85d2019-07-09 15:26:47 -0700234 childCount++;
235 } else {
236 hasBubbles = true;
237 }
238 }
239
Selim Cinek2a739342016-03-17 10:28:55 -0700240 boolean prevSuppressed = group.suppressed;
Selim Cinek23c80342016-03-17 18:27:36 -0700241 group.suppressed = group.summary != null && !group.expanded
Mady Mellor740d85d2019-07-09 15:26:47 -0700242 && (childCount == 1
243 || (childCount == 0
Ned Burns00b4b2d2019-10-17 22:09:27 -0400244 && group.summary.getSbn().getNotification().isGroupSummary()
Mady Mellor740d85d2019-07-09 15:26:47 -0700245 && (hasIsolatedChildren(group) || hasBubbles)));
Selim Cinek2a739342016-03-17 10:28:55 -0700246 if (prevSuppressed != group.suppressed) {
Kevin01a53cb2018-11-09 18:19:54 -0800247 for (OnGroupChangeListener listener : mListeners) {
248 if (!mIsUpdatingUnchangedGroup) {
249 listener.onGroupSuppressionChanged(group, group.suppressed);
250 listener.onGroupsChanged();
Kevina97ea052018-09-11 13:53:18 -0700251 }
Selim Cinek967ed2a2016-04-08 18:29:11 -0700252 }
Selim Cinek2a739342016-03-17 10:28:55 -0700253 }
254 }
255
Selim Cinek23c80342016-03-17 18:27:36 -0700256 private boolean hasIsolatedChildren(NotificationGroup group) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400257 return getNumberOfIsolatedChildren(group.summary.getSbn().getGroupKey()) != 0;
Selim Cinek23c80342016-03-17 18:27:36 -0700258 }
259
260 private int getNumberOfIsolatedChildren(String groupKey) {
261 int count = 0;
Selim Cineka6c0cef2016-03-18 11:42:25 -0700262 for (StatusBarNotification sbn : mIsolatedEntries.values()) {
Beverlyc5094122020-01-09 17:05:15 -0500263 if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn.getKey())) {
Selim Cinek23c80342016-03-17 18:27:36 -0700264 count++;
265 }
266 }
267 return count;
268 }
269
Beverlyc5094122020-01-09 17:05:15 -0500270 /**
271 * Update an entry's group information
272 * @param entry notification entry to update
273 * @param oldNotification previous notification info before this update
274 */
275 public void onEntryUpdated(NotificationEntry entry, StatusBarNotification oldNotification) {
276 onEntryUpdated(entry, oldNotification.getGroupKey(), oldNotification.isGroup(),
277 oldNotification.getNotification().isGroupSummary());
278 }
279
280 /**
281 * Updates an entry's group information
282 * @param entry notification entry to update
283 * @param oldGroupKey the notification's previous group key before this update
284 * @param oldIsGroup whether this notification was a group before this update
285 * @param oldIsGroupSummary whether this notification was a group summary before this update
286 */
287 public void onEntryUpdated(NotificationEntry entry, String oldGroupKey, boolean oldIsGroup,
288 boolean oldIsGroupSummary) {
289 String newGroupKey = entry.getSbn().getGroupKey();
290 boolean groupKeysChanged = !oldGroupKey.equals(newGroupKey);
291 boolean wasGroupChild = isGroupChild(entry.getKey(), oldIsGroup, oldIsGroupSummary);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400292 boolean isGroupChild = isGroupChild(entry.getSbn());
Selim Cinek80c44dd2016-08-15 19:39:55 -0700293 mIsUpdatingUnchangedGroup = !groupKeysChanged && wasGroupChild == isGroupChild;
Beverlyc5094122020-01-09 17:05:15 -0500294 if (mGroupMap.get(getGroupKey(entry.getKey(), oldGroupKey)) != null) {
295 onEntryRemovedInternal(entry, oldGroupKey, oldIsGroup, oldIsGroupSummary);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100296 }
Selim Cinekba069ae2020-04-01 19:45:16 -0700297 onEntryAddedInternal(entry);
Selim Cinek80c44dd2016-08-15 19:39:55 -0700298 mIsUpdatingUnchangedGroup = false;
Beverlyc5094122020-01-09 17:05:15 -0500299 if (isIsolated(entry.getSbn().getKey())) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400300 mIsolatedEntries.put(entry.getKey(), entry.getSbn());
Selim Cinek68bdff12016-08-03 13:38:56 -0700301 if (groupKeysChanged) {
Beverlyc5094122020-01-09 17:05:15 -0500302 updateSuppression(mGroupMap.get(oldGroupKey));
303 updateSuppression(mGroupMap.get(newGroupKey));
Selim Cinek23c80342016-03-17 18:27:36 -0700304 }
Selim Cinek68bdff12016-08-03 13:38:56 -0700305 } else if (!wasGroupChild && isGroupChild) {
Selim Cinek50e74672016-05-05 15:58:10 -0400306 onEntryBecomingChild(entry);
Selim Cinek23c80342016-03-17 18:27:36 -0700307 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100308 }
309
Selim Cinek2a739342016-03-17 10:28:55 -0700310 public boolean isSummaryOfSuppressedGroup(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700311 return isGroupSuppressed(getGroupKey(sbn)) && sbn.getNotification().isGroupSummary();
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100312 }
313
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700314 private boolean isOnlyChild(StatusBarNotification sbn) {
Selim Cinek36b02232016-05-11 23:07:05 -0400315 return !sbn.getNotification().isGroupSummary()
Selim Cinek23c80342016-03-17 18:27:36 -0700316 && getTotalNumberOfChildren(sbn) == 1;
Selim Cinek2a739342016-03-17 10:28:55 -0700317 }
318
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700319 public boolean isOnlyChildInGroup(StatusBarNotification sbn) {
Selim Cinek88086e72016-06-17 21:01:32 -0700320 if (!isOnlyChild(sbn)) {
321 return false;
322 }
Ned Burnsf81c4c42019-01-07 14:10:43 -0500323 NotificationEntry logicalGroupSummary = getLogicalGroupSummary(sbn);
Selim Cinek88086e72016-06-17 21:01:32 -0700324 return logicalGroupSummary != null
Ned Burns00b4b2d2019-10-17 22:09:27 -0400325 && !logicalGroupSummary.getSbn().equals(sbn);
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700326 }
327
Selim Cinek23c80342016-03-17 18:27:36 -0700328 private int getTotalNumberOfChildren(StatusBarNotification sbn) {
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700329 int isolatedChildren = getNumberOfIsolatedChildren(sbn.getGroupKey());
330 NotificationGroup group = mGroupMap.get(sbn.getGroupKey());
331 int realChildren = group != null ? group.children.size() : 0;
332 return isolatedChildren + realChildren;
Selim Cinek23c80342016-03-17 18:27:36 -0700333 }
334
335 private boolean isGroupSuppressed(String groupKey) {
336 NotificationGroup group = mGroupMap.get(groupKey);
Selim Cinek2a739342016-03-17 10:28:55 -0700337 return group != null && group.suppressed;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100338 }
339
Jason Monk1fd3fc32018-08-14 17:20:09 -0400340 private void setStatusBarState(int newState) {
Selim Cinek9c4c4142015-12-04 16:44:56 -0800341 mBarState = newState;
342 if (mBarState == StatusBarState.KEYGUARD) {
Selim Cinek9184f9c2016-02-02 17:36:53 -0800343 collapseAllGroups();
344 }
345 }
346
347 public void collapseAllGroups() {
Selim Cinekc0b14b02016-05-09 13:12:40 -0400348 // Because notifications can become isolated when the group becomes suppressed it can
349 // lead to concurrent modifications while looping. We need to make a copy.
350 ArrayList<NotificationGroup> groupCopy = new ArrayList<>(mGroupMap.values());
351 int size = groupCopy.size();
352 for (int i = 0; i < size; i++) {
353 NotificationGroup group = groupCopy.get(i);
Selim Cinek9184f9c2016-02-02 17:36:53 -0800354 if (group.expanded) {
355 setGroupExpanded(group, false);
Selim Cinek9c4c4142015-12-04 16:44:56 -0800356 }
Selim Cinek2a739342016-03-17 10:28:55 -0700357 updateSuppression(group);
Selim Cinek9c4c4142015-12-04 16:44:56 -0800358 }
359 }
360
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100361 /**
362 * @return whether a given notification is a child in a group which has a summary
363 */
364 public boolean isChildInGroupWithSummary(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800365 if (!isGroupChild(sbn)) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100366 return false;
367 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800368 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek2a739342016-03-17 10:28:55 -0700369 if (group == null || group.summary == null || group.suppressed) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100370 return false;
371 }
Selim Cinek3f19f602016-05-02 18:01:56 -0700372 if (group.children.isEmpty()) {
373 // If the suppression of a group changes because the last child was removed, this can
374 // still be called temporarily because the child hasn't been fully removed yet. Let's
375 // make sure we still return false in that case.
376 return false;
377 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100378 return true;
379 }
380
Selim Cinek263398f2015-10-21 17:40:23 -0700381 /**
382 * @return whether a given notification is a summary in a group which has children
383 */
384 public boolean isSummaryOfGroup(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800385 if (!isGroupSummary(sbn)) {
Selim Cinek263398f2015-10-21 17:40:23 -0700386 return false;
387 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800388 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Kevin96eed642018-09-05 18:53:33 -0700389 if (group == null || group.summary == null) {
Selim Cinek263398f2015-10-21 17:40:23 -0700390 return false;
391 }
Ned Burns00b4b2d2019-10-17 22:09:27 -0400392 return !group.children.isEmpty() && Objects.equals(group.summary.getSbn(), sbn);
Selim Cinek263398f2015-10-21 17:40:23 -0700393 }
394
Selim Cinek23c80342016-03-17 18:27:36 -0700395 /**
396 * Get the summary of a specified status bar notification. For isolated notification this return
397 * itself.
398 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500399 public NotificationEntry getGroupSummary(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700400 return getGroupSummary(getGroupKey(sbn));
401 }
402
403 /**
404 * Similar to {@link #getGroupSummary(StatusBarNotification)} but doesn't get the visual summary
405 * but the logical summary, i.e when a child is isolated, it still returns the summary as if
406 * it wasn't isolated.
407 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500408 public NotificationEntry getLogicalGroupSummary(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700409 return getGroupSummary(sbn.getGroupKey());
410 }
411
412 @Nullable
Ned Burnsf81c4c42019-01-07 14:10:43 -0500413 private NotificationEntry getGroupSummary(String groupKey) {
Selim Cinek23c80342016-03-17 18:27:36 -0700414 NotificationGroup group = mGroupMap.get(groupKey);
Evan Laird94492852018-10-25 13:43:01 -0400415 //TODO: see if this can become an Entry
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100416 return group == null ? null
Evan Laird9a9a6192019-12-19 10:44:21 -0500417 : group.summary;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100418 }
419
Kevin01a53cb2018-11-09 18:19:54 -0800420 /**
421 * Get the children that are logically in the summary's group, whether or not they are isolated.
422 *
423 * @param summary summary of a group
424 * @return list of the children
425 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500426 public ArrayList<NotificationEntry> getLogicalChildren(StatusBarNotification summary) {
Kevin01a53cb2018-11-09 18:19:54 -0800427 NotificationGroup group = mGroupMap.get(summary.getGroupKey());
428 if (group == null) {
429 return null;
430 }
Ned Burnsf81c4c42019-01-07 14:10:43 -0500431 ArrayList<NotificationEntry> children = new ArrayList<>(group.children.values());
Selim Cinekba069ae2020-04-01 19:45:16 -0700432 for (StatusBarNotification sbn : mIsolatedEntries.values()) {
433 if (sbn.getGroupKey().equals(summary.getGroupKey())) {
434 children.add(mGroupMap.get(sbn.getKey()).summary);
435 }
Kevin01a53cb2018-11-09 18:19:54 -0800436 }
437 return children;
438 }
439
440 /**
Selim Cinekba069ae2020-04-01 19:45:16 -0700441 * Get the children that are in the summary's group, not including those isolated.
442 *
443 * @param summary summary of a group
444 * @return list of the children
445 */
446 public @Nullable ArrayList<NotificationEntry> getChildren(StatusBarNotification summary) {
447 NotificationGroup group = mGroupMap.get(summary.getGroupKey());
448 if (group == null) {
449 return null;
450 }
451 return new ArrayList<>(group.children.values());
452 }
453
454 /**
Mady Mellor740d85d2019-07-09 15:26:47 -0700455 * If there is a {@link NotificationGroup} associated with the provided entry, this method
456 * will update the suppression of that group.
457 */
Pinyao Ting293b83d2020-05-06 17:10:56 -0700458 public void updateSuppression(@NonNull final NotificationEntry entry) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400459 NotificationGroup group = mGroupMap.get(getGroupKey(entry.getSbn()));
Mady Mellor740d85d2019-07-09 15:26:47 -0700460 if (group != null) {
461 updateSuppression(group);
462 }
463 }
464
465 /**
Kevin01a53cb2018-11-09 18:19:54 -0800466 * Get the group key. May differ from the one in the notification due to the notification
467 * being temporarily isolated.
468 *
469 * @param sbn notification to check
470 * @return the key of the notification
471 */
472 public String getGroupKey(StatusBarNotification sbn) {
Beverlyc5094122020-01-09 17:05:15 -0500473 return getGroupKey(sbn.getKey(), sbn.getGroupKey());
474 }
475
476 private String getGroupKey(String key, String groupKey) {
477 if (isIsolated(key)) {
478 return key;
Kevin01a53cb2018-11-09 18:19:54 -0800479 }
Beverlyc5094122020-01-09 17:05:15 -0500480 return groupKey;
Kevin01a53cb2018-11-09 18:19:54 -0800481 }
482
Chris Wren698b1702016-05-23 11:16:32 -0400483 /** @return group expansion state after toggling. */
484 public boolean toggleGroupExpansion(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800485 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek83bc7832015-10-22 13:26:54 -0700486 if (group == null) {
Chris Wren698b1702016-05-23 11:16:32 -0400487 return false;
Selim Cinek83bc7832015-10-22 13:26:54 -0700488 }
489 setGroupExpanded(group, !group.expanded);
Chris Wren698b1702016-05-23 11:16:32 -0400490 return group.expanded;
Selim Cinek83bc7832015-10-22 13:26:54 -0700491 }
492
Beverlyc5094122020-01-09 17:05:15 -0500493 private boolean isIsolated(String sbnKey) {
494 return mIsolatedEntries.containsKey(sbnKey);
Selim Cinekef5127e2015-12-21 16:55:58 -0800495 }
496
Kevin01a53cb2018-11-09 18:19:54 -0800497 /**
498 * Whether a notification is visually a group summary.
499 *
500 * @param sbn notification to check
501 * @return true if it is visually a group summary
502 */
503 public boolean isGroupSummary(StatusBarNotification sbn) {
Beverlyc5094122020-01-09 17:05:15 -0500504 if (isIsolated(sbn.getKey())) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800505 return true;
506 }
507 return sbn.getNotification().isGroupSummary();
508 }
Julia Reynoldse46bb372016-03-17 11:05:58 -0400509
Kevin01a53cb2018-11-09 18:19:54 -0800510 /**
511 * Whether a notification is visually a group child.
512 *
513 * @param sbn notification to check
514 * @return true if it is visually a group child
515 */
516 public boolean isGroupChild(StatusBarNotification sbn) {
Beverlyc5094122020-01-09 17:05:15 -0500517 return isGroupChild(sbn.getKey(), sbn.isGroup(), sbn.getNotification().isGroupSummary());
518 }
519
520 private boolean isGroupChild(String key, boolean isGroup, boolean isGroupSummary) {
521 if (isIsolated(key)) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800522 return false;
523 }
Beverlyc5094122020-01-09 17:05:15 -0500524 return isGroup && !isGroupSummary;
Selim Cinekef5127e2015-12-21 16:55:58 -0800525 }
526
Selim Cinekef5127e2015-12-21 16:55:58 -0800527 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500528 public void onHeadsUpStateChanged(NotificationEntry entry, boolean isHeadsUp) {
Selim Cinekba069ae2020-04-01 19:45:16 -0700529 updateIsolation(entry);
Selim Cinekef5127e2015-12-21 16:55:58 -0800530 }
531
Kevina97ea052018-09-11 13:53:18 -0700532 /**
Kevina97ea052018-09-11 13:53:18 -0700533 * Whether a notification that is normally part of a group should be temporarily isolated from
534 * the group and put in their own group visually. This generally happens when the notification
535 * is alerting.
536 *
537 * @param entry the notification to check
538 * @return true if the entry should be isolated
539 */
540
Ned Burnsf81c4c42019-01-07 14:10:43 -0500541 private boolean shouldIsolate(NotificationEntry entry) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400542 StatusBarNotification sbn = entry.getSbn();
Kevina97ea052018-09-11 13:53:18 -0700543 if (!sbn.isGroup() || sbn.getNotification().isGroupSummary()) {
544 return false;
545 }
Steve Elliottd38a70f2020-05-11 14:01:08 -0400546 int peopleNotificationType = mPeopleNotificationIdentifier.get().getPeopleNotificationType(
547 entry.getSbn(), entry.getRanking());
548 if (peopleNotificationType == PeopleNotificationIdentifier.TYPE_IMPORTANT_PERSON) {
Selim Cinekba069ae2020-04-01 19:45:16 -0700549 return true;
550 }
Ned Burns00b4b2d2019-10-17 22:09:27 -0400551 if (mHeadsUpManager != null && !mHeadsUpManager.isAlerting(entry.getKey())) {
Kevina97ea052018-09-11 13:53:18 -0700552 return false;
553 }
Selim Cinekba069ae2020-04-01 19:45:16 -0700554 NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey());
Kevina97ea052018-09-11 13:53:18 -0700555 return (sbn.getNotification().fullScreenIntent != null
556 || notificationGroup == null
557 || !notificationGroup.expanded
558 || isGroupNotFullyVisible(notificationGroup));
559 }
560
561 /**
562 * Isolate a notification from its group so that it visually shows as its own group.
563 *
564 * @param entry the notification to isolate
565 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500566 private void isolateNotification(NotificationEntry entry) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400567 StatusBarNotification sbn = entry.getSbn();
Kevina97ea052018-09-11 13:53:18 -0700568
569 // We will be isolated now, so lets update the groups
Ned Burns00b4b2d2019-10-17 22:09:27 -0400570 onEntryRemovedInternal(entry, entry.getSbn());
Kevina97ea052018-09-11 13:53:18 -0700571
572 mIsolatedEntries.put(sbn.getKey(), sbn);
573
Selim Cinekba069ae2020-04-01 19:45:16 -0700574 onEntryAddedInternal(entry);
Kevina97ea052018-09-11 13:53:18 -0700575 // We also need to update the suppression of the old group, because this call comes
576 // even before the groupManager knows about the notification at all.
577 // When the notification gets added afterwards it is already isolated and therefore
578 // it doesn't lead to an update.
Ned Burns00b4b2d2019-10-17 22:09:27 -0400579 updateSuppression(mGroupMap.get(entry.getSbn().getGroupKey()));
Kevin01a53cb2018-11-09 18:19:54 -0800580 for (OnGroupChangeListener listener : mListeners) {
581 listener.onGroupsChanged();
582 }
Kevina97ea052018-09-11 13:53:18 -0700583 }
584
585 /**
Selim Cinekba069ae2020-04-01 19:45:16 -0700586 * Update the isolation of an entry, splitting it from the group.
587 */
588 public void updateIsolation(NotificationEntry entry) {
589 boolean isIsolated = isIsolated(entry.getSbn().getKey());
590 if (shouldIsolate(entry)) {
591 if (!isIsolated) {
592 isolateNotification(entry);
593 }
594 } else if (isIsolated) {
595 stopIsolatingNotification(entry);
596 }
597 }
598
599 /**
Kevina97ea052018-09-11 13:53:18 -0700600 * Stop isolating a notification and re-group it with its original logical group.
601 *
602 * @param entry the notification to un-isolate
603 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500604 private void stopIsolatingNotification(NotificationEntry entry) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400605 StatusBarNotification sbn = entry.getSbn();
Selim Cinekba069ae2020-04-01 19:45:16 -0700606 if (isIsolated(sbn.getKey())) {
Kevina97ea052018-09-11 13:53:18 -0700607 // not isolated anymore, we need to update the groups
Ned Burns00b4b2d2019-10-17 22:09:27 -0400608 onEntryRemovedInternal(entry, entry.getSbn());
Kevina97ea052018-09-11 13:53:18 -0700609 mIsolatedEntries.remove(sbn.getKey());
Selim Cinekba069ae2020-04-01 19:45:16 -0700610 onEntryAddedInternal(entry);
Kevin01a53cb2018-11-09 18:19:54 -0800611 for (OnGroupChangeListener listener : mListeners) {
612 listener.onGroupsChanged();
613 }
Kevina97ea052018-09-11 13:53:18 -0700614 }
Selim Cineka6c0cef2016-03-18 11:42:25 -0700615 }
616
617 private boolean isGroupNotFullyVisible(NotificationGroup notificationGroup) {
618 return notificationGroup.summary == null
Evan Laird94492852018-10-25 13:43:01 -0400619 || notificationGroup.summary.isGroupNotFullyVisible();
Selim Cineka6c0cef2016-03-18 11:42:25 -0700620 }
621
Selim Cinek967ed2a2016-04-08 18:29:11 -0700622 public void setHeadsUpManager(HeadsUpManager headsUpManager) {
623 mHeadsUpManager = headsUpManager;
624 }
625
Selim Cinek52941c52016-05-07 18:29:32 -0400626 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
627 pw.println("GroupManager state:");
628 pw.println(" number of groups: " + mGroupMap.size());
629 for (Map.Entry<String, NotificationGroup> entry : mGroupMap.entrySet()) {
630 pw.println("\n key: " + entry.getKey()); pw.println(entry.getValue());
631 }
632 pw.println("\n isolated entries: " + mIsolatedEntries.size());
633 for (Map.Entry<String, StatusBarNotification> entry : mIsolatedEntries.entrySet()) {
634 pw.print(" "); pw.print(entry.getKey());
635 pw.print(", "); pw.println(entry.getValue());
636 }
637 }
638
Evan Laird878c8532018-10-15 15:54:29 -0400639 @Override
640 public void onStateChanged(int newState) {
641 setStatusBarState(newState);
642 }
643
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100644 public static class NotificationGroup {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500645 public final HashMap<String, NotificationEntry> children = new HashMap<>();
646 public NotificationEntry summary;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100647 public boolean expanded;
Selim Cinek2a739342016-03-17 10:28:55 -0700648 /**
649 * Is this notification group suppressed, i.e its summary is hidden
650 */
651 public boolean suppressed;
Selim Cinek52941c52016-05-07 18:29:32 -0400652
653 @Override
654 public String toString() {
Selim Cinek60ca7872016-05-12 11:24:07 -0700655 String result = " summary:\n "
Ned Burns00b4b2d2019-10-17 22:09:27 -0400656 + (summary != null ? summary.getSbn() : "null")
Selim Cinek04be3892017-08-08 10:58:32 -0700657 + (summary != null && summary.getDebugThrowable() != null
658 ? Log.getStackTraceString(summary.getDebugThrowable())
659 : "");
Selim Cinek52941c52016-05-07 18:29:32 -0400660 result += "\n children size: " + children.size();
Ned Burnsf81c4c42019-01-07 14:10:43 -0500661 for (NotificationEntry child : children.values()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400662 result += "\n " + child.getSbn()
Selim Cinek04be3892017-08-08 10:58:32 -0700663 + (child.getDebugThrowable() != null
664 ? Log.getStackTraceString(child.getDebugThrowable())
665 : "");
Selim Cinek52941c52016-05-07 18:29:32 -0400666 }
Mady Mellor740d85d2019-07-09 15:26:47 -0700667 result += "\n summary suppressed: " + suppressed;
Selim Cinek52941c52016-05-07 18:29:32 -0400668 return result;
669 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100670 }
671
672 public interface OnGroupChangeListener {
Kevin01a53cb2018-11-09 18:19:54 -0800673
674 /**
675 * A new group has been created.
676 *
677 * @param group the group that was created
678 * @param groupKey the group's key
679 */
680 default void onGroupCreated(NotificationGroup group, String groupKey) {}
681
682 /**
683 * A group has been removed.
684 *
685 * @param group the group that was removed
686 * @param groupKey the group's key
687 */
688 default void onGroupRemoved(NotificationGroup group, String groupKey) {}
689
690 /**
691 * The suppression of a group has changed.
692 *
693 * @param group the group that has changed
694 * @param suppressed true if the group is now suppressed, false o/w
695 */
696 default void onGroupSuppressionChanged(NotificationGroup group, boolean suppressed) {}
697
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100698 /**
699 * The expansion of a group has changed.
700 *
701 * @param changedRow the row for which the expansion has changed, which is also the summary
702 * @param expanded a boolean indicating the new expanded state
703 */
Kevin01a53cb2018-11-09 18:19:54 -0800704 default void onGroupExpansionChanged(ExpandableNotificationRow changedRow,
705 boolean expanded) {}
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100706
707 /**
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100708 * A group of children just received a summary notification and should therefore become
709 * children of it.
710 *
711 * @param group the group created
712 */
Kevin01a53cb2018-11-09 18:19:54 -0800713 default void onGroupCreatedFromChildren(NotificationGroup group) {}
Selim Cinekef5127e2015-12-21 16:55:58 -0800714
715 /**
Selim Cinek2a739342016-03-17 10:28:55 -0700716 * The groups have changed. This can happen if the isolation of a child has changes or if a
717 * group became suppressed / unsuppressed
Selim Cinekef5127e2015-12-21 16:55:58 -0800718 */
Kevin01a53cb2018-11-09 18:19:54 -0800719 default void onGroupsChanged() {}
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100720 }
721}