blob: b75c7e0ce8067bb250cea095d06260280b52cecb [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
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010019import android.service.notification.StatusBarNotification;
Selim Cinek23c80342016-03-17 18:27:36 -070020import android.support.annotation.Nullable;
Selim Cinek04be3892017-08-08 10:58:32 -070021import android.util.Log;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010022
23import com.android.systemui.statusbar.ExpandableNotificationRow;
24import com.android.systemui.statusbar.NotificationData;
Selim Cinek9c4c4142015-12-04 16:44:56 -080025import com.android.systemui.statusbar.StatusBarState;
Selim Cinekef5127e2015-12-21 16:55:58 -080026import com.android.systemui.statusbar.policy.HeadsUpManager;
Selim Cineka7d4f822016-12-06 14:34:47 -080027import com.android.systemui.statusbar.policy.OnHeadsUpChangedListener;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010028
Selim Cinek52941c52016-05-07 18:29:32 -040029import java.io.FileDescriptor;
30import java.io.PrintWriter;
Selim Cinekc0b14b02016-05-09 13:12:40 -040031import java.util.ArrayList;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010032import java.util.HashMap;
Selim Cinek967ed2a2016-04-08 18:29:11 -070033import java.util.Iterator;
Selim Cinek52941c52016-05-07 18:29:32 -040034import java.util.Map;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010035
36/**
37 * A class to handle notifications and their corresponding groups.
38 */
Selim Cineka7d4f822016-12-06 14:34:47 -080039public class NotificationGroupManager implements OnHeadsUpChangedListener {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010040
Selim Cinek04be3892017-08-08 10:58:32 -070041 private static final String TAG = "NotificationGroupManager";
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010042 private final HashMap<String, NotificationGroup> mGroupMap = new HashMap<>();
43 private OnGroupChangeListener mListener;
44 private int mBarState = -1;
Selim Cineka6c0cef2016-03-18 11:42:25 -070045 private HashMap<String, StatusBarNotification> mIsolatedEntries = new HashMap<>();
Selim Cinek967ed2a2016-04-08 18:29:11 -070046 private HeadsUpManager mHeadsUpManager;
Selim Cinek80c44dd2016-08-15 19:39:55 -070047 private boolean mIsUpdatingUnchangedGroup;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010048
49 public void setOnGroupChangeListener(OnGroupChangeListener listener) {
50 mListener = listener;
51 }
52
53 public boolean isGroupExpanded(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -080054 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010055 if (group == null) {
56 return false;
57 }
58 return group.expanded;
59 }
60
61 public void setGroupExpanded(StatusBarNotification sbn, boolean expanded) {
Selim Cinekef5127e2015-12-21 16:55:58 -080062 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010063 if (group == null) {
64 return;
65 }
66 setGroupExpanded(group, expanded);
67 }
68
69 private void setGroupExpanded(NotificationGroup group, boolean expanded) {
70 group.expanded = expanded;
71 if (group.summary != null) {
72 mListener.onGroupExpansionChanged(group.summary.row, expanded);
73 }
74 }
75
76 public void onEntryRemoved(NotificationData.Entry removed) {
77 onEntryRemovedInternal(removed, removed.notification);
Selim Cinek52941c52016-05-07 18:29:32 -040078 mIsolatedEntries.remove(removed.key);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010079 }
80
81 /**
82 * An entry was removed.
83 *
84 * @param removed the removed entry
85 * @param sbn the notification the entry has, which doesn't need to be the same as it's internal
86 * notification
87 */
88 private void onEntryRemovedInternal(NotificationData.Entry removed,
89 final StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -080090 String groupKey = getGroupKey(sbn);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010091 final NotificationGroup group = mGroupMap.get(groupKey);
Selim Cinek0b4aeab2015-09-01 17:07:38 -070092 if (group == null) {
93 // When an app posts 2 different notifications as summary of the same group, then a
94 // cancellation of the first notification removes this group.
95 // This situation is not supported and we will not allow such notifications anymore in
96 // the close future. See b/23676310 for reference.
97 return;
98 }
Selim Cinekef5127e2015-12-21 16:55:58 -080099 if (isGroupChild(sbn)) {
Selim Cinek04be3892017-08-08 10:58:32 -0700100 group.children.remove(removed.key);
Selim Cineke73ad212015-11-03 19:11:08 -0800101 } else {
102 group.summary = null;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100103 }
Selim Cinek2a739342016-03-17 10:28:55 -0700104 updateSuppression(group);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100105 if (group.children.isEmpty()) {
106 if (group.summary == null) {
107 mGroupMap.remove(groupKey);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100108 }
109 }
110 }
111
Selim Cinekef5127e2015-12-21 16:55:58 -0800112 public void onEntryAdded(final NotificationData.Entry added) {
Selim Cinek04be3892017-08-08 10:58:32 -0700113 if (added.row.isRemoved()) {
114 added.setDebugThrowable(new Throwable());
115 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800116 final StatusBarNotification sbn = added.notification;
117 boolean isGroupChild = isGroupChild(sbn);
118 String groupKey = getGroupKey(sbn);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100119 NotificationGroup group = mGroupMap.get(groupKey);
120 if (group == null) {
121 group = new NotificationGroup();
122 mGroupMap.put(groupKey, group);
123 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800124 if (isGroupChild) {
Selim Cinek04be3892017-08-08 10:58:32 -0700125 NotificationData.Entry existing = group.children.get(added.key);
126 if (existing != null && existing != added) {
127 Throwable existingThrowable = existing.getDebugThrowable();
128 Log.wtf(TAG, "Inconsistent entries found with the same key " + added.key
129 + "existing removed: " + existing.row.isRemoved()
130 + (existingThrowable != null
131 ? Log.getStackTraceString(existingThrowable) + "\n": "")
132 + " added removed" + added.row.isRemoved()
133 , new Throwable());
134 }
135 group.children.put(added.key, added);
Selim Cinek2a739342016-03-17 10:28:55 -0700136 updateSuppression(group);
Selim Cineke73ad212015-11-03 19:11:08 -0800137 } else {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100138 group.summary = added;
Selim Cinekb5605e52015-02-20 18:21:41 +0100139 group.expanded = added.row.areChildrenExpanded();
Selim Cinek2a739342016-03-17 10:28:55 -0700140 updateSuppression(group);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100141 if (!group.children.isEmpty()) {
Selim Cinek04be3892017-08-08 10:58:32 -0700142 ArrayList<NotificationData.Entry> childrenCopy
143 = new ArrayList<>(group.children.values());
Selim Cinek50e74672016-05-05 15:58:10 -0400144 for (NotificationData.Entry child : childrenCopy) {
145 onEntryBecomingChild(child);
146 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100147 mListener.onGroupCreatedFromChildren(group);
148 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100149 }
150 }
151
Selim Cinek50e74672016-05-05 15:58:10 -0400152 private void onEntryBecomingChild(NotificationData.Entry entry) {
153 if (entry.row.isHeadsUp()) {
154 onHeadsUpStateChanged(entry, true);
155 }
156 }
157
Selim Cinek2a739342016-03-17 10:28:55 -0700158 private void updateSuppression(NotificationGroup group) {
Selim Cinek80c44dd2016-08-15 19:39:55 -0700159 if (group == null) {
Selim Cinek23c80342016-03-17 18:27:36 -0700160 return;
161 }
Selim Cinek2a739342016-03-17 10:28:55 -0700162 boolean prevSuppressed = group.suppressed;
Selim Cinek23c80342016-03-17 18:27:36 -0700163 group.suppressed = group.summary != null && !group.expanded
164 && (group.children.size() == 1
165 || (group.children.size() == 0
Julia Reynoldse46bb372016-03-17 11:05:58 -0400166 && group.summary.notification.getNotification().isGroupSummary()
Selim Cinek23c80342016-03-17 18:27:36 -0700167 && hasIsolatedChildren(group)));
Selim Cinek2a739342016-03-17 10:28:55 -0700168 if (prevSuppressed != group.suppressed) {
Selim Cinek967ed2a2016-04-08 18:29:11 -0700169 if (group.suppressed) {
170 handleSuppressedSummaryHeadsUpped(group.summary);
171 }
Selim Cinek80c44dd2016-08-15 19:39:55 -0700172 if (!mIsUpdatingUnchangedGroup) {
173 mListener.onGroupsChanged();
174 }
Selim Cinek2a739342016-03-17 10:28:55 -0700175 }
176 }
177
Selim Cinek23c80342016-03-17 18:27:36 -0700178 private boolean hasIsolatedChildren(NotificationGroup group) {
179 return getNumberOfIsolatedChildren(group.summary.notification.getGroupKey()) != 0;
180 }
181
182 private int getNumberOfIsolatedChildren(String groupKey) {
183 int count = 0;
Selim Cineka6c0cef2016-03-18 11:42:25 -0700184 for (StatusBarNotification sbn : mIsolatedEntries.values()) {
Selim Cinek23c80342016-03-17 18:27:36 -0700185 if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) {
186 count++;
187 }
188 }
189 return count;
190 }
191
Selim Cinek967ed2a2016-04-08 18:29:11 -0700192 private NotificationData.Entry getIsolatedChild(String groupKey) {
193 for (StatusBarNotification sbn : mIsolatedEntries.values()) {
194 if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) {
195 return mGroupMap.get(sbn.getKey()).summary;
196 }
197 }
198 return null;
199 }
200
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100201 public void onEntryUpdated(NotificationData.Entry entry,
202 StatusBarNotification oldNotification) {
Selim Cinek68bdff12016-08-03 13:38:56 -0700203 String oldKey = oldNotification.getGroupKey();
204 String newKey = entry.notification.getGroupKey();
205 boolean groupKeysChanged = !oldKey.equals(newKey);
206 boolean wasGroupChild = isGroupChild(oldNotification);
207 boolean isGroupChild = isGroupChild(entry.notification);
Selim Cinek80c44dd2016-08-15 19:39:55 -0700208 mIsUpdatingUnchangedGroup = !groupKeysChanged && wasGroupChild == isGroupChild;
Selim Cinekef5127e2015-12-21 16:55:58 -0800209 if (mGroupMap.get(getGroupKey(oldNotification)) != null) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100210 onEntryRemovedInternal(entry, oldNotification);
211 }
212 onEntryAdded(entry);
Selim Cinek80c44dd2016-08-15 19:39:55 -0700213 mIsUpdatingUnchangedGroup = false;
Selim Cineka6c0cef2016-03-18 11:42:25 -0700214 if (isIsolated(entry.notification)) {
215 mIsolatedEntries.put(entry.key, entry.notification);
Selim Cinek68bdff12016-08-03 13:38:56 -0700216 if (groupKeysChanged) {
Selim Cinek23c80342016-03-17 18:27:36 -0700217 updateSuppression(mGroupMap.get(oldKey));
218 updateSuppression(mGroupMap.get(newKey));
219 }
Selim Cinek68bdff12016-08-03 13:38:56 -0700220 } else if (!wasGroupChild && isGroupChild) {
Selim Cinek50e74672016-05-05 15:58:10 -0400221 onEntryBecomingChild(entry);
Selim Cinek23c80342016-03-17 18:27:36 -0700222 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100223 }
224
Selim Cinek2a739342016-03-17 10:28:55 -0700225 public boolean isSummaryOfSuppressedGroup(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700226 return isGroupSuppressed(getGroupKey(sbn)) && sbn.getNotification().isGroupSummary();
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100227 }
228
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700229 private boolean isOnlyChild(StatusBarNotification sbn) {
Selim Cinek36b02232016-05-11 23:07:05 -0400230 return !sbn.getNotification().isGroupSummary()
Selim Cinek23c80342016-03-17 18:27:36 -0700231 && getTotalNumberOfChildren(sbn) == 1;
Selim Cinek2a739342016-03-17 10:28:55 -0700232 }
233
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700234 public boolean isOnlyChildInGroup(StatusBarNotification sbn) {
Selim Cinek88086e72016-06-17 21:01:32 -0700235 if (!isOnlyChild(sbn)) {
236 return false;
237 }
238 ExpandableNotificationRow logicalGroupSummary = getLogicalGroupSummary(sbn);
239 return logicalGroupSummary != null
240 && !logicalGroupSummary.getStatusBarNotification().equals(sbn);
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700241 }
242
Selim Cinek23c80342016-03-17 18:27:36 -0700243 private int getTotalNumberOfChildren(StatusBarNotification sbn) {
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700244 int isolatedChildren = getNumberOfIsolatedChildren(sbn.getGroupKey());
245 NotificationGroup group = mGroupMap.get(sbn.getGroupKey());
246 int realChildren = group != null ? group.children.size() : 0;
247 return isolatedChildren + realChildren;
Selim Cinek23c80342016-03-17 18:27:36 -0700248 }
249
250 private boolean isGroupSuppressed(String groupKey) {
251 NotificationGroup group = mGroupMap.get(groupKey);
Selim Cinek2a739342016-03-17 10:28:55 -0700252 return group != null && group.suppressed;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100253 }
254
Selim Cinek9c4c4142015-12-04 16:44:56 -0800255 public void setStatusBarState(int newState) {
256 if (mBarState == newState) {
257 return;
258 }
259 mBarState = newState;
260 if (mBarState == StatusBarState.KEYGUARD) {
Selim Cinek9184f9c2016-02-02 17:36:53 -0800261 collapseAllGroups();
262 }
263 }
264
265 public void collapseAllGroups() {
Selim Cinekc0b14b02016-05-09 13:12:40 -0400266 // Because notifications can become isolated when the group becomes suppressed it can
267 // lead to concurrent modifications while looping. We need to make a copy.
268 ArrayList<NotificationGroup> groupCopy = new ArrayList<>(mGroupMap.values());
269 int size = groupCopy.size();
270 for (int i = 0; i < size; i++) {
271 NotificationGroup group = groupCopy.get(i);
Selim Cinek9184f9c2016-02-02 17:36:53 -0800272 if (group.expanded) {
273 setGroupExpanded(group, false);
Selim Cinek9c4c4142015-12-04 16:44:56 -0800274 }
Selim Cinek2a739342016-03-17 10:28:55 -0700275 updateSuppression(group);
Selim Cinek9c4c4142015-12-04 16:44:56 -0800276 }
277 }
278
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100279 /**
280 * @return whether a given notification is a child in a group which has a summary
281 */
282 public boolean isChildInGroupWithSummary(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800283 if (!isGroupChild(sbn)) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100284 return false;
285 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800286 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek2a739342016-03-17 10:28:55 -0700287 if (group == null || group.summary == null || group.suppressed) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100288 return false;
289 }
Selim Cinek3f19f602016-05-02 18:01:56 -0700290 if (group.children.isEmpty()) {
291 // If the suppression of a group changes because the last child was removed, this can
292 // still be called temporarily because the child hasn't been fully removed yet. Let's
293 // make sure we still return false in that case.
294 return false;
295 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100296 return true;
297 }
298
Selim Cinek263398f2015-10-21 17:40:23 -0700299 /**
300 * @return whether a given notification is a summary in a group which has children
301 */
302 public boolean isSummaryOfGroup(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800303 if (!isGroupSummary(sbn)) {
Selim Cinek263398f2015-10-21 17:40:23 -0700304 return false;
305 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800306 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek263398f2015-10-21 17:40:23 -0700307 if (group == null) {
308 return false;
309 }
310 return !group.children.isEmpty();
311 }
312
Selim Cinek23c80342016-03-17 18:27:36 -0700313 /**
314 * Get the summary of a specified status bar notification. For isolated notification this return
315 * itself.
316 */
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100317 public ExpandableNotificationRow getGroupSummary(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700318 return getGroupSummary(getGroupKey(sbn));
319 }
320
321 /**
322 * Similar to {@link #getGroupSummary(StatusBarNotification)} but doesn't get the visual summary
323 * but the logical summary, i.e when a child is isolated, it still returns the summary as if
324 * it wasn't isolated.
325 */
326 public ExpandableNotificationRow getLogicalGroupSummary(
327 StatusBarNotification sbn) {
328 return getGroupSummary(sbn.getGroupKey());
329 }
330
331 @Nullable
332 private ExpandableNotificationRow getGroupSummary(String groupKey) {
333 NotificationGroup group = mGroupMap.get(groupKey);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100334 return group == null ? null
335 : group.summary == null ? null
Selim Cinek23c80342016-03-17 18:27:36 -0700336 : group.summary.row;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100337 }
338
Chris Wren698b1702016-05-23 11:16:32 -0400339 /** @return group expansion state after toggling. */
340 public boolean toggleGroupExpansion(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800341 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek83bc7832015-10-22 13:26:54 -0700342 if (group == null) {
Chris Wren698b1702016-05-23 11:16:32 -0400343 return false;
Selim Cinek83bc7832015-10-22 13:26:54 -0700344 }
345 setGroupExpanded(group, !group.expanded);
Chris Wren698b1702016-05-23 11:16:32 -0400346 return group.expanded;
Selim Cinek83bc7832015-10-22 13:26:54 -0700347 }
348
Selim Cinekef5127e2015-12-21 16:55:58 -0800349 private boolean isIsolated(StatusBarNotification sbn) {
Selim Cineka6c0cef2016-03-18 11:42:25 -0700350 return mIsolatedEntries.containsKey(sbn.getKey());
Selim Cinekef5127e2015-12-21 16:55:58 -0800351 }
352
353 private boolean isGroupSummary(StatusBarNotification sbn) {
354 if (isIsolated(sbn)) {
355 return true;
356 }
357 return sbn.getNotification().isGroupSummary();
358 }
Julia Reynoldse46bb372016-03-17 11:05:58 -0400359
Selim Cinekef5127e2015-12-21 16:55:58 -0800360 private boolean isGroupChild(StatusBarNotification sbn) {
361 if (isIsolated(sbn)) {
362 return false;
363 }
Julia Reynoldse46bb372016-03-17 11:05:58 -0400364 return sbn.isGroup() && !sbn.getNotification().isGroupSummary();
Selim Cinekef5127e2015-12-21 16:55:58 -0800365 }
366
367 private String getGroupKey(StatusBarNotification sbn) {
368 if (isIsolated(sbn)) {
369 return sbn.getKey();
370 }
371 return sbn.getGroupKey();
372 }
373
374 @Override
375 public void onHeadsUpPinnedModeChanged(boolean inPinnedMode) {
376 }
377
378 @Override
379 public void onHeadsUpPinned(ExpandableNotificationRow headsUp) {
380 }
381
382 @Override
383 public void onHeadsUpUnPinned(ExpandableNotificationRow headsUp) {
384 }
385
386 @Override
387 public void onHeadsUpStateChanged(NotificationData.Entry entry, boolean isHeadsUp) {
388 final StatusBarNotification sbn = entry.notification;
389 if (entry.row.isHeadsUp()) {
Selim Cineka6c0cef2016-03-18 11:42:25 -0700390 if (shouldIsolate(sbn)) {
Selim Cinek23c80342016-03-17 18:27:36 -0700391 // We will be isolated now, so lets update the groups
392 onEntryRemovedInternal(entry, entry.notification);
Selim Cineka6c0cef2016-03-18 11:42:25 -0700393
394 mIsolatedEntries.put(sbn.getKey(), sbn);
395
Selim Cinek23c80342016-03-17 18:27:36 -0700396 onEntryAdded(entry);
397 // We also need to update the suppression of the old group, because this call comes
398 // even before the groupManager knows about the notification at all.
399 // When the notification gets added afterwards it is already isolated and therefore
400 // it doesn't lead to an update.
401 updateSuppression(mGroupMap.get(entry.notification.getGroupKey()));
402 mListener.onGroupsChanged();
Selim Cinek967ed2a2016-04-08 18:29:11 -0700403 } else {
404 handleSuppressedSummaryHeadsUpped(entry);
Selim Cinekef5127e2015-12-21 16:55:58 -0800405 }
406 } else {
Selim Cineka6c0cef2016-03-18 11:42:25 -0700407 if (mIsolatedEntries.containsKey(sbn.getKey())) {
408 // not isolated anymore, we need to update the groups
409 onEntryRemovedInternal(entry, entry.notification);
410 mIsolatedEntries.remove(sbn.getKey());
411 onEntryAdded(entry);
412 mListener.onGroupsChanged();
Selim Cinekef5127e2015-12-21 16:55:58 -0800413 }
414 }
415 }
416
Selim Cinek967ed2a2016-04-08 18:29:11 -0700417 private void handleSuppressedSummaryHeadsUpped(NotificationData.Entry entry) {
418 StatusBarNotification sbn = entry.notification;
419 if (!isGroupSuppressed(sbn.getGroupKey())
420 || !sbn.getNotification().isGroupSummary()
421 || !entry.row.isHeadsUp()) {
422 return;
423 }
424 // The parent of a suppressed group got huned, lets hun the child!
425 NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey());
426 if (notificationGroup != null) {
Selim Cinek04be3892017-08-08 10:58:32 -0700427 Iterator<NotificationData.Entry> iterator
428 = notificationGroup.children.values().iterator();
Selim Cinek967ed2a2016-04-08 18:29:11 -0700429 NotificationData.Entry child = iterator.hasNext() ? iterator.next() : null;
430 if (child == null) {
431 child = getIsolatedChild(sbn.getGroupKey());
432 }
433 if (child != null) {
Selim Cinek40f88762016-12-16 16:56:41 -0800434 if (child.row.keepInParent() || child.row.isRemoved() || child.row.isDismissed()) {
435 // the notification is actually already removed, no need to do heads-up on it.
436 return;
437 }
Selim Cinek967ed2a2016-04-08 18:29:11 -0700438 if (mHeadsUpManager.isHeadsUp(child.key)) {
439 mHeadsUpManager.updateNotification(child, true);
440 } else {
441 mHeadsUpManager.showNotification(child);
442 }
443 }
444 }
445 mHeadsUpManager.releaseImmediately(entry.key);
446 }
447
Selim Cineka6c0cef2016-03-18 11:42:25 -0700448 private boolean shouldIsolate(StatusBarNotification sbn) {
449 NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey());
Julia Reynoldse46bb372016-03-17 11:05:58 -0400450 return (sbn.isGroup() && !sbn.getNotification().isGroupSummary())
Selim Cineka6c0cef2016-03-18 11:42:25 -0700451 && (sbn.getNotification().fullScreenIntent != null
452 || notificationGroup == null
453 || !notificationGroup.expanded
454 || isGroupNotFullyVisible(notificationGroup));
455 }
456
457 private boolean isGroupNotFullyVisible(NotificationGroup notificationGroup) {
458 return notificationGroup.summary == null
Selim Cineka6c0cef2016-03-18 11:42:25 -0700459 || notificationGroup.summary.row.getClipTopAmount() > 0
460 || notificationGroup.summary.row.getTranslationY() < 0;
461 }
462
Selim Cinek967ed2a2016-04-08 18:29:11 -0700463 public void setHeadsUpManager(HeadsUpManager headsUpManager) {
464 mHeadsUpManager = headsUpManager;
465 }
466
Selim Cinek52941c52016-05-07 18:29:32 -0400467 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
468 pw.println("GroupManager state:");
469 pw.println(" number of groups: " + mGroupMap.size());
470 for (Map.Entry<String, NotificationGroup> entry : mGroupMap.entrySet()) {
471 pw.println("\n key: " + entry.getKey()); pw.println(entry.getValue());
472 }
473 pw.println("\n isolated entries: " + mIsolatedEntries.size());
474 for (Map.Entry<String, StatusBarNotification> entry : mIsolatedEntries.entrySet()) {
475 pw.print(" "); pw.print(entry.getKey());
476 pw.print(", "); pw.println(entry.getValue());
477 }
478 }
479
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100480 public static class NotificationGroup {
Selim Cinek04be3892017-08-08 10:58:32 -0700481 public final HashMap<String, NotificationData.Entry> children = new HashMap<>();
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100482 public NotificationData.Entry summary;
483 public boolean expanded;
Selim Cinek2a739342016-03-17 10:28:55 -0700484 /**
485 * Is this notification group suppressed, i.e its summary is hidden
486 */
487 public boolean suppressed;
Selim Cinek52941c52016-05-07 18:29:32 -0400488
489 @Override
490 public String toString() {
Selim Cinek60ca7872016-05-12 11:24:07 -0700491 String result = " summary:\n "
Selim Cinek04be3892017-08-08 10:58:32 -0700492 + (summary != null ? summary.notification : "null")
493 + (summary != null && summary.getDebugThrowable() != null
494 ? Log.getStackTraceString(summary.getDebugThrowable())
495 : "");
Selim Cinek52941c52016-05-07 18:29:32 -0400496 result += "\n children size: " + children.size();
Selim Cinek04be3892017-08-08 10:58:32 -0700497 for (NotificationData.Entry child : children.values()) {
498 result += "\n " + child.notification
499 + (child.getDebugThrowable() != null
500 ? Log.getStackTraceString(child.getDebugThrowable())
501 : "");
Selim Cinek52941c52016-05-07 18:29:32 -0400502 }
503 return result;
504 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100505 }
506
507 public interface OnGroupChangeListener {
508 /**
509 * The expansion of a group has changed.
510 *
511 * @param changedRow the row for which the expansion has changed, which is also the summary
512 * @param expanded a boolean indicating the new expanded state
513 */
514 void onGroupExpansionChanged(ExpandableNotificationRow changedRow, boolean expanded);
515
516 /**
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100517 * A group of children just received a summary notification and should therefore become
518 * children of it.
519 *
520 * @param group the group created
521 */
522 void onGroupCreatedFromChildren(NotificationGroup group);
Selim Cinekef5127e2015-12-21 16:55:58 -0800523
524 /**
Selim Cinek2a739342016-03-17 10:28:55 -0700525 * The groups have changed. This can happen if the isolation of a child has changes or if a
526 * group became suppressed / unsuppressed
Selim Cinekef5127e2015-12-21 16:55:58 -0800527 */
Selim Cinek2a739342016-03-17 10:28:55 -0700528 void onGroupsChanged();
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100529 }
530}