Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.statusbar.phone; |
| 18 | |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 19 | import android.app.Notification; |
| 20 | import android.os.SystemClock; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 21 | import android.service.notification.StatusBarNotification; |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 22 | import android.util.Log; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 23 | |
Evan Laird | 878c853 | 2018-10-15 15:54:29 -0400 | [diff] [blame^] | 24 | import androidx.annotation.NonNull; |
| 25 | import androidx.annotation.Nullable; |
| 26 | |
| 27 | import com.android.internal.annotations.VisibleForTesting; |
Jason Monk | 1fd3fc3 | 2018-08-14 17:20:09 -0400 | [diff] [blame] | 28 | import com.android.systemui.Dependency; |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 29 | import com.android.systemui.statusbar.AlertingNotificationManager; |
| 30 | import com.android.systemui.statusbar.AmbientPulseManager; |
| 31 | import com.android.systemui.statusbar.AmbientPulseManager.OnAmbientChangedListener; |
Jason Monk | 1fd3fc3 | 2018-08-14 17:20:09 -0400 | [diff] [blame] | 32 | import com.android.systemui.statusbar.StatusBarState; |
Jason Monk | 1fd3fc3 | 2018-08-14 17:20:09 -0400 | [diff] [blame] | 33 | import com.android.systemui.statusbar.StatusBarStateController; |
Evan Laird | 878c853 | 2018-10-15 15:54:29 -0400 | [diff] [blame^] | 34 | import com.android.systemui.statusbar.StatusBarStateController.StateListener; |
| 35 | import com.android.systemui.statusbar.notification.NotificationData; |
| 36 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 37 | import com.android.systemui.statusbar.policy.HeadsUpManager; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 38 | import com.android.systemui.statusbar.policy.OnHeadsUpChangedListener; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 39 | |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame] | 40 | import java.io.FileDescriptor; |
| 41 | import java.io.PrintWriter; |
Selim Cinek | c0b14b0 | 2016-05-09 13:12:40 -0400 | [diff] [blame] | 42 | import java.util.ArrayList; |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 43 | import java.util.Collection; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 44 | import java.util.HashMap; |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 45 | import java.util.Iterator; |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame] | 46 | import java.util.Map; |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 47 | import java.util.Objects; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * A class to handle notifications and their corresponding groups. |
| 51 | */ |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 52 | public class NotificationGroupManager implements OnHeadsUpChangedListener, |
Evan Laird | 878c853 | 2018-10-15 15:54:29 -0400 | [diff] [blame^] | 53 | OnAmbientChangedListener, StateListener { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 54 | |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 55 | private static final String TAG = "NotificationGroupManager"; |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 56 | private static final long ALERT_TRANSFER_TIMEOUT = 300; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 57 | private final HashMap<String, NotificationGroup> mGroupMap = new HashMap<>(); |
| 58 | private OnGroupChangeListener mListener; |
| 59 | private int mBarState = -1; |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 60 | private HashMap<String, StatusBarNotification> mIsolatedEntries = new HashMap<>(); |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 61 | private HeadsUpManager mHeadsUpManager; |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 62 | private AmbientPulseManager mAmbientPulseManager = Dependency.get(AmbientPulseManager.class); |
| 63 | private boolean mIsDozing; |
Selim Cinek | 80c44dd | 2016-08-15 19:39:55 -0700 | [diff] [blame] | 64 | private boolean mIsUpdatingUnchangedGroup; |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 65 | private HashMap<String, NotificationData.Entry> mPendingNotifications; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 66 | |
Jason Monk | 1fd3fc3 | 2018-08-14 17:20:09 -0400 | [diff] [blame] | 67 | public NotificationGroupManager() { |
Evan Laird | 878c853 | 2018-10-15 15:54:29 -0400 | [diff] [blame^] | 68 | Dependency.get(StatusBarStateController.class).addListener(this); |
Jason Monk | 1fd3fc3 | 2018-08-14 17:20:09 -0400 | [diff] [blame] | 69 | } |
| 70 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 71 | public void setOnGroupChangeListener(OnGroupChangeListener listener) { |
| 72 | mListener = listener; |
| 73 | } |
| 74 | |
| 75 | public boolean isGroupExpanded(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 76 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 77 | if (group == null) { |
| 78 | return false; |
| 79 | } |
| 80 | return group.expanded; |
| 81 | } |
| 82 | |
| 83 | public void setGroupExpanded(StatusBarNotification sbn, boolean expanded) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 84 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 85 | 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 Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame] | 100 | mIsolatedEntries.remove(removed.key); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 101 | } |
| 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 Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 112 | String groupKey = getGroupKey(sbn); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 113 | final NotificationGroup group = mGroupMap.get(groupKey); |
Selim Cinek | 0b4aeab | 2015-09-01 17:07:38 -0700 | [diff] [blame] | 114 | 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 Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 121 | if (isGroupChild(sbn)) { |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 122 | group.children.remove(removed.key); |
Selim Cinek | e73ad21 | 2015-11-03 19:11:08 -0800 | [diff] [blame] | 123 | } else { |
| 124 | group.summary = null; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 125 | } |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 126 | updateSuppression(group); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 127 | if (group.children.isEmpty()) { |
| 128 | if (group.summary == null) { |
| 129 | mGroupMap.remove(groupKey); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 134 | public void onEntryAdded(final NotificationData.Entry added) { |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 135 | if (added.row.isRemoved()) { |
| 136 | added.setDebugThrowable(new Throwable()); |
| 137 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 138 | final StatusBarNotification sbn = added.notification; |
| 139 | boolean isGroupChild = isGroupChild(sbn); |
| 140 | String groupKey = getGroupKey(sbn); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 141 | NotificationGroup group = mGroupMap.get(groupKey); |
| 142 | if (group == null) { |
| 143 | group = new NotificationGroup(); |
| 144 | mGroupMap.put(groupKey, group); |
| 145 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 146 | if (isGroupChild) { |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 147 | 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 Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 158 | updateSuppression(group); |
Selim Cinek | e73ad21 | 2015-11-03 19:11:08 -0800 | [diff] [blame] | 159 | } else { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 160 | group.summary = added; |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 161 | group.expanded = added.row.areChildrenExpanded(); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 162 | updateSuppression(group); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 163 | if (!group.children.isEmpty()) { |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 164 | ArrayList<NotificationData.Entry> childrenCopy |
| 165 | = new ArrayList<>(group.children.values()); |
Selim Cinek | 50e7467 | 2016-05-05 15:58:10 -0400 | [diff] [blame] | 166 | for (NotificationData.Entry child : childrenCopy) { |
| 167 | onEntryBecomingChild(child); |
| 168 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 169 | mListener.onGroupCreatedFromChildren(group); |
| 170 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 171 | } |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 172 | cleanUpAlertStatesOnAdd(group, false /* addIsPending */); |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | public void onPendingEntryAdded(NotificationData.Entry shadeEntry) { |
| 176 | String groupKey = getGroupKey(shadeEntry.notification); |
| 177 | NotificationGroup group = mGroupMap.get(groupKey); |
| 178 | if (group != null) { |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 179 | cleanUpAlertStatesOnAdd(group, true /* addIsPending */); |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | /** |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 184 | * 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 | */ |
Evan Laird | 878c853 | 2018-10-15 15:54:29 -0400 | [diff] [blame^] | 188 | @VisibleForTesting |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 189 | public void setDozing(boolean isDozing) { |
| 190 | if (mIsDozing != isDozing) { |
| 191 | for (NotificationGroup group : mGroupMap.values()) { |
| 192 | group.lastAlertTransfer = 0; |
| 193 | group.alertSummaryOnNextAddition = false; |
| 194 | } |
| 195 | } |
| 196 | mIsDozing = isDozing; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Clean up the alert states when a new child was added. |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 201 | * @param group The group where a view was added or will be added. |
| 202 | * @param addIsPending True if is the addition still pending or false has it already been added. |
| 203 | */ |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 204 | private void cleanUpAlertStatesOnAdd(NotificationGroup group, boolean addIsPending) { |
| 205 | |
| 206 | AlertingNotificationManager alertManager = |
| 207 | mIsDozing ? mAmbientPulseManager : mHeadsUpManager; |
| 208 | if (!addIsPending && group.alertSummaryOnNextAddition) { |
| 209 | if (!alertManager.isAlerting(group.summary.key)) { |
| 210 | alertManager.showNotification(group.summary); |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 211 | } |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 212 | group.alertSummaryOnNextAddition = false; |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 213 | } |
| 214 | // Because notification groups are not delivered as a whole unit, it may happen that a |
| 215 | // group child gets added quite a bit after the summary got posted. Our guidance is, that |
| 216 | // apps should always post the group summary as well and we'll hide it for them if the child |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 217 | // is the only child in a group. Because of this, we also have to transfer alert to the |
| 218 | // child, otherwise the invisible summary would be alerted. |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 219 | // This transfer to the child is not always correct in case the app has just posted another |
| 220 | // child in addition to the existing one, but it hasn't arrived in systemUI yet. In such |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 221 | // a scenario we would transfer the alert to the old child and the wrong notification |
| 222 | // would be alerted. In order to avoid this, we'll recover from this issue and alert the |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 223 | // summary again instead of the old child if it's within a certain timeout. |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 224 | if (SystemClock.elapsedRealtime() - group.lastAlertTransfer < ALERT_TRANSFER_TIMEOUT) { |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 225 | if (!onlySummaryAlerts(group.summary)) { |
| 226 | return; |
| 227 | } |
| 228 | int numChildren = group.children.size(); |
| 229 | NotificationData.Entry isolatedChild = getIsolatedChild(getGroupKey( |
| 230 | group.summary.notification)); |
| 231 | int numPendingChildren = getPendingChildrenNotAlerting(group); |
| 232 | numChildren += numPendingChildren; |
| 233 | if (isolatedChild != null) { |
| 234 | numChildren++; |
| 235 | } |
| 236 | if (numChildren <= 1) { |
| 237 | return; |
| 238 | } |
| 239 | boolean releasedChild = false; |
| 240 | ArrayList<NotificationData.Entry> children = new ArrayList<>(group.children.values()); |
| 241 | int size = children.size(); |
| 242 | for (int i = 0; i < size; i++) { |
| 243 | NotificationData.Entry entry = children.get(i); |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 244 | if (onlySummaryAlerts(entry) && alertManager.isAlerting(entry.key)) { |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 245 | releasedChild = true; |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 246 | alertManager.removeNotification(entry.key, true /* releaseImmediately */); |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | if (isolatedChild != null && onlySummaryAlerts(isolatedChild) |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 250 | && alertManager.isAlerting(isolatedChild.key)) { |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 251 | releasedChild = true; |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 252 | alertManager.removeNotification(isolatedChild.key, true /* releaseImmediately */); |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 253 | } |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 254 | if (releasedChild && !alertManager.isAlerting(group.summary.key)) { |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 255 | boolean notifyImmediately = (numChildren - numPendingChildren) > 1; |
| 256 | if (notifyImmediately) { |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 257 | alertManager.showNotification(group.summary); |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 258 | } else { |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 259 | group.alertSummaryOnNextAddition = true; |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 260 | } |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 261 | group.lastAlertTransfer = 0; |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | private int getPendingChildrenNotAlerting(NotificationGroup group) { |
| 267 | if (mPendingNotifications == null) { |
| 268 | return 0; |
| 269 | } |
| 270 | int number = 0; |
| 271 | String groupKey = getGroupKey(group.summary.notification); |
| 272 | Collection<NotificationData.Entry> values = mPendingNotifications.values(); |
| 273 | for (NotificationData.Entry entry : values) { |
| 274 | if (!isGroupChild(entry.notification)) { |
| 275 | continue; |
| 276 | } |
| 277 | if (!Objects.equals(getGroupKey(entry.notification), groupKey)) { |
| 278 | continue; |
| 279 | } |
| 280 | if (group.children.containsKey(entry.key)) { |
| 281 | continue; |
| 282 | } |
| 283 | if (onlySummaryAlerts(entry)) { |
| 284 | number++; |
| 285 | } |
| 286 | } |
| 287 | return number; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 288 | } |
| 289 | |
Selim Cinek | 50e7467 | 2016-05-05 15:58:10 -0400 | [diff] [blame] | 290 | private void onEntryBecomingChild(NotificationData.Entry entry) { |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 291 | if (shouldIsolate(entry)) { |
| 292 | isolateNotification(entry); |
Selim Cinek | 50e7467 | 2016-05-05 15:58:10 -0400 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 296 | private void updateSuppression(NotificationGroup group) { |
Selim Cinek | 80c44dd | 2016-08-15 19:39:55 -0700 | [diff] [blame] | 297 | if (group == null) { |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 298 | return; |
| 299 | } |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 300 | boolean prevSuppressed = group.suppressed; |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 301 | group.suppressed = group.summary != null && !group.expanded |
| 302 | && (group.children.size() == 1 |
| 303 | || (group.children.size() == 0 |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 304 | && group.summary.notification.getNotification().isGroupSummary() |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 305 | && hasIsolatedChildren(group))); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 306 | if (prevSuppressed != group.suppressed) { |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 307 | if (group.suppressed) { |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 308 | if (mHeadsUpManager.isAlerting(group.summary.key)) { |
| 309 | handleSuppressedSummaryAlerted(group.summary, mHeadsUpManager); |
| 310 | } else if (mAmbientPulseManager.isAlerting(group.summary.key)) { |
| 311 | handleSuppressedSummaryAlerted(group.summary, mAmbientPulseManager); |
| 312 | } |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 313 | } |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 314 | if (!mIsUpdatingUnchangedGroup && mListener != null) { |
Selim Cinek | 80c44dd | 2016-08-15 19:39:55 -0700 | [diff] [blame] | 315 | mListener.onGroupsChanged(); |
| 316 | } |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 320 | private boolean hasIsolatedChildren(NotificationGroup group) { |
| 321 | return getNumberOfIsolatedChildren(group.summary.notification.getGroupKey()) != 0; |
| 322 | } |
| 323 | |
| 324 | private int getNumberOfIsolatedChildren(String groupKey) { |
| 325 | int count = 0; |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 326 | for (StatusBarNotification sbn : mIsolatedEntries.values()) { |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 327 | if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) { |
| 328 | count++; |
| 329 | } |
| 330 | } |
| 331 | return count; |
| 332 | } |
| 333 | |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 334 | private NotificationData.Entry getIsolatedChild(String groupKey) { |
| 335 | for (StatusBarNotification sbn : mIsolatedEntries.values()) { |
| 336 | if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) { |
| 337 | return mGroupMap.get(sbn.getKey()).summary; |
| 338 | } |
| 339 | } |
| 340 | return null; |
| 341 | } |
| 342 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 343 | public void onEntryUpdated(NotificationData.Entry entry, |
| 344 | StatusBarNotification oldNotification) { |
Selim Cinek | 68bdff1 | 2016-08-03 13:38:56 -0700 | [diff] [blame] | 345 | String oldKey = oldNotification.getGroupKey(); |
| 346 | String newKey = entry.notification.getGroupKey(); |
| 347 | boolean groupKeysChanged = !oldKey.equals(newKey); |
| 348 | boolean wasGroupChild = isGroupChild(oldNotification); |
| 349 | boolean isGroupChild = isGroupChild(entry.notification); |
Selim Cinek | 80c44dd | 2016-08-15 19:39:55 -0700 | [diff] [blame] | 350 | mIsUpdatingUnchangedGroup = !groupKeysChanged && wasGroupChild == isGroupChild; |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 351 | if (mGroupMap.get(getGroupKey(oldNotification)) != null) { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 352 | onEntryRemovedInternal(entry, oldNotification); |
| 353 | } |
| 354 | onEntryAdded(entry); |
Selim Cinek | 80c44dd | 2016-08-15 19:39:55 -0700 | [diff] [blame] | 355 | mIsUpdatingUnchangedGroup = false; |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 356 | if (isIsolated(entry.notification)) { |
| 357 | mIsolatedEntries.put(entry.key, entry.notification); |
Selim Cinek | 68bdff1 | 2016-08-03 13:38:56 -0700 | [diff] [blame] | 358 | if (groupKeysChanged) { |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 359 | updateSuppression(mGroupMap.get(oldKey)); |
| 360 | updateSuppression(mGroupMap.get(newKey)); |
| 361 | } |
Selim Cinek | 68bdff1 | 2016-08-03 13:38:56 -0700 | [diff] [blame] | 362 | } else if (!wasGroupChild && isGroupChild) { |
Selim Cinek | 50e7467 | 2016-05-05 15:58:10 -0400 | [diff] [blame] | 363 | onEntryBecomingChild(entry); |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 364 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 365 | } |
| 366 | |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 367 | public boolean isSummaryOfSuppressedGroup(StatusBarNotification sbn) { |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 368 | return isGroupSuppressed(getGroupKey(sbn)) && sbn.getNotification().isGroupSummary(); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 369 | } |
| 370 | |
Selim Cinek | 53c4d3d | 2016-05-23 10:56:34 -0700 | [diff] [blame] | 371 | private boolean isOnlyChild(StatusBarNotification sbn) { |
Selim Cinek | 36b0223 | 2016-05-11 23:07:05 -0400 | [diff] [blame] | 372 | return !sbn.getNotification().isGroupSummary() |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 373 | && getTotalNumberOfChildren(sbn) == 1; |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Selim Cinek | 53c4d3d | 2016-05-23 10:56:34 -0700 | [diff] [blame] | 376 | public boolean isOnlyChildInGroup(StatusBarNotification sbn) { |
Selim Cinek | 88086e7 | 2016-06-17 21:01:32 -0700 | [diff] [blame] | 377 | if (!isOnlyChild(sbn)) { |
| 378 | return false; |
| 379 | } |
| 380 | ExpandableNotificationRow logicalGroupSummary = getLogicalGroupSummary(sbn); |
| 381 | return logicalGroupSummary != null |
| 382 | && !logicalGroupSummary.getStatusBarNotification().equals(sbn); |
Selim Cinek | 53c4d3d | 2016-05-23 10:56:34 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 385 | private int getTotalNumberOfChildren(StatusBarNotification sbn) { |
Selim Cinek | 53c4d3d | 2016-05-23 10:56:34 -0700 | [diff] [blame] | 386 | int isolatedChildren = getNumberOfIsolatedChildren(sbn.getGroupKey()); |
| 387 | NotificationGroup group = mGroupMap.get(sbn.getGroupKey()); |
| 388 | int realChildren = group != null ? group.children.size() : 0; |
| 389 | return isolatedChildren + realChildren; |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | private boolean isGroupSuppressed(String groupKey) { |
| 393 | NotificationGroup group = mGroupMap.get(groupKey); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 394 | return group != null && group.suppressed; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 395 | } |
| 396 | |
Jason Monk | 1fd3fc3 | 2018-08-14 17:20:09 -0400 | [diff] [blame] | 397 | private void setStatusBarState(int newState) { |
Selim Cinek | 9c4c414 | 2015-12-04 16:44:56 -0800 | [diff] [blame] | 398 | mBarState = newState; |
| 399 | if (mBarState == StatusBarState.KEYGUARD) { |
Selim Cinek | 9184f9c | 2016-02-02 17:36:53 -0800 | [diff] [blame] | 400 | collapseAllGroups(); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | public void collapseAllGroups() { |
Selim Cinek | c0b14b0 | 2016-05-09 13:12:40 -0400 | [diff] [blame] | 405 | // Because notifications can become isolated when the group becomes suppressed it can |
| 406 | // lead to concurrent modifications while looping. We need to make a copy. |
| 407 | ArrayList<NotificationGroup> groupCopy = new ArrayList<>(mGroupMap.values()); |
| 408 | int size = groupCopy.size(); |
| 409 | for (int i = 0; i < size; i++) { |
| 410 | NotificationGroup group = groupCopy.get(i); |
Selim Cinek | 9184f9c | 2016-02-02 17:36:53 -0800 | [diff] [blame] | 411 | if (group.expanded) { |
| 412 | setGroupExpanded(group, false); |
Selim Cinek | 9c4c414 | 2015-12-04 16:44:56 -0800 | [diff] [blame] | 413 | } |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 414 | updateSuppression(group); |
Selim Cinek | 9c4c414 | 2015-12-04 16:44:56 -0800 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 418 | /** |
| 419 | * @return whether a given notification is a child in a group which has a summary |
| 420 | */ |
| 421 | public boolean isChildInGroupWithSummary(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 422 | if (!isGroupChild(sbn)) { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 423 | return false; |
| 424 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 425 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 426 | if (group == null || group.summary == null || group.suppressed) { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 427 | return false; |
| 428 | } |
Selim Cinek | 3f19f60 | 2016-05-02 18:01:56 -0700 | [diff] [blame] | 429 | if (group.children.isEmpty()) { |
| 430 | // If the suppression of a group changes because the last child was removed, this can |
| 431 | // still be called temporarily because the child hasn't been fully removed yet. Let's |
| 432 | // make sure we still return false in that case. |
| 433 | return false; |
| 434 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 435 | return true; |
| 436 | } |
| 437 | |
Selim Cinek | 263398f | 2015-10-21 17:40:23 -0700 | [diff] [blame] | 438 | /** |
| 439 | * @return whether a given notification is a summary in a group which has children |
| 440 | */ |
| 441 | public boolean isSummaryOfGroup(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 442 | if (!isGroupSummary(sbn)) { |
Selim Cinek | 263398f | 2015-10-21 17:40:23 -0700 | [diff] [blame] | 443 | return false; |
| 444 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 445 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Kevin | 96eed64 | 2018-09-05 18:53:33 -0700 | [diff] [blame] | 446 | if (group == null || group.summary == null) { |
Selim Cinek | 263398f | 2015-10-21 17:40:23 -0700 | [diff] [blame] | 447 | return false; |
| 448 | } |
Kevin | 96eed64 | 2018-09-05 18:53:33 -0700 | [diff] [blame] | 449 | return !group.children.isEmpty() && Objects.equals(group.summary.notification, sbn); |
Selim Cinek | 263398f | 2015-10-21 17:40:23 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 452 | /** |
| 453 | * Get the summary of a specified status bar notification. For isolated notification this return |
| 454 | * itself. |
| 455 | */ |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 456 | public ExpandableNotificationRow getGroupSummary(StatusBarNotification sbn) { |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 457 | return getGroupSummary(getGroupKey(sbn)); |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Similar to {@link #getGroupSummary(StatusBarNotification)} but doesn't get the visual summary |
| 462 | * but the logical summary, i.e when a child is isolated, it still returns the summary as if |
| 463 | * it wasn't isolated. |
| 464 | */ |
| 465 | public ExpandableNotificationRow getLogicalGroupSummary( |
| 466 | StatusBarNotification sbn) { |
| 467 | return getGroupSummary(sbn.getGroupKey()); |
| 468 | } |
| 469 | |
| 470 | @Nullable |
| 471 | private ExpandableNotificationRow getGroupSummary(String groupKey) { |
| 472 | NotificationGroup group = mGroupMap.get(groupKey); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 473 | return group == null ? null |
| 474 | : group.summary == null ? null |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 475 | : group.summary.row; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 476 | } |
| 477 | |
Chris Wren | 698b170 | 2016-05-23 11:16:32 -0400 | [diff] [blame] | 478 | /** @return group expansion state after toggling. */ |
| 479 | public boolean toggleGroupExpansion(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 480 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 83bc783 | 2015-10-22 13:26:54 -0700 | [diff] [blame] | 481 | if (group == null) { |
Chris Wren | 698b170 | 2016-05-23 11:16:32 -0400 | [diff] [blame] | 482 | return false; |
Selim Cinek | 83bc783 | 2015-10-22 13:26:54 -0700 | [diff] [blame] | 483 | } |
| 484 | setGroupExpanded(group, !group.expanded); |
Chris Wren | 698b170 | 2016-05-23 11:16:32 -0400 | [diff] [blame] | 485 | return group.expanded; |
Selim Cinek | 83bc783 | 2015-10-22 13:26:54 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 488 | private boolean isIsolated(StatusBarNotification sbn) { |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 489 | return mIsolatedEntries.containsKey(sbn.getKey()); |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | private boolean isGroupSummary(StatusBarNotification sbn) { |
| 493 | if (isIsolated(sbn)) { |
| 494 | return true; |
| 495 | } |
| 496 | return sbn.getNotification().isGroupSummary(); |
| 497 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 498 | |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 499 | private boolean isGroupChild(StatusBarNotification sbn) { |
| 500 | if (isIsolated(sbn)) { |
| 501 | return false; |
| 502 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 503 | return sbn.isGroup() && !sbn.getNotification().isGroupSummary(); |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | private String getGroupKey(StatusBarNotification sbn) { |
| 507 | if (isIsolated(sbn)) { |
| 508 | return sbn.getKey(); |
| 509 | } |
| 510 | return sbn.getGroupKey(); |
| 511 | } |
| 512 | |
| 513 | @Override |
| 514 | public void onHeadsUpPinnedModeChanged(boolean inPinnedMode) { |
| 515 | } |
| 516 | |
| 517 | @Override |
| 518 | public void onHeadsUpPinned(ExpandableNotificationRow headsUp) { |
| 519 | } |
| 520 | |
| 521 | @Override |
| 522 | public void onHeadsUpUnPinned(ExpandableNotificationRow headsUp) { |
| 523 | } |
| 524 | |
| 525 | @Override |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 526 | public void onAmbientStateChanged(NotificationData.Entry entry, boolean isAmbient) { |
| 527 | onAlertStateChanged(entry, isAmbient, mAmbientPulseManager); |
| 528 | } |
| 529 | |
| 530 | @Override |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 531 | public void onHeadsUpStateChanged(NotificationData.Entry entry, boolean isHeadsUp) { |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 532 | onAlertStateChanged(entry, isHeadsUp, mHeadsUpManager); |
| 533 | } |
| 534 | |
| 535 | private void onAlertStateChanged(NotificationData.Entry entry, boolean isAlerting, |
| 536 | AlertingNotificationManager alertManager) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 537 | final StatusBarNotification sbn = entry.notification; |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 538 | if (isAlerting) { |
| 539 | if (shouldIsolate(entry)) { |
| 540 | isolateNotification(entry); |
| 541 | } else if (sbn.getNotification().isGroupSummary() |
| 542 | && isGroupSuppressed(sbn.getGroupKey())){ |
| 543 | handleSuppressedSummaryAlerted(entry, alertManager); |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 544 | } |
| 545 | } else { |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 546 | stopIsolatingNotification(entry); |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 550 | /** |
| 551 | * Handles the scenario where a summary that has been suppressed is alerted. A suppressed |
| 552 | * summary should for all intents and purposes be invisible to the user and as a result should |
| 553 | * not alert. When this is the case, it is our responsibility to pass the alert to the |
| 554 | * appropriate child which will be the representative notification alerting for the group. |
| 555 | * @param summary the summary that is suppressed and alerting |
| 556 | * @param alertManager the alert manager that manages the alerting summary |
| 557 | */ |
| 558 | private void handleSuppressedSummaryAlerted(@NonNull NotificationData.Entry summary, |
| 559 | @NonNull AlertingNotificationManager alertManager) { |
| 560 | StatusBarNotification sbn = summary.notification; |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 561 | if (!isGroupSuppressed(sbn.getGroupKey()) |
| 562 | || !sbn.getNotification().isGroupSummary() |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 563 | || !alertManager.isAlerting(sbn.getKey())) { |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 564 | return; |
| 565 | } |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 566 | |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 567 | // The parent of a suppressed group got alerted, lets alert the child! |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 568 | NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey()); |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 569 | |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 570 | if (notificationGroup != null) { |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 571 | if (pendingInflationsWillAddChildren(notificationGroup)) { |
| 572 | // New children will actually be added to this group, let's not transfer the alert. |
| 573 | return; |
| 574 | } |
| 575 | |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 576 | Iterator<NotificationData.Entry> iterator |
| 577 | = notificationGroup.children.values().iterator(); |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 578 | NotificationData.Entry child = iterator.hasNext() ? iterator.next() : null; |
| 579 | if (child == null) { |
| 580 | child = getIsolatedChild(sbn.getGroupKey()); |
| 581 | } |
| 582 | if (child != null) { |
Selim Cinek | 40f8876 | 2016-12-16 16:56:41 -0800 | [diff] [blame] | 583 | if (child.row.keepInParent() || child.row.isRemoved() || child.row.isDismissed()) { |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 584 | // the notification is actually already removed, no need to do alert on it. |
Selim Cinek | 40f8876 | 2016-12-16 16:56:41 -0800 | [diff] [blame] | 585 | return; |
| 586 | } |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 587 | transferAlertStateToChild(summary, child, alertManager); |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 588 | } |
| 589 | } |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Transfers the alert state from a given summary notification to the specified child. The |
| 594 | * result is the child will now alert while the summary does not. |
| 595 | * |
| 596 | * @param summary the currently alerting summary notification |
| 597 | * @param child the child that should receive the alert |
| 598 | * @param alertManager the manager for the alert |
| 599 | */ |
| 600 | private void transferAlertStateToChild(@NonNull NotificationData.Entry summary, |
| 601 | @NonNull NotificationData.Entry child, |
| 602 | @NonNull AlertingNotificationManager alertManager) { |
| 603 | NotificationGroup notificationGroup = mGroupMap.get(summary.notification.getGroupKey()); |
| 604 | if (alertManager.isAlerting(child.key)) { |
| 605 | alertManager.updateNotification(child.key, true /* alert */); |
| 606 | } else { |
| 607 | if (onlySummaryAlerts(summary)) { |
| 608 | notificationGroup.lastAlertTransfer = SystemClock.elapsedRealtime(); |
| 609 | } |
| 610 | alertManager.showNotification(child); |
| 611 | } |
| 612 | alertManager.removeNotification(summary.key, true /* releaseImmediately */); |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 615 | private boolean onlySummaryAlerts(NotificationData.Entry entry) { |
| 616 | return entry.notification.getNotification().getGroupAlertBehavior() |
| 617 | == Notification.GROUP_ALERT_SUMMARY; |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * Check if the pending inflations will add children to this group. |
| 622 | * @param group The group to check. |
| 623 | */ |
| 624 | private boolean pendingInflationsWillAddChildren(NotificationGroup group) { |
| 625 | if (mPendingNotifications == null) { |
| 626 | return false; |
| 627 | } |
| 628 | Collection<NotificationData.Entry> values = mPendingNotifications.values(); |
| 629 | String groupKey = getGroupKey(group.summary.notification); |
| 630 | for (NotificationData.Entry entry : values) { |
| 631 | if (!isGroupChild(entry.notification)) { |
| 632 | continue; |
| 633 | } |
| 634 | if (!Objects.equals(getGroupKey(entry.notification), groupKey)) { |
| 635 | continue; |
| 636 | } |
| 637 | if (!group.children.containsKey(entry.key)) { |
| 638 | return true; |
| 639 | } |
| 640 | } |
| 641 | return false; |
| 642 | } |
| 643 | |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 644 | /** |
| 645 | * Whether a notification that is normally part of a group should be temporarily isolated from |
| 646 | * the group and put in their own group visually. This generally happens when the notification |
| 647 | * is alerting. |
| 648 | * |
| 649 | * @param entry the notification to check |
| 650 | * @return true if the entry should be isolated |
| 651 | */ |
| 652 | |
| 653 | private boolean shouldIsolate(NotificationData.Entry entry) { |
| 654 | StatusBarNotification sbn = entry.notification; |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 655 | NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey()); |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 656 | if (!sbn.isGroup() || sbn.getNotification().isGroupSummary()) { |
| 657 | return false; |
| 658 | } |
| 659 | if (!mIsDozing && !mHeadsUpManager.isAlerting(entry.key)) { |
| 660 | return false; |
| 661 | } |
| 662 | if (mIsDozing && !mAmbientPulseManager.isAlerting(entry.key)) { |
| 663 | return false; |
| 664 | } |
| 665 | return (sbn.getNotification().fullScreenIntent != null |
| 666 | || notificationGroup == null |
| 667 | || !notificationGroup.expanded |
| 668 | || isGroupNotFullyVisible(notificationGroup)); |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * Isolate a notification from its group so that it visually shows as its own group. |
| 673 | * |
| 674 | * @param entry the notification to isolate |
| 675 | */ |
| 676 | private void isolateNotification(NotificationData.Entry entry) { |
| 677 | StatusBarNotification sbn = entry.notification; |
| 678 | |
| 679 | // We will be isolated now, so lets update the groups |
| 680 | onEntryRemovedInternal(entry, entry.notification); |
| 681 | |
| 682 | mIsolatedEntries.put(sbn.getKey(), sbn); |
| 683 | |
| 684 | onEntryAdded(entry); |
| 685 | // We also need to update the suppression of the old group, because this call comes |
| 686 | // even before the groupManager knows about the notification at all. |
| 687 | // When the notification gets added afterwards it is already isolated and therefore |
| 688 | // it doesn't lead to an update. |
| 689 | updateSuppression(mGroupMap.get(entry.notification.getGroupKey())); |
| 690 | mListener.onGroupsChanged(); |
| 691 | } |
| 692 | |
| 693 | /** |
| 694 | * Stop isolating a notification and re-group it with its original logical group. |
| 695 | * |
| 696 | * @param entry the notification to un-isolate |
| 697 | */ |
| 698 | private void stopIsolatingNotification(NotificationData.Entry entry) { |
| 699 | StatusBarNotification sbn = entry.notification; |
| 700 | if (mIsolatedEntries.containsKey(sbn.getKey())) { |
| 701 | // not isolated anymore, we need to update the groups |
| 702 | onEntryRemovedInternal(entry, entry.notification); |
| 703 | mIsolatedEntries.remove(sbn.getKey()); |
| 704 | onEntryAdded(entry); |
| 705 | mListener.onGroupsChanged(); |
| 706 | } |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | private boolean isGroupNotFullyVisible(NotificationGroup notificationGroup) { |
| 710 | return notificationGroup.summary == null |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 711 | || notificationGroup.summary.row.getClipTopAmount() > 0 |
| 712 | || notificationGroup.summary.row.getTranslationY() < 0; |
| 713 | } |
| 714 | |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 715 | public void setHeadsUpManager(HeadsUpManager headsUpManager) { |
| 716 | mHeadsUpManager = headsUpManager; |
| 717 | } |
| 718 | |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame] | 719 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 720 | pw.println("GroupManager state:"); |
| 721 | pw.println(" number of groups: " + mGroupMap.size()); |
| 722 | for (Map.Entry<String, NotificationGroup> entry : mGroupMap.entrySet()) { |
| 723 | pw.println("\n key: " + entry.getKey()); pw.println(entry.getValue()); |
| 724 | } |
| 725 | pw.println("\n isolated entries: " + mIsolatedEntries.size()); |
| 726 | for (Map.Entry<String, StatusBarNotification> entry : mIsolatedEntries.entrySet()) { |
| 727 | pw.print(" "); pw.print(entry.getKey()); |
| 728 | pw.print(", "); pw.println(entry.getValue()); |
| 729 | } |
| 730 | } |
| 731 | |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 732 | public void setPendingEntries(HashMap<String, NotificationData.Entry> pendingNotifications) { |
| 733 | mPendingNotifications = pendingNotifications; |
| 734 | } |
| 735 | |
Evan Laird | 878c853 | 2018-10-15 15:54:29 -0400 | [diff] [blame^] | 736 | @Override |
| 737 | public void onStateChanged(int newState) { |
| 738 | setStatusBarState(newState); |
| 739 | } |
| 740 | |
| 741 | @Override |
| 742 | public void onDozingChanged(boolean isDozing) { |
| 743 | setDozing(isDozing); |
| 744 | } |
| 745 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 746 | public static class NotificationGroup { |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 747 | public final HashMap<String, NotificationData.Entry> children = new HashMap<>(); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 748 | public NotificationData.Entry summary; |
| 749 | public boolean expanded; |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 750 | /** |
| 751 | * Is this notification group suppressed, i.e its summary is hidden |
| 752 | */ |
| 753 | public boolean suppressed; |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 754 | /** |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 755 | * The time when the last alert transfer from group to child happened, while the summary |
| 756 | * has the flags to alert up on its own. |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 757 | */ |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 758 | public long lastAlertTransfer; |
| 759 | public boolean alertSummaryOnNextAddition; |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame] | 760 | |
| 761 | @Override |
| 762 | public String toString() { |
Selim Cinek | 60ca787 | 2016-05-12 11:24:07 -0700 | [diff] [blame] | 763 | String result = " summary:\n " |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 764 | + (summary != null ? summary.notification : "null") |
| 765 | + (summary != null && summary.getDebugThrowable() != null |
| 766 | ? Log.getStackTraceString(summary.getDebugThrowable()) |
| 767 | : ""); |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame] | 768 | result += "\n children size: " + children.size(); |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 769 | for (NotificationData.Entry child : children.values()) { |
| 770 | result += "\n " + child.notification |
| 771 | + (child.getDebugThrowable() != null |
| 772 | ? Log.getStackTraceString(child.getDebugThrowable()) |
| 773 | : ""); |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame] | 774 | } |
| 775 | return result; |
| 776 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | public interface OnGroupChangeListener { |
| 780 | /** |
| 781 | * The expansion of a group has changed. |
| 782 | * |
| 783 | * @param changedRow the row for which the expansion has changed, which is also the summary |
| 784 | * @param expanded a boolean indicating the new expanded state |
| 785 | */ |
| 786 | void onGroupExpansionChanged(ExpandableNotificationRow changedRow, boolean expanded); |
| 787 | |
| 788 | /** |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 789 | * A group of children just received a summary notification and should therefore become |
| 790 | * children of it. |
| 791 | * |
| 792 | * @param group the group created |
| 793 | */ |
| 794 | void onGroupCreatedFromChildren(NotificationGroup group); |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 795 | |
| 796 | /** |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 797 | * The groups have changed. This can happen if the isolation of a child has changes or if a |
| 798 | * group became suppressed / unsuppressed |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 799 | */ |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 800 | void onGroupsChanged(); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 801 | } |
| 802 | } |