blob: 448b5c38da51781411d7bab94552688f17c6bd53 [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;
Kevina97ea052018-09-11 13:53:18 -070025import com.android.systemui.statusbar.AmbientPulseManager;
26import com.android.systemui.statusbar.AmbientPulseManager.OnAmbientChangedListener;
Jason Monk1fd3fc32018-08-14 17:20:09 -040027import com.android.systemui.statusbar.StatusBarState;
Jason Monk1fd3fc32018-08-14 17:20:09 -040028import com.android.systemui.statusbar.StatusBarStateController;
Evan Laird878c8532018-10-15 15:54:29 -040029import com.android.systemui.statusbar.StatusBarStateController.StateListener;
30import com.android.systemui.statusbar.notification.NotificationData;
31import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
Selim Cinekef5127e2015-12-21 16:55:58 -080032import com.android.systemui.statusbar.policy.HeadsUpManager;
Selim Cineka7d4f822016-12-06 14:34:47 -080033import com.android.systemui.statusbar.policy.OnHeadsUpChangedListener;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010034
Selim Cinek52941c52016-05-07 18:29:32 -040035import java.io.FileDescriptor;
36import java.io.PrintWriter;
Selim Cinekc0b14b02016-05-09 13:12:40 -040037import java.util.ArrayList;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010038import java.util.HashMap;
Selim Cinek52941c52016-05-07 18:29:32 -040039import java.util.Map;
Selim Cinekf93bf3e2018-05-08 14:43:21 -070040import java.util.Objects;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010041
42/**
43 * A class to handle notifications and their corresponding groups.
44 */
Kevina97ea052018-09-11 13:53:18 -070045public class NotificationGroupManager implements OnHeadsUpChangedListener,
Evan Laird878c8532018-10-15 15:54:29 -040046 OnAmbientChangedListener, StateListener {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010047
Selim Cinek04be3892017-08-08 10:58:32 -070048 private static final String TAG = "NotificationGroupManager";
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010049 private final HashMap<String, NotificationGroup> mGroupMap = new HashMap<>();
Kevin01a53cb2018-11-09 18:19:54 -080050 private final ArraySet<OnGroupChangeListener> mListeners = new ArraySet<>();
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010051 private int mBarState = -1;
Selim Cineka6c0cef2016-03-18 11:42:25 -070052 private HashMap<String, StatusBarNotification> mIsolatedEntries = new HashMap<>();
Selim Cinek967ed2a2016-04-08 18:29:11 -070053 private HeadsUpManager mHeadsUpManager;
Kevina97ea052018-09-11 13:53:18 -070054 private AmbientPulseManager mAmbientPulseManager = Dependency.get(AmbientPulseManager.class);
Selim Cinek80c44dd2016-08-15 19:39:55 -070055 private boolean mIsUpdatingUnchangedGroup;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010056
Jason Monk1fd3fc32018-08-14 17:20:09 -040057 public NotificationGroupManager() {
Evan Laird878c8532018-10-15 15:54:29 -040058 Dependency.get(StatusBarStateController.class).addListener(this);
Jason Monk1fd3fc32018-08-14 17:20:09 -040059 }
60
Kevin01a53cb2018-11-09 18:19:54 -080061 /**
62 * Add a listener for changes to groups.
63 *
64 * @param listener listener to add
65 */
66 public void addOnGroupChangeListener(OnGroupChangeListener listener) {
67 mListeners.add(listener);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010068 }
69
70 public boolean isGroupExpanded(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -080071 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010072 if (group == null) {
73 return false;
74 }
75 return group.expanded;
76 }
77
78 public void setGroupExpanded(StatusBarNotification sbn, boolean expanded) {
Selim Cinekef5127e2015-12-21 16:55:58 -080079 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010080 if (group == null) {
81 return;
82 }
83 setGroupExpanded(group, expanded);
84 }
85
86 private void setGroupExpanded(NotificationGroup group, boolean expanded) {
87 group.expanded = expanded;
88 if (group.summary != null) {
Kevin01a53cb2018-11-09 18:19:54 -080089 for (OnGroupChangeListener listener : mListeners) {
Evan Laird94492852018-10-25 13:43:01 -040090 listener.onGroupExpansionChanged(group.summary.getRow(), expanded);
Kevin01a53cb2018-11-09 18:19:54 -080091 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010092 }
93 }
94
95 public void onEntryRemoved(NotificationData.Entry removed) {
96 onEntryRemovedInternal(removed, removed.notification);
Selim Cinek52941c52016-05-07 18:29:32 -040097 mIsolatedEntries.remove(removed.key);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010098 }
99
100 /**
101 * An entry was removed.
102 *
103 * @param removed the removed entry
104 * @param sbn the notification the entry has, which doesn't need to be the same as it's internal
105 * notification
106 */
107 private void onEntryRemovedInternal(NotificationData.Entry removed,
108 final StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800109 String groupKey = getGroupKey(sbn);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100110 final NotificationGroup group = mGroupMap.get(groupKey);
Selim Cinek0b4aeab2015-09-01 17:07:38 -0700111 if (group == null) {
112 // When an app posts 2 different notifications as summary of the same group, then a
113 // cancellation of the first notification removes this group.
114 // This situation is not supported and we will not allow such notifications anymore in
115 // the close future. See b/23676310 for reference.
116 return;
117 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800118 if (isGroupChild(sbn)) {
Selim Cinek04be3892017-08-08 10:58:32 -0700119 group.children.remove(removed.key);
Selim Cineke73ad212015-11-03 19:11:08 -0800120 } else {
121 group.summary = null;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100122 }
Selim Cinek2a739342016-03-17 10:28:55 -0700123 updateSuppression(group);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100124 if (group.children.isEmpty()) {
125 if (group.summary == null) {
126 mGroupMap.remove(groupKey);
Kevin01a53cb2018-11-09 18:19:54 -0800127 for (OnGroupChangeListener listener : mListeners) {
128 listener.onGroupRemoved(group, groupKey);
129 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100130 }
131 }
132 }
133
Selim Cinekef5127e2015-12-21 16:55:58 -0800134 public void onEntryAdded(final NotificationData.Entry added) {
Evan Laird94492852018-10-25 13:43:01 -0400135 if (added.isRowRemoved()) {
Selim Cinek04be3892017-08-08 10:58:32 -0700136 added.setDebugThrowable(new Throwable());
137 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800138 final StatusBarNotification sbn = added.notification;
139 boolean isGroupChild = isGroupChild(sbn);
140 String groupKey = getGroupKey(sbn);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100141 NotificationGroup group = mGroupMap.get(groupKey);
142 if (group == null) {
143 group = new NotificationGroup();
144 mGroupMap.put(groupKey, group);
Kevin01a53cb2018-11-09 18:19:54 -0800145 for (OnGroupChangeListener listener : mListeners) {
146 listener.onGroupCreated(group, groupKey);
147 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100148 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800149 if (isGroupChild) {
Selim Cinek04be3892017-08-08 10:58:32 -0700150 NotificationData.Entry existing = group.children.get(added.key);
151 if (existing != null && existing != added) {
152 Throwable existingThrowable = existing.getDebugThrowable();
153 Log.wtf(TAG, "Inconsistent entries found with the same key " + added.key
Evan Laird94492852018-10-25 13:43:01 -0400154 + "existing removed: " + existing.isRowRemoved()
Selim Cinek04be3892017-08-08 10:58:32 -0700155 + (existingThrowable != null
156 ? Log.getStackTraceString(existingThrowable) + "\n": "")
Evan Laird94492852018-10-25 13:43:01 -0400157 + " added removed" + added.isRowRemoved()
Selim Cinek04be3892017-08-08 10:58:32 -0700158 , new Throwable());
159 }
160 group.children.put(added.key, added);
Selim Cinek2a739342016-03-17 10:28:55 -0700161 updateSuppression(group);
Selim Cineke73ad212015-11-03 19:11:08 -0800162 } else {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100163 group.summary = added;
Evan Laird94492852018-10-25 13:43:01 -0400164 group.expanded = added.areChildrenExpanded();
Selim Cinek2a739342016-03-17 10:28:55 -0700165 updateSuppression(group);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100166 if (!group.children.isEmpty()) {
Selim Cinek04be3892017-08-08 10:58:32 -0700167 ArrayList<NotificationData.Entry> childrenCopy
168 = new ArrayList<>(group.children.values());
Selim Cinek50e74672016-05-05 15:58:10 -0400169 for (NotificationData.Entry child : childrenCopy) {
170 onEntryBecomingChild(child);
171 }
Kevin01a53cb2018-11-09 18:19:54 -0800172 for (OnGroupChangeListener listener : mListeners) {
173 listener.onGroupCreatedFromChildren(group);
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700174 }
175 }
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700176 }
177 }
178
Selim Cinek50e74672016-05-05 15:58:10 -0400179 private void onEntryBecomingChild(NotificationData.Entry entry) {
Kevina97ea052018-09-11 13:53:18 -0700180 if (shouldIsolate(entry)) {
181 isolateNotification(entry);
Selim Cinek50e74672016-05-05 15:58:10 -0400182 }
183 }
184
Selim Cinek2a739342016-03-17 10:28:55 -0700185 private void updateSuppression(NotificationGroup group) {
Selim Cinek80c44dd2016-08-15 19:39:55 -0700186 if (group == null) {
Selim Cinek23c80342016-03-17 18:27:36 -0700187 return;
188 }
Selim Cinek2a739342016-03-17 10:28:55 -0700189 boolean prevSuppressed = group.suppressed;
Selim Cinek23c80342016-03-17 18:27:36 -0700190 group.suppressed = group.summary != null && !group.expanded
191 && (group.children.size() == 1
192 || (group.children.size() == 0
Julia Reynoldse46bb372016-03-17 11:05:58 -0400193 && group.summary.notification.getNotification().isGroupSummary()
Selim Cinek23c80342016-03-17 18:27:36 -0700194 && hasIsolatedChildren(group)));
Selim Cinek2a739342016-03-17 10:28:55 -0700195 if (prevSuppressed != group.suppressed) {
Kevin01a53cb2018-11-09 18:19:54 -0800196 for (OnGroupChangeListener listener : mListeners) {
197 if (!mIsUpdatingUnchangedGroup) {
198 listener.onGroupSuppressionChanged(group, group.suppressed);
199 listener.onGroupsChanged();
Kevina97ea052018-09-11 13:53:18 -0700200 }
Selim Cinek967ed2a2016-04-08 18:29:11 -0700201 }
Selim Cinek2a739342016-03-17 10:28:55 -0700202 }
203 }
204
Selim Cinek23c80342016-03-17 18:27:36 -0700205 private boolean hasIsolatedChildren(NotificationGroup group) {
206 return getNumberOfIsolatedChildren(group.summary.notification.getGroupKey()) != 0;
207 }
208
209 private int getNumberOfIsolatedChildren(String groupKey) {
210 int count = 0;
Selim Cineka6c0cef2016-03-18 11:42:25 -0700211 for (StatusBarNotification sbn : mIsolatedEntries.values()) {
Selim Cinek23c80342016-03-17 18:27:36 -0700212 if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) {
213 count++;
214 }
215 }
216 return count;
217 }
218
Selim Cinek967ed2a2016-04-08 18:29:11 -0700219 private NotificationData.Entry getIsolatedChild(String groupKey) {
220 for (StatusBarNotification sbn : mIsolatedEntries.values()) {
221 if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) {
222 return mGroupMap.get(sbn.getKey()).summary;
223 }
224 }
225 return null;
226 }
227
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100228 public void onEntryUpdated(NotificationData.Entry entry,
229 StatusBarNotification oldNotification) {
Selim Cinek68bdff12016-08-03 13:38:56 -0700230 String oldKey = oldNotification.getGroupKey();
231 String newKey = entry.notification.getGroupKey();
232 boolean groupKeysChanged = !oldKey.equals(newKey);
233 boolean wasGroupChild = isGroupChild(oldNotification);
234 boolean isGroupChild = isGroupChild(entry.notification);
Selim Cinek80c44dd2016-08-15 19:39:55 -0700235 mIsUpdatingUnchangedGroup = !groupKeysChanged && wasGroupChild == isGroupChild;
Selim Cinekef5127e2015-12-21 16:55:58 -0800236 if (mGroupMap.get(getGroupKey(oldNotification)) != null) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100237 onEntryRemovedInternal(entry, oldNotification);
238 }
239 onEntryAdded(entry);
Selim Cinek80c44dd2016-08-15 19:39:55 -0700240 mIsUpdatingUnchangedGroup = false;
Selim Cineka6c0cef2016-03-18 11:42:25 -0700241 if (isIsolated(entry.notification)) {
242 mIsolatedEntries.put(entry.key, entry.notification);
Selim Cinek68bdff12016-08-03 13:38:56 -0700243 if (groupKeysChanged) {
Selim Cinek23c80342016-03-17 18:27:36 -0700244 updateSuppression(mGroupMap.get(oldKey));
245 updateSuppression(mGroupMap.get(newKey));
246 }
Selim Cinek68bdff12016-08-03 13:38:56 -0700247 } else if (!wasGroupChild && isGroupChild) {
Selim Cinek50e74672016-05-05 15:58:10 -0400248 onEntryBecomingChild(entry);
Selim Cinek23c80342016-03-17 18:27:36 -0700249 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100250 }
251
Selim Cinek2a739342016-03-17 10:28:55 -0700252 public boolean isSummaryOfSuppressedGroup(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700253 return isGroupSuppressed(getGroupKey(sbn)) && sbn.getNotification().isGroupSummary();
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100254 }
255
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700256 private boolean isOnlyChild(StatusBarNotification sbn) {
Selim Cinek36b02232016-05-11 23:07:05 -0400257 return !sbn.getNotification().isGroupSummary()
Selim Cinek23c80342016-03-17 18:27:36 -0700258 && getTotalNumberOfChildren(sbn) == 1;
Selim Cinek2a739342016-03-17 10:28:55 -0700259 }
260
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700261 public boolean isOnlyChildInGroup(StatusBarNotification sbn) {
Selim Cinek88086e72016-06-17 21:01:32 -0700262 if (!isOnlyChild(sbn)) {
263 return false;
264 }
Evan Laird94492852018-10-25 13:43:01 -0400265 NotificationData.Entry logicalGroupSummary = getLogicalGroupSummary(sbn);
Selim Cinek88086e72016-06-17 21:01:32 -0700266 return logicalGroupSummary != null
Evan Laird94492852018-10-25 13:43:01 -0400267 && !logicalGroupSummary.notification.equals(sbn);
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700268 }
269
Selim Cinek23c80342016-03-17 18:27:36 -0700270 private int getTotalNumberOfChildren(StatusBarNotification sbn) {
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700271 int isolatedChildren = getNumberOfIsolatedChildren(sbn.getGroupKey());
272 NotificationGroup group = mGroupMap.get(sbn.getGroupKey());
273 int realChildren = group != null ? group.children.size() : 0;
274 return isolatedChildren + realChildren;
Selim Cinek23c80342016-03-17 18:27:36 -0700275 }
276
277 private boolean isGroupSuppressed(String groupKey) {
278 NotificationGroup group = mGroupMap.get(groupKey);
Selim Cinek2a739342016-03-17 10:28:55 -0700279 return group != null && group.suppressed;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100280 }
281
Jason Monk1fd3fc32018-08-14 17:20:09 -0400282 private void setStatusBarState(int newState) {
Selim Cinek9c4c4142015-12-04 16:44:56 -0800283 mBarState = newState;
284 if (mBarState == StatusBarState.KEYGUARD) {
Selim Cinek9184f9c2016-02-02 17:36:53 -0800285 collapseAllGroups();
286 }
287 }
288
289 public void collapseAllGroups() {
Selim Cinekc0b14b02016-05-09 13:12:40 -0400290 // Because notifications can become isolated when the group becomes suppressed it can
291 // lead to concurrent modifications while looping. We need to make a copy.
292 ArrayList<NotificationGroup> groupCopy = new ArrayList<>(mGroupMap.values());
293 int size = groupCopy.size();
294 for (int i = 0; i < size; i++) {
295 NotificationGroup group = groupCopy.get(i);
Selim Cinek9184f9c2016-02-02 17:36:53 -0800296 if (group.expanded) {
297 setGroupExpanded(group, false);
Selim Cinek9c4c4142015-12-04 16:44:56 -0800298 }
Selim Cinek2a739342016-03-17 10:28:55 -0700299 updateSuppression(group);
Selim Cinek9c4c4142015-12-04 16:44:56 -0800300 }
301 }
302
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100303 /**
304 * @return whether a given notification is a child in a group which has a summary
305 */
306 public boolean isChildInGroupWithSummary(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800307 if (!isGroupChild(sbn)) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100308 return false;
309 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800310 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek2a739342016-03-17 10:28:55 -0700311 if (group == null || group.summary == null || group.suppressed) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100312 return false;
313 }
Selim Cinek3f19f602016-05-02 18:01:56 -0700314 if (group.children.isEmpty()) {
315 // If the suppression of a group changes because the last child was removed, this can
316 // still be called temporarily because the child hasn't been fully removed yet. Let's
317 // make sure we still return false in that case.
318 return false;
319 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100320 return true;
321 }
322
Selim Cinek263398f2015-10-21 17:40:23 -0700323 /**
324 * @return whether a given notification is a summary in a group which has children
325 */
326 public boolean isSummaryOfGroup(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800327 if (!isGroupSummary(sbn)) {
Selim Cinek263398f2015-10-21 17:40:23 -0700328 return false;
329 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800330 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Kevin96eed642018-09-05 18:53:33 -0700331 if (group == null || group.summary == null) {
Selim Cinek263398f2015-10-21 17:40:23 -0700332 return false;
333 }
Kevin96eed642018-09-05 18:53:33 -0700334 return !group.children.isEmpty() && Objects.equals(group.summary.notification, sbn);
Selim Cinek263398f2015-10-21 17:40:23 -0700335 }
336
Selim Cinek23c80342016-03-17 18:27:36 -0700337 /**
338 * Get the summary of a specified status bar notification. For isolated notification this return
339 * itself.
340 */
Evan Laird94492852018-10-25 13:43:01 -0400341 public NotificationData.Entry getGroupSummary(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700342 return getGroupSummary(getGroupKey(sbn));
343 }
344
345 /**
346 * Similar to {@link #getGroupSummary(StatusBarNotification)} but doesn't get the visual summary
347 * but the logical summary, i.e when a child is isolated, it still returns the summary as if
348 * it wasn't isolated.
349 */
Evan Laird94492852018-10-25 13:43:01 -0400350 public NotificationData.Entry getLogicalGroupSummary(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700351 return getGroupSummary(sbn.getGroupKey());
352 }
353
354 @Nullable
Evan Laird94492852018-10-25 13:43:01 -0400355 private NotificationData.Entry getGroupSummary(String groupKey) {
Selim Cinek23c80342016-03-17 18:27:36 -0700356 NotificationGroup group = mGroupMap.get(groupKey);
Evan Laird94492852018-10-25 13:43:01 -0400357 //TODO: see if this can become an Entry
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100358 return group == null ? null
359 : group.summary == null ? null
Evan Laird94492852018-10-25 13:43:01 -0400360 : group.summary;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100361 }
362
Kevin01a53cb2018-11-09 18:19:54 -0800363 /**
364 * Get the children that are logically in the summary's group, whether or not they are isolated.
365 *
366 * @param summary summary of a group
367 * @return list of the children
368 */
369 public ArrayList<NotificationData.Entry> getLogicalChildren(StatusBarNotification summary) {
370 NotificationGroup group = mGroupMap.get(summary.getGroupKey());
371 if (group == null) {
372 return null;
373 }
374 ArrayList<NotificationData.Entry> children = new ArrayList<>(group.children.values());
375 NotificationData.Entry isolatedChild = getIsolatedChild(summary.getGroupKey());
376 if (isolatedChild != null) {
377 children.add(isolatedChild);
378 }
379 return children;
380 }
381
382 /**
383 * Get the group key. May differ from the one in the notification due to the notification
384 * being temporarily isolated.
385 *
386 * @param sbn notification to check
387 * @return the key of the notification
388 */
389 public String getGroupKey(StatusBarNotification sbn) {
390 if (isIsolated(sbn)) {
391 return sbn.getKey();
392 }
393 return sbn.getGroupKey();
394 }
395
Chris Wren698b1702016-05-23 11:16:32 -0400396 /** @return group expansion state after toggling. */
397 public boolean toggleGroupExpansion(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800398 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek83bc7832015-10-22 13:26:54 -0700399 if (group == null) {
Chris Wren698b1702016-05-23 11:16:32 -0400400 return false;
Selim Cinek83bc7832015-10-22 13:26:54 -0700401 }
402 setGroupExpanded(group, !group.expanded);
Chris Wren698b1702016-05-23 11:16:32 -0400403 return group.expanded;
Selim Cinek83bc7832015-10-22 13:26:54 -0700404 }
405
Selim Cinekef5127e2015-12-21 16:55:58 -0800406 private boolean isIsolated(StatusBarNotification sbn) {
Selim Cineka6c0cef2016-03-18 11:42:25 -0700407 return mIsolatedEntries.containsKey(sbn.getKey());
Selim Cinekef5127e2015-12-21 16:55:58 -0800408 }
409
Kevin01a53cb2018-11-09 18:19:54 -0800410 /**
411 * Whether a notification is visually a group summary.
412 *
413 * @param sbn notification to check
414 * @return true if it is visually a group summary
415 */
416 public boolean isGroupSummary(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800417 if (isIsolated(sbn)) {
418 return true;
419 }
420 return sbn.getNotification().isGroupSummary();
421 }
Julia Reynoldse46bb372016-03-17 11:05:58 -0400422
Kevin01a53cb2018-11-09 18:19:54 -0800423 /**
424 * Whether a notification is visually a group child.
425 *
426 * @param sbn notification to check
427 * @return true if it is visually a group child
428 */
429 public boolean isGroupChild(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800430 if (isIsolated(sbn)) {
431 return false;
432 }
Julia Reynoldse46bb372016-03-17 11:05:58 -0400433 return sbn.isGroup() && !sbn.getNotification().isGroupSummary();
Selim Cinekef5127e2015-12-21 16:55:58 -0800434 }
435
Selim Cinekef5127e2015-12-21 16:55:58 -0800436 @Override
437 public void onHeadsUpPinnedModeChanged(boolean inPinnedMode) {
438 }
439
440 @Override
Evan Laird94492852018-10-25 13:43:01 -0400441 public void onHeadsUpPinned(NotificationData.Entry entry) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800442 }
443
444 @Override
Evan Laird94492852018-10-25 13:43:01 -0400445 public void onHeadsUpUnPinned(NotificationData.Entry entry) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800446 }
447
448 @Override
Kevina97ea052018-09-11 13:53:18 -0700449 public void onAmbientStateChanged(NotificationData.Entry entry, boolean isAmbient) {
Kevin01a53cb2018-11-09 18:19:54 -0800450 onAlertStateChanged(entry, isAmbient);
Kevina97ea052018-09-11 13:53:18 -0700451 }
452
453 @Override
Selim Cinekef5127e2015-12-21 16:55:58 -0800454 public void onHeadsUpStateChanged(NotificationData.Entry entry, boolean isHeadsUp) {
Kevin01a53cb2018-11-09 18:19:54 -0800455 onAlertStateChanged(entry, isHeadsUp);
Kevina97ea052018-09-11 13:53:18 -0700456 }
457
Kevin01a53cb2018-11-09 18:19:54 -0800458 private void onAlertStateChanged(NotificationData.Entry entry, boolean isAlerting) {
Kevina97ea052018-09-11 13:53:18 -0700459 if (isAlerting) {
460 if (shouldIsolate(entry)) {
461 isolateNotification(entry);
Selim Cinekef5127e2015-12-21 16:55:58 -0800462 }
463 } else {
Kevina97ea052018-09-11 13:53:18 -0700464 stopIsolatingNotification(entry);
Selim Cinekef5127e2015-12-21 16:55:58 -0800465 }
466 }
467
Kevina97ea052018-09-11 13:53:18 -0700468 /**
Kevina97ea052018-09-11 13:53:18 -0700469 * Whether a notification that is normally part of a group should be temporarily isolated from
470 * the group and put in their own group visually. This generally happens when the notification
471 * is alerting.
472 *
473 * @param entry the notification to check
474 * @return true if the entry should be isolated
475 */
476
477 private boolean shouldIsolate(NotificationData.Entry entry) {
478 StatusBarNotification sbn = entry.notification;
Selim Cineka6c0cef2016-03-18 11:42:25 -0700479 NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey());
Kevina97ea052018-09-11 13:53:18 -0700480 if (!sbn.isGroup() || sbn.getNotification().isGroupSummary()) {
481 return false;
482 }
Kevin01a53cb2018-11-09 18:19:54 -0800483 if (!mHeadsUpManager.isAlerting(entry.key) && !mAmbientPulseManager.isAlerting(entry.key)) {
Kevina97ea052018-09-11 13:53:18 -0700484 return false;
485 }
486 return (sbn.getNotification().fullScreenIntent != null
487 || notificationGroup == null
488 || !notificationGroup.expanded
489 || isGroupNotFullyVisible(notificationGroup));
490 }
491
492 /**
493 * Isolate a notification from its group so that it visually shows as its own group.
494 *
495 * @param entry the notification to isolate
496 */
497 private void isolateNotification(NotificationData.Entry entry) {
498 StatusBarNotification sbn = entry.notification;
499
500 // We will be isolated now, so lets update the groups
501 onEntryRemovedInternal(entry, entry.notification);
502
503 mIsolatedEntries.put(sbn.getKey(), sbn);
504
505 onEntryAdded(entry);
506 // We also need to update the suppression of the old group, because this call comes
507 // even before the groupManager knows about the notification at all.
508 // When the notification gets added afterwards it is already isolated and therefore
509 // it doesn't lead to an update.
510 updateSuppression(mGroupMap.get(entry.notification.getGroupKey()));
Kevin01a53cb2018-11-09 18:19:54 -0800511 for (OnGroupChangeListener listener : mListeners) {
512 listener.onGroupsChanged();
513 }
Kevina97ea052018-09-11 13:53:18 -0700514 }
515
516 /**
517 * Stop isolating a notification and re-group it with its original logical group.
518 *
519 * @param entry the notification to un-isolate
520 */
521 private void stopIsolatingNotification(NotificationData.Entry entry) {
522 StatusBarNotification sbn = entry.notification;
523 if (mIsolatedEntries.containsKey(sbn.getKey())) {
524 // not isolated anymore, we need to update the groups
525 onEntryRemovedInternal(entry, entry.notification);
526 mIsolatedEntries.remove(sbn.getKey());
527 onEntryAdded(entry);
Kevin01a53cb2018-11-09 18:19:54 -0800528 for (OnGroupChangeListener listener : mListeners) {
529 listener.onGroupsChanged();
530 }
Kevina97ea052018-09-11 13:53:18 -0700531 }
Selim Cineka6c0cef2016-03-18 11:42:25 -0700532 }
533
534 private boolean isGroupNotFullyVisible(NotificationGroup notificationGroup) {
535 return notificationGroup.summary == null
Evan Laird94492852018-10-25 13:43:01 -0400536 || notificationGroup.summary.isGroupNotFullyVisible();
Selim Cineka6c0cef2016-03-18 11:42:25 -0700537 }
538
Selim Cinek967ed2a2016-04-08 18:29:11 -0700539 public void setHeadsUpManager(HeadsUpManager headsUpManager) {
540 mHeadsUpManager = headsUpManager;
541 }
542
Selim Cinek52941c52016-05-07 18:29:32 -0400543 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
544 pw.println("GroupManager state:");
545 pw.println(" number of groups: " + mGroupMap.size());
546 for (Map.Entry<String, NotificationGroup> entry : mGroupMap.entrySet()) {
547 pw.println("\n key: " + entry.getKey()); pw.println(entry.getValue());
548 }
549 pw.println("\n isolated entries: " + mIsolatedEntries.size());
550 for (Map.Entry<String, StatusBarNotification> entry : mIsolatedEntries.entrySet()) {
551 pw.print(" "); pw.print(entry.getKey());
552 pw.print(", "); pw.println(entry.getValue());
553 }
554 }
555
Evan Laird878c8532018-10-15 15:54:29 -0400556 @Override
557 public void onStateChanged(int newState) {
558 setStatusBarState(newState);
559 }
560
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100561 public static class NotificationGroup {
Selim Cinek04be3892017-08-08 10:58:32 -0700562 public final HashMap<String, NotificationData.Entry> children = new HashMap<>();
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100563 public NotificationData.Entry summary;
564 public boolean expanded;
Selim Cinek2a739342016-03-17 10:28:55 -0700565 /**
566 * Is this notification group suppressed, i.e its summary is hidden
567 */
568 public boolean suppressed;
Selim Cinek52941c52016-05-07 18:29:32 -0400569
570 @Override
571 public String toString() {
Selim Cinek60ca7872016-05-12 11:24:07 -0700572 String result = " summary:\n "
Selim Cinek04be3892017-08-08 10:58:32 -0700573 + (summary != null ? summary.notification : "null")
574 + (summary != null && summary.getDebugThrowable() != null
575 ? Log.getStackTraceString(summary.getDebugThrowable())
576 : "");
Selim Cinek52941c52016-05-07 18:29:32 -0400577 result += "\n children size: " + children.size();
Selim Cinek04be3892017-08-08 10:58:32 -0700578 for (NotificationData.Entry child : children.values()) {
579 result += "\n " + child.notification
580 + (child.getDebugThrowable() != null
581 ? Log.getStackTraceString(child.getDebugThrowable())
582 : "");
Selim Cinek52941c52016-05-07 18:29:32 -0400583 }
584 return result;
585 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100586 }
587
588 public interface OnGroupChangeListener {
Kevin01a53cb2018-11-09 18:19:54 -0800589
590 /**
591 * A new group has been created.
592 *
593 * @param group the group that was created
594 * @param groupKey the group's key
595 */
596 default void onGroupCreated(NotificationGroup group, String groupKey) {}
597
598 /**
599 * A group has been removed.
600 *
601 * @param group the group that was removed
602 * @param groupKey the group's key
603 */
604 default void onGroupRemoved(NotificationGroup group, String groupKey) {}
605
606 /**
607 * The suppression of a group has changed.
608 *
609 * @param group the group that has changed
610 * @param suppressed true if the group is now suppressed, false o/w
611 */
612 default void onGroupSuppressionChanged(NotificationGroup group, boolean suppressed) {}
613
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100614 /**
615 * The expansion of a group has changed.
616 *
617 * @param changedRow the row for which the expansion has changed, which is also the summary
618 * @param expanded a boolean indicating the new expanded state
619 */
Kevin01a53cb2018-11-09 18:19:54 -0800620 default void onGroupExpansionChanged(ExpandableNotificationRow changedRow,
621 boolean expanded) {}
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100622
623 /**
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100624 * A group of children just received a summary notification and should therefore become
625 * children of it.
626 *
627 * @param group the group created
628 */
Kevin01a53cb2018-11-09 18:19:54 -0800629 default void onGroupCreatedFromChildren(NotificationGroup group) {}
Selim Cinekef5127e2015-12-21 16:55:58 -0800630
631 /**
Selim Cinek2a739342016-03-17 10:28:55 -0700632 * The groups have changed. This can happen if the isolation of a child has changes or if a
633 * group became suppressed / unsuppressed
Selim Cinekef5127e2015-12-21 16:55:58 -0800634 */
Kevin01a53cb2018-11-09 18:19:54 -0800635 default void onGroupsChanged() {}
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100636 }
637}