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