blob: 3c1c0765a3b800d93dcfb296f421cf496a22c2ec [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
Jason Monk27d01a622018-12-10 15:57:09 -050042import javax.inject.Inject;
43import javax.inject.Singleton;
44
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010045/**
46 * A class to handle notifications and their corresponding groups.
47 */
Jason Monk27d01a622018-12-10 15:57:09 -050048@Singleton
Kevina97ea052018-09-11 13:53:18 -070049public class NotificationGroupManager implements OnHeadsUpChangedListener,
Evan Laird878c8532018-10-15 15:54:29 -040050 OnAmbientChangedListener, StateListener {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010051
Selim Cinek04be3892017-08-08 10:58:32 -070052 private static final String TAG = "NotificationGroupManager";
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010053 private final HashMap<String, NotificationGroup> mGroupMap = new HashMap<>();
Kevin01a53cb2018-11-09 18:19:54 -080054 private final ArraySet<OnGroupChangeListener> mListeners = new ArraySet<>();
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010055 private int mBarState = -1;
Selim Cineka6c0cef2016-03-18 11:42:25 -070056 private HashMap<String, StatusBarNotification> mIsolatedEntries = new HashMap<>();
Selim Cinek967ed2a2016-04-08 18:29:11 -070057 private HeadsUpManager mHeadsUpManager;
Kevina97ea052018-09-11 13:53:18 -070058 private AmbientPulseManager mAmbientPulseManager = Dependency.get(AmbientPulseManager.class);
Selim Cinek80c44dd2016-08-15 19:39:55 -070059 private boolean mIsUpdatingUnchangedGroup;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010060
Jason Monk27d01a622018-12-10 15:57:09 -050061 @Inject
Jason Monk1fd3fc32018-08-14 17:20:09 -040062 public NotificationGroupManager() {
Jason Monkaf08c152018-12-04 11:12:39 -050063 Dependency.get(StatusBarStateController.class).addCallback(this);
Jason Monk1fd3fc32018-08-14 17:20:09 -040064 }
65
Kevin01a53cb2018-11-09 18:19:54 -080066 /**
67 * Add a listener for changes to groups.
68 *
69 * @param listener listener to add
70 */
71 public void addOnGroupChangeListener(OnGroupChangeListener listener) {
72 mListeners.add(listener);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010073 }
74
75 public boolean isGroupExpanded(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -080076 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010077 if (group == null) {
78 return false;
79 }
80 return group.expanded;
81 }
82
83 public void setGroupExpanded(StatusBarNotification sbn, boolean expanded) {
Selim Cinekef5127e2015-12-21 16:55:58 -080084 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010085 if (group == null) {
86 return;
87 }
88 setGroupExpanded(group, expanded);
89 }
90
91 private void setGroupExpanded(NotificationGroup group, boolean expanded) {
92 group.expanded = expanded;
93 if (group.summary != null) {
Kevin01a53cb2018-11-09 18:19:54 -080094 for (OnGroupChangeListener listener : mListeners) {
Evan Laird94492852018-10-25 13:43:01 -040095 listener.onGroupExpansionChanged(group.summary.getRow(), expanded);
Kevin01a53cb2018-11-09 18:19:54 -080096 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010097 }
98 }
99
100 public void onEntryRemoved(NotificationData.Entry removed) {
101 onEntryRemovedInternal(removed, removed.notification);
Selim Cinek52941c52016-05-07 18:29:32 -0400102 mIsolatedEntries.remove(removed.key);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100103 }
104
105 /**
106 * An entry was removed.
107 *
108 * @param removed the removed entry
109 * @param sbn the notification the entry has, which doesn't need to be the same as it's internal
110 * notification
111 */
112 private void onEntryRemovedInternal(NotificationData.Entry removed,
113 final StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800114 String groupKey = getGroupKey(sbn);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100115 final NotificationGroup group = mGroupMap.get(groupKey);
Selim Cinek0b4aeab2015-09-01 17:07:38 -0700116 if (group == null) {
117 // When an app posts 2 different notifications as summary of the same group, then a
118 // cancellation of the first notification removes this group.
119 // This situation is not supported and we will not allow such notifications anymore in
120 // the close future. See b/23676310 for reference.
121 return;
122 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800123 if (isGroupChild(sbn)) {
Selim Cinek04be3892017-08-08 10:58:32 -0700124 group.children.remove(removed.key);
Selim Cineke73ad212015-11-03 19:11:08 -0800125 } else {
126 group.summary = null;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100127 }
Selim Cinek2a739342016-03-17 10:28:55 -0700128 updateSuppression(group);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100129 if (group.children.isEmpty()) {
130 if (group.summary == null) {
131 mGroupMap.remove(groupKey);
Kevin01a53cb2018-11-09 18:19:54 -0800132 for (OnGroupChangeListener listener : mListeners) {
133 listener.onGroupRemoved(group, groupKey);
134 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100135 }
136 }
137 }
138
Selim Cinekef5127e2015-12-21 16:55:58 -0800139 public void onEntryAdded(final NotificationData.Entry added) {
Evan Laird94492852018-10-25 13:43:01 -0400140 if (added.isRowRemoved()) {
Selim Cinek04be3892017-08-08 10:58:32 -0700141 added.setDebugThrowable(new Throwable());
142 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800143 final StatusBarNotification sbn = added.notification;
144 boolean isGroupChild = isGroupChild(sbn);
145 String groupKey = getGroupKey(sbn);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100146 NotificationGroup group = mGroupMap.get(groupKey);
147 if (group == null) {
148 group = new NotificationGroup();
149 mGroupMap.put(groupKey, group);
Kevin01a53cb2018-11-09 18:19:54 -0800150 for (OnGroupChangeListener listener : mListeners) {
151 listener.onGroupCreated(group, groupKey);
152 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100153 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800154 if (isGroupChild) {
Selim Cinek04be3892017-08-08 10:58:32 -0700155 NotificationData.Entry existing = group.children.get(added.key);
156 if (existing != null && existing != added) {
157 Throwable existingThrowable = existing.getDebugThrowable();
158 Log.wtf(TAG, "Inconsistent entries found with the same key " + added.key
Evan Laird94492852018-10-25 13:43:01 -0400159 + "existing removed: " + existing.isRowRemoved()
Selim Cinek04be3892017-08-08 10:58:32 -0700160 + (existingThrowable != null
161 ? Log.getStackTraceString(existingThrowable) + "\n": "")
Evan Laird94492852018-10-25 13:43:01 -0400162 + " added removed" + added.isRowRemoved()
Selim Cinek04be3892017-08-08 10:58:32 -0700163 , new Throwable());
164 }
165 group.children.put(added.key, added);
Selim Cinek2a739342016-03-17 10:28:55 -0700166 updateSuppression(group);
Selim Cineke73ad212015-11-03 19:11:08 -0800167 } else {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100168 group.summary = added;
Evan Laird94492852018-10-25 13:43:01 -0400169 group.expanded = added.areChildrenExpanded();
Selim Cinek2a739342016-03-17 10:28:55 -0700170 updateSuppression(group);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100171 if (!group.children.isEmpty()) {
Selim Cinek04be3892017-08-08 10:58:32 -0700172 ArrayList<NotificationData.Entry> childrenCopy
173 = new ArrayList<>(group.children.values());
Selim Cinek50e74672016-05-05 15:58:10 -0400174 for (NotificationData.Entry child : childrenCopy) {
175 onEntryBecomingChild(child);
176 }
Kevin01a53cb2018-11-09 18:19:54 -0800177 for (OnGroupChangeListener listener : mListeners) {
178 listener.onGroupCreatedFromChildren(group);
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700179 }
180 }
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700181 }
182 }
183
Selim Cinek50e74672016-05-05 15:58:10 -0400184 private void onEntryBecomingChild(NotificationData.Entry entry) {
Kevina97ea052018-09-11 13:53:18 -0700185 if (shouldIsolate(entry)) {
186 isolateNotification(entry);
Selim Cinek50e74672016-05-05 15:58:10 -0400187 }
188 }
189
Selim Cinek2a739342016-03-17 10:28:55 -0700190 private void updateSuppression(NotificationGroup group) {
Selim Cinek80c44dd2016-08-15 19:39:55 -0700191 if (group == null) {
Selim Cinek23c80342016-03-17 18:27:36 -0700192 return;
193 }
Selim Cinek2a739342016-03-17 10:28:55 -0700194 boolean prevSuppressed = group.suppressed;
Selim Cinek23c80342016-03-17 18:27:36 -0700195 group.suppressed = group.summary != null && !group.expanded
196 && (group.children.size() == 1
197 || (group.children.size() == 0
Julia Reynoldse46bb372016-03-17 11:05:58 -0400198 && group.summary.notification.getNotification().isGroupSummary()
Selim Cinek23c80342016-03-17 18:27:36 -0700199 && hasIsolatedChildren(group)));
Selim Cinek2a739342016-03-17 10:28:55 -0700200 if (prevSuppressed != group.suppressed) {
Kevin01a53cb2018-11-09 18:19:54 -0800201 for (OnGroupChangeListener listener : mListeners) {
202 if (!mIsUpdatingUnchangedGroup) {
203 listener.onGroupSuppressionChanged(group, group.suppressed);
204 listener.onGroupsChanged();
Kevina97ea052018-09-11 13:53:18 -0700205 }
Selim Cinek967ed2a2016-04-08 18:29:11 -0700206 }
Selim Cinek2a739342016-03-17 10:28:55 -0700207 }
208 }
209
Selim Cinek23c80342016-03-17 18:27:36 -0700210 private boolean hasIsolatedChildren(NotificationGroup group) {
211 return getNumberOfIsolatedChildren(group.summary.notification.getGroupKey()) != 0;
212 }
213
214 private int getNumberOfIsolatedChildren(String groupKey) {
215 int count = 0;
Selim Cineka6c0cef2016-03-18 11:42:25 -0700216 for (StatusBarNotification sbn : mIsolatedEntries.values()) {
Selim Cinek23c80342016-03-17 18:27:36 -0700217 if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) {
218 count++;
219 }
220 }
221 return count;
222 }
223
Selim Cinek967ed2a2016-04-08 18:29:11 -0700224 private NotificationData.Entry getIsolatedChild(String groupKey) {
225 for (StatusBarNotification sbn : mIsolatedEntries.values()) {
226 if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) {
227 return mGroupMap.get(sbn.getKey()).summary;
228 }
229 }
230 return null;
231 }
232
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100233 public void onEntryUpdated(NotificationData.Entry entry,
234 StatusBarNotification oldNotification) {
Selim Cinek68bdff12016-08-03 13:38:56 -0700235 String oldKey = oldNotification.getGroupKey();
236 String newKey = entry.notification.getGroupKey();
237 boolean groupKeysChanged = !oldKey.equals(newKey);
238 boolean wasGroupChild = isGroupChild(oldNotification);
239 boolean isGroupChild = isGroupChild(entry.notification);
Selim Cinek80c44dd2016-08-15 19:39:55 -0700240 mIsUpdatingUnchangedGroup = !groupKeysChanged && wasGroupChild == isGroupChild;
Selim Cinekef5127e2015-12-21 16:55:58 -0800241 if (mGroupMap.get(getGroupKey(oldNotification)) != null) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100242 onEntryRemovedInternal(entry, oldNotification);
243 }
244 onEntryAdded(entry);
Selim Cinek80c44dd2016-08-15 19:39:55 -0700245 mIsUpdatingUnchangedGroup = false;
Selim Cineka6c0cef2016-03-18 11:42:25 -0700246 if (isIsolated(entry.notification)) {
247 mIsolatedEntries.put(entry.key, entry.notification);
Selim Cinek68bdff12016-08-03 13:38:56 -0700248 if (groupKeysChanged) {
Selim Cinek23c80342016-03-17 18:27:36 -0700249 updateSuppression(mGroupMap.get(oldKey));
250 updateSuppression(mGroupMap.get(newKey));
251 }
Selim Cinek68bdff12016-08-03 13:38:56 -0700252 } else if (!wasGroupChild && isGroupChild) {
Selim Cinek50e74672016-05-05 15:58:10 -0400253 onEntryBecomingChild(entry);
Selim Cinek23c80342016-03-17 18:27:36 -0700254 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100255 }
256
Selim Cinek2a739342016-03-17 10:28:55 -0700257 public boolean isSummaryOfSuppressedGroup(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700258 return isGroupSuppressed(getGroupKey(sbn)) && sbn.getNotification().isGroupSummary();
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100259 }
260
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700261 private boolean isOnlyChild(StatusBarNotification sbn) {
Selim Cinek36b02232016-05-11 23:07:05 -0400262 return !sbn.getNotification().isGroupSummary()
Selim Cinek23c80342016-03-17 18:27:36 -0700263 && getTotalNumberOfChildren(sbn) == 1;
Selim Cinek2a739342016-03-17 10:28:55 -0700264 }
265
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700266 public boolean isOnlyChildInGroup(StatusBarNotification sbn) {
Selim Cinek88086e72016-06-17 21:01:32 -0700267 if (!isOnlyChild(sbn)) {
268 return false;
269 }
Evan Laird94492852018-10-25 13:43:01 -0400270 NotificationData.Entry logicalGroupSummary = getLogicalGroupSummary(sbn);
Selim Cinek88086e72016-06-17 21:01:32 -0700271 return logicalGroupSummary != null
Evan Laird94492852018-10-25 13:43:01 -0400272 && !logicalGroupSummary.notification.equals(sbn);
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700273 }
274
Selim Cinek23c80342016-03-17 18:27:36 -0700275 private int getTotalNumberOfChildren(StatusBarNotification sbn) {
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700276 int isolatedChildren = getNumberOfIsolatedChildren(sbn.getGroupKey());
277 NotificationGroup group = mGroupMap.get(sbn.getGroupKey());
278 int realChildren = group != null ? group.children.size() : 0;
279 return isolatedChildren + realChildren;
Selim Cinek23c80342016-03-17 18:27:36 -0700280 }
281
282 private boolean isGroupSuppressed(String groupKey) {
283 NotificationGroup group = mGroupMap.get(groupKey);
Selim Cinek2a739342016-03-17 10:28:55 -0700284 return group != null && group.suppressed;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100285 }
286
Jason Monk1fd3fc32018-08-14 17:20:09 -0400287 private void setStatusBarState(int newState) {
Selim Cinek9c4c4142015-12-04 16:44:56 -0800288 mBarState = newState;
289 if (mBarState == StatusBarState.KEYGUARD) {
Selim Cinek9184f9c2016-02-02 17:36:53 -0800290 collapseAllGroups();
291 }
292 }
293
294 public void collapseAllGroups() {
Selim Cinekc0b14b02016-05-09 13:12:40 -0400295 // Because notifications can become isolated when the group becomes suppressed it can
296 // lead to concurrent modifications while looping. We need to make a copy.
297 ArrayList<NotificationGroup> groupCopy = new ArrayList<>(mGroupMap.values());
298 int size = groupCopy.size();
299 for (int i = 0; i < size; i++) {
300 NotificationGroup group = groupCopy.get(i);
Selim Cinek9184f9c2016-02-02 17:36:53 -0800301 if (group.expanded) {
302 setGroupExpanded(group, false);
Selim Cinek9c4c4142015-12-04 16:44:56 -0800303 }
Selim Cinek2a739342016-03-17 10:28:55 -0700304 updateSuppression(group);
Selim Cinek9c4c4142015-12-04 16:44:56 -0800305 }
306 }
307
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100308 /**
309 * @return whether a given notification is a child in a group which has a summary
310 */
311 public boolean isChildInGroupWithSummary(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800312 if (!isGroupChild(sbn)) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100313 return false;
314 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800315 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek2a739342016-03-17 10:28:55 -0700316 if (group == null || group.summary == null || group.suppressed) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100317 return false;
318 }
Selim Cinek3f19f602016-05-02 18:01:56 -0700319 if (group.children.isEmpty()) {
320 // If the suppression of a group changes because the last child was removed, this can
321 // still be called temporarily because the child hasn't been fully removed yet. Let's
322 // make sure we still return false in that case.
323 return false;
324 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100325 return true;
326 }
327
Selim Cinek263398f2015-10-21 17:40:23 -0700328 /**
329 * @return whether a given notification is a summary in a group which has children
330 */
331 public boolean isSummaryOfGroup(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800332 if (!isGroupSummary(sbn)) {
Selim Cinek263398f2015-10-21 17:40:23 -0700333 return false;
334 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800335 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Kevin96eed642018-09-05 18:53:33 -0700336 if (group == null || group.summary == null) {
Selim Cinek263398f2015-10-21 17:40:23 -0700337 return false;
338 }
Kevin96eed642018-09-05 18:53:33 -0700339 return !group.children.isEmpty() && Objects.equals(group.summary.notification, sbn);
Selim Cinek263398f2015-10-21 17:40:23 -0700340 }
341
Selim Cinek23c80342016-03-17 18:27:36 -0700342 /**
343 * Get the summary of a specified status bar notification. For isolated notification this return
344 * itself.
345 */
Evan Laird94492852018-10-25 13:43:01 -0400346 public NotificationData.Entry getGroupSummary(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700347 return getGroupSummary(getGroupKey(sbn));
348 }
349
350 /**
351 * Similar to {@link #getGroupSummary(StatusBarNotification)} but doesn't get the visual summary
352 * but the logical summary, i.e when a child is isolated, it still returns the summary as if
353 * it wasn't isolated.
354 */
Evan Laird94492852018-10-25 13:43:01 -0400355 public NotificationData.Entry getLogicalGroupSummary(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700356 return getGroupSummary(sbn.getGroupKey());
357 }
358
359 @Nullable
Evan Laird94492852018-10-25 13:43:01 -0400360 private NotificationData.Entry getGroupSummary(String groupKey) {
Selim Cinek23c80342016-03-17 18:27:36 -0700361 NotificationGroup group = mGroupMap.get(groupKey);
Evan Laird94492852018-10-25 13:43:01 -0400362 //TODO: see if this can become an Entry
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100363 return group == null ? null
364 : group.summary == null ? null
Evan Laird94492852018-10-25 13:43:01 -0400365 : group.summary;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100366 }
367
Kevin01a53cb2018-11-09 18:19:54 -0800368 /**
369 * Get the children that are logically in the summary's group, whether or not they are isolated.
370 *
371 * @param summary summary of a group
372 * @return list of the children
373 */
374 public ArrayList<NotificationData.Entry> getLogicalChildren(StatusBarNotification summary) {
375 NotificationGroup group = mGroupMap.get(summary.getGroupKey());
376 if (group == null) {
377 return null;
378 }
379 ArrayList<NotificationData.Entry> children = new ArrayList<>(group.children.values());
380 NotificationData.Entry isolatedChild = getIsolatedChild(summary.getGroupKey());
381 if (isolatedChild != null) {
382 children.add(isolatedChild);
383 }
384 return children;
385 }
386
387 /**
388 * Get the group key. May differ from the one in the notification due to the notification
389 * being temporarily isolated.
390 *
391 * @param sbn notification to check
392 * @return the key of the notification
393 */
394 public String getGroupKey(StatusBarNotification sbn) {
395 if (isIsolated(sbn)) {
396 return sbn.getKey();
397 }
398 return sbn.getGroupKey();
399 }
400
Chris Wren698b1702016-05-23 11:16:32 -0400401 /** @return group expansion state after toggling. */
402 public boolean toggleGroupExpansion(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800403 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek83bc7832015-10-22 13:26:54 -0700404 if (group == null) {
Chris Wren698b1702016-05-23 11:16:32 -0400405 return false;
Selim Cinek83bc7832015-10-22 13:26:54 -0700406 }
407 setGroupExpanded(group, !group.expanded);
Chris Wren698b1702016-05-23 11:16:32 -0400408 return group.expanded;
Selim Cinek83bc7832015-10-22 13:26:54 -0700409 }
410
Selim Cinekef5127e2015-12-21 16:55:58 -0800411 private boolean isIsolated(StatusBarNotification sbn) {
Selim Cineka6c0cef2016-03-18 11:42:25 -0700412 return mIsolatedEntries.containsKey(sbn.getKey());
Selim Cinekef5127e2015-12-21 16:55:58 -0800413 }
414
Kevin01a53cb2018-11-09 18:19:54 -0800415 /**
416 * Whether a notification is visually a group summary.
417 *
418 * @param sbn notification to check
419 * @return true if it is visually a group summary
420 */
421 public boolean isGroupSummary(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800422 if (isIsolated(sbn)) {
423 return true;
424 }
425 return sbn.getNotification().isGroupSummary();
426 }
Julia Reynoldse46bb372016-03-17 11:05:58 -0400427
Kevin01a53cb2018-11-09 18:19:54 -0800428 /**
429 * Whether a notification is visually a group child.
430 *
431 * @param sbn notification to check
432 * @return true if it is visually a group child
433 */
434 public boolean isGroupChild(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800435 if (isIsolated(sbn)) {
436 return false;
437 }
Julia Reynoldse46bb372016-03-17 11:05:58 -0400438 return sbn.isGroup() && !sbn.getNotification().isGroupSummary();
Selim Cinekef5127e2015-12-21 16:55:58 -0800439 }
440
Selim Cinekef5127e2015-12-21 16:55:58 -0800441 @Override
442 public void onHeadsUpPinnedModeChanged(boolean inPinnedMode) {
443 }
444
445 @Override
Evan Laird94492852018-10-25 13:43:01 -0400446 public void onHeadsUpPinned(NotificationData.Entry entry) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800447 }
448
449 @Override
Evan Laird94492852018-10-25 13:43:01 -0400450 public void onHeadsUpUnPinned(NotificationData.Entry entry) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800451 }
452
453 @Override
Kevina97ea052018-09-11 13:53:18 -0700454 public void onAmbientStateChanged(NotificationData.Entry entry, boolean isAmbient) {
Kevin01a53cb2018-11-09 18:19:54 -0800455 onAlertStateChanged(entry, isAmbient);
Kevina97ea052018-09-11 13:53:18 -0700456 }
457
458 @Override
Selim Cinekef5127e2015-12-21 16:55:58 -0800459 public void onHeadsUpStateChanged(NotificationData.Entry entry, boolean isHeadsUp) {
Kevin01a53cb2018-11-09 18:19:54 -0800460 onAlertStateChanged(entry, isHeadsUp);
Kevina97ea052018-09-11 13:53:18 -0700461 }
462
Kevin01a53cb2018-11-09 18:19:54 -0800463 private void onAlertStateChanged(NotificationData.Entry entry, boolean isAlerting) {
Kevina97ea052018-09-11 13:53:18 -0700464 if (isAlerting) {
465 if (shouldIsolate(entry)) {
466 isolateNotification(entry);
Selim Cinekef5127e2015-12-21 16:55:58 -0800467 }
468 } else {
Kevina97ea052018-09-11 13:53:18 -0700469 stopIsolatingNotification(entry);
Selim Cinekef5127e2015-12-21 16:55:58 -0800470 }
471 }
472
Kevina97ea052018-09-11 13:53:18 -0700473 /**
Kevina97ea052018-09-11 13:53:18 -0700474 * Whether a notification that is normally part of a group should be temporarily isolated from
475 * the group and put in their own group visually. This generally happens when the notification
476 * is alerting.
477 *
478 * @param entry the notification to check
479 * @return true if the entry should be isolated
480 */
481
482 private boolean shouldIsolate(NotificationData.Entry entry) {
483 StatusBarNotification sbn = entry.notification;
Selim Cineka6c0cef2016-03-18 11:42:25 -0700484 NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey());
Kevina97ea052018-09-11 13:53:18 -0700485 if (!sbn.isGroup() || sbn.getNotification().isGroupSummary()) {
486 return false;
487 }
Kevin01a53cb2018-11-09 18:19:54 -0800488 if (!mHeadsUpManager.isAlerting(entry.key) && !mAmbientPulseManager.isAlerting(entry.key)) {
Kevina97ea052018-09-11 13:53:18 -0700489 return false;
490 }
491 return (sbn.getNotification().fullScreenIntent != null
492 || notificationGroup == null
493 || !notificationGroup.expanded
494 || isGroupNotFullyVisible(notificationGroup));
495 }
496
497 /**
498 * Isolate a notification from its group so that it visually shows as its own group.
499 *
500 * @param entry the notification to isolate
501 */
502 private void isolateNotification(NotificationData.Entry entry) {
503 StatusBarNotification sbn = entry.notification;
504
505 // We will be isolated now, so lets update the groups
506 onEntryRemovedInternal(entry, entry.notification);
507
508 mIsolatedEntries.put(sbn.getKey(), sbn);
509
510 onEntryAdded(entry);
511 // We also need to update the suppression of the old group, because this call comes
512 // even before the groupManager knows about the notification at all.
513 // When the notification gets added afterwards it is already isolated and therefore
514 // it doesn't lead to an update.
515 updateSuppression(mGroupMap.get(entry.notification.getGroupKey()));
Kevin01a53cb2018-11-09 18:19:54 -0800516 for (OnGroupChangeListener listener : mListeners) {
517 listener.onGroupsChanged();
518 }
Kevina97ea052018-09-11 13:53:18 -0700519 }
520
521 /**
522 * Stop isolating a notification and re-group it with its original logical group.
523 *
524 * @param entry the notification to un-isolate
525 */
526 private void stopIsolatingNotification(NotificationData.Entry entry) {
527 StatusBarNotification sbn = entry.notification;
528 if (mIsolatedEntries.containsKey(sbn.getKey())) {
529 // not isolated anymore, we need to update the groups
530 onEntryRemovedInternal(entry, entry.notification);
531 mIsolatedEntries.remove(sbn.getKey());
532 onEntryAdded(entry);
Kevin01a53cb2018-11-09 18:19:54 -0800533 for (OnGroupChangeListener listener : mListeners) {
534 listener.onGroupsChanged();
535 }
Kevina97ea052018-09-11 13:53:18 -0700536 }
Selim Cineka6c0cef2016-03-18 11:42:25 -0700537 }
538
539 private boolean isGroupNotFullyVisible(NotificationGroup notificationGroup) {
540 return notificationGroup.summary == null
Evan Laird94492852018-10-25 13:43:01 -0400541 || notificationGroup.summary.isGroupNotFullyVisible();
Selim Cineka6c0cef2016-03-18 11:42:25 -0700542 }
543
Selim Cinek967ed2a2016-04-08 18:29:11 -0700544 public void setHeadsUpManager(HeadsUpManager headsUpManager) {
545 mHeadsUpManager = headsUpManager;
546 }
547
Selim Cinek52941c52016-05-07 18:29:32 -0400548 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
549 pw.println("GroupManager state:");
550 pw.println(" number of groups: " + mGroupMap.size());
551 for (Map.Entry<String, NotificationGroup> entry : mGroupMap.entrySet()) {
552 pw.println("\n key: " + entry.getKey()); pw.println(entry.getValue());
553 }
554 pw.println("\n isolated entries: " + mIsolatedEntries.size());
555 for (Map.Entry<String, StatusBarNotification> entry : mIsolatedEntries.entrySet()) {
556 pw.print(" "); pw.print(entry.getKey());
557 pw.print(", "); pw.println(entry.getValue());
558 }
559 }
560
Evan Laird878c8532018-10-15 15:54:29 -0400561 @Override
562 public void onStateChanged(int newState) {
563 setStatusBarState(newState);
564 }
565
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100566 public static class NotificationGroup {
Selim Cinek04be3892017-08-08 10:58:32 -0700567 public final HashMap<String, NotificationData.Entry> children = new HashMap<>();
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100568 public NotificationData.Entry summary;
569 public boolean expanded;
Selim Cinek2a739342016-03-17 10:28:55 -0700570 /**
571 * Is this notification group suppressed, i.e its summary is hidden
572 */
573 public boolean suppressed;
Selim Cinek52941c52016-05-07 18:29:32 -0400574
575 @Override
576 public String toString() {
Selim Cinek60ca7872016-05-12 11:24:07 -0700577 String result = " summary:\n "
Selim Cinek04be3892017-08-08 10:58:32 -0700578 + (summary != null ? summary.notification : "null")
579 + (summary != null && summary.getDebugThrowable() != null
580 ? Log.getStackTraceString(summary.getDebugThrowable())
581 : "");
Selim Cinek52941c52016-05-07 18:29:32 -0400582 result += "\n children size: " + children.size();
Selim Cinek04be3892017-08-08 10:58:32 -0700583 for (NotificationData.Entry child : children.values()) {
584 result += "\n " + child.notification
585 + (child.getDebugThrowable() != null
586 ? Log.getStackTraceString(child.getDebugThrowable())
587 : "");
Selim Cinek52941c52016-05-07 18:29:32 -0400588 }
589 return result;
590 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100591 }
592
593 public interface OnGroupChangeListener {
Kevin01a53cb2018-11-09 18:19:54 -0800594
595 /**
596 * A new group has been created.
597 *
598 * @param group the group that was created
599 * @param groupKey the group's key
600 */
601 default void onGroupCreated(NotificationGroup group, String groupKey) {}
602
603 /**
604 * A group has been removed.
605 *
606 * @param group the group that was removed
607 * @param groupKey the group's key
608 */
609 default void onGroupRemoved(NotificationGroup group, String groupKey) {}
610
611 /**
612 * The suppression of a group has changed.
613 *
614 * @param group the group that has changed
615 * @param suppressed true if the group is now suppressed, false o/w
616 */
617 default void onGroupSuppressionChanged(NotificationGroup group, boolean suppressed) {}
618
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100619 /**
620 * The expansion of a group has changed.
621 *
622 * @param changedRow the row for which the expansion has changed, which is also the summary
623 * @param expanded a boolean indicating the new expanded state
624 */
Kevin01a53cb2018-11-09 18:19:54 -0800625 default void onGroupExpansionChanged(ExpandableNotificationRow changedRow,
626 boolean expanded) {}
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100627
628 /**
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100629 * A group of children just received a summary notification and should therefore become
630 * children of it.
631 *
632 * @param group the group created
633 */
Kevin01a53cb2018-11-09 18:19:54 -0800634 default void onGroupCreatedFromChildren(NotificationGroup group) {}
Selim Cinekef5127e2015-12-21 16:55:58 -0800635
636 /**
Selim Cinek2a739342016-03-17 10:28:55 -0700637 * The groups have changed. This can happen if the isolation of a child has changes or if a
638 * group became suppressed / unsuppressed
Selim Cinekef5127e2015-12-21 16:55:58 -0800639 */
Kevin01a53cb2018-11-09 18:19:54 -0800640 default void onGroupsChanged() {}
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100641 }
642}