Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.statusbar.phone; |
| 18 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 19 | import android.service.notification.StatusBarNotification; |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 20 | import android.support.annotation.Nullable; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 21 | |
| 22 | import com.android.systemui.statusbar.ExpandableNotificationRow; |
| 23 | import com.android.systemui.statusbar.NotificationData; |
Selim Cinek | 9c4c414 | 2015-12-04 16:44:56 -0800 | [diff] [blame] | 24 | import com.android.systemui.statusbar.StatusBarState; |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 25 | import com.android.systemui.statusbar.policy.HeadsUpManager; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 26 | |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame^] | 27 | import java.io.FileDescriptor; |
| 28 | import java.io.PrintWriter; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 29 | import java.util.HashMap; |
| 30 | import java.util.HashSet; |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 31 | import java.util.Iterator; |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame^] | 32 | import java.util.Map; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 33 | import java.util.Objects; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * A class to handle notifications and their corresponding groups. |
| 37 | */ |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 38 | public class NotificationGroupManager implements HeadsUpManager.OnHeadsUpChangedListener { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 39 | |
| 40 | private final HashMap<String, NotificationGroup> mGroupMap = new HashMap<>(); |
| 41 | private OnGroupChangeListener mListener; |
| 42 | private int mBarState = -1; |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 43 | private HashMap<String, StatusBarNotification> mIsolatedEntries = new HashMap<>(); |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 44 | private HeadsUpManager mHeadsUpManager; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 45 | |
| 46 | public void setOnGroupChangeListener(OnGroupChangeListener listener) { |
| 47 | mListener = listener; |
| 48 | } |
| 49 | |
| 50 | public boolean isGroupExpanded(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 51 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 52 | if (group == null) { |
| 53 | return false; |
| 54 | } |
| 55 | return group.expanded; |
| 56 | } |
| 57 | |
| 58 | public void setGroupExpanded(StatusBarNotification sbn, boolean expanded) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 59 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 60 | if (group == null) { |
| 61 | return; |
| 62 | } |
| 63 | setGroupExpanded(group, expanded); |
| 64 | } |
| 65 | |
| 66 | private void setGroupExpanded(NotificationGroup group, boolean expanded) { |
| 67 | group.expanded = expanded; |
| 68 | if (group.summary != null) { |
| 69 | mListener.onGroupExpansionChanged(group.summary.row, expanded); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | public void onEntryRemoved(NotificationData.Entry removed) { |
| 74 | onEntryRemovedInternal(removed, removed.notification); |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame^] | 75 | mIsolatedEntries.remove(removed.key); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
| 79 | * An entry was removed. |
| 80 | * |
| 81 | * @param removed the removed entry |
| 82 | * @param sbn the notification the entry has, which doesn't need to be the same as it's internal |
| 83 | * notification |
| 84 | */ |
| 85 | private void onEntryRemovedInternal(NotificationData.Entry removed, |
| 86 | final StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 87 | String groupKey = getGroupKey(sbn); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 88 | final NotificationGroup group = mGroupMap.get(groupKey); |
Selim Cinek | 0b4aeab | 2015-09-01 17:07:38 -0700 | [diff] [blame] | 89 | if (group == null) { |
| 90 | // When an app posts 2 different notifications as summary of the same group, then a |
| 91 | // cancellation of the first notification removes this group. |
| 92 | // This situation is not supported and we will not allow such notifications anymore in |
| 93 | // the close future. See b/23676310 for reference. |
| 94 | return; |
| 95 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 96 | if (isGroupChild(sbn)) { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 97 | group.children.remove(removed); |
Selim Cinek | e73ad21 | 2015-11-03 19:11:08 -0800 | [diff] [blame] | 98 | } else { |
| 99 | group.summary = null; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 100 | } |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 101 | updateSuppression(group); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 102 | if (group.children.isEmpty()) { |
| 103 | if (group.summary == null) { |
| 104 | mGroupMap.remove(groupKey); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 109 | public void onEntryAdded(final NotificationData.Entry added) { |
| 110 | final StatusBarNotification sbn = added.notification; |
| 111 | boolean isGroupChild = isGroupChild(sbn); |
| 112 | String groupKey = getGroupKey(sbn); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 113 | NotificationGroup group = mGroupMap.get(groupKey); |
| 114 | if (group == null) { |
| 115 | group = new NotificationGroup(); |
| 116 | mGroupMap.put(groupKey, group); |
| 117 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 118 | if (isGroupChild) { |
Selim Cinek | e73ad21 | 2015-11-03 19:11:08 -0800 | [diff] [blame] | 119 | group.children.add(added); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 120 | updateSuppression(group); |
Selim Cinek | e73ad21 | 2015-11-03 19:11:08 -0800 | [diff] [blame] | 121 | } else { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 122 | group.summary = added; |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 123 | group.expanded = added.row.areChildrenExpanded(); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 124 | updateSuppression(group); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 125 | if (!group.children.isEmpty()) { |
Selim Cinek | 50e7467 | 2016-05-05 15:58:10 -0400 | [diff] [blame] | 126 | HashSet<NotificationData.Entry> childrenCopy = |
| 127 | (HashSet<NotificationData.Entry>) group.children.clone(); |
| 128 | for (NotificationData.Entry child : childrenCopy) { |
| 129 | onEntryBecomingChild(child); |
| 130 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 131 | mListener.onGroupCreatedFromChildren(group); |
| 132 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Selim Cinek | 50e7467 | 2016-05-05 15:58:10 -0400 | [diff] [blame] | 136 | private void onEntryBecomingChild(NotificationData.Entry entry) { |
| 137 | if (entry.row.isHeadsUp()) { |
| 138 | onHeadsUpStateChanged(entry, true); |
| 139 | } |
| 140 | } |
| 141 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 142 | public void onEntryBundlingUpdated(final NotificationData.Entry updated, |
| 143 | final String overrideGroupKey) { |
| 144 | final StatusBarNotification oldSbn = updated.notification.clone(); |
| 145 | if (!Objects.equals(oldSbn.getOverrideGroupKey(), overrideGroupKey)) { |
| 146 | updated.notification.setOverrideGroupKey(overrideGroupKey); |
| 147 | onEntryUpdated(updated, oldSbn); |
| 148 | } |
| 149 | } |
| 150 | |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 151 | private void updateSuppression(NotificationGroup group) { |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 152 | if (group == null) { |
| 153 | return; |
| 154 | } |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 155 | boolean prevSuppressed = group.suppressed; |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 156 | group.suppressed = group.summary != null && !group.expanded |
| 157 | && (group.children.size() == 1 |
| 158 | || (group.children.size() == 0 |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 159 | && group.summary.notification.getNotification().isGroupSummary() |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 160 | && hasIsolatedChildren(group))); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 161 | if (prevSuppressed != group.suppressed) { |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 162 | if (group.suppressed) { |
| 163 | handleSuppressedSummaryHeadsUpped(group.summary); |
| 164 | } |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 165 | mListener.onGroupsChanged(); |
| 166 | } |
| 167 | } |
| 168 | |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 169 | private boolean hasIsolatedChildren(NotificationGroup group) { |
| 170 | return getNumberOfIsolatedChildren(group.summary.notification.getGroupKey()) != 0; |
| 171 | } |
| 172 | |
| 173 | private int getNumberOfIsolatedChildren(String groupKey) { |
| 174 | int count = 0; |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 175 | for (StatusBarNotification sbn : mIsolatedEntries.values()) { |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 176 | if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) { |
| 177 | count++; |
| 178 | } |
| 179 | } |
| 180 | return count; |
| 181 | } |
| 182 | |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 183 | private NotificationData.Entry getIsolatedChild(String groupKey) { |
| 184 | for (StatusBarNotification sbn : mIsolatedEntries.values()) { |
| 185 | if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) { |
| 186 | return mGroupMap.get(sbn.getKey()).summary; |
| 187 | } |
| 188 | } |
| 189 | return null; |
| 190 | } |
| 191 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 192 | public void onEntryUpdated(NotificationData.Entry entry, |
| 193 | StatusBarNotification oldNotification) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 194 | if (mGroupMap.get(getGroupKey(oldNotification)) != null) { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 195 | onEntryRemovedInternal(entry, oldNotification); |
| 196 | } |
| 197 | onEntryAdded(entry); |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 198 | if (isIsolated(entry.notification)) { |
| 199 | mIsolatedEntries.put(entry.key, entry.notification); |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 200 | String oldKey = oldNotification.getGroupKey(); |
| 201 | String newKey = entry.notification.getGroupKey(); |
| 202 | if (!oldKey.equals(newKey)) { |
| 203 | updateSuppression(mGroupMap.get(oldKey)); |
| 204 | updateSuppression(mGroupMap.get(newKey)); |
| 205 | } |
Selim Cinek | 50e7467 | 2016-05-05 15:58:10 -0400 | [diff] [blame] | 206 | } else if (!isGroupChild(oldNotification) && isGroupChild(entry.notification)) { |
| 207 | onEntryBecomingChild(entry); |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 208 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 211 | public boolean isSummaryOfSuppressedGroup(StatusBarNotification sbn) { |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 212 | return isGroupSuppressed(getGroupKey(sbn)) && sbn.getNotification().isGroupSummary(); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 213 | } |
| 214 | |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 215 | public boolean isOnlyChildInSuppressedGroup(StatusBarNotification sbn) { |
| 216 | return isGroupSuppressed(sbn.getGroupKey()) |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 217 | && !sbn.getNotification().isGroupSummary() |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 218 | && getTotalNumberOfChildren(sbn) == 1; |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 221 | private int getTotalNumberOfChildren(StatusBarNotification sbn) { |
| 222 | return getNumberOfIsolatedChildren(sbn.getGroupKey()) |
| 223 | + mGroupMap.get(sbn.getGroupKey()).children.size(); |
| 224 | } |
| 225 | |
| 226 | private boolean isGroupSuppressed(String groupKey) { |
| 227 | NotificationGroup group = mGroupMap.get(groupKey); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 228 | return group != null && group.suppressed; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 229 | } |
| 230 | |
Selim Cinek | 9c4c414 | 2015-12-04 16:44:56 -0800 | [diff] [blame] | 231 | public void setStatusBarState(int newState) { |
| 232 | if (mBarState == newState) { |
| 233 | return; |
| 234 | } |
| 235 | mBarState = newState; |
| 236 | if (mBarState == StatusBarState.KEYGUARD) { |
Selim Cinek | 9184f9c | 2016-02-02 17:36:53 -0800 | [diff] [blame] | 237 | collapseAllGroups(); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | public void collapseAllGroups() { |
| 242 | for (NotificationGroup group : mGroupMap.values()) { |
| 243 | if (group.expanded) { |
| 244 | setGroupExpanded(group, false); |
Selim Cinek | 9c4c414 | 2015-12-04 16:44:56 -0800 | [diff] [blame] | 245 | } |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 246 | updateSuppression(group); |
Selim Cinek | 9c4c414 | 2015-12-04 16:44:56 -0800 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 250 | /** |
| 251 | * @return whether a given notification is a child in a group which has a summary |
| 252 | */ |
| 253 | public boolean isChildInGroupWithSummary(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 254 | if (!isGroupChild(sbn)) { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 255 | return false; |
| 256 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 257 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 258 | if (group == null || group.summary == null || group.suppressed) { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 259 | return false; |
| 260 | } |
Selim Cinek | 3f19f60 | 2016-05-02 18:01:56 -0700 | [diff] [blame] | 261 | if (group.children.isEmpty()) { |
| 262 | // If the suppression of a group changes because the last child was removed, this can |
| 263 | // still be called temporarily because the child hasn't been fully removed yet. Let's |
| 264 | // make sure we still return false in that case. |
| 265 | return false; |
| 266 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 267 | return true; |
| 268 | } |
| 269 | |
Selim Cinek | 263398f | 2015-10-21 17:40:23 -0700 | [diff] [blame] | 270 | /** |
| 271 | * @return whether a given notification is a summary in a group which has children |
| 272 | */ |
| 273 | public boolean isSummaryOfGroup(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 274 | if (!isGroupSummary(sbn)) { |
Selim Cinek | 263398f | 2015-10-21 17:40:23 -0700 | [diff] [blame] | 275 | return false; |
| 276 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 277 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 263398f | 2015-10-21 17:40:23 -0700 | [diff] [blame] | 278 | if (group == null) { |
| 279 | return false; |
| 280 | } |
| 281 | return !group.children.isEmpty(); |
| 282 | } |
| 283 | |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 284 | /** |
| 285 | * Get the summary of a specified status bar notification. For isolated notification this return |
| 286 | * itself. |
| 287 | */ |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 288 | public ExpandableNotificationRow getGroupSummary(StatusBarNotification sbn) { |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 289 | return getGroupSummary(getGroupKey(sbn)); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Similar to {@link #getGroupSummary(StatusBarNotification)} but doesn't get the visual summary |
| 294 | * but the logical summary, i.e when a child is isolated, it still returns the summary as if |
| 295 | * it wasn't isolated. |
| 296 | */ |
| 297 | public ExpandableNotificationRow getLogicalGroupSummary( |
| 298 | StatusBarNotification sbn) { |
| 299 | return getGroupSummary(sbn.getGroupKey()); |
| 300 | } |
| 301 | |
| 302 | @Nullable |
| 303 | private ExpandableNotificationRow getGroupSummary(String groupKey) { |
| 304 | NotificationGroup group = mGroupMap.get(groupKey); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 305 | return group == null ? null |
| 306 | : group.summary == null ? null |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 307 | : group.summary.row; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 308 | } |
| 309 | |
Selim Cinek | 83bc783 | 2015-10-22 13:26:54 -0700 | [diff] [blame] | 310 | public void toggleGroupExpansion(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 311 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 83bc783 | 2015-10-22 13:26:54 -0700 | [diff] [blame] | 312 | if (group == null) { |
| 313 | return; |
| 314 | } |
| 315 | setGroupExpanded(group, !group.expanded); |
| 316 | } |
| 317 | |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 318 | private boolean isIsolated(StatusBarNotification sbn) { |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 319 | return mIsolatedEntries.containsKey(sbn.getKey()); |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | private boolean isGroupSummary(StatusBarNotification sbn) { |
| 323 | if (isIsolated(sbn)) { |
| 324 | return true; |
| 325 | } |
| 326 | return sbn.getNotification().isGroupSummary(); |
| 327 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 328 | |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 329 | private boolean isGroupChild(StatusBarNotification sbn) { |
| 330 | if (isIsolated(sbn)) { |
| 331 | return false; |
| 332 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 333 | return sbn.isGroup() && !sbn.getNotification().isGroupSummary(); |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | private String getGroupKey(StatusBarNotification sbn) { |
| 337 | if (isIsolated(sbn)) { |
| 338 | return sbn.getKey(); |
| 339 | } |
| 340 | return sbn.getGroupKey(); |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public void onHeadsUpPinnedModeChanged(boolean inPinnedMode) { |
| 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public void onHeadsUpPinned(ExpandableNotificationRow headsUp) { |
| 349 | } |
| 350 | |
| 351 | @Override |
| 352 | public void onHeadsUpUnPinned(ExpandableNotificationRow headsUp) { |
| 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public void onHeadsUpStateChanged(NotificationData.Entry entry, boolean isHeadsUp) { |
| 357 | final StatusBarNotification sbn = entry.notification; |
| 358 | if (entry.row.isHeadsUp()) { |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 359 | if (shouldIsolate(sbn)) { |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 360 | // We will be isolated now, so lets update the groups |
| 361 | onEntryRemovedInternal(entry, entry.notification); |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 362 | |
| 363 | mIsolatedEntries.put(sbn.getKey(), sbn); |
| 364 | |
Selim Cinek | 23c8034 | 2016-03-17 18:27:36 -0700 | [diff] [blame] | 365 | onEntryAdded(entry); |
| 366 | // We also need to update the suppression of the old group, because this call comes |
| 367 | // even before the groupManager knows about the notification at all. |
| 368 | // When the notification gets added afterwards it is already isolated and therefore |
| 369 | // it doesn't lead to an update. |
| 370 | updateSuppression(mGroupMap.get(entry.notification.getGroupKey())); |
| 371 | mListener.onGroupsChanged(); |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 372 | } else { |
| 373 | handleSuppressedSummaryHeadsUpped(entry); |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 374 | } |
| 375 | } else { |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 376 | if (mIsolatedEntries.containsKey(sbn.getKey())) { |
| 377 | // not isolated anymore, we need to update the groups |
| 378 | onEntryRemovedInternal(entry, entry.notification); |
| 379 | mIsolatedEntries.remove(sbn.getKey()); |
| 380 | onEntryAdded(entry); |
| 381 | mListener.onGroupsChanged(); |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 386 | private void handleSuppressedSummaryHeadsUpped(NotificationData.Entry entry) { |
| 387 | StatusBarNotification sbn = entry.notification; |
| 388 | if (!isGroupSuppressed(sbn.getGroupKey()) |
| 389 | || !sbn.getNotification().isGroupSummary() |
| 390 | || !entry.row.isHeadsUp()) { |
| 391 | return; |
| 392 | } |
| 393 | // The parent of a suppressed group got huned, lets hun the child! |
| 394 | NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey()); |
| 395 | if (notificationGroup != null) { |
| 396 | Iterator<NotificationData.Entry> iterator = notificationGroup.children.iterator(); |
| 397 | NotificationData.Entry child = iterator.hasNext() ? iterator.next() : null; |
| 398 | if (child == null) { |
| 399 | child = getIsolatedChild(sbn.getGroupKey()); |
| 400 | } |
| 401 | if (child != null) { |
| 402 | if (mHeadsUpManager.isHeadsUp(child.key)) { |
| 403 | mHeadsUpManager.updateNotification(child, true); |
| 404 | } else { |
| 405 | mHeadsUpManager.showNotification(child); |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | mHeadsUpManager.releaseImmediately(entry.key); |
| 410 | } |
| 411 | |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 412 | private boolean shouldIsolate(StatusBarNotification sbn) { |
| 413 | NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey()); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 414 | return (sbn.isGroup() && !sbn.getNotification().isGroupSummary()) |
Selim Cinek | a6c0cef | 2016-03-18 11:42:25 -0700 | [diff] [blame] | 415 | && (sbn.getNotification().fullScreenIntent != null |
| 416 | || notificationGroup == null |
| 417 | || !notificationGroup.expanded |
| 418 | || isGroupNotFullyVisible(notificationGroup)); |
| 419 | } |
| 420 | |
| 421 | private boolean isGroupNotFullyVisible(NotificationGroup notificationGroup) { |
| 422 | return notificationGroup.summary == null |
| 423 | || notificationGroup.summary.row.getClipTopOptimization() > 0 |
| 424 | || notificationGroup.summary.row.getClipTopAmount() > 0 |
| 425 | || notificationGroup.summary.row.getTranslationY() < 0; |
| 426 | } |
| 427 | |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 428 | public void setHeadsUpManager(HeadsUpManager headsUpManager) { |
| 429 | mHeadsUpManager = headsUpManager; |
| 430 | } |
| 431 | |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame^] | 432 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 433 | pw.println("GroupManager state:"); |
| 434 | pw.println(" number of groups: " + mGroupMap.size()); |
| 435 | for (Map.Entry<String, NotificationGroup> entry : mGroupMap.entrySet()) { |
| 436 | pw.println("\n key: " + entry.getKey()); pw.println(entry.getValue()); |
| 437 | } |
| 438 | pw.println("\n isolated entries: " + mIsolatedEntries.size()); |
| 439 | for (Map.Entry<String, StatusBarNotification> entry : mIsolatedEntries.entrySet()) { |
| 440 | pw.print(" "); pw.print(entry.getKey()); |
| 441 | pw.print(", "); pw.println(entry.getValue()); |
| 442 | } |
| 443 | } |
| 444 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 445 | public static class NotificationGroup { |
| 446 | public final HashSet<NotificationData.Entry> children = new HashSet<>(); |
| 447 | public NotificationData.Entry summary; |
| 448 | public boolean expanded; |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 449 | /** |
| 450 | * Is this notification group suppressed, i.e its summary is hidden |
| 451 | */ |
| 452 | public boolean suppressed; |
Selim Cinek | 52941c5 | 2016-05-07 18:29:32 -0400 | [diff] [blame^] | 453 | |
| 454 | @Override |
| 455 | public String toString() { |
| 456 | String result = " summary:\n " + summary.notification; |
| 457 | result += "\n children size: " + children.size(); |
| 458 | for (NotificationData.Entry child : children) { |
| 459 | result += "\n " + child.notification; |
| 460 | } |
| 461 | return result; |
| 462 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | public interface OnGroupChangeListener { |
| 466 | /** |
| 467 | * The expansion of a group has changed. |
| 468 | * |
| 469 | * @param changedRow the row for which the expansion has changed, which is also the summary |
| 470 | * @param expanded a boolean indicating the new expanded state |
| 471 | */ |
| 472 | void onGroupExpansionChanged(ExpandableNotificationRow changedRow, boolean expanded); |
| 473 | |
| 474 | /** |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 475 | * A group of children just received a summary notification and should therefore become |
| 476 | * children of it. |
| 477 | * |
| 478 | * @param group the group created |
| 479 | */ |
| 480 | void onGroupCreatedFromChildren(NotificationGroup group); |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 481 | |
| 482 | /** |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 483 | * The groups have changed. This can happen if the isolation of a child has changes or if a |
| 484 | * group became suppressed / unsuppressed |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 485 | */ |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame] | 486 | void onGroupsChanged(); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 487 | } |
| 488 | } |