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 | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 20 | import android.util.ArraySet; |
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 | |
| 27 | import java.util.HashMap; |
| 28 | import java.util.HashSet; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * A class to handle notifications and their corresponding groups. |
| 32 | */ |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 33 | public class NotificationGroupManager implements HeadsUpManager.OnHeadsUpChangedListener { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 34 | |
| 35 | private final HashMap<String, NotificationGroup> mGroupMap = new HashMap<>(); |
| 36 | private OnGroupChangeListener mListener; |
| 37 | private int mBarState = -1; |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 38 | private ArraySet<String> mHeadsUpedEntries = new ArraySet<>(); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 39 | |
| 40 | public void setOnGroupChangeListener(OnGroupChangeListener listener) { |
| 41 | mListener = listener; |
| 42 | } |
| 43 | |
| 44 | public boolean isGroupExpanded(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 45 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 46 | if (group == null) { |
| 47 | return false; |
| 48 | } |
| 49 | return group.expanded; |
| 50 | } |
| 51 | |
| 52 | public void setGroupExpanded(StatusBarNotification sbn, boolean expanded) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 53 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 54 | if (group == null) { |
| 55 | return; |
| 56 | } |
| 57 | setGroupExpanded(group, expanded); |
| 58 | } |
| 59 | |
| 60 | private void setGroupExpanded(NotificationGroup group, boolean expanded) { |
| 61 | group.expanded = expanded; |
| 62 | if (group.summary != null) { |
| 63 | mListener.onGroupExpansionChanged(group.summary.row, expanded); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public void onEntryRemoved(NotificationData.Entry removed) { |
| 68 | onEntryRemovedInternal(removed, removed.notification); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * An entry was removed. |
| 73 | * |
| 74 | * @param removed the removed entry |
| 75 | * @param sbn the notification the entry has, which doesn't need to be the same as it's internal |
| 76 | * notification |
| 77 | */ |
| 78 | private void onEntryRemovedInternal(NotificationData.Entry removed, |
| 79 | final StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 80 | String groupKey = getGroupKey(sbn); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 81 | final NotificationGroup group = mGroupMap.get(groupKey); |
Selim Cinek | 0b4aeab | 2015-09-01 17:07:38 -0700 | [diff] [blame] | 82 | if (group == null) { |
| 83 | // When an app posts 2 different notifications as summary of the same group, then a |
| 84 | // cancellation of the first notification removes this group. |
| 85 | // This situation is not supported and we will not allow such notifications anymore in |
| 86 | // the close future. See b/23676310 for reference. |
| 87 | return; |
| 88 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 89 | if (isGroupChild(sbn)) { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 90 | group.children.remove(removed); |
Selim Cinek | e73ad21 | 2015-11-03 19:11:08 -0800 | [diff] [blame] | 91 | } else { |
| 92 | group.summary = null; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 93 | } |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame^] | 94 | updateSuppression(group); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 95 | if (group.children.isEmpty()) { |
| 96 | if (group.summary == null) { |
| 97 | mGroupMap.remove(groupKey); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 102 | public void onEntryAdded(final NotificationData.Entry added) { |
| 103 | final StatusBarNotification sbn = added.notification; |
| 104 | boolean isGroupChild = isGroupChild(sbn); |
| 105 | String groupKey = getGroupKey(sbn); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 106 | NotificationGroup group = mGroupMap.get(groupKey); |
| 107 | if (group == null) { |
| 108 | group = new NotificationGroup(); |
| 109 | mGroupMap.put(groupKey, group); |
| 110 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 111 | if (isGroupChild) { |
Selim Cinek | e73ad21 | 2015-11-03 19:11:08 -0800 | [diff] [blame] | 112 | group.children.add(added); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame^] | 113 | updateSuppression(group); |
Selim Cinek | e73ad21 | 2015-11-03 19:11:08 -0800 | [diff] [blame] | 114 | } else { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 115 | group.summary = added; |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 116 | group.expanded = added.row.areChildrenExpanded(); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame^] | 117 | updateSuppression(group); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 118 | if (!group.children.isEmpty()) { |
| 119 | mListener.onGroupCreatedFromChildren(group); |
| 120 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame^] | 124 | private void updateSuppression(NotificationGroup group) { |
| 125 | boolean prevSuppressed = group.suppressed; |
| 126 | group.suppressed = group.summary != null && group.children.size() == 1 && !group.expanded; |
| 127 | if (prevSuppressed != group.suppressed) { |
| 128 | mListener.onGroupsChanged(); |
| 129 | } |
| 130 | } |
| 131 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 132 | public void onEntryUpdated(NotificationData.Entry entry, |
| 133 | StatusBarNotification oldNotification) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 134 | if (mGroupMap.get(getGroupKey(oldNotification)) != null) { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 135 | onEntryRemovedInternal(entry, oldNotification); |
| 136 | } |
| 137 | onEntryAdded(entry); |
| 138 | } |
| 139 | |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame^] | 140 | public boolean isSummaryOfSuppressedGroup(StatusBarNotification sbn) { |
| 141 | return isGroupSuppressed(sbn) && sbn.getNotification().isGroupSummary(); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame^] | 144 | public boolean isChildInSuppressedGroup(StatusBarNotification sbn) { |
| 145 | return isGroupSuppressed(sbn) && sbn.getNotification().isGroupChild(); |
| 146 | } |
| 147 | |
| 148 | private boolean isGroupSuppressed(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 149 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame^] | 150 | return group != null && group.suppressed; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Selim Cinek | 9c4c414 | 2015-12-04 16:44:56 -0800 | [diff] [blame] | 153 | public void setStatusBarState(int newState) { |
| 154 | if (mBarState == newState) { |
| 155 | return; |
| 156 | } |
| 157 | mBarState = newState; |
| 158 | if (mBarState == StatusBarState.KEYGUARD) { |
Selim Cinek | 9184f9c | 2016-02-02 17:36:53 -0800 | [diff] [blame] | 159 | collapseAllGroups(); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | public void collapseAllGroups() { |
| 164 | for (NotificationGroup group : mGroupMap.values()) { |
| 165 | if (group.expanded) { |
| 166 | setGroupExpanded(group, false); |
Selim Cinek | 9c4c414 | 2015-12-04 16:44:56 -0800 | [diff] [blame] | 167 | } |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame^] | 168 | updateSuppression(group); |
Selim Cinek | 9c4c414 | 2015-12-04 16:44:56 -0800 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 172 | /** |
| 173 | * @return whether a given notification is a child in a group which has a summary |
| 174 | */ |
| 175 | public boolean isChildInGroupWithSummary(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 176 | if (!isGroupChild(sbn)) { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 177 | return false; |
| 178 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 179 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame^] | 180 | if (group == null || group.summary == null || group.suppressed) { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 181 | return false; |
| 182 | } |
| 183 | return true; |
| 184 | } |
| 185 | |
Selim Cinek | 263398f | 2015-10-21 17:40:23 -0700 | [diff] [blame] | 186 | /** |
| 187 | * @return whether a given notification is a summary in a group which has children |
| 188 | */ |
| 189 | public boolean isSummaryOfGroup(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 190 | if (!isGroupSummary(sbn)) { |
Selim Cinek | 263398f | 2015-10-21 17:40:23 -0700 | [diff] [blame] | 191 | return false; |
| 192 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 193 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 263398f | 2015-10-21 17:40:23 -0700 | [diff] [blame] | 194 | if (group == null) { |
| 195 | return false; |
| 196 | } |
| 197 | return !group.children.isEmpty(); |
| 198 | } |
| 199 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 200 | public ExpandableNotificationRow getGroupSummary(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 201 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 202 | return group == null ? null |
| 203 | : group.summary == null ? null |
| 204 | : group.summary.row; |
| 205 | } |
| 206 | |
Selim Cinek | 83bc783 | 2015-10-22 13:26:54 -0700 | [diff] [blame] | 207 | public void toggleGroupExpansion(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 208 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 83bc783 | 2015-10-22 13:26:54 -0700 | [diff] [blame] | 209 | if (group == null) { |
| 210 | return; |
| 211 | } |
| 212 | setGroupExpanded(group, !group.expanded); |
| 213 | } |
| 214 | |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 215 | private boolean isIsolated(StatusBarNotification sbn) { |
| 216 | return mHeadsUpedEntries.contains(sbn.getKey()) && sbn.getNotification().isGroupChild(); |
| 217 | } |
| 218 | |
| 219 | private boolean isGroupSummary(StatusBarNotification sbn) { |
| 220 | if (isIsolated(sbn)) { |
| 221 | return true; |
| 222 | } |
| 223 | return sbn.getNotification().isGroupSummary(); |
| 224 | } |
| 225 | private boolean isGroupChild(StatusBarNotification sbn) { |
| 226 | if (isIsolated(sbn)) { |
| 227 | return false; |
| 228 | } |
| 229 | return sbn.getNotification().isGroupChild(); |
| 230 | } |
| 231 | |
| 232 | private String getGroupKey(StatusBarNotification sbn) { |
| 233 | if (isIsolated(sbn)) { |
| 234 | return sbn.getKey(); |
| 235 | } |
| 236 | return sbn.getGroupKey(); |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public void onHeadsUpPinnedModeChanged(boolean inPinnedMode) { |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public void onHeadsUpPinned(ExpandableNotificationRow headsUp) { |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public void onHeadsUpUnPinned(ExpandableNotificationRow headsUp) { |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public void onHeadsUpStateChanged(NotificationData.Entry entry, boolean isHeadsUp) { |
| 253 | final StatusBarNotification sbn = entry.notification; |
| 254 | if (entry.row.isHeadsUp()) { |
| 255 | if (!mHeadsUpedEntries.contains(sbn.getKey())) { |
| 256 | final boolean groupChild = sbn.getNotification().isGroupChild(); |
| 257 | if (groupChild) { |
| 258 | // We will be isolated now, so lets update the groups |
| 259 | onEntryRemovedInternal(entry, entry.notification); |
| 260 | } |
| 261 | mHeadsUpedEntries.add(sbn.getKey()); |
| 262 | if (groupChild) { |
| 263 | onEntryAdded(entry); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame^] | 264 | mListener.onGroupsChanged(); |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | } else { |
| 268 | if (mHeadsUpedEntries.contains(sbn.getKey())) { |
| 269 | boolean isolatedBefore = isIsolated(sbn); |
| 270 | if (isolatedBefore) { |
| 271 | // not isolated anymore, we need to update the groups |
| 272 | onEntryRemovedInternal(entry, entry.notification); |
| 273 | } |
| 274 | mHeadsUpedEntries.remove(sbn.getKey()); |
| 275 | if (isolatedBefore) { |
| 276 | onEntryAdded(entry); |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame^] | 277 | mListener.onGroupsChanged(); |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 283 | public static class NotificationGroup { |
| 284 | public final HashSet<NotificationData.Entry> children = new HashSet<>(); |
| 285 | public NotificationData.Entry summary; |
| 286 | public boolean expanded; |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame^] | 287 | /** |
| 288 | * Is this notification group suppressed, i.e its summary is hidden |
| 289 | */ |
| 290 | public boolean suppressed; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | public interface OnGroupChangeListener { |
| 294 | /** |
| 295 | * The expansion of a group has changed. |
| 296 | * |
| 297 | * @param changedRow the row for which the expansion has changed, which is also the summary |
| 298 | * @param expanded a boolean indicating the new expanded state |
| 299 | */ |
| 300 | void onGroupExpansionChanged(ExpandableNotificationRow changedRow, boolean expanded); |
| 301 | |
| 302 | /** |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 303 | * A group of children just received a summary notification and should therefore become |
| 304 | * children of it. |
| 305 | * |
| 306 | * @param group the group created |
| 307 | */ |
| 308 | void onGroupCreatedFromChildren(NotificationGroup group); |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 309 | |
| 310 | /** |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame^] | 311 | * The groups have changed. This can happen if the isolation of a child has changes or if a |
| 312 | * group became suppressed / unsuppressed |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame] | 313 | */ |
Selim Cinek | 2a73934 | 2016-03-17 10:28:55 -0700 | [diff] [blame^] | 314 | void onGroupsChanged(); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 315 | } |
| 316 | } |