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 | } |
| 94 | if (group.children.isEmpty()) { |
| 95 | if (group.summary == null) { |
| 96 | mGroupMap.remove(groupKey); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame^] | 101 | public void onEntryAdded(final NotificationData.Entry added) { |
| 102 | final StatusBarNotification sbn = added.notification; |
| 103 | boolean isGroupChild = isGroupChild(sbn); |
| 104 | String groupKey = getGroupKey(sbn); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 105 | NotificationGroup group = mGroupMap.get(groupKey); |
| 106 | if (group == null) { |
| 107 | group = new NotificationGroup(); |
| 108 | mGroupMap.put(groupKey, group); |
| 109 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame^] | 110 | if (isGroupChild) { |
Selim Cinek | e73ad21 | 2015-11-03 19:11:08 -0800 | [diff] [blame] | 111 | group.children.add(added); |
Selim Cinek | e73ad21 | 2015-11-03 19:11:08 -0800 | [diff] [blame] | 112 | } else { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 113 | group.summary = added; |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 114 | group.expanded = added.row.areChildrenExpanded(); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 115 | if (!group.children.isEmpty()) { |
| 116 | mListener.onGroupCreatedFromChildren(group); |
| 117 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | public void onEntryUpdated(NotificationData.Entry entry, |
| 122 | StatusBarNotification oldNotification) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame^] | 123 | if (mGroupMap.get(getGroupKey(oldNotification)) != null) { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 124 | onEntryRemovedInternal(entry, oldNotification); |
| 125 | } |
| 126 | onEntryAdded(entry); |
| 127 | } |
| 128 | |
| 129 | public boolean isVisible(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame^] | 130 | if (!isGroupChild(sbn)) { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 131 | return true; |
| 132 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame^] | 133 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | acf52ba | 2015-07-07 14:54:10 -0700 | [diff] [blame] | 134 | if (group != null && (group.expanded || group.summary == null)) { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 135 | return true; |
| 136 | } |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | public boolean hasGroupChildren(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame^] | 141 | if (!isGroupSummary(sbn)) { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 142 | return false; |
| 143 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame^] | 144 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 145 | if (group == null) { |
| 146 | return false; |
| 147 | } |
| 148 | return !group.children.isEmpty(); |
| 149 | } |
| 150 | |
Selim Cinek | 9c4c414 | 2015-12-04 16:44:56 -0800 | [diff] [blame] | 151 | public void setStatusBarState(int newState) { |
| 152 | if (mBarState == newState) { |
| 153 | return; |
| 154 | } |
| 155 | mBarState = newState; |
| 156 | if (mBarState == StatusBarState.KEYGUARD) { |
| 157 | for (NotificationGroup group : mGroupMap.values()) { |
| 158 | if (group.expanded) { |
| 159 | setGroupExpanded(group, false); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 165 | /** |
| 166 | * @return whether a given notification is a child in a group which has a summary |
| 167 | */ |
| 168 | public boolean isChildInGroupWithSummary(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame^] | 169 | if (!isGroupChild(sbn)) { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 170 | return false; |
| 171 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame^] | 172 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 173 | if (group == null || group.summary == null) { |
| 174 | return false; |
| 175 | } |
| 176 | return true; |
| 177 | } |
| 178 | |
Selim Cinek | 263398f | 2015-10-21 17:40:23 -0700 | [diff] [blame] | 179 | /** |
| 180 | * @return whether a given notification is a summary in a group which has children |
| 181 | */ |
| 182 | public boolean isSummaryOfGroup(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame^] | 183 | if (!isGroupSummary(sbn)) { |
Selim Cinek | 263398f | 2015-10-21 17:40:23 -0700 | [diff] [blame] | 184 | return false; |
| 185 | } |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame^] | 186 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 263398f | 2015-10-21 17:40:23 -0700 | [diff] [blame] | 187 | if (group == null) { |
| 188 | return false; |
| 189 | } |
| 190 | return !group.children.isEmpty(); |
| 191 | } |
| 192 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 193 | public ExpandableNotificationRow getGroupSummary(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame^] | 194 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 195 | return group == null ? null |
| 196 | : group.summary == null ? null |
| 197 | : group.summary.row; |
| 198 | } |
| 199 | |
Selim Cinek | 83bc783 | 2015-10-22 13:26:54 -0700 | [diff] [blame] | 200 | public void toggleGroupExpansion(StatusBarNotification sbn) { |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame^] | 201 | NotificationGroup group = mGroupMap.get(getGroupKey(sbn)); |
Selim Cinek | 83bc783 | 2015-10-22 13:26:54 -0700 | [diff] [blame] | 202 | if (group == null) { |
| 203 | return; |
| 204 | } |
| 205 | setGroupExpanded(group, !group.expanded); |
| 206 | } |
| 207 | |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame^] | 208 | private boolean isIsolated(StatusBarNotification sbn) { |
| 209 | return mHeadsUpedEntries.contains(sbn.getKey()) && sbn.getNotification().isGroupChild(); |
| 210 | } |
| 211 | |
| 212 | private boolean isGroupSummary(StatusBarNotification sbn) { |
| 213 | if (isIsolated(sbn)) { |
| 214 | return true; |
| 215 | } |
| 216 | return sbn.getNotification().isGroupSummary(); |
| 217 | } |
| 218 | private boolean isGroupChild(StatusBarNotification sbn) { |
| 219 | if (isIsolated(sbn)) { |
| 220 | return false; |
| 221 | } |
| 222 | return sbn.getNotification().isGroupChild(); |
| 223 | } |
| 224 | |
| 225 | private String getGroupKey(StatusBarNotification sbn) { |
| 226 | if (isIsolated(sbn)) { |
| 227 | return sbn.getKey(); |
| 228 | } |
| 229 | return sbn.getGroupKey(); |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public void onHeadsUpPinnedModeChanged(boolean inPinnedMode) { |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public void onHeadsUpPinned(ExpandableNotificationRow headsUp) { |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public void onHeadsUpUnPinned(ExpandableNotificationRow headsUp) { |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public void onHeadsUpStateChanged(NotificationData.Entry entry, boolean isHeadsUp) { |
| 246 | final StatusBarNotification sbn = entry.notification; |
| 247 | if (entry.row.isHeadsUp()) { |
| 248 | if (!mHeadsUpedEntries.contains(sbn.getKey())) { |
| 249 | final boolean groupChild = sbn.getNotification().isGroupChild(); |
| 250 | if (groupChild) { |
| 251 | // We will be isolated now, so lets update the groups |
| 252 | onEntryRemovedInternal(entry, entry.notification); |
| 253 | } |
| 254 | mHeadsUpedEntries.add(sbn.getKey()); |
| 255 | if (groupChild) { |
| 256 | onEntryAdded(entry); |
| 257 | mListener.onChildIsolationChanged(); |
| 258 | } |
| 259 | } |
| 260 | } else { |
| 261 | if (mHeadsUpedEntries.contains(sbn.getKey())) { |
| 262 | boolean isolatedBefore = isIsolated(sbn); |
| 263 | if (isolatedBefore) { |
| 264 | // not isolated anymore, we need to update the groups |
| 265 | onEntryRemovedInternal(entry, entry.notification); |
| 266 | } |
| 267 | mHeadsUpedEntries.remove(sbn.getKey()); |
| 268 | if (isolatedBefore) { |
| 269 | onEntryAdded(entry); |
| 270 | mListener.onChildIsolationChanged(); |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 276 | public static class NotificationGroup { |
| 277 | public final HashSet<NotificationData.Entry> children = new HashSet<>(); |
| 278 | public NotificationData.Entry summary; |
| 279 | public boolean expanded; |
| 280 | } |
| 281 | |
| 282 | public interface OnGroupChangeListener { |
| 283 | /** |
| 284 | * The expansion of a group has changed. |
| 285 | * |
| 286 | * @param changedRow the row for which the expansion has changed, which is also the summary |
| 287 | * @param expanded a boolean indicating the new expanded state |
| 288 | */ |
| 289 | void onGroupExpansionChanged(ExpandableNotificationRow changedRow, boolean expanded); |
| 290 | |
| 291 | /** |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 292 | * A group of children just received a summary notification and should therefore become |
| 293 | * children of it. |
| 294 | * |
| 295 | * @param group the group created |
| 296 | */ |
| 297 | void onGroupCreatedFromChildren(NotificationGroup group); |
Selim Cinek | ef5127e | 2015-12-21 16:55:58 -0800 | [diff] [blame^] | 298 | |
| 299 | /** |
| 300 | * The isolation of a child has changed i.e it's group changes. |
| 301 | */ |
| 302 | void onChildIsolationChanged(); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 303 | } |
| 304 | } |