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 | |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 20 | import android.app.NotificationChannel; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 21 | import android.service.notification.StatusBarNotification; |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 22 | import android.util.ArraySet; |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 23 | import android.util.Log; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 24 | |
Jason Monk | 1fd3fc3 | 2018-08-14 17:20:09 -0400 | [diff] [blame] | 25 | import com.android.systemui.Dependency; |
Mady Mellor | 740d85d | 2019-07-09 15:26:47 -0700 | [diff] [blame] | 26 | import com.android.systemui.bubbles.BubbleController; |
Beverly | 8fdb533 | 2019-02-04 14:29:49 -0500 | [diff] [blame] | 27 | import com.android.systemui.plugins.statusbar.StatusBarStateController; |
| 28 | import com.android.systemui.plugins.statusbar.StatusBarStateController.StateListener; |
Jason Monk | 1fd3fc3 | 2018-08-14 17:20:09 -0400 | [diff] [blame] | 29 | import com.android.systemui.statusbar.StatusBarState; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 30 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Evan Laird | 878c853 | 2018-10-15 15:54:29 -0400 | [diff] [blame] | 31 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 32 | import com.android.systemui.statusbar.policy.HeadsUpManager; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 33 | import com.android.systemui.statusbar.policy.OnHeadsUpChangedListener; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 34 | |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame] | 35 | import java.io.FileDescriptor; |
| 36 | import java.io.PrintWriter; |
Selim Cinek | c0b14b0 | 2016-05-09 13:12:40 -0400 | [diff] [blame] | 37 | import java.util.ArrayList; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 38 | import java.util.HashMap; |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame] | 39 | import java.util.Map; |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 40 | import java.util.Objects; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 41 | |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 42 | import javax.inject.Inject; |
| 43 | import javax.inject.Singleton; |
| 44 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 45 | /** |
| 46 | * A class to handle notifications and their corresponding groups. |
| 47 | */ |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 48 | @Singleton |
Selim Cinek | c3fec68 | 2019-06-06 18:11:07 -0700 | [diff] [blame] | 49 | public class NotificationGroupManager implements OnHeadsUpChangedListener, StateListener { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 50 | |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 51 | private static final String TAG = "NotificationGroupManager"; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 52 | private final HashMap<String, NotificationGroup> mGroupMap = new HashMap<>(); |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 53 | private final ArraySet<OnGroupChangeListener> mListeners = new ArraySet<>(); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 54 | private int mBarState = -1; |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 55 | private HashMap<String, StatusBarNotification> mIsolatedEntries = new HashMap<>(); |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 56 | private HeadsUpManager mHeadsUpManager; |
Selim Cinek | 80c44dd | 2016-08-15 19:39:55 -0700 | [diff] [blame] | 57 | private boolean mIsUpdatingUnchangedGroup; |
Mady Mellor | 740d85d | 2019-07-09 15:26:47 -0700 | [diff] [blame] | 58 | @Nullable private BubbleController mBubbleController = null; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 59 | |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 60 | @Inject |
Selim Cinek | c3fec68 | 2019-06-06 18:11:07 -0700 | [diff] [blame] | 61 | public NotificationGroupManager(StatusBarStateController statusBarStateController) { |
| 62 | statusBarStateController.addCallback(this); |
Jason Monk | 1fd3fc3 | 2018-08-14 17:20:09 -0400 | [diff] [blame] | 63 | } |
| 64 | |
Mady Mellor | 740d85d | 2019-07-09 15:26:47 -0700 | [diff] [blame] | 65 | private BubbleController getBubbleController() { |
| 66 | if (mBubbleController == null) { |
| 67 | mBubbleController = Dependency.get(BubbleController.class); |
| 68 | } |
| 69 | return mBubbleController; |
| 70 | } |
| 71 | |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 72 | /** |
| 73 | * Add a listener for changes to groups. |
| 74 | * |
| 75 | * @param listener listener to add |
| 76 | */ |
| 77 | public void addOnGroupChangeListener(OnGroupChangeListener listener) { |
| 78 | mListeners.add(listener); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | public boolean isGroupExpanded(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 82 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 83 | if (group == null) { |
| 84 | return false; |
| 85 | } |
| 86 | return group.expanded; |
| 87 | } |
| 88 | |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 89 | /** |
| 90 | * @return if the group that this notification is associated with logically is expanded |
| 91 | */ |
| 92 | public boolean isLogicalGroupExpanded(StatusBarNotification sbn) { |
| 93 | NotificationGroup group = mGroupMap.get(sbn.getGroupKey()); |
| 94 | if (group == null) { |
| 95 | return false; |
| 96 | } |
| 97 | return group.expanded; |
| 98 | } |
| 99 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 100 | public void setGroupExpanded(StatusBarNotification sbn, boolean expanded) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 101 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 102 | if (group == null) { |
| 103 | return; |
| 104 | } |
| 105 | setGroupExpanded(group, expanded); |
| 106 | } |
| 107 | |
| 108 | private void setGroupExpanded(NotificationGroup group, boolean expanded) { |
| 109 | group.expanded = expanded; |
| 110 | if (group.summary != null) { |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 111 | for (OnGroupChangeListener listener : mListeners) { |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 112 | listener.onGroupExpansionChanged(group.summary.getRow(), expanded); |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 113 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 117 | public void onEntryRemoved(NotificationEntry removed) { |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 118 | onEntryRemovedInternal(removed, removed.getSbn()); |
| 119 | mIsolatedEntries.remove(removed.getKey()); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
| 123 | * An entry was removed. |
| 124 | * |
| 125 | * @param removed the removed entry |
| 126 | * @param sbn the notification the entry has, which doesn't need to be the same as it's internal |
| 127 | * notification |
| 128 | */ |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 129 | private void onEntryRemovedInternal(NotificationEntry removed, |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 130 | final StatusBarNotification sbn) { |
Beverly | c509412 | 2020-01-09 17:05:15 -0500 | [diff] [blame] | 131 | onEntryRemovedInternal(removed, sbn.getGroupKey(), sbn.isGroup(), |
| 132 | sbn.getNotification().isGroupSummary()); |
| 133 | } |
| 134 | |
| 135 | private void onEntryRemovedInternal(NotificationEntry removed, String notifGroupKey, boolean |
| 136 | isGroup, boolean isGroupSummary) { |
| 137 | String groupKey = getGroupKey(removed.getKey(), notifGroupKey); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 138 | final NotificationGroup group = mGroupMap.get(groupKey); |
Selim Cinek | 0b4aeab | 2015-09-01 17:07:38 -0700 | [diff] [blame] | 139 | if (group == null) { |
| 140 | // When an app posts 2 different notifications as summary of the same group, then a |
| 141 | // cancellation of the first notification removes this group. |
| 142 | // This situation is not supported and we will not allow such notifications anymore in |
| 143 | // the close future. See b/23676310 for reference. |
| 144 | return; |
| 145 | } |
Beverly | c509412 | 2020-01-09 17:05:15 -0500 | [diff] [blame] | 146 | if (isGroupChild(removed.getKey(), isGroup, isGroupSummary)) { |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 147 | group.children.remove(removed.getKey()); |
Selim Cinek | e73ad21 | 2015-11-03 19:11:08 -0800 | [diff] [blame] | 148 | } else { |
| 149 | group.summary = null; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 150 | } |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 151 | updateSuppression(group); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 152 | if (group.children.isEmpty()) { |
| 153 | if (group.summary == null) { |
| 154 | mGroupMap.remove(groupKey); |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 155 | for (OnGroupChangeListener listener : mListeners) { |
| 156 | listener.onGroupRemoved(group, groupKey); |
| 157 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 162 | /** |
| 163 | * Notify the group manager that a new entry was added |
| 164 | */ |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 165 | public void onEntryAdded(final NotificationEntry added) { |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 166 | updateIsolation(added); |
| 167 | onEntryAddedInternal(added); |
| 168 | } |
| 169 | |
| 170 | private void onEntryAddedInternal(final NotificationEntry added) { |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 171 | if (added.isRowRemoved()) { |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 172 | added.setDebugThrowable(new Throwable()); |
| 173 | } |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 174 | final StatusBarNotification sbn = added.getSbn(); |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 175 | boolean isGroupChild = isGroupChild(sbn); |
| 176 | String groupKey = getGroupKey(sbn); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 177 | NotificationGroup group = mGroupMap.get(groupKey); |
| 178 | if (group == null) { |
| 179 | group = new NotificationGroup(); |
| 180 | mGroupMap.put(groupKey, group); |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 181 | for (OnGroupChangeListener listener : mListeners) { |
| 182 | listener.onGroupCreated(group, groupKey); |
| 183 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 184 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 185 | if (isGroupChild) { |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 186 | NotificationEntry existing = group.children.get(added.getKey()); |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 187 | if (existing != null && existing != added) { |
| 188 | Throwable existingThrowable = existing.getDebugThrowable(); |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 189 | Log.wtf(TAG, "Inconsistent entries found with the same key " + added.getKey() |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 190 | + "existing removed: " + existing.isRowRemoved() |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 191 | + (existingThrowable != null |
| 192 | ? Log.getStackTraceString(existingThrowable) + "\n": "") |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 193 | + " added removed" + added.isRowRemoved() |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 194 | , new Throwable()); |
| 195 | } |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 196 | group.children.put(added.getKey(), added); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 197 | updateSuppression(group); |
Selim Cinek | e73ad21 | 2015-11-03 19:11:08 -0800 | [diff] [blame] | 198 | } else { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 199 | group.summary = added; |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 200 | group.expanded = added.areChildrenExpanded(); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 201 | updateSuppression(group); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 202 | if (!group.children.isEmpty()) { |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 203 | ArrayList<NotificationEntry> childrenCopy |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 204 | = new ArrayList<>(group.children.values()); |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 205 | for (NotificationEntry child : childrenCopy) { |
Selim Cinek | 50e7467 | 2016-05-05 15:58:10 -0400 | [diff] [blame] | 206 | onEntryBecomingChild(child); |
| 207 | } |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 208 | for (OnGroupChangeListener listener : mListeners) { |
| 209 | listener.onGroupCreatedFromChildren(group); |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
Selim Cinek | f93bf3e | 2018-05-08 14:43:21 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 215 | private void onEntryBecomingChild(NotificationEntry entry) { |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 216 | updateIsolation(entry); |
Selim Cinek | 50e7467 | 2016-05-05 15:58:10 -0400 | [diff] [blame] | 217 | } |
| 218 | |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 219 | private void updateSuppression(NotificationGroup group) { |
Selim Cinek | 80c44dd | 2016-08-15 19:39:55 -0700 | [diff] [blame] | 220 | if (group == null) { |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 221 | return; |
| 222 | } |
Mady Mellor | 740d85d | 2019-07-09 15:26:47 -0700 | [diff] [blame] | 223 | int childCount = 0; |
| 224 | boolean hasBubbles = false; |
Beverly | ed8aea2 | 2020-01-22 16:52:47 -0500 | [diff] [blame] | 225 | for (NotificationEntry entry : group.children.values()) { |
| 226 | if (!getBubbleController().isBubbleNotificationSuppressedFromShade(entry)) { |
Mady Mellor | 740d85d | 2019-07-09 15:26:47 -0700 | [diff] [blame] | 227 | childCount++; |
| 228 | } else { |
| 229 | hasBubbles = true; |
| 230 | } |
| 231 | } |
| 232 | |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 233 | boolean prevSuppressed = group.suppressed; |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 234 | group.suppressed = group.summary != null && !group.expanded |
Mady Mellor | 740d85d | 2019-07-09 15:26:47 -0700 | [diff] [blame] | 235 | && (childCount == 1 |
| 236 | || (childCount == 0 |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 237 | && group.summary.getSbn().getNotification().isGroupSummary() |
Mady Mellor | 740d85d | 2019-07-09 15:26:47 -0700 | [diff] [blame] | 238 | && (hasIsolatedChildren(group) || hasBubbles))); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 239 | if (prevSuppressed != group.suppressed) { |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 240 | for (OnGroupChangeListener listener : mListeners) { |
| 241 | if (!mIsUpdatingUnchangedGroup) { |
| 242 | listener.onGroupSuppressionChanged(group, group.suppressed); |
| 243 | listener.onGroupsChanged(); |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 244 | } |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 245 | } |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 249 | private boolean hasIsolatedChildren(NotificationGroup group) { |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 250 | return getNumberOfIsolatedChildren(group.summary.getSbn().getGroupKey()) != 0; |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | private int getNumberOfIsolatedChildren(String groupKey) { |
| 254 | int count = 0; |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 255 | for (StatusBarNotification sbn : mIsolatedEntries.values()) { |
Beverly | c509412 | 2020-01-09 17:05:15 -0500 | [diff] [blame] | 256 | if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn.getKey())) { |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 257 | count++; |
| 258 | } |
| 259 | } |
| 260 | return count; |
| 261 | } |
| 262 | |
Beverly | c509412 | 2020-01-09 17:05:15 -0500 | [diff] [blame] | 263 | /** |
| 264 | * Update an entry's group information |
| 265 | * @param entry notification entry to update |
| 266 | * @param oldNotification previous notification info before this update |
| 267 | */ |
| 268 | public void onEntryUpdated(NotificationEntry entry, StatusBarNotification oldNotification) { |
| 269 | onEntryUpdated(entry, oldNotification.getGroupKey(), oldNotification.isGroup(), |
| 270 | oldNotification.getNotification().isGroupSummary()); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Updates an entry's group information |
| 275 | * @param entry notification entry to update |
| 276 | * @param oldGroupKey the notification's previous group key before this update |
| 277 | * @param oldIsGroup whether this notification was a group before this update |
| 278 | * @param oldIsGroupSummary whether this notification was a group summary before this update |
| 279 | */ |
| 280 | public void onEntryUpdated(NotificationEntry entry, String oldGroupKey, boolean oldIsGroup, |
| 281 | boolean oldIsGroupSummary) { |
| 282 | String newGroupKey = entry.getSbn().getGroupKey(); |
| 283 | boolean groupKeysChanged = !oldGroupKey.equals(newGroupKey); |
| 284 | boolean wasGroupChild = isGroupChild(entry.getKey(), oldIsGroup, oldIsGroupSummary); |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 285 | boolean isGroupChild = isGroupChild(entry.getSbn()); |
Selim Cinek | 80c44dd | 2016-08-15 19:39:55 -0700 | [diff] [blame] | 286 | mIsUpdatingUnchangedGroup = !groupKeysChanged && wasGroupChild == isGroupChild; |
Beverly | c509412 | 2020-01-09 17:05:15 -0500 | [diff] [blame] | 287 | if (mGroupMap.get(getGroupKey(entry.getKey(), oldGroupKey)) != null) { |
| 288 | onEntryRemovedInternal(entry, oldGroupKey, oldIsGroup, oldIsGroupSummary); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 289 | } |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 290 | onEntryAddedInternal(entry); |
Selim Cinek | 80c44dd | 2016-08-15 19:39:55 -0700 | [diff] [blame] | 291 | mIsUpdatingUnchangedGroup = false; |
Beverly | c509412 | 2020-01-09 17:05:15 -0500 | [diff] [blame] | 292 | if (isIsolated(entry.getSbn().getKey())) { |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 293 | mIsolatedEntries.put(entry.getKey(), entry.getSbn()); |
Selim Cinek | 68bdff1 | 2016-08-03 13:38:56 -0700 | [diff] [blame] | 294 | if (groupKeysChanged) { |
Beverly | c509412 | 2020-01-09 17:05:15 -0500 | [diff] [blame] | 295 | updateSuppression(mGroupMap.get(oldGroupKey)); |
| 296 | updateSuppression(mGroupMap.get(newGroupKey)); |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 297 | } |
Selim Cinek | 68bdff1 | 2016-08-03 13:38:56 -0700 | [diff] [blame] | 298 | } else if (!wasGroupChild && isGroupChild) { |
Selim Cinek | 50e7467 | 2016-05-05 15:58:10 -0400 | [diff] [blame] | 299 | onEntryBecomingChild(entry); |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 300 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 301 | } |
| 302 | |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 303 | public boolean isSummaryOfSuppressedGroup(StatusBarNotification sbn) { |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 304 | return isGroupSuppressed(getGroupKey(sbn)) && sbn.getNotification().isGroupSummary(); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 305 | } |
| 306 | |
Selim Cinek | 53c4d3d | 2016-05-23 10:56:34 -0700 | [diff] [blame] | 307 | private boolean isOnlyChild(StatusBarNotification sbn) { |
Selim Cinek | 36b0223 | 2016-05-11 23:07:05 -0400 | [diff] [blame] | 308 | return !sbn.getNotification().isGroupSummary() |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 309 | && getTotalNumberOfChildren(sbn) == 1; |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Selim Cinek | 53c4d3d | 2016-05-23 10:56:34 -0700 | [diff] [blame] | 312 | public boolean isOnlyChildInGroup(StatusBarNotification sbn) { |
Selim Cinek | 88086e7 | 2016-06-17 21:01:32 -0700 | [diff] [blame] | 313 | if (!isOnlyChild(sbn)) { |
| 314 | return false; |
| 315 | } |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 316 | NotificationEntry logicalGroupSummary = getLogicalGroupSummary(sbn); |
Selim Cinek | 88086e7 | 2016-06-17 21:01:32 -0700 | [diff] [blame] | 317 | return logicalGroupSummary != null |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 318 | && !logicalGroupSummary.getSbn().equals(sbn); |
Selim Cinek | 53c4d3d | 2016-05-23 10:56:34 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 321 | private int getTotalNumberOfChildren(StatusBarNotification sbn) { |
Selim Cinek | 53c4d3d | 2016-05-23 10:56:34 -0700 | [diff] [blame] | 322 | int isolatedChildren = getNumberOfIsolatedChildren(sbn.getGroupKey()); |
| 323 | NotificationGroup group = mGroupMap.get(sbn.getGroupKey()); |
| 324 | int realChildren = group != null ? group.children.size() : 0; |
| 325 | return isolatedChildren + realChildren; |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | private boolean isGroupSuppressed(String groupKey) { |
| 329 | NotificationGroup group = mGroupMap.get(groupKey); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 330 | return group != null && group.suppressed; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 331 | } |
| 332 | |
Jason Monk | 1fd3fc3 | 2018-08-14 17:20:09 -0400 | [diff] [blame] | 333 | private void setStatusBarState(int newState) { |
Selim Cinek | 9c4c414 | 2015-12-04 16:44:56 -0800 | [diff] [blame] | 334 | mBarState = newState; |
| 335 | if (mBarState == StatusBarState.KEYGUARD) { |
Selim Cinek | 9184f9c | 2016-02-02 17:36:53 -0800 | [diff] [blame] | 336 | collapseAllGroups(); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | public void collapseAllGroups() { |
Selim Cinek | c0b14b0 | 2016-05-09 13:12:40 -0400 | [diff] [blame] | 341 | // Because notifications can become isolated when the group becomes suppressed it can |
| 342 | // lead to concurrent modifications while looping. We need to make a copy. |
| 343 | ArrayList<NotificationGroup> groupCopy = new ArrayList<>(mGroupMap.values()); |
| 344 | int size = groupCopy.size(); |
| 345 | for (int i = 0; i < size; i++) { |
| 346 | NotificationGroup group = groupCopy.get(i); |
Selim Cinek | 9184f9c | 2016-02-02 17:36:53 -0800 | [diff] [blame] | 347 | if (group.expanded) { |
| 348 | setGroupExpanded(group, false); |
Selim Cinek | 9c4c414 | 2015-12-04 16:44:56 -0800 | [diff] [blame] | 349 | } |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 350 | updateSuppression(group); |
Selim Cinek | 9c4c414 | 2015-12-04 16:44:56 -0800 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 354 | /** |
| 355 | * @return whether a given notification is a child in a group which has a summary |
| 356 | */ |
| 357 | public boolean isChildInGroupWithSummary(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 358 | if (!isGroupChild(sbn)) { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 359 | return false; |
| 360 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 361 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 362 | if (group == null || group.summary == null || group.suppressed) { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 363 | return false; |
| 364 | } |
Selim Cinek | 3f19f60 | 2016-05-02 18:01:56 -0700 | [diff] [blame] | 365 | if (group.children.isEmpty()) { |
| 366 | // If the suppression of a group changes because the last child was removed, this can |
| 367 | // still be called temporarily because the child hasn't been fully removed yet. Let's |
| 368 | // make sure we still return false in that case. |
| 369 | return false; |
| 370 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 371 | return true; |
| 372 | } |
| 373 | |
Selim Cinek | 263398f | 2015-10-21 17:40:23 -0700 | [diff] [blame] | 374 | /** |
| 375 | * @return whether a given notification is a summary in a group which has children |
| 376 | */ |
| 377 | public boolean isSummaryOfGroup(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 378 | if (!isGroupSummary(sbn)) { |
Selim Cinek | 263398f | 2015-10-21 17:40:23 -0700 | [diff] [blame] | 379 | return false; |
| 380 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 381 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Kevin | 96eed64 | 2018-09-05 18:53:33 -0700 | [diff] [blame] | 382 | if (group == null || group.summary == null) { |
Selim Cinek | 263398f | 2015-10-21 17:40:23 -0700 | [diff] [blame] | 383 | return false; |
| 384 | } |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 385 | return !group.children.isEmpty() && Objects.equals(group.summary.getSbn(), sbn); |
Selim Cinek | 263398f | 2015-10-21 17:40:23 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 388 | /** |
| 389 | * Get the summary of a specified status bar notification. For isolated notification this return |
| 390 | * itself. |
| 391 | */ |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 392 | public NotificationEntry getGroupSummary(StatusBarNotification sbn) { |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 393 | return getGroupSummary(getGroupKey(sbn)); |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Similar to {@link #getGroupSummary(StatusBarNotification)} but doesn't get the visual summary |
| 398 | * but the logical summary, i.e when a child is isolated, it still returns the summary as if |
| 399 | * it wasn't isolated. |
| 400 | */ |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 401 | public NotificationEntry getLogicalGroupSummary(StatusBarNotification sbn) { |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 402 | return getGroupSummary(sbn.getGroupKey()); |
| 403 | } |
| 404 | |
| 405 | @Nullable |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 406 | private NotificationEntry getGroupSummary(String groupKey) { |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 407 | NotificationGroup group = mGroupMap.get(groupKey); |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 408 | //TODO: see if this can become an Entry |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 409 | return group == null ? null |
Evan Laird | 9a9a619 | 2019-12-19 10:44:21 -0500 | [diff] [blame] | 410 | : group.summary; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 411 | } |
| 412 | |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 413 | /** |
| 414 | * Get the children that are logically in the summary's group, whether or not they are isolated. |
| 415 | * |
| 416 | * @param summary summary of a group |
| 417 | * @return list of the children |
| 418 | */ |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 419 | public ArrayList<NotificationEntry> getLogicalChildren(StatusBarNotification summary) { |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 420 | NotificationGroup group = mGroupMap.get(summary.getGroupKey()); |
| 421 | if (group == null) { |
| 422 | return null; |
| 423 | } |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 424 | ArrayList<NotificationEntry> children = new ArrayList<>(group.children.values()); |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 425 | for (StatusBarNotification sbn : mIsolatedEntries.values()) { |
| 426 | if (sbn.getGroupKey().equals(summary.getGroupKey())) { |
| 427 | children.add(mGroupMap.get(sbn.getKey()).summary); |
| 428 | } |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 429 | } |
| 430 | return children; |
| 431 | } |
| 432 | |
| 433 | /** |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 434 | * Get the children that are in the summary's group, not including those isolated. |
| 435 | * |
| 436 | * @param summary summary of a group |
| 437 | * @return list of the children |
| 438 | */ |
| 439 | public @Nullable ArrayList<NotificationEntry> getChildren(StatusBarNotification summary) { |
| 440 | NotificationGroup group = mGroupMap.get(summary.getGroupKey()); |
| 441 | if (group == null) { |
| 442 | return null; |
| 443 | } |
| 444 | return new ArrayList<>(group.children.values()); |
| 445 | } |
| 446 | |
| 447 | /** |
Mady Mellor | 740d85d | 2019-07-09 15:26:47 -0700 | [diff] [blame] | 448 | * If there is a {@link NotificationGroup} associated with the provided entry, this method |
| 449 | * will update the suppression of that group. |
| 450 | */ |
| 451 | public void updateSuppression(NotificationEntry entry) { |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 452 | NotificationGroup group = mGroupMap.get(getGroupKey(entry.getSbn())); |
Mady Mellor | 740d85d | 2019-07-09 15:26:47 -0700 | [diff] [blame] | 453 | if (group != null) { |
| 454 | updateSuppression(group); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | /** |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 459 | * Get the group key. May differ from the one in the notification due to the notification |
| 460 | * being temporarily isolated. |
| 461 | * |
| 462 | * @param sbn notification to check |
| 463 | * @return the key of the notification |
| 464 | */ |
| 465 | public String getGroupKey(StatusBarNotification sbn) { |
Beverly | c509412 | 2020-01-09 17:05:15 -0500 | [diff] [blame] | 466 | return getGroupKey(sbn.getKey(), sbn.getGroupKey()); |
| 467 | } |
| 468 | |
| 469 | private String getGroupKey(String key, String groupKey) { |
| 470 | if (isIsolated(key)) { |
| 471 | return key; |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 472 | } |
Beverly | c509412 | 2020-01-09 17:05:15 -0500 | [diff] [blame] | 473 | return groupKey; |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 474 | } |
| 475 | |
Chris Wren | 698b170 | 2016-05-23 11:16:32 -0400 | [diff] [blame] | 476 | /** @return group expansion state after toggling. */ |
| 477 | public boolean toggleGroupExpansion(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 478 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 83bc783 | 2015-10-22 13:26:54 -0700 | [diff] [blame] | 479 | if (group == null) { |
Chris Wren | 698b170 | 2016-05-23 11:16:32 -0400 | [diff] [blame] | 480 | return false; |
Selim Cinek | 83bc783 | 2015-10-22 13:26:54 -0700 | [diff] [blame] | 481 | } |
| 482 | setGroupExpanded(group, !group.expanded); |
Chris Wren | 698b170 | 2016-05-23 11:16:32 -0400 | [diff] [blame] | 483 | return group.expanded; |
Selim Cinek | 83bc783 | 2015-10-22 13:26:54 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Beverly | c509412 | 2020-01-09 17:05:15 -0500 | [diff] [blame] | 486 | private boolean isIsolated(String sbnKey) { |
| 487 | return mIsolatedEntries.containsKey(sbnKey); |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 490 | /** |
| 491 | * Whether a notification is visually a group summary. |
| 492 | * |
| 493 | * @param sbn notification to check |
| 494 | * @return true if it is visually a group summary |
| 495 | */ |
| 496 | public boolean isGroupSummary(StatusBarNotification sbn) { |
Beverly | c509412 | 2020-01-09 17:05:15 -0500 | [diff] [blame] | 497 | if (isIsolated(sbn.getKey())) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 498 | return true; |
| 499 | } |
| 500 | return sbn.getNotification().isGroupSummary(); |
| 501 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 502 | |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 503 | /** |
| 504 | * Whether a notification is visually a group child. |
| 505 | * |
| 506 | * @param sbn notification to check |
| 507 | * @return true if it is visually a group child |
| 508 | */ |
| 509 | public boolean isGroupChild(StatusBarNotification sbn) { |
Beverly | c509412 | 2020-01-09 17:05:15 -0500 | [diff] [blame] | 510 | return isGroupChild(sbn.getKey(), sbn.isGroup(), sbn.getNotification().isGroupSummary()); |
| 511 | } |
| 512 | |
| 513 | private boolean isGroupChild(String key, boolean isGroup, boolean isGroupSummary) { |
| 514 | if (isIsolated(key)) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 515 | return false; |
| 516 | } |
Beverly | c509412 | 2020-01-09 17:05:15 -0500 | [diff] [blame] | 517 | return isGroup && !isGroupSummary; |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 520 | @Override |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 521 | public void onHeadsUpStateChanged(NotificationEntry entry, boolean isHeadsUp) { |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 522 | updateIsolation(entry); |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 525 | /** |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 526 | * Whether a notification that is normally part of a group should be temporarily isolated from |
| 527 | * the group and put in their own group visually. This generally happens when the notification |
| 528 | * is alerting. |
| 529 | * |
| 530 | * @param entry the notification to check |
| 531 | * @return true if the entry should be isolated |
| 532 | */ |
| 533 | |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 534 | private boolean shouldIsolate(NotificationEntry entry) { |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 535 | StatusBarNotification sbn = entry.getSbn(); |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 536 | if (!sbn.isGroup() || sbn.getNotification().isGroupSummary()) { |
| 537 | return false; |
| 538 | } |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 539 | NotificationChannel channel = entry.getChannel(); |
| 540 | if (channel != null && channel.isImportantConversation()) { |
| 541 | return true; |
| 542 | } |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 543 | if (mHeadsUpManager != null && !mHeadsUpManager.isAlerting(entry.getKey())) { |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 544 | return false; |
| 545 | } |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 546 | NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey()); |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 547 | return (sbn.getNotification().fullScreenIntent != null |
| 548 | || notificationGroup == null |
| 549 | || !notificationGroup.expanded |
| 550 | || isGroupNotFullyVisible(notificationGroup)); |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Isolate a notification from its group so that it visually shows as its own group. |
| 555 | * |
| 556 | * @param entry the notification to isolate |
| 557 | */ |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 558 | private void isolateNotification(NotificationEntry entry) { |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 559 | StatusBarNotification sbn = entry.getSbn(); |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 560 | |
| 561 | // We will be isolated now, so lets update the groups |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 562 | onEntryRemovedInternal(entry, entry.getSbn()); |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 563 | |
| 564 | mIsolatedEntries.put(sbn.getKey(), sbn); |
| 565 | |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 566 | onEntryAddedInternal(entry); |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 567 | // We also need to update the suppression of the old group, because this call comes |
| 568 | // even before the groupManager knows about the notification at all. |
| 569 | // When the notification gets added afterwards it is already isolated and therefore |
| 570 | // it doesn't lead to an update. |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 571 | updateSuppression(mGroupMap.get(entry.getSbn().getGroupKey())); |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 572 | for (OnGroupChangeListener listener : mListeners) { |
| 573 | listener.onGroupsChanged(); |
| 574 | } |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /** |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 578 | * Update the isolation of an entry, splitting it from the group. |
| 579 | */ |
| 580 | public void updateIsolation(NotificationEntry entry) { |
| 581 | boolean isIsolated = isIsolated(entry.getSbn().getKey()); |
| 582 | if (shouldIsolate(entry)) { |
| 583 | if (!isIsolated) { |
| 584 | isolateNotification(entry); |
| 585 | } |
| 586 | } else if (isIsolated) { |
| 587 | stopIsolatingNotification(entry); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | /** |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 592 | * Stop isolating a notification and re-group it with its original logical group. |
| 593 | * |
| 594 | * @param entry the notification to un-isolate |
| 595 | */ |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 596 | private void stopIsolatingNotification(NotificationEntry entry) { |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 597 | StatusBarNotification sbn = entry.getSbn(); |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 598 | if (isIsolated(sbn.getKey())) { |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 599 | // not isolated anymore, we need to update the groups |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 600 | onEntryRemovedInternal(entry, entry.getSbn()); |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 601 | mIsolatedEntries.remove(sbn.getKey()); |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 602 | onEntryAddedInternal(entry); |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 603 | for (OnGroupChangeListener listener : mListeners) { |
| 604 | listener.onGroupsChanged(); |
| 605 | } |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 606 | } |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | private boolean isGroupNotFullyVisible(NotificationGroup notificationGroup) { |
| 610 | return notificationGroup.summary == null |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 611 | || notificationGroup.summary.isGroupNotFullyVisible(); |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 614 | public void setHeadsUpManager(HeadsUpManager headsUpManager) { |
| 615 | mHeadsUpManager = headsUpManager; |
| 616 | } |
| 617 | |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame] | 618 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 619 | pw.println("GroupManager state:"); |
| 620 | pw.println(" number of groups: " + mGroupMap.size()); |
| 621 | for (Map.Entry<String, NotificationGroup> entry : mGroupMap.entrySet()) { |
| 622 | pw.println("\n key: " + entry.getKey()); pw.println(entry.getValue()); |
| 623 | } |
| 624 | pw.println("\n isolated entries: " + mIsolatedEntries.size()); |
| 625 | for (Map.Entry<String, StatusBarNotification> entry : mIsolatedEntries.entrySet()) { |
| 626 | pw.print(" "); pw.print(entry.getKey()); |
| 627 | pw.print(", "); pw.println(entry.getValue()); |
| 628 | } |
| 629 | } |
| 630 | |
Evan Laird | 878c853 | 2018-10-15 15:54:29 -0400 | [diff] [blame] | 631 | @Override |
| 632 | public void onStateChanged(int newState) { |
| 633 | setStatusBarState(newState); |
| 634 | } |
| 635 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 636 | public static class NotificationGroup { |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 637 | public final HashMap<String, NotificationEntry> children = new HashMap<>(); |
| 638 | public NotificationEntry summary; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 639 | public boolean expanded; |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 640 | /** |
| 641 | * Is this notification group suppressed, i.e its summary is hidden |
| 642 | */ |
| 643 | public boolean suppressed; |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame] | 644 | |
| 645 | @Override |
| 646 | public String toString() { |
Selim Cinek | 60ca787 | 2016-05-12 11:24:07 -0700 | [diff] [blame] | 647 | String result = " summary:\n " |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 648 | + (summary != null ? summary.getSbn() : "null") |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 649 | + (summary != null && summary.getDebugThrowable() != null |
| 650 | ? Log.getStackTraceString(summary.getDebugThrowable()) |
| 651 | : ""); |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame] | 652 | result += "\n children size: " + children.size(); |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 653 | for (NotificationEntry child : children.values()) { |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 654 | result += "\n " + child.getSbn() |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 655 | + (child.getDebugThrowable() != null |
| 656 | ? Log.getStackTraceString(child.getDebugThrowable()) |
| 657 | : ""); |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame] | 658 | } |
Mady Mellor | 740d85d | 2019-07-09 15:26:47 -0700 | [diff] [blame] | 659 | result += "\n summary suppressed: " + suppressed; |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame] | 660 | return result; |
| 661 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | public interface OnGroupChangeListener { |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 665 | |
| 666 | /** |
| 667 | * A new group has been created. |
| 668 | * |
| 669 | * @param group the group that was created |
| 670 | * @param groupKey the group's key |
| 671 | */ |
| 672 | default void onGroupCreated(NotificationGroup group, String groupKey) {} |
| 673 | |
| 674 | /** |
| 675 | * A group has been removed. |
| 676 | * |
| 677 | * @param group the group that was removed |
| 678 | * @param groupKey the group's key |
| 679 | */ |
| 680 | default void onGroupRemoved(NotificationGroup group, String groupKey) {} |
| 681 | |
| 682 | /** |
| 683 | * The suppression of a group has changed. |
| 684 | * |
| 685 | * @param group the group that has changed |
| 686 | * @param suppressed true if the group is now suppressed, false o/w |
| 687 | */ |
| 688 | default void onGroupSuppressionChanged(NotificationGroup group, boolean suppressed) {} |
| 689 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 690 | /** |
| 691 | * The expansion of a group has changed. |
| 692 | * |
| 693 | * @param changedRow the row for which the expansion has changed, which is also the summary |
| 694 | * @param expanded a boolean indicating the new expanded state |
| 695 | */ |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 696 | default void onGroupExpansionChanged(ExpandableNotificationRow changedRow, |
| 697 | boolean expanded) {} |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 698 | |
| 699 | /** |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 700 | * A group of children just received a summary notification and should therefore become |
| 701 | * children of it. |
| 702 | * |
| 703 | * @param group the group created |
| 704 | */ |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 705 | default void onGroupCreatedFromChildren(NotificationGroup group) {} |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 706 | |
| 707 | /** |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 708 | * The groups have changed. This can happen if the isolation of a child has changes or if a |
| 709 | * group became suppressed / unsuppressed |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 710 | */ |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 711 | default void onGroupsChanged() {} |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 712 | } |
| 713 | } |