blob: c08366a95f0847c4a87699fc447e9f4eb9367a0a [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
Kevina97ea052018-09-11 13:53:18 -070019import android.annotation.NonNull;
Selim Cinekf93bf3e2018-05-08 14:43:21 -070020import android.app.Notification;
21import android.os.SystemClock;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010022import android.service.notification.StatusBarNotification;
Aurimas Liutikasfd52c142018-04-17 09:50:46 -070023import androidx.annotation.Nullable;
Selim Cinek04be3892017-08-08 10:58:32 -070024import android.util.Log;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010025
Jason Monk1fd3fc32018-08-14 17:20:09 -040026import com.android.systemui.Dependency;
Kevina97ea052018-09-11 13:53:18 -070027import com.android.systemui.statusbar.AlertingNotificationManager;
28import com.android.systemui.statusbar.AmbientPulseManager;
29import com.android.systemui.statusbar.AmbientPulseManager.OnAmbientChangedListener;
Jason Monk1fd3fc32018-08-14 17:20:09 -040030import com.android.systemui.statusbar.StatusBarState;
31import com.android.systemui.statusbar.StatusBarStateController.StateListener;
Rohan Shah20790b82018-07-02 17:21:04 -070032import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
33import com.android.systemui.statusbar.notification.NotificationData;
Jason Monk1fd3fc32018-08-14 17:20:09 -040034import com.android.systemui.statusbar.StatusBarStateController;
Selim Cinekef5127e2015-12-21 16:55:58 -080035import com.android.systemui.statusbar.policy.HeadsUpManager;
Selim Cineka7d4f822016-12-06 14:34:47 -080036import com.android.systemui.statusbar.policy.OnHeadsUpChangedListener;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010037
Selim Cinek52941c52016-05-07 18:29:32 -040038import java.io.FileDescriptor;
39import java.io.PrintWriter;
Selim Cinekc0b14b02016-05-09 13:12:40 -040040import java.util.ArrayList;
Selim Cinekf93bf3e2018-05-08 14:43:21 -070041import java.util.Collection;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010042import java.util.HashMap;
Selim Cinek967ed2a2016-04-08 18:29:11 -070043import java.util.Iterator;
Selim Cinek52941c52016-05-07 18:29:32 -040044import java.util.Map;
Selim Cinekf93bf3e2018-05-08 14:43:21 -070045import java.util.Objects;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010046
47/**
48 * A class to handle notifications and their corresponding groups.
49 */
Kevina97ea052018-09-11 13:53:18 -070050public class NotificationGroupManager implements OnHeadsUpChangedListener,
51 OnAmbientChangedListener {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010052
Selim Cinek04be3892017-08-08 10:58:32 -070053 private static final String TAG = "NotificationGroupManager";
Kevina97ea052018-09-11 13:53:18 -070054 private static final long ALERT_TRANSFER_TIMEOUT = 300;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010055 private final HashMap<String, NotificationGroup> mGroupMap = new HashMap<>();
56 private OnGroupChangeListener mListener;
57 private int mBarState = -1;
Selim Cineka6c0cef2016-03-18 11:42:25 -070058 private HashMap<String, StatusBarNotification> mIsolatedEntries = new HashMap<>();
Selim Cinek967ed2a2016-04-08 18:29:11 -070059 private HeadsUpManager mHeadsUpManager;
Kevina97ea052018-09-11 13:53:18 -070060 private AmbientPulseManager mAmbientPulseManager = Dependency.get(AmbientPulseManager.class);
61 private boolean mIsDozing;
Selim Cinek80c44dd2016-08-15 19:39:55 -070062 private boolean mIsUpdatingUnchangedGroup;
Selim Cinekf93bf3e2018-05-08 14:43:21 -070063 private HashMap<String, NotificationData.Entry> mPendingNotifications;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010064
Jason Monk1fd3fc32018-08-14 17:20:09 -040065 private final StateListener mStateListener = this::setStatusBarState;
66
67 public NotificationGroupManager() {
68 Dependency.get(StatusBarStateController.class).addListener(mStateListener);
69 }
70
Selim Cinek25fd4e2b2015-02-20 17:46:07 +010071 public void setOnGroupChangeListener(OnGroupChangeListener listener) {
72 mListener = listener;
73 }
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) {
94 mListener.onGroupExpansionChanged(group.summary.row, expanded);
95 }
96 }
97
98 public void onEntryRemoved(NotificationData.Entry removed) {
99 onEntryRemovedInternal(removed, removed.notification);
Selim Cinek52941c52016-05-07 18:29:32 -0400100 mIsolatedEntries.remove(removed.key);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100101 }
102
103 /**
104 * An entry was removed.
105 *
106 * @param removed the removed entry
107 * @param sbn the notification the entry has, which doesn't need to be the same as it's internal
108 * notification
109 */
110 private void onEntryRemovedInternal(NotificationData.Entry removed,
111 final StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800112 String groupKey = getGroupKey(sbn);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100113 final NotificationGroup group = mGroupMap.get(groupKey);
Selim Cinek0b4aeab2015-09-01 17:07:38 -0700114 if (group == null) {
115 // When an app posts 2 different notifications as summary of the same group, then a
116 // cancellation of the first notification removes this group.
117 // This situation is not supported and we will not allow such notifications anymore in
118 // the close future. See b/23676310 for reference.
119 return;
120 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800121 if (isGroupChild(sbn)) {
Selim Cinek04be3892017-08-08 10:58:32 -0700122 group.children.remove(removed.key);
Selim Cineke73ad212015-11-03 19:11:08 -0800123 } else {
124 group.summary = null;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100125 }
Selim Cinek2a739342016-03-17 10:28:55 -0700126 updateSuppression(group);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100127 if (group.children.isEmpty()) {
128 if (group.summary == null) {
129 mGroupMap.remove(groupKey);
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) {
Selim Cinek04be3892017-08-08 10:58:32 -0700135 if (added.row.isRemoved()) {
136 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);
145 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800146 if (isGroupChild) {
Selim Cinek04be3892017-08-08 10:58:32 -0700147 NotificationData.Entry existing = group.children.get(added.key);
148 if (existing != null && existing != added) {
149 Throwable existingThrowable = existing.getDebugThrowable();
150 Log.wtf(TAG, "Inconsistent entries found with the same key " + added.key
151 + "existing removed: " + existing.row.isRemoved()
152 + (existingThrowable != null
153 ? Log.getStackTraceString(existingThrowable) + "\n": "")
154 + " added removed" + added.row.isRemoved()
155 , new Throwable());
156 }
157 group.children.put(added.key, added);
Selim Cinek2a739342016-03-17 10:28:55 -0700158 updateSuppression(group);
Selim Cineke73ad212015-11-03 19:11:08 -0800159 } else {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100160 group.summary = added;
Selim Cinekb5605e52015-02-20 18:21:41 +0100161 group.expanded = added.row.areChildrenExpanded();
Selim Cinek2a739342016-03-17 10:28:55 -0700162 updateSuppression(group);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100163 if (!group.children.isEmpty()) {
Selim Cinek04be3892017-08-08 10:58:32 -0700164 ArrayList<NotificationData.Entry> childrenCopy
165 = new ArrayList<>(group.children.values());
Selim Cinek50e74672016-05-05 15:58:10 -0400166 for (NotificationData.Entry child : childrenCopy) {
167 onEntryBecomingChild(child);
168 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100169 mListener.onGroupCreatedFromChildren(group);
170 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100171 }
Kevina97ea052018-09-11 13:53:18 -0700172 cleanUpAlertStatesOnAdd(group, false /* addIsPending */);
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700173 }
174
175 public void onPendingEntryAdded(NotificationData.Entry shadeEntry) {
176 String groupKey = getGroupKey(shadeEntry.notification);
177 NotificationGroup group = mGroupMap.get(groupKey);
178 if (group != null) {
Kevina97ea052018-09-11 13:53:18 -0700179 cleanUpAlertStatesOnAdd(group, true /* addIsPending */);
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700180 }
181 }
182
183 /**
Kevina97ea052018-09-11 13:53:18 -0700184 * Set whether or not the device is dozing. This allows the group manager to reset some
185 * specific alert state logic based off when the state changes.
186 * @param isDozing if the device is dozing.
187 */
188 public void setDozing(boolean isDozing) {
189 if (mIsDozing != isDozing) {
190 for (NotificationGroup group : mGroupMap.values()) {
191 group.lastAlertTransfer = 0;
192 group.alertSummaryOnNextAddition = false;
193 }
194 }
195 mIsDozing = isDozing;
196 }
197
198 /**
199 * Clean up the alert states when a new child was added.
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700200 * @param group The group where a view was added or will be added.
201 * @param addIsPending True if is the addition still pending or false has it already been added.
202 */
Kevina97ea052018-09-11 13:53:18 -0700203 private void cleanUpAlertStatesOnAdd(NotificationGroup group, boolean addIsPending) {
204
205 AlertingNotificationManager alertManager =
206 mIsDozing ? mAmbientPulseManager : mHeadsUpManager;
207 if (!addIsPending && group.alertSummaryOnNextAddition) {
208 if (!alertManager.isAlerting(group.summary.key)) {
209 alertManager.showNotification(group.summary);
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700210 }
Kevina97ea052018-09-11 13:53:18 -0700211 group.alertSummaryOnNextAddition = false;
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700212 }
213 // Because notification groups are not delivered as a whole unit, it may happen that a
214 // group child gets added quite a bit after the summary got posted. Our guidance is, that
215 // apps should always post the group summary as well and we'll hide it for them if the child
Kevina97ea052018-09-11 13:53:18 -0700216 // is the only child in a group. Because of this, we also have to transfer alert to the
217 // child, otherwise the invisible summary would be alerted.
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700218 // This transfer to the child is not always correct in case the app has just posted another
219 // child in addition to the existing one, but it hasn't arrived in systemUI yet. In such
Kevina97ea052018-09-11 13:53:18 -0700220 // a scenario we would transfer the alert to the old child and the wrong notification
221 // would be alerted. In order to avoid this, we'll recover from this issue and alert the
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700222 // summary again instead of the old child if it's within a certain timeout.
Kevina97ea052018-09-11 13:53:18 -0700223 if (SystemClock.elapsedRealtime() - group.lastAlertTransfer < ALERT_TRANSFER_TIMEOUT) {
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700224 if (!onlySummaryAlerts(group.summary)) {
225 return;
226 }
227 int numChildren = group.children.size();
228 NotificationData.Entry isolatedChild = getIsolatedChild(getGroupKey(
229 group.summary.notification));
230 int numPendingChildren = getPendingChildrenNotAlerting(group);
231 numChildren += numPendingChildren;
232 if (isolatedChild != null) {
233 numChildren++;
234 }
235 if (numChildren <= 1) {
236 return;
237 }
238 boolean releasedChild = false;
239 ArrayList<NotificationData.Entry> children = new ArrayList<>(group.children.values());
240 int size = children.size();
241 for (int i = 0; i < size; i++) {
242 NotificationData.Entry entry = children.get(i);
Kevina97ea052018-09-11 13:53:18 -0700243 if (onlySummaryAlerts(entry) && alertManager.isAlerting(entry.key)) {
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700244 releasedChild = true;
Kevina97ea052018-09-11 13:53:18 -0700245 alertManager.removeNotification(entry.key, true /* releaseImmediately */);
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700246 }
247 }
248 if (isolatedChild != null && onlySummaryAlerts(isolatedChild)
Kevina97ea052018-09-11 13:53:18 -0700249 && alertManager.isAlerting(isolatedChild.key)) {
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700250 releasedChild = true;
Kevina97ea052018-09-11 13:53:18 -0700251 alertManager.removeNotification(isolatedChild.key, true /* releaseImmediately */);
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700252 }
Kevina97ea052018-09-11 13:53:18 -0700253 if (releasedChild && !alertManager.isAlerting(group.summary.key)) {
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700254 boolean notifyImmediately = (numChildren - numPendingChildren) > 1;
255 if (notifyImmediately) {
Kevina97ea052018-09-11 13:53:18 -0700256 alertManager.showNotification(group.summary);
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700257 } else {
Kevina97ea052018-09-11 13:53:18 -0700258 group.alertSummaryOnNextAddition = true;
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700259 }
Kevina97ea052018-09-11 13:53:18 -0700260 group.lastAlertTransfer = 0;
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700261 }
262 }
263 }
264
265 private int getPendingChildrenNotAlerting(NotificationGroup group) {
266 if (mPendingNotifications == null) {
267 return 0;
268 }
269 int number = 0;
270 String groupKey = getGroupKey(group.summary.notification);
271 Collection<NotificationData.Entry> values = mPendingNotifications.values();
272 for (NotificationData.Entry entry : values) {
273 if (!isGroupChild(entry.notification)) {
274 continue;
275 }
276 if (!Objects.equals(getGroupKey(entry.notification), groupKey)) {
277 continue;
278 }
279 if (group.children.containsKey(entry.key)) {
280 continue;
281 }
282 if (onlySummaryAlerts(entry)) {
283 number++;
284 }
285 }
286 return number;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100287 }
288
Selim Cinek50e74672016-05-05 15:58:10 -0400289 private void onEntryBecomingChild(NotificationData.Entry entry) {
Kevina97ea052018-09-11 13:53:18 -0700290 if (shouldIsolate(entry)) {
291 isolateNotification(entry);
Selim Cinek50e74672016-05-05 15:58:10 -0400292 }
293 }
294
Selim Cinek2a739342016-03-17 10:28:55 -0700295 private void updateSuppression(NotificationGroup group) {
Selim Cinek80c44dd2016-08-15 19:39:55 -0700296 if (group == null) {
Selim Cinek23c80342016-03-17 18:27:36 -0700297 return;
298 }
Selim Cinek2a739342016-03-17 10:28:55 -0700299 boolean prevSuppressed = group.suppressed;
Selim Cinek23c80342016-03-17 18:27:36 -0700300 group.suppressed = group.summary != null && !group.expanded
301 && (group.children.size() == 1
302 || (group.children.size() == 0
Julia Reynoldse46bb372016-03-17 11:05:58 -0400303 && group.summary.notification.getNotification().isGroupSummary()
Selim Cinek23c80342016-03-17 18:27:36 -0700304 && hasIsolatedChildren(group)));
Selim Cinek2a739342016-03-17 10:28:55 -0700305 if (prevSuppressed != group.suppressed) {
Selim Cinek967ed2a2016-04-08 18:29:11 -0700306 if (group.suppressed) {
Kevina97ea052018-09-11 13:53:18 -0700307 if (mHeadsUpManager.isAlerting(group.summary.key)) {
308 handleSuppressedSummaryAlerted(group.summary, mHeadsUpManager);
309 } else if (mAmbientPulseManager.isAlerting(group.summary.key)) {
310 handleSuppressedSummaryAlerted(group.summary, mAmbientPulseManager);
311 }
Selim Cinek967ed2a2016-04-08 18:29:11 -0700312 }
Rohan Shah524cf7b2018-03-15 14:40:02 -0700313 if (!mIsUpdatingUnchangedGroup && mListener != null) {
Selim Cinek80c44dd2016-08-15 19:39:55 -0700314 mListener.onGroupsChanged();
315 }
Selim Cinek2a739342016-03-17 10:28:55 -0700316 }
317 }
318
Selim Cinek23c80342016-03-17 18:27:36 -0700319 private boolean hasIsolatedChildren(NotificationGroup group) {
320 return getNumberOfIsolatedChildren(group.summary.notification.getGroupKey()) != 0;
321 }
322
323 private int getNumberOfIsolatedChildren(String groupKey) {
324 int count = 0;
Selim Cineka6c0cef2016-03-18 11:42:25 -0700325 for (StatusBarNotification sbn : mIsolatedEntries.values()) {
Selim Cinek23c80342016-03-17 18:27:36 -0700326 if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) {
327 count++;
328 }
329 }
330 return count;
331 }
332
Selim Cinek967ed2a2016-04-08 18:29:11 -0700333 private NotificationData.Entry getIsolatedChild(String groupKey) {
334 for (StatusBarNotification sbn : mIsolatedEntries.values()) {
335 if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) {
336 return mGroupMap.get(sbn.getKey()).summary;
337 }
338 }
339 return null;
340 }
341
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100342 public void onEntryUpdated(NotificationData.Entry entry,
343 StatusBarNotification oldNotification) {
Selim Cinek68bdff12016-08-03 13:38:56 -0700344 String oldKey = oldNotification.getGroupKey();
345 String newKey = entry.notification.getGroupKey();
346 boolean groupKeysChanged = !oldKey.equals(newKey);
347 boolean wasGroupChild = isGroupChild(oldNotification);
348 boolean isGroupChild = isGroupChild(entry.notification);
Selim Cinek80c44dd2016-08-15 19:39:55 -0700349 mIsUpdatingUnchangedGroup = !groupKeysChanged && wasGroupChild == isGroupChild;
Selim Cinekef5127e2015-12-21 16:55:58 -0800350 if (mGroupMap.get(getGroupKey(oldNotification)) != null) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100351 onEntryRemovedInternal(entry, oldNotification);
352 }
353 onEntryAdded(entry);
Selim Cinek80c44dd2016-08-15 19:39:55 -0700354 mIsUpdatingUnchangedGroup = false;
Selim Cineka6c0cef2016-03-18 11:42:25 -0700355 if (isIsolated(entry.notification)) {
356 mIsolatedEntries.put(entry.key, entry.notification);
Selim Cinek68bdff12016-08-03 13:38:56 -0700357 if (groupKeysChanged) {
Selim Cinek23c80342016-03-17 18:27:36 -0700358 updateSuppression(mGroupMap.get(oldKey));
359 updateSuppression(mGroupMap.get(newKey));
360 }
Selim Cinek68bdff12016-08-03 13:38:56 -0700361 } else if (!wasGroupChild && isGroupChild) {
Selim Cinek50e74672016-05-05 15:58:10 -0400362 onEntryBecomingChild(entry);
Selim Cinek23c80342016-03-17 18:27:36 -0700363 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100364 }
365
Selim Cinek2a739342016-03-17 10:28:55 -0700366 public boolean isSummaryOfSuppressedGroup(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700367 return isGroupSuppressed(getGroupKey(sbn)) && sbn.getNotification().isGroupSummary();
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100368 }
369
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700370 private boolean isOnlyChild(StatusBarNotification sbn) {
Selim Cinek36b02232016-05-11 23:07:05 -0400371 return !sbn.getNotification().isGroupSummary()
Selim Cinek23c80342016-03-17 18:27:36 -0700372 && getTotalNumberOfChildren(sbn) == 1;
Selim Cinek2a739342016-03-17 10:28:55 -0700373 }
374
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700375 public boolean isOnlyChildInGroup(StatusBarNotification sbn) {
Selim Cinek88086e72016-06-17 21:01:32 -0700376 if (!isOnlyChild(sbn)) {
377 return false;
378 }
379 ExpandableNotificationRow logicalGroupSummary = getLogicalGroupSummary(sbn);
380 return logicalGroupSummary != null
381 && !logicalGroupSummary.getStatusBarNotification().equals(sbn);
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700382 }
383
Selim Cinek23c80342016-03-17 18:27:36 -0700384 private int getTotalNumberOfChildren(StatusBarNotification sbn) {
Selim Cinek53c4d3d2016-05-23 10:56:34 -0700385 int isolatedChildren = getNumberOfIsolatedChildren(sbn.getGroupKey());
386 NotificationGroup group = mGroupMap.get(sbn.getGroupKey());
387 int realChildren = group != null ? group.children.size() : 0;
388 return isolatedChildren + realChildren;
Selim Cinek23c80342016-03-17 18:27:36 -0700389 }
390
391 private boolean isGroupSuppressed(String groupKey) {
392 NotificationGroup group = mGroupMap.get(groupKey);
Selim Cinek2a739342016-03-17 10:28:55 -0700393 return group != null && group.suppressed;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100394 }
395
Jason Monk1fd3fc32018-08-14 17:20:09 -0400396 private void setStatusBarState(int newState) {
Selim Cinek9c4c4142015-12-04 16:44:56 -0800397 mBarState = newState;
398 if (mBarState == StatusBarState.KEYGUARD) {
Selim Cinek9184f9c2016-02-02 17:36:53 -0800399 collapseAllGroups();
400 }
401 }
402
403 public void collapseAllGroups() {
Selim Cinekc0b14b02016-05-09 13:12:40 -0400404 // Because notifications can become isolated when the group becomes suppressed it can
405 // lead to concurrent modifications while looping. We need to make a copy.
406 ArrayList<NotificationGroup> groupCopy = new ArrayList<>(mGroupMap.values());
407 int size = groupCopy.size();
408 for (int i = 0; i < size; i++) {
409 NotificationGroup group = groupCopy.get(i);
Selim Cinek9184f9c2016-02-02 17:36:53 -0800410 if (group.expanded) {
411 setGroupExpanded(group, false);
Selim Cinek9c4c4142015-12-04 16:44:56 -0800412 }
Selim Cinek2a739342016-03-17 10:28:55 -0700413 updateSuppression(group);
Selim Cinek9c4c4142015-12-04 16:44:56 -0800414 }
415 }
416
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100417 /**
418 * @return whether a given notification is a child in a group which has a summary
419 */
420 public boolean isChildInGroupWithSummary(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800421 if (!isGroupChild(sbn)) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100422 return false;
423 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800424 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek2a739342016-03-17 10:28:55 -0700425 if (group == null || group.summary == null || group.suppressed) {
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100426 return false;
427 }
Selim Cinek3f19f602016-05-02 18:01:56 -0700428 if (group.children.isEmpty()) {
429 // If the suppression of a group changes because the last child was removed, this can
430 // still be called temporarily because the child hasn't been fully removed yet. Let's
431 // make sure we still return false in that case.
432 return false;
433 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100434 return true;
435 }
436
Selim Cinek263398f2015-10-21 17:40:23 -0700437 /**
438 * @return whether a given notification is a summary in a group which has children
439 */
440 public boolean isSummaryOfGroup(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800441 if (!isGroupSummary(sbn)) {
Selim Cinek263398f2015-10-21 17:40:23 -0700442 return false;
443 }
Selim Cinekef5127e2015-12-21 16:55:58 -0800444 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Kevin96eed642018-09-05 18:53:33 -0700445 if (group == null || group.summary == null) {
Selim Cinek263398f2015-10-21 17:40:23 -0700446 return false;
447 }
Kevin96eed642018-09-05 18:53:33 -0700448 return !group.children.isEmpty() && Objects.equals(group.summary.notification, sbn);
Selim Cinek263398f2015-10-21 17:40:23 -0700449 }
450
Selim Cinek23c80342016-03-17 18:27:36 -0700451 /**
452 * Get the summary of a specified status bar notification. For isolated notification this return
453 * itself.
454 */
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100455 public ExpandableNotificationRow getGroupSummary(StatusBarNotification sbn) {
Selim Cinek23c80342016-03-17 18:27:36 -0700456 return getGroupSummary(getGroupKey(sbn));
457 }
458
459 /**
460 * Similar to {@link #getGroupSummary(StatusBarNotification)} but doesn't get the visual summary
461 * but the logical summary, i.e when a child is isolated, it still returns the summary as if
462 * it wasn't isolated.
463 */
464 public ExpandableNotificationRow getLogicalGroupSummary(
465 StatusBarNotification sbn) {
466 return getGroupSummary(sbn.getGroupKey());
467 }
468
469 @Nullable
470 private ExpandableNotificationRow getGroupSummary(String groupKey) {
471 NotificationGroup group = mGroupMap.get(groupKey);
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100472 return group == null ? null
473 : group.summary == null ? null
Selim Cinek23c80342016-03-17 18:27:36 -0700474 : group.summary.row;
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100475 }
476
Chris Wren698b1702016-05-23 11:16:32 -0400477 /** @return group expansion state after toggling. */
478 public boolean toggleGroupExpansion(StatusBarNotification sbn) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800479 NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
Selim Cinek83bc7832015-10-22 13:26:54 -0700480 if (group == null) {
Chris Wren698b1702016-05-23 11:16:32 -0400481 return false;
Selim Cinek83bc7832015-10-22 13:26:54 -0700482 }
483 setGroupExpanded(group, !group.expanded);
Chris Wren698b1702016-05-23 11:16:32 -0400484 return group.expanded;
Selim Cinek83bc7832015-10-22 13:26:54 -0700485 }
486
Selim Cinekef5127e2015-12-21 16:55:58 -0800487 private boolean isIsolated(StatusBarNotification sbn) {
Selim Cineka6c0cef2016-03-18 11:42:25 -0700488 return mIsolatedEntries.containsKey(sbn.getKey());
Selim Cinekef5127e2015-12-21 16:55:58 -0800489 }
490
491 private boolean isGroupSummary(StatusBarNotification sbn) {
492 if (isIsolated(sbn)) {
493 return true;
494 }
495 return sbn.getNotification().isGroupSummary();
496 }
Julia Reynoldse46bb372016-03-17 11:05:58 -0400497
Selim Cinekef5127e2015-12-21 16:55:58 -0800498 private boolean isGroupChild(StatusBarNotification sbn) {
499 if (isIsolated(sbn)) {
500 return false;
501 }
Julia Reynoldse46bb372016-03-17 11:05:58 -0400502 return sbn.isGroup() && !sbn.getNotification().isGroupSummary();
Selim Cinekef5127e2015-12-21 16:55:58 -0800503 }
504
505 private String getGroupKey(StatusBarNotification sbn) {
506 if (isIsolated(sbn)) {
507 return sbn.getKey();
508 }
509 return sbn.getGroupKey();
510 }
511
512 @Override
513 public void onHeadsUpPinnedModeChanged(boolean inPinnedMode) {
514 }
515
516 @Override
517 public void onHeadsUpPinned(ExpandableNotificationRow headsUp) {
518 }
519
520 @Override
521 public void onHeadsUpUnPinned(ExpandableNotificationRow headsUp) {
522 }
523
524 @Override
Kevina97ea052018-09-11 13:53:18 -0700525 public void onAmbientStateChanged(NotificationData.Entry entry, boolean isAmbient) {
526 onAlertStateChanged(entry, isAmbient, mAmbientPulseManager);
527 }
528
529 @Override
Selim Cinekef5127e2015-12-21 16:55:58 -0800530 public void onHeadsUpStateChanged(NotificationData.Entry entry, boolean isHeadsUp) {
Kevina97ea052018-09-11 13:53:18 -0700531 onAlertStateChanged(entry, isHeadsUp, mHeadsUpManager);
532 }
533
534 private void onAlertStateChanged(NotificationData.Entry entry, boolean isAlerting,
535 AlertingNotificationManager alertManager) {
Selim Cinekef5127e2015-12-21 16:55:58 -0800536 final StatusBarNotification sbn = entry.notification;
Kevina97ea052018-09-11 13:53:18 -0700537 if (isAlerting) {
538 if (shouldIsolate(entry)) {
539 isolateNotification(entry);
540 } else if (sbn.getNotification().isGroupSummary()
541 && isGroupSuppressed(sbn.getGroupKey())){
542 handleSuppressedSummaryAlerted(entry, alertManager);
Selim Cinekef5127e2015-12-21 16:55:58 -0800543 }
544 } else {
Kevina97ea052018-09-11 13:53:18 -0700545 stopIsolatingNotification(entry);
Selim Cinekef5127e2015-12-21 16:55:58 -0800546 }
547 }
548
Kevina97ea052018-09-11 13:53:18 -0700549 /**
550 * Handles the scenario where a summary that has been suppressed is alerted. A suppressed
551 * summary should for all intents and purposes be invisible to the user and as a result should
552 * not alert. When this is the case, it is our responsibility to pass the alert to the
553 * appropriate child which will be the representative notification alerting for the group.
554 * @param summary the summary that is suppressed and alerting
555 * @param alertManager the alert manager that manages the alerting summary
556 */
557 private void handleSuppressedSummaryAlerted(@NonNull NotificationData.Entry summary,
558 @NonNull AlertingNotificationManager alertManager) {
559 StatusBarNotification sbn = summary.notification;
Selim Cinek967ed2a2016-04-08 18:29:11 -0700560 if (!isGroupSuppressed(sbn.getGroupKey())
561 || !sbn.getNotification().isGroupSummary()
Kevina97ea052018-09-11 13:53:18 -0700562 || !alertManager.isAlerting(sbn.getKey())) {
Selim Cinek967ed2a2016-04-08 18:29:11 -0700563 return;
564 }
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700565
Kevina97ea052018-09-11 13:53:18 -0700566 // The parent of a suppressed group got alerted, lets alert the child!
Selim Cinek967ed2a2016-04-08 18:29:11 -0700567 NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey());
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700568
Selim Cinek967ed2a2016-04-08 18:29:11 -0700569 if (notificationGroup != null) {
Kevina97ea052018-09-11 13:53:18 -0700570 if (pendingInflationsWillAddChildren(notificationGroup)) {
571 // New children will actually be added to this group, let's not transfer the alert.
572 return;
573 }
574
Selim Cinek04be3892017-08-08 10:58:32 -0700575 Iterator<NotificationData.Entry> iterator
576 = notificationGroup.children.values().iterator();
Selim Cinek967ed2a2016-04-08 18:29:11 -0700577 NotificationData.Entry child = iterator.hasNext() ? iterator.next() : null;
578 if (child == null) {
579 child = getIsolatedChild(sbn.getGroupKey());
580 }
581 if (child != null) {
Selim Cinek40f88762016-12-16 16:56:41 -0800582 if (child.row.keepInParent() || child.row.isRemoved() || child.row.isDismissed()) {
Kevina97ea052018-09-11 13:53:18 -0700583 // the notification is actually already removed, no need to do alert on it.
Selim Cinek40f88762016-12-16 16:56:41 -0800584 return;
585 }
Kevina97ea052018-09-11 13:53:18 -0700586 transferAlertStateToChild(summary, child, alertManager);
Selim Cinek967ed2a2016-04-08 18:29:11 -0700587 }
588 }
Kevina97ea052018-09-11 13:53:18 -0700589 }
590
591 /**
592 * Transfers the alert state from a given summary notification to the specified child. The
593 * result is the child will now alert while the summary does not.
594 *
595 * @param summary the currently alerting summary notification
596 * @param child the child that should receive the alert
597 * @param alertManager the manager for the alert
598 */
599 private void transferAlertStateToChild(@NonNull NotificationData.Entry summary,
600 @NonNull NotificationData.Entry child,
601 @NonNull AlertingNotificationManager alertManager) {
602 NotificationGroup notificationGroup = mGroupMap.get(summary.notification.getGroupKey());
603 if (alertManager.isAlerting(child.key)) {
604 alertManager.updateNotification(child.key, true /* alert */);
605 } else {
606 if (onlySummaryAlerts(summary)) {
607 notificationGroup.lastAlertTransfer = SystemClock.elapsedRealtime();
608 }
609 alertManager.showNotification(child);
610 }
611 alertManager.removeNotification(summary.key, true /* releaseImmediately */);
Selim Cinek967ed2a2016-04-08 18:29:11 -0700612 }
613
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700614 private boolean onlySummaryAlerts(NotificationData.Entry entry) {
615 return entry.notification.getNotification().getGroupAlertBehavior()
616 == Notification.GROUP_ALERT_SUMMARY;
617 }
618
619 /**
620 * Check if the pending inflations will add children to this group.
621 * @param group The group to check.
622 */
623 private boolean pendingInflationsWillAddChildren(NotificationGroup group) {
624 if (mPendingNotifications == null) {
625 return false;
626 }
627 Collection<NotificationData.Entry> values = mPendingNotifications.values();
628 String groupKey = getGroupKey(group.summary.notification);
629 for (NotificationData.Entry entry : values) {
630 if (!isGroupChild(entry.notification)) {
631 continue;
632 }
633 if (!Objects.equals(getGroupKey(entry.notification), groupKey)) {
634 continue;
635 }
636 if (!group.children.containsKey(entry.key)) {
637 return true;
638 }
639 }
640 return false;
641 }
642
Kevina97ea052018-09-11 13:53:18 -0700643 /**
644 * Whether a notification that is normally part of a group should be temporarily isolated from
645 * the group and put in their own group visually. This generally happens when the notification
646 * is alerting.
647 *
648 * @param entry the notification to check
649 * @return true if the entry should be isolated
650 */
651
652 private boolean shouldIsolate(NotificationData.Entry entry) {
653 StatusBarNotification sbn = entry.notification;
Selim Cineka6c0cef2016-03-18 11:42:25 -0700654 NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey());
Kevina97ea052018-09-11 13:53:18 -0700655 if (!sbn.isGroup() || sbn.getNotification().isGroupSummary()) {
656 return false;
657 }
658 if (!mIsDozing && !mHeadsUpManager.isAlerting(entry.key)) {
659 return false;
660 }
661 if (mIsDozing && !mAmbientPulseManager.isAlerting(entry.key)) {
662 return false;
663 }
664 return (sbn.getNotification().fullScreenIntent != null
665 || notificationGroup == null
666 || !notificationGroup.expanded
667 || isGroupNotFullyVisible(notificationGroup));
668 }
669
670 /**
671 * Isolate a notification from its group so that it visually shows as its own group.
672 *
673 * @param entry the notification to isolate
674 */
675 private void isolateNotification(NotificationData.Entry entry) {
676 StatusBarNotification sbn = entry.notification;
677
678 // We will be isolated now, so lets update the groups
679 onEntryRemovedInternal(entry, entry.notification);
680
681 mIsolatedEntries.put(sbn.getKey(), sbn);
682
683 onEntryAdded(entry);
684 // We also need to update the suppression of the old group, because this call comes
685 // even before the groupManager knows about the notification at all.
686 // When the notification gets added afterwards it is already isolated and therefore
687 // it doesn't lead to an update.
688 updateSuppression(mGroupMap.get(entry.notification.getGroupKey()));
689 mListener.onGroupsChanged();
690 }
691
692 /**
693 * Stop isolating a notification and re-group it with its original logical group.
694 *
695 * @param entry the notification to un-isolate
696 */
697 private void stopIsolatingNotification(NotificationData.Entry entry) {
698 StatusBarNotification sbn = entry.notification;
699 if (mIsolatedEntries.containsKey(sbn.getKey())) {
700 // not isolated anymore, we need to update the groups
701 onEntryRemovedInternal(entry, entry.notification);
702 mIsolatedEntries.remove(sbn.getKey());
703 onEntryAdded(entry);
704 mListener.onGroupsChanged();
705 }
Selim Cineka6c0cef2016-03-18 11:42:25 -0700706 }
707
708 private boolean isGroupNotFullyVisible(NotificationGroup notificationGroup) {
709 return notificationGroup.summary == null
Selim Cineka6c0cef2016-03-18 11:42:25 -0700710 || notificationGroup.summary.row.getClipTopAmount() > 0
711 || notificationGroup.summary.row.getTranslationY() < 0;
712 }
713
Selim Cinek967ed2a2016-04-08 18:29:11 -0700714 public void setHeadsUpManager(HeadsUpManager headsUpManager) {
715 mHeadsUpManager = headsUpManager;
716 }
717
Selim Cinek52941c52016-05-07 18:29:32 -0400718 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
719 pw.println("GroupManager state:");
720 pw.println(" number of groups: " + mGroupMap.size());
721 for (Map.Entry<String, NotificationGroup> entry : mGroupMap.entrySet()) {
722 pw.println("\n key: " + entry.getKey()); pw.println(entry.getValue());
723 }
724 pw.println("\n isolated entries: " + mIsolatedEntries.size());
725 for (Map.Entry<String, StatusBarNotification> entry : mIsolatedEntries.entrySet()) {
726 pw.print(" "); pw.print(entry.getKey());
727 pw.print(", "); pw.println(entry.getValue());
728 }
729 }
730
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700731 public void setPendingEntries(HashMap<String, NotificationData.Entry> pendingNotifications) {
732 mPendingNotifications = pendingNotifications;
733 }
734
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100735 public static class NotificationGroup {
Selim Cinek04be3892017-08-08 10:58:32 -0700736 public final HashMap<String, NotificationData.Entry> children = new HashMap<>();
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100737 public NotificationData.Entry summary;
738 public boolean expanded;
Selim Cinek2a739342016-03-17 10:28:55 -0700739 /**
740 * Is this notification group suppressed, i.e its summary is hidden
741 */
742 public boolean suppressed;
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700743 /**
Kevina97ea052018-09-11 13:53:18 -0700744 * The time when the last alert transfer from group to child happened, while the summary
745 * has the flags to alert up on its own.
Selim Cinekf93bf3e2018-05-08 14:43:21 -0700746 */
Kevina97ea052018-09-11 13:53:18 -0700747 public long lastAlertTransfer;
748 public boolean alertSummaryOnNextAddition;
Selim Cinek52941c52016-05-07 18:29:32 -0400749
750 @Override
751 public String toString() {
Selim Cinek60ca7872016-05-12 11:24:07 -0700752 String result = " summary:\n "
Selim Cinek04be3892017-08-08 10:58:32 -0700753 + (summary != null ? summary.notification : "null")
754 + (summary != null && summary.getDebugThrowable() != null
755 ? Log.getStackTraceString(summary.getDebugThrowable())
756 : "");
Selim Cinek52941c52016-05-07 18:29:32 -0400757 result += "\n children size: " + children.size();
Selim Cinek04be3892017-08-08 10:58:32 -0700758 for (NotificationData.Entry child : children.values()) {
759 result += "\n " + child.notification
760 + (child.getDebugThrowable() != null
761 ? Log.getStackTraceString(child.getDebugThrowable())
762 : "");
Selim Cinek52941c52016-05-07 18:29:32 -0400763 }
764 return result;
765 }
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100766 }
767
768 public interface OnGroupChangeListener {
769 /**
770 * The expansion of a group has changed.
771 *
772 * @param changedRow the row for which the expansion has changed, which is also the summary
773 * @param expanded a boolean indicating the new expanded state
774 */
775 void onGroupExpansionChanged(ExpandableNotificationRow changedRow, boolean expanded);
776
777 /**
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100778 * A group of children just received a summary notification and should therefore become
779 * children of it.
780 *
781 * @param group the group created
782 */
783 void onGroupCreatedFromChildren(NotificationGroup group);
Selim Cinekef5127e2015-12-21 16:55:58 -0800784
785 /**
Selim Cinek2a739342016-03-17 10:28:55 -0700786 * The groups have changed. This can happen if the isolation of a child has changes or if a
787 * group became suppressed / unsuppressed
Selim Cinekef5127e2015-12-21 16:55:58 -0800788 */
Selim Cinek2a739342016-03-17 10:28:55 -0700789 void onGroupsChanged();
Selim Cinek25fd4e2b2015-02-20 17:46:07 +0100790 }
791}