blob: 0bbfbefb788335ec7f9ac5858dcbf9f33e581c56 [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;
Beverly8fdb5332019-02-04 14:29:49 -050025import com.android.systemui.plugins.statusbar.StatusBarStateController;
26import com.android.systemui.plugins.statusbar.StatusBarStateController.StateListener;
Jason Monk1fd3fc32018-08-14 17:20:09 -040027import com.android.systemui.statusbar.StatusBarState;
Ned Burnsf81c4c42019-01-07 14:10:43 -050028import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Evan Laird878c8532018-10-15 15:54:29 -040029import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
Selim Cinekef5127e2015-12-21 16:55:58 -080030import com.android.systemui.statusbar.policy.HeadsUpManager;
Selim Cineka7d4f822016-12-06 14:34:47 -080031import com.android.systemui.statusbar.policy.OnHeadsUpChangedListener;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010032
Selim Cinek52941c52016-05-07 18:29:32 -040033import java.io.FileDescriptor;
34import java.io.PrintWriter;
Selim Cinekc0b14b02016-05-09 13:12:40 -040035import java.util.ArrayList;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010036import java.util.HashMap;
Selim Cinek52941c52016-05-07 18:29:32 -040037import java.util.Map;
Selim Cinekf93bf3e2018-05-08 14:43:21 -070038import java.util.Objects;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010039
Jason Monk27d01a622018-12-10 15:57:09 -050040import javax.inject.Inject;
41import javax.inject.Singleton;
42
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010043/**
44 * A class to handle notifications and their corresponding groups.
45 */
Jason Monk27d01a622018-12-10 15:57:09 -050046@Singleton
Selim Cinekc3fec682019-06-06 18:11:07 -070047public class NotificationGroupManager implements OnHeadsUpChangedListener, StateListener {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010048
Selim Cinek04be3892017-08-08 10:58:32 -070049 private static final String TAG = "NotificationGroupManager";
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010050 private final HashMap<String, NotificationGroup> mGroupMap = new HashMap<>();
Kevin01a53cb2018-11-09 18:19:54 -080051 private final ArraySet<OnGroupChangeListener> mListeners = new ArraySet<>();
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010052 private int mBarState = -1;
Selim Cineka6c0cef2016-03-18 11:42:25 -070053 private HashMap<String, StatusBarNotification> mIsolatedEntries = new HashMap<>();
Selim Cinek967ed2a2016-04-08 18:29:11 -070054 private HeadsUpManager mHeadsUpManager;
Selim Cinek80c44dd2016-08-15 19:39:55 -070055 private boolean mIsUpdatingUnchangedGroup;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010056
Jason Monk27d01a622018-12-10 15:57:09 -050057 @Inject
Selim Cinekc3fec682019-06-06 18:11:07 -070058 public NotificationGroupManager(StatusBarStateController statusBarStateController) {
59 statusBarStateController.addCallback(this);
Jason Monk1fd3fc32018-08-14 17:20:09 -040060 }
61
Kevin01a53cb2018-11-09 18:19:54 -080062 /**
63 * Add a listener for changes to groups.
64 *
65 * @param listener listener to add
66 */
67 public void addOnGroupChangeListener(OnGroupChangeListener listener) {
68 mListeners.add(listener);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010069 }
70
71 public boolean isGroupExpanded(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -080072 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010073 if (group == null) {
74 return false;
75 }
76 return group.expanded;
77 }
78
79 public void setGroupExpanded(StatusBarNotification sbn, boolean expanded) {
Selim Cinekef5127e2015-12-21 16:55:58 -080080 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010081 if (group == null) {
82 return;
83 }
84 setGroupExpanded(group, expanded);
85 }
86
87 private void setGroupExpanded(NotificationGroup group, boolean expanded) {
88 group.expanded = expanded;
89 if (group.summary != null) {
Kevin01a53cb2018-11-09 18:19:54 -080090 for (OnGroupChangeListener listener : mListeners) {
Evan Laird94492852018-10-25 13:43:01 -040091 listener.onGroupExpansionChanged(group.summary.getRow(), expanded);
Kevin01a53cb2018-11-09 18:19:54 -080092 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010093 }
94 }
95
Ned Burnsf81c4c42019-01-07 14:10:43 -050096 public void onEntryRemoved(NotificationEntry removed) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010097 onEntryRemovedInternal(removed, removed.notification);
Selim Cinek52941c52016-05-07 18:29:32 -040098 mIsolatedEntries.remove(removed.key);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010099 }
100
101 /**
102 * An entry was removed.
103 *
104 * @param removed the removed entry
105 * @param sbn the notification the entry has, which doesn't need to be the same as it's internal
106 * notification
107 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500108 private void onEntryRemovedInternal(NotificationEntry removed,
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100109 final StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800110 String groupKey = getGroupKey(sbn);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100111 final NotificationGroup group = mGroupMap.get(groupKey);
Selim Cinek0b4aeab2015-09-01 17:07:38 -0700112 if (group == null) {
113 // When an app posts 2 different notifications as summary of the same group, then a
114 // cancellation of the first notification removes this group.
115 // This situation is not supported and we will not allow such notifications anymore in
116 // the close future. See b/23676310 for reference.
117 return;
118 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800119 if (isGroupChild(sbn)) {
Selim Cinek04be3892017-08-08 10:58:32 -0700120 group.children.remove(removed.key);
Selim Cineke73ad212015-11-03 19:11:08 -0800121 } else {
122 group.summary = null;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100123 }
Selim Cinek2a739342016-03-17 10:28:55 -0700124 updateSuppression(group);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100125 if (group.children.isEmpty()) {
126 if (group.summary == null) {
127 mGroupMap.remove(groupKey);
Kevin01a53cb2018-11-09 18:19:54 -0800128 for (OnGroupChangeListener listener : mListeners) {
129 listener.onGroupRemoved(group, groupKey);
130 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100131 }
132 }
133 }
134
Ned Burnsf81c4c42019-01-07 14:10:43 -0500135 public void onEntryAdded(final NotificationEntry added) {
Evan Laird94492852018-10-25 13:43:01 -0400136 if (added.isRowRemoved()) {
Selim Cinek04be3892017-08-08 10:58:32 -0700137 added.setDebugThrowable(new Throwable());
138 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800139 final StatusBarNotification sbn = added.notification;
140 boolean isGroupChild = isGroupChild(sbn);
141 String groupKey = getGroupKey(sbn);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100142 NotificationGroup group = mGroupMap.get(groupKey);
143 if (group == null) {
144 group = new NotificationGroup();
145 mGroupMap.put(groupKey, group);
Kevin01a53cb2018-11-09 18:19:54 -0800146 for (OnGroupChangeListener listener : mListeners) {
147 listener.onGroupCreated(group, groupKey);
148 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100149 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800150 if (isGroupChild) {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500151 NotificationEntry existing = group.children.get(added.key);
Selim Cinek04be3892017-08-08 10:58:32 -0700152 if (existing != null && existing != added) {
153 Throwable existingThrowable = existing.getDebugThrowable();
154 Log.wtf(TAG, "Inconsistent entries found with the same key " + added.key
Evan Laird94492852018-10-25 13:43:01 -0400155 + "existing removed: " + existing.isRowRemoved()
Selim Cinek04be3892017-08-08 10:58:32 -0700156 + (existingThrowable != null
157 ? Log.getStackTraceString(existingThrowable) + "\n": "")
Evan Laird94492852018-10-25 13:43:01 -0400158 + " added removed" + added.isRowRemoved()
Selim Cinek04be3892017-08-08 10:58:32 -0700159 , new Throwable());
160 }
161 group.children.put(added.key, added);
Selim Cinek2a739342016-03-17 10:28:55 -0700162 updateSuppression(group);
Selim Cineke73ad212015-11-03 19:11:08 -0800163 } else {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100164 group.summary = added;
Evan Laird94492852018-10-25 13:43:01 -0400165 group.expanded = added.areChildrenExpanded();
Selim Cinek2a739342016-03-17 10:28:55 -0700166 updateSuppression(group);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100167 if (!group.children.isEmpty()) {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500168 ArrayList<NotificationEntry> childrenCopy
Selim Cinek04be3892017-08-08 10:58:32 -0700169 = new ArrayList<>(group.children.values());
Ned Burnsf81c4c42019-01-07 14:10:43 -0500170 for (NotificationEntry child : childrenCopy) {
Selim Cinek50e74672016-05-05 15:58:10 -0400171 onEntryBecomingChild(child);
172 }
Kevin01a53cb2018-11-09 18:19:54 -0800173 for (OnGroupChangeListener listener : mListeners) {
174 listener.onGroupCreatedFromChildren(group);
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700175 }
176 }
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700177 }
178 }
179
Ned Burnsf81c4c42019-01-07 14:10:43 -0500180 private void onEntryBecomingChild(NotificationEntry entry) {
Kevina97ea052018-09-11 13:53:18 -0700181 if (shouldIsolate(entry)) {
182 isolateNotification(entry);
Selim Cinek50e74672016-05-05 15:58:10 -0400183 }
184 }
185
Selim Cinek2a739342016-03-17 10:28:55 -0700186 private void updateSuppression(NotificationGroup group) {
Selim Cinek80c44dd2016-08-15 19:39:55 -0700187 if (group == null) {
Selim Cinek23c80342016-03-17 18:27:36 -0700188 return;
189 }
Selim Cinek2a739342016-03-17 10:28:55 -0700190 boolean prevSuppressed = group.suppressed;
Selim Cinek23c80342016-03-17 18:27:36 -0700191 group.suppressed = group.summary != null && !group.expanded
192 && (group.children.size() == 1
193 || (group.children.size() == 0
Julia Reynoldse46bb372016-03-17 11:05:58 -0400194 && group.summary.notification.getNotification().isGroupSummary()
Selim Cinek23c80342016-03-17 18:27:36 -0700195 && hasIsolatedChildren(group)));
Selim Cinek2a739342016-03-17 10:28:55 -0700196 if (prevSuppressed != group.suppressed) {
Kevin01a53cb2018-11-09 18:19:54 -0800197 for (OnGroupChangeListener listener : mListeners) {
198 if (!mIsUpdatingUnchangedGroup) {
199 listener.onGroupSuppressionChanged(group, group.suppressed);
200 listener.onGroupsChanged();
Kevina97ea052018-09-11 13:53:18 -0700201 }
Selim Cinek967ed2a2016-04-08 18:29:11 -0700202 }
Selim Cinek2a739342016-03-17 10:28:55 -0700203 }
204 }
205
Selim Cinek23c80342016-03-17 18:27:36 -0700206 private boolean hasIsolatedChildren(NotificationGroup group) {
207 return getNumberOfIsolatedChildren(group.summary.notification.getGroupKey()) != 0;
208 }
209
210 private int getNumberOfIsolatedChildren(String groupKey) {
211 int count = 0;
Selim Cineka6c0cef2016-03-18 11:42:25 -0700212 for (StatusBarNotification sbn : mIsolatedEntries.values()) {
Selim Cinek23c80342016-03-17 18:27:36 -0700213 if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) {
214 count++;
215 }
216 }
217 return count;
218 }
219
Ned Burnsf81c4c42019-01-07 14:10:43 -0500220 private NotificationEntry getIsolatedChild(String groupKey) {
Selim Cinek967ed2a2016-04-08 18:29:11 -0700221 for (StatusBarNotification sbn : mIsolatedEntries.values()) {
222 if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) {
223 return mGroupMap.get(sbn.getKey()).summary;
224 }
225 }
226 return null;
227 }
228
Ned Burnsf81c4c42019-01-07 14:10:43 -0500229 public void onEntryUpdated(NotificationEntry entry,
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100230 StatusBarNotification oldNotification) {
Selim Cinek68bdff12016-08-03 13:38:56 -0700231 String oldKey = oldNotification.getGroupKey();
232 String newKey = entry.notification.getGroupKey();
233 boolean groupKeysChanged = !oldKey.equals(newKey);
234 boolean wasGroupChild = isGroupChild(oldNotification);
235 boolean isGroupChild = isGroupChild(entry.notification);
Selim Cinek80c44dd2016-08-15 19:39:55 -0700236 mIsUpdatingUnchangedGroup = !groupKeysChanged && wasGroupChild == isGroupChild;
Selim Cinekef5127e2015-12-21 16:55:58 -0800237 if (mGroupMap.get(getGroupKey(oldNotification)) != null) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100238 onEntryRemovedInternal(entry, oldNotification);
239 }
240 onEntryAdded(entry);
Selim Cinek80c44dd2016-08-15 19:39:55 -0700241 mIsUpdatingUnchangedGroup = false;
Selim Cineka6c0cef2016-03-18 11:42:25 -0700242 if (isIsolated(entry.notification)) {
243 mIsolatedEntries.put(entry.key, entry.notification);
Selim Cinek68bdff12016-08-03 13:38:56 -0700244 if (groupKeysChanged) {
Selim Cinek23c80342016-03-17 18:27:36 -0700245 updateSuppression(mGroupMap.get(oldKey));
246 updateSuppression(mGroupMap.get(newKey));
247 }
Selim Cinek68bdff12016-08-03 13:38:56 -0700248 } else if (!wasGroupChild && isGroupChild) {
Selim Cinek50e74672016-05-05 15:58:10 -0400249 onEntryBecomingChild(entry);
Selim Cinek23c80342016-03-17 18:27:36 -0700250 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100251 }
252
Selim Cinek2a739342016-03-17 10:28:55 -0700253 public boolean isSummaryOfSuppressedGroup(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700254 return isGroupSuppressed(getGroupKey(sbn)) && sbn.getNotification().isGroupSummary();
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100255 }
256
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700257 private boolean isOnlyChild(StatusBarNotification sbn) {
Selim Cinek36b02232016-05-11 23:07:05 -0400258 return !sbn.getNotification().isGroupSummary()
Selim Cinek23c80342016-03-17 18:27:36 -0700259 && getTotalNumberOfChildren(sbn) == 1;
Selim Cinek2a739342016-03-17 10:28:55 -0700260 }
261
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700262 public boolean isOnlyChildInGroup(StatusBarNotification sbn) {
Selim Cinek88086e72016-06-17 21:01:32 -0700263 if (!isOnlyChild(sbn)) {
264 return false;
265 }
Ned Burnsf81c4c42019-01-07 14:10:43 -0500266 NotificationEntry logicalGroupSummary = getLogicalGroupSummary(sbn);
Selim Cinek88086e72016-06-17 21:01:32 -0700267 return logicalGroupSummary != null
Evan Laird94492852018-10-25 13:43:01 -0400268 && !logicalGroupSummary.notification.equals(sbn);
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700269 }
270
Selim Cinek23c80342016-03-17 18:27:36 -0700271 private int getTotalNumberOfChildren(StatusBarNotification sbn) {
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700272 int isolatedChildren = getNumberOfIsolatedChildren(sbn.getGroupKey());
273 NotificationGroup group = mGroupMap.get(sbn.getGroupKey());
274 int realChildren = group != null ? group.children.size() : 0;
275 return isolatedChildren + realChildren;
Selim Cinek23c80342016-03-17 18:27:36 -0700276 }
277
278 private boolean isGroupSuppressed(String groupKey) {
279 NotificationGroup group = mGroupMap.get(groupKey);
Selim Cinek2a739342016-03-17 10:28:55 -0700280 return group != null && group.suppressed;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100281 }
282
Jason Monk1fd3fc32018-08-14 17:20:09 -0400283 private void setStatusBarState(int newState) {
Selim Cinek9c4c4142015-12-04 16:44:56 -0800284 mBarState = newState;
285 if (mBarState == StatusBarState.KEYGUARD) {
Selim Cinek9184f9c2016-02-02 17:36:53 -0800286 collapseAllGroups();
287 }
288 }
289
290 public void collapseAllGroups() {
Selim Cinekc0b14b02016-05-09 13:12:40 -0400291 // Because notifications can become isolated when the group becomes suppressed it can
292 // lead to concurrent modifications while looping. We need to make a copy.
293 ArrayList<NotificationGroup> groupCopy = new ArrayList<>(mGroupMap.values());
294 int size = groupCopy.size();
295 for (int i = 0; i < size; i++) {
296 NotificationGroup group = groupCopy.get(i);
Selim Cinek9184f9c2016-02-02 17:36:53 -0800297 if (group.expanded) {
298 setGroupExpanded(group, false);
Selim Cinek9c4c4142015-12-04 16:44:56 -0800299 }
Selim Cinek2a739342016-03-17 10:28:55 -0700300 updateSuppression(group);
Selim Cinek9c4c4142015-12-04 16:44:56 -0800301 }
302 }
303
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100304 /**
305 * @return whether a given notification is a child in a group which has a summary
306 */
307 public boolean isChildInGroupWithSummary(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800308 if (!isGroupChild(sbn)) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100309 return false;
310 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800311 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek2a739342016-03-17 10:28:55 -0700312 if (group == null || group.summary == null || group.suppressed) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100313 return false;
314 }
Selim Cinek3f19f602016-05-02 18:01:56 -0700315 if (group.children.isEmpty()) {
316 // If the suppression of a group changes because the last child was removed, this can
317 // still be called temporarily because the child hasn't been fully removed yet. Let's
318 // make sure we still return false in that case.
319 return false;
320 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100321 return true;
322 }
323
Selim Cinek263398f2015-10-21 17:40:23 -0700324 /**
325 * @return whether a given notification is a summary in a group which has children
326 */
327 public boolean isSummaryOfGroup(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800328 if (!isGroupSummary(sbn)) {
Selim Cinek263398f2015-10-21 17:40:23 -0700329 return false;
330 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800331 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Kevin96eed642018-09-05 18:53:33 -0700332 if (group == null || group.summary == null) {
Selim Cinek263398f2015-10-21 17:40:23 -0700333 return false;
334 }
Kevin96eed642018-09-05 18:53:33 -0700335 return !group.children.isEmpty() && Objects.equals(group.summary.notification, sbn);
Selim Cinek263398f2015-10-21 17:40:23 -0700336 }
337
Selim Cinek23c80342016-03-17 18:27:36 -0700338 /**
339 * Get the summary of a specified status bar notification. For isolated notification this return
340 * itself.
341 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500342 public NotificationEntry getGroupSummary(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700343 return getGroupSummary(getGroupKey(sbn));
344 }
345
346 /**
347 * Similar to {@link #getGroupSummary(StatusBarNotification)} but doesn't get the visual summary
348 * but the logical summary, i.e when a child is isolated, it still returns the summary as if
349 * it wasn't isolated.
350 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500351 public NotificationEntry getLogicalGroupSummary(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700352 return getGroupSummary(sbn.getGroupKey());
353 }
354
355 @Nullable
Ned Burnsf81c4c42019-01-07 14:10:43 -0500356 private NotificationEntry getGroupSummary(String groupKey) {
Selim Cinek23c80342016-03-17 18:27:36 -0700357 NotificationGroup group = mGroupMap.get(groupKey);
Evan Laird94492852018-10-25 13:43:01 -0400358 //TODO: see if this can become an Entry
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100359 return group == null ? null
360 : group.summary == null ? null
Evan Laird94492852018-10-25 13:43:01 -0400361 : group.summary;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100362 }
363
Kevin01a53cb2018-11-09 18:19:54 -0800364 /**
365 * Get the children that are logically in the summary's group, whether or not they are isolated.
366 *
367 * @param summary summary of a group
368 * @return list of the children
369 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500370 public ArrayList<NotificationEntry> getLogicalChildren(StatusBarNotification summary) {
Kevin01a53cb2018-11-09 18:19:54 -0800371 NotificationGroup group = mGroupMap.get(summary.getGroupKey());
372 if (group == null) {
373 return null;
374 }
Ned Burnsf81c4c42019-01-07 14:10:43 -0500375 ArrayList<NotificationEntry> children = new ArrayList<>(group.children.values());
376 NotificationEntry isolatedChild = getIsolatedChild(summary.getGroupKey());
Kevin01a53cb2018-11-09 18:19:54 -0800377 if (isolatedChild != null) {
378 children.add(isolatedChild);
379 }
380 return children;
381 }
382
383 /**
384 * Get the group key. May differ from the one in the notification due to the notification
385 * being temporarily isolated.
386 *
387 * @param sbn notification to check
388 * @return the key of the notification
389 */
390 public String getGroupKey(StatusBarNotification sbn) {
391 if (isIsolated(sbn)) {
392 return sbn.getKey();
393 }
394 return sbn.getGroupKey();
395 }
396
Chris Wren698b1702016-05-23 11:16:32 -0400397 /** @return group expansion state after toggling. */
398 public boolean toggleGroupExpansion(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800399 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek83bc7832015-10-22 13:26:54 -0700400 if (group == null) {
Chris Wren698b1702016-05-23 11:16:32 -0400401 return false;
Selim Cinek83bc7832015-10-22 13:26:54 -0700402 }
403 setGroupExpanded(group, !group.expanded);
Chris Wren698b1702016-05-23 11:16:32 -0400404 return group.expanded;
Selim Cinek83bc7832015-10-22 13:26:54 -0700405 }
406
Selim Cinekef5127e2015-12-21 16:55:58 -0800407 private boolean isIsolated(StatusBarNotification sbn) {
Selim Cineka6c0cef2016-03-18 11:42:25 -0700408 return mIsolatedEntries.containsKey(sbn.getKey());
Selim Cinekef5127e2015-12-21 16:55:58 -0800409 }
410
Kevin01a53cb2018-11-09 18:19:54 -0800411 /**
412 * Whether a notification is visually a group summary.
413 *
414 * @param sbn notification to check
415 * @return true if it is visually a group summary
416 */
417 public boolean isGroupSummary(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800418 if (isIsolated(sbn)) {
419 return true;
420 }
421 return sbn.getNotification().isGroupSummary();
422 }
Julia Reynoldse46bb372016-03-17 11:05:58 -0400423
Kevin01a53cb2018-11-09 18:19:54 -0800424 /**
425 * Whether a notification is visually a group child.
426 *
427 * @param sbn notification to check
428 * @return true if it is visually a group child
429 */
430 public boolean isGroupChild(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800431 if (isIsolated(sbn)) {
432 return false;
433 }
Julia Reynoldse46bb372016-03-17 11:05:58 -0400434 return sbn.isGroup() && !sbn.getNotification().isGroupSummary();
Selim Cinekef5127e2015-12-21 16:55:58 -0800435 }
436
Selim Cinekef5127e2015-12-21 16:55:58 -0800437 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500438 public void onHeadsUpStateChanged(NotificationEntry entry, boolean isHeadsUp) {
Kevin01a53cb2018-11-09 18:19:54 -0800439 onAlertStateChanged(entry, isHeadsUp);
Kevina97ea052018-09-11 13:53:18 -0700440 }
441
Ned Burnsf81c4c42019-01-07 14:10:43 -0500442 private void onAlertStateChanged(NotificationEntry entry, boolean isAlerting) {
Kevina97ea052018-09-11 13:53:18 -0700443 if (isAlerting) {
444 if (shouldIsolate(entry)) {
445 isolateNotification(entry);
Selim Cinekef5127e2015-12-21 16:55:58 -0800446 }
447 } else {
Kevina97ea052018-09-11 13:53:18 -0700448 stopIsolatingNotification(entry);
Selim Cinekef5127e2015-12-21 16:55:58 -0800449 }
450 }
451
Kevina97ea052018-09-11 13:53:18 -0700452 /**
Kevina97ea052018-09-11 13:53:18 -0700453 * Whether a notification that is normally part of a group should be temporarily isolated from
454 * the group and put in their own group visually. This generally happens when the notification
455 * is alerting.
456 *
457 * @param entry the notification to check
458 * @return true if the entry should be isolated
459 */
460
Ned Burnsf81c4c42019-01-07 14:10:43 -0500461 private boolean shouldIsolate(NotificationEntry entry) {
Kevina97ea052018-09-11 13:53:18 -0700462 StatusBarNotification sbn = entry.notification;
Selim Cineka6c0cef2016-03-18 11:42:25 -0700463 NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey());
Kevina97ea052018-09-11 13:53:18 -0700464 if (!sbn.isGroup() || sbn.getNotification().isGroupSummary()) {
465 return false;
466 }
Selim Cinekc3fec682019-06-06 18:11:07 -0700467 if (!mHeadsUpManager.isAlerting(entry.key)) {
Kevina97ea052018-09-11 13:53:18 -0700468 return false;
469 }
470 return (sbn.getNotification().fullScreenIntent != null
471 || notificationGroup == null
472 || !notificationGroup.expanded
473 || isGroupNotFullyVisible(notificationGroup));
474 }
475
476 /**
477 * Isolate a notification from its group so that it visually shows as its own group.
478 *
479 * @param entry the notification to isolate
480 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500481 private void isolateNotification(NotificationEntry entry) {
Kevina97ea052018-09-11 13:53:18 -0700482 StatusBarNotification sbn = entry.notification;
483
484 // We will be isolated now, so lets update the groups
485 onEntryRemovedInternal(entry, entry.notification);
486
487 mIsolatedEntries.put(sbn.getKey(), sbn);
488
489 onEntryAdded(entry);
490 // We also need to update the suppression of the old group, because this call comes
491 // even before the groupManager knows about the notification at all.
492 // When the notification gets added afterwards it is already isolated and therefore
493 // it doesn't lead to an update.
494 updateSuppression(mGroupMap.get(entry.notification.getGroupKey()));
Kevin01a53cb2018-11-09 18:19:54 -0800495 for (OnGroupChangeListener listener : mListeners) {
496 listener.onGroupsChanged();
497 }
Kevina97ea052018-09-11 13:53:18 -0700498 }
499
500 /**
501 * Stop isolating a notification and re-group it with its original logical group.
502 *
503 * @param entry the notification to un-isolate
504 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500505 private void stopIsolatingNotification(NotificationEntry entry) {
Kevina97ea052018-09-11 13:53:18 -0700506 StatusBarNotification sbn = entry.notification;
507 if (mIsolatedEntries.containsKey(sbn.getKey())) {
508 // not isolated anymore, we need to update the groups
509 onEntryRemovedInternal(entry, entry.notification);
510 mIsolatedEntries.remove(sbn.getKey());
511 onEntryAdded(entry);
Kevin01a53cb2018-11-09 18:19:54 -0800512 for (OnGroupChangeListener listener : mListeners) {
513 listener.onGroupsChanged();
514 }
Kevina97ea052018-09-11 13:53:18 -0700515 }
Selim Cineka6c0cef2016-03-18 11:42:25 -0700516 }
517
518 private boolean isGroupNotFullyVisible(NotificationGroup notificationGroup) {
519 return notificationGroup.summary == null
Evan Laird94492852018-10-25 13:43:01 -0400520 || notificationGroup.summary.isGroupNotFullyVisible();
Selim Cineka6c0cef2016-03-18 11:42:25 -0700521 }
522
Selim Cinek967ed2a2016-04-08 18:29:11 -0700523 public void setHeadsUpManager(HeadsUpManager headsUpManager) {
524 mHeadsUpManager = headsUpManager;
525 }
526
Selim Cinek52941c52016-05-07 18:29:32 -0400527 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
528 pw.println("GroupManager state:");
529 pw.println(" number of groups: " + mGroupMap.size());
530 for (Map.Entry<String, NotificationGroup> entry : mGroupMap.entrySet()) {
531 pw.println("\n key: " + entry.getKey()); pw.println(entry.getValue());
532 }
533 pw.println("\n isolated entries: " + mIsolatedEntries.size());
534 for (Map.Entry<String, StatusBarNotification> entry : mIsolatedEntries.entrySet()) {
535 pw.print(" "); pw.print(entry.getKey());
536 pw.print(", "); pw.println(entry.getValue());
537 }
538 }
539
Evan Laird878c8532018-10-15 15:54:29 -0400540 @Override
541 public void onStateChanged(int newState) {
542 setStatusBarState(newState);
543 }
544
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100545 public static class NotificationGroup {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500546 public final HashMap<String, NotificationEntry> children = new HashMap<>();
547 public NotificationEntry summary;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100548 public boolean expanded;
Selim Cinek2a739342016-03-17 10:28:55 -0700549 /**
550 * Is this notification group suppressed, i.e its summary is hidden
551 */
552 public boolean suppressed;
Selim Cinek52941c52016-05-07 18:29:32 -0400553
554 @Override
555 public String toString() {
Selim Cinek60ca7872016-05-12 11:24:07 -0700556 String result = " summary:\n "
Selim Cinek04be3892017-08-08 10:58:32 -0700557 + (summary != null ? summary.notification : "null")
558 + (summary != null && summary.getDebugThrowable() != null
559 ? Log.getStackTraceString(summary.getDebugThrowable())
560 : "");
Selim Cinek52941c52016-05-07 18:29:32 -0400561 result += "\n children size: " + children.size();
Ned Burnsf81c4c42019-01-07 14:10:43 -0500562 for (NotificationEntry child : children.values()) {
Selim Cinek04be3892017-08-08 10:58:32 -0700563 result += "\n " + child.notification
564 + (child.getDebugThrowable() != null
565 ? Log.getStackTraceString(child.getDebugThrowable())
566 : "");
Selim Cinek52941c52016-05-07 18:29:32 -0400567 }
568 return result;
569 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100570 }
571
572 public interface OnGroupChangeListener {
Kevin01a53cb2018-11-09 18:19:54 -0800573
574 /**
575 * A new group has been created.
576 *
577 * @param group the group that was created
578 * @param groupKey the group's key
579 */
580 default void onGroupCreated(NotificationGroup group, String groupKey) {}
581
582 /**
583 * A group has been removed.
584 *
585 * @param group the group that was removed
586 * @param groupKey the group's key
587 */
588 default void onGroupRemoved(NotificationGroup group, String groupKey) {}
589
590 /**
591 * The suppression of a group has changed.
592 *
593 * @param group the group that has changed
594 * @param suppressed true if the group is now suppressed, false o/w
595 */
596 default void onGroupSuppressionChanged(NotificationGroup group, boolean suppressed) {}
597
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100598 /**
599 * The expansion of a group has changed.
600 *
601 * @param changedRow the row for which the expansion has changed, which is also the summary
602 * @param expanded a boolean indicating the new expanded state
603 */
Kevin01a53cb2018-11-09 18:19:54 -0800604 default void onGroupExpansionChanged(ExpandableNotificationRow changedRow,
605 boolean expanded) {}
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100606
607 /**
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100608 * A group of children just received a summary notification and should therefore become
609 * children of it.
610 *
611 * @param group the group created
612 */
Kevin01a53cb2018-11-09 18:19:54 -0800613 default void onGroupCreatedFromChildren(NotificationGroup group) {}
Selim Cinekef5127e2015-12-21 16:55:58 -0800614
615 /**
Selim Cinek2a739342016-03-17 10:28:55 -0700616 * The groups have changed. This can happen if the isolation of a child has changes or if a
617 * group became suppressed / unsuppressed
Selim Cinekef5127e2015-12-21 16:55:58 -0800618 */
Kevin01a53cb2018-11-09 18:19:54 -0800619 default void onGroupsChanged() {}
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100620 }
621}