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