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