Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | package com.android.systemui.bubbles; |
| 17 | |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 18 | import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE; |
Issei Suzuki | a8d0731 | 2019-06-07 12:56:19 +0200 | [diff] [blame] | 19 | import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_BUBBLE_DATA; |
| 20 | import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES; |
| 21 | import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME; |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 22 | |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 23 | import static java.util.stream.Collectors.toList; |
| 24 | |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 25 | import android.app.Notification; |
| 26 | import android.app.PendingIntent; |
| 27 | import android.content.Context; |
Mark Renouf | bbcf07f | 2019-05-09 10:42:43 -0400 | [diff] [blame] | 28 | import android.service.notification.NotificationListenerService; |
| 29 | import android.service.notification.NotificationListenerService.RankingMap; |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 30 | import android.util.Log; |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 31 | import android.util.Pair; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 32 | |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 33 | import androidx.annotation.Nullable; |
| 34 | |
Selim Cinek | fdf8033 | 2019-03-07 17:29:55 -0800 | [diff] [blame] | 35 | import com.android.internal.annotations.VisibleForTesting; |
Mady Mellor | 3df7ab0 | 2019-12-09 15:07:10 -0800 | [diff] [blame] | 36 | import com.android.systemui.R; |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 37 | import com.android.systemui.bubbles.BubbleController.DismissReason; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 38 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
| 39 | |
Mady Mellor | 70cba7bb | 2019-07-02 15:06:07 -0700 | [diff] [blame] | 40 | import java.io.FileDescriptor; |
| 41 | import java.io.PrintWriter; |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 42 | import java.util.ArrayList; |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 43 | import java.util.Collections; |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 44 | import java.util.Comparator; |
| 45 | import java.util.HashMap; |
Mark Renouf | 3bc5b36 | 2019-04-05 14:37:59 -0400 | [diff] [blame] | 46 | import java.util.List; |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 47 | import java.util.Map; |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 48 | import java.util.Objects; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 49 | |
Mady Mellor | cfd06c1 | 2019-02-13 14:32:12 -0800 | [diff] [blame] | 50 | import javax.inject.Inject; |
| 51 | import javax.inject.Singleton; |
| 52 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 53 | /** |
| 54 | * Keeps track of active bubbles. |
| 55 | */ |
Mady Mellor | cfd06c1 | 2019-02-13 14:32:12 -0800 | [diff] [blame] | 56 | @Singleton |
Selim Cinek | fdf8033 | 2019-03-07 17:29:55 -0800 | [diff] [blame] | 57 | public class BubbleData { |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 58 | |
Issei Suzuki | a8d0731 | 2019-06-07 12:56:19 +0200 | [diff] [blame] | 59 | private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleData" : TAG_BUBBLES; |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 60 | |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 61 | private static final Comparator<Bubble> BUBBLES_BY_SORT_KEY_DESCENDING = |
| 62 | Comparator.comparing(BubbleData::sortKey).reversed(); |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 63 | |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 64 | private static final Comparator<Map.Entry<String, Long>> GROUPS_BY_MAX_SORT_KEY_DESCENDING = |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 65 | Comparator.<Map.Entry<String, Long>, Long>comparing(Map.Entry::getValue).reversed(); |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 66 | |
Mark Renouf | 82a40e6 | 2019-05-23 16:16:24 -0400 | [diff] [blame] | 67 | /** Contains information about changes that have been made to the state of bubbles. */ |
| 68 | static final class Update { |
| 69 | boolean expandedChanged; |
| 70 | boolean selectionChanged; |
| 71 | boolean orderChanged; |
| 72 | boolean expanded; |
| 73 | @Nullable Bubble selectedBubble; |
| 74 | @Nullable Bubble addedBubble; |
| 75 | @Nullable Bubble updatedBubble; |
| 76 | // Pair with Bubble and @DismissReason Integer |
| 77 | final List<Pair<Bubble, Integer>> removedBubbles = new ArrayList<>(); |
| 78 | |
| 79 | // A read-only view of the bubbles list, changes there will be reflected here. |
| 80 | final List<Bubble> bubbles; |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 81 | final List<Bubble> overflowBubbles; |
Mark Renouf | 82a40e6 | 2019-05-23 16:16:24 -0400 | [diff] [blame] | 82 | |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 83 | private Update(List<Bubble> row, List<Bubble> overflow) { |
| 84 | bubbles = Collections.unmodifiableList(row); |
| 85 | overflowBubbles = Collections.unmodifiableList(overflow); |
Mark Renouf | 82a40e6 | 2019-05-23 16:16:24 -0400 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | boolean anythingChanged() { |
| 89 | return expandedChanged |
| 90 | || selectionChanged |
| 91 | || addedBubble != null |
| 92 | || updatedBubble != null |
| 93 | || !removedBubbles.isEmpty() |
| 94 | || orderChanged; |
| 95 | } |
| 96 | |
| 97 | void bubbleRemoved(Bubble bubbleToRemove, @DismissReason int reason) { |
| 98 | removedBubbles.add(new Pair<>(bubbleToRemove, reason)); |
| 99 | } |
| 100 | } |
| 101 | |
Mark Renouf | 3bc5b36 | 2019-04-05 14:37:59 -0400 | [diff] [blame] | 102 | /** |
| 103 | * This interface reports changes to the state and appearance of bubbles which should be applied |
| 104 | * as necessary to the UI. |
Mark Renouf | 3bc5b36 | 2019-04-05 14:37:59 -0400 | [diff] [blame] | 105 | */ |
| 106 | interface Listener { |
Mark Renouf | 82a40e6 | 2019-05-23 16:16:24 -0400 | [diff] [blame] | 107 | /** Reports changes have have occurred as a result of the most recent operation. */ |
| 108 | void applyUpdate(Update update); |
Mark Renouf | 3bc5b36 | 2019-04-05 14:37:59 -0400 | [diff] [blame] | 109 | } |
| 110 | |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 111 | interface TimeSource { |
| 112 | long currentTimeMillis(); |
| 113 | } |
| 114 | |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 115 | private final Context mContext; |
Mady Mellor | 9ec3796 | 2020-01-10 10:21:03 -0800 | [diff] [blame] | 116 | /** Bubbles that are actively in the stack. */ |
Mark Renouf | 82a40e6 | 2019-05-23 16:16:24 -0400 | [diff] [blame] | 117 | private final List<Bubble> mBubbles; |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 118 | /** Bubbles that aged out to overflow. */ |
| 119 | private final List<Bubble> mOverflowBubbles; |
Mady Mellor | 9ec3796 | 2020-01-10 10:21:03 -0800 | [diff] [blame] | 120 | /** Bubbles that are being loaded but haven't been added to the stack just yet. */ |
| 121 | private final List<Bubble> mPendingBubbles; |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 122 | private Bubble mSelectedBubble; |
| 123 | private boolean mExpanded; |
Lyn Han | 3a7ce7a | 2019-12-10 18:16:35 -0800 | [diff] [blame] | 124 | private final int mMaxBubbles; |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 125 | private final int mMaxOverflowBubbles; |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 126 | |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 127 | // State tracked during an operation -- keeps track of what listener events to dispatch. |
Mark Renouf | 82a40e6 | 2019-05-23 16:16:24 -0400 | [diff] [blame] | 128 | private Update mStateChange; |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 129 | |
Mark Renouf | bbcf07f | 2019-05-09 10:42:43 -0400 | [diff] [blame] | 130 | private NotificationListenerService.Ranking mTmpRanking; |
| 131 | |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 132 | private TimeSource mTimeSource = System::currentTimeMillis; |
| 133 | |
| 134 | @Nullable |
Mark Renouf | 3bc5b36 | 2019-04-05 14:37:59 -0400 | [diff] [blame] | 135 | private Listener mListener; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 136 | |
Mady Mellor | f44b683 | 2020-01-14 13:26:14 -0800 | [diff] [blame] | 137 | @Nullable |
| 138 | private BubbleController.NotificationSuppressionChangedListener mSuppressionListener; |
| 139 | |
Mady Mellor | e28fe10 | 2019-07-09 15:33:32 -0700 | [diff] [blame] | 140 | /** |
| 141 | * We track groups with summaries that aren't visibly displayed but still kept around because |
| 142 | * the bubble(s) associated with the summary still exist. |
| 143 | * |
| 144 | * The summary must be kept around so that developers can cancel it (and hence the bubbles |
| 145 | * associated with it). This list is used to check if the summary should be hidden from the |
| 146 | * shade. |
| 147 | * |
| 148 | * Key: group key of the NotificationEntry |
| 149 | * Value: key of the NotificationEntry |
| 150 | */ |
| 151 | private HashMap<String, String> mSuppressedGroupKeys = new HashMap<>(); |
| 152 | |
Mady Mellor | cfd06c1 | 2019-02-13 14:32:12 -0800 | [diff] [blame] | 153 | @Inject |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 154 | public BubbleData(Context context) { |
| 155 | mContext = context; |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 156 | mBubbles = new ArrayList<>(); |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 157 | mOverflowBubbles = new ArrayList<>(); |
Mady Mellor | 9ec3796 | 2020-01-10 10:21:03 -0800 | [diff] [blame] | 158 | mPendingBubbles = new ArrayList<>(); |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 159 | mStateChange = new Update(mBubbles, mOverflowBubbles); |
Lyn Han | 3a7ce7a | 2019-12-10 18:16:35 -0800 | [diff] [blame] | 160 | mMaxBubbles = mContext.getResources().getInteger(R.integer.bubbles_max_rendered); |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 161 | mMaxOverflowBubbles = mContext.getResources().getInteger(R.integer.bubbles_max_overflow); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Mady Mellor | f44b683 | 2020-01-14 13:26:14 -0800 | [diff] [blame] | 164 | public void setSuppressionChangedListener( |
| 165 | BubbleController.NotificationSuppressionChangedListener listener) { |
| 166 | mSuppressionListener = listener; |
| 167 | } |
| 168 | |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 169 | public boolean hasBubbles() { |
| 170 | return !mBubbles.isEmpty(); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 173 | public boolean isExpanded() { |
| 174 | return mExpanded; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 177 | public boolean hasBubbleWithKey(String key) { |
| 178 | return getBubbleWithKey(key) != null; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 181 | @Nullable |
| 182 | public Bubble getSelectedBubble() { |
| 183 | return mSelectedBubble; |
| 184 | } |
| 185 | |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 186 | public void setExpanded(boolean expanded) { |
Issei Suzuki | a8d0731 | 2019-06-07 12:56:19 +0200 | [diff] [blame] | 187 | if (DEBUG_BUBBLE_DATA) { |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 188 | Log.d(TAG, "setExpanded: " + expanded); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 189 | } |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 190 | setExpandedInternal(expanded); |
| 191 | dispatchPendingChanges(); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 194 | public void setSelectedBubble(Bubble bubble) { |
Issei Suzuki | a8d0731 | 2019-06-07 12:56:19 +0200 | [diff] [blame] | 195 | if (DEBUG_BUBBLE_DATA) { |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 196 | Log.d(TAG, "setSelectedBubble: " + bubble); |
| 197 | } |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 198 | setSelectedBubbleInternal(bubble); |
| 199 | dispatchPendingChanges(); |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 200 | } |
| 201 | |
Lyn Han | 1e19d7f | 2020-02-05 19:10:58 -0800 | [diff] [blame] | 202 | public void promoteBubbleFromOverflow(Bubble bubble, BubbleStackView stack, |
| 203 | BubbleIconFactory factory) { |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 204 | if (DEBUG_BUBBLE_DATA) { |
| 205 | Log.d(TAG, "promoteBubbleFromOverflow: " + bubble); |
| 206 | } |
Lyn Han | 1e19d7f | 2020-02-05 19:10:58 -0800 | [diff] [blame] | 207 | |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 208 | // Preserve new order for next repack, which sorts by last updated time. |
| 209 | bubble.markUpdatedAt(mTimeSource.currentTimeMillis()); |
Lyn Han | 1e19d7f | 2020-02-05 19:10:58 -0800 | [diff] [blame] | 210 | mOverflowBubbles.remove(bubble); |
Lyn Han | 1e19d7f | 2020-02-05 19:10:58 -0800 | [diff] [blame] | 211 | bubble.inflate( |
Lyn Han | 4438d9b | 2020-03-11 18:04:35 -0700 | [diff] [blame^] | 212 | b -> { |
| 213 | notificationEntryUpdated(bubble, /* suppressFlyout */ |
| 214 | false, /* showInShade */ true); |
| 215 | setSelectedBubbleInternal(bubble); |
| 216 | }, |
Lyn Han | 1e19d7f | 2020-02-05 19:10:58 -0800 | [diff] [blame] | 217 | mContext, stack, factory); |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 218 | dispatchPendingChanges(); |
| 219 | } |
| 220 | |
Mady Mellor | 3df7ab0 | 2019-12-09 15:07:10 -0800 | [diff] [blame] | 221 | /** |
| 222 | * Constructs a new bubble or returns an existing one. Does not add new bubbles to |
| 223 | * bubble data, must go through {@link #notificationEntryUpdated(Bubble, boolean, boolean)} |
| 224 | * for that. |
| 225 | */ |
| 226 | Bubble getOrCreateBubble(NotificationEntry entry) { |
| 227 | Bubble bubble = getBubbleWithKey(entry.getKey()); |
| 228 | if (bubble == null) { |
Mady Mellor | 9ec3796 | 2020-01-10 10:21:03 -0800 | [diff] [blame] | 229 | // Check for it in pending |
| 230 | for (int i = 0; i < mPendingBubbles.size(); i++) { |
| 231 | Bubble b = mPendingBubbles.get(i); |
| 232 | if (b.getKey().equals(entry.getKey())) { |
| 233 | return b; |
| 234 | } |
| 235 | } |
Mady Mellor | f44b683 | 2020-01-14 13:26:14 -0800 | [diff] [blame] | 236 | bubble = new Bubble(entry, mSuppressionListener); |
Mady Mellor | 9ec3796 | 2020-01-10 10:21:03 -0800 | [diff] [blame] | 237 | mPendingBubbles.add(bubble); |
Mady Mellor | 3df7ab0 | 2019-12-09 15:07:10 -0800 | [diff] [blame] | 238 | } else { |
| 239 | bubble.setEntry(entry); |
| 240 | } |
| 241 | return bubble; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * When this method is called it is expected that all info in the bubble has completed loading. |
| 246 | * @see Bubble#inflate(BubbleViewInfoTask.Callback, Context, |
| 247 | * BubbleStackView, BubbleIconFactory). |
| 248 | */ |
| 249 | void notificationEntryUpdated(Bubble bubble, boolean suppressFlyout, boolean showInShade) { |
Issei Suzuki | a8d0731 | 2019-06-07 12:56:19 +0200 | [diff] [blame] | 250 | if (DEBUG_BUBBLE_DATA) { |
Mady Mellor | 3df7ab0 | 2019-12-09 15:07:10 -0800 | [diff] [blame] | 251 | Log.d(TAG, "notificationEntryUpdated: " + bubble); |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 252 | } |
Mady Mellor | 9ec3796 | 2020-01-10 10:21:03 -0800 | [diff] [blame] | 253 | mPendingBubbles.remove(bubble); // No longer pending once we're here |
Mady Mellor | 3df7ab0 | 2019-12-09 15:07:10 -0800 | [diff] [blame] | 254 | Bubble prevBubble = getBubbleWithKey(bubble.getKey()); |
Mady Mellor | 8e97a96 | 2020-01-21 16:20:15 -0800 | [diff] [blame] | 255 | suppressFlyout |= !bubble.getEntry().getRanking().visuallyInterruptive(); |
Lyn Han | 405e0b7 | 2019-08-13 16:07:55 -0700 | [diff] [blame] | 256 | |
Mady Mellor | 3df7ab0 | 2019-12-09 15:07:10 -0800 | [diff] [blame] | 257 | if (prevBubble == null) { |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 258 | // Create a new bubble |
Mark Renouf | c19b473 | 2019-06-26 12:08:33 -0400 | [diff] [blame] | 259 | bubble.setSuppressFlyout(suppressFlyout); |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 260 | doAdd(bubble); |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 261 | trim(); |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 262 | } else { |
| 263 | // Updates an existing bubble |
Lyn Han | 405e0b7 | 2019-08-13 16:07:55 -0700 | [diff] [blame] | 264 | bubble.setSuppressFlyout(suppressFlyout); |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 265 | doUpdate(bubble); |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 266 | } |
Mady Mellor | 70cba7bb | 2019-07-02 15:06:07 -0700 | [diff] [blame] | 267 | if (bubble.shouldAutoExpand()) { |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 268 | setSelectedBubbleInternal(bubble); |
| 269 | if (!mExpanded) { |
| 270 | setExpandedInternal(true); |
| 271 | } |
| 272 | } else if (mSelectedBubble == null) { |
| 273 | setSelectedBubbleInternal(bubble); |
| 274 | } |
Mady Mellor | 3df7ab0 | 2019-12-09 15:07:10 -0800 | [diff] [blame] | 275 | |
Mady Mellor | f44b683 | 2020-01-14 13:26:14 -0800 | [diff] [blame] | 276 | boolean isBubbleExpandedAndSelected = mExpanded && mSelectedBubble == bubble; |
| 277 | boolean suppress = isBubbleExpandedAndSelected || !showInShade || !bubble.showInShade(); |
| 278 | bubble.setSuppressNotification(suppress); |
| 279 | bubble.setShowDot(!isBubbleExpandedAndSelected /* show */, true /* animate */); |
| 280 | |
| 281 | dispatchPendingChanges(); |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | public void notificationEntryRemoved(NotificationEntry entry, @DismissReason int reason) { |
Issei Suzuki | a8d0731 | 2019-06-07 12:56:19 +0200 | [diff] [blame] | 285 | if (DEBUG_BUBBLE_DATA) { |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 286 | Log.d(TAG, "notificationEntryRemoved: entry=" + entry + " reason=" + reason); |
| 287 | } |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 288 | doRemove(entry.getKey(), reason); |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 289 | dispatchPendingChanges(); |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 290 | } |
| 291 | |
Mark Renouf | bbcf07f | 2019-05-09 10:42:43 -0400 | [diff] [blame] | 292 | /** |
| 293 | * Called when NotificationListener has received adjusted notification rank and reapplied |
| 294 | * filtering and sorting. This is used to dismiss any bubbles which should no longer be shown |
| 295 | * due to changes in permissions on the notification channel or the global setting. |
| 296 | * |
| 297 | * @param rankingMap the updated ranking map from NotificationListenerService |
| 298 | */ |
| 299 | public void notificationRankingUpdated(RankingMap rankingMap) { |
| 300 | if (mTmpRanking == null) { |
| 301 | mTmpRanking = new NotificationListenerService.Ranking(); |
| 302 | } |
| 303 | |
| 304 | String[] orderedKeys = rankingMap.getOrderedKeys(); |
| 305 | for (int i = 0; i < orderedKeys.length; i++) { |
| 306 | String key = orderedKeys[i]; |
| 307 | if (hasBubbleWithKey(key)) { |
| 308 | rankingMap.getRanking(key, mTmpRanking); |
| 309 | if (!mTmpRanking.canBubble()) { |
| 310 | doRemove(key, BubbleController.DISMISS_BLOCKED); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | dispatchPendingChanges(); |
| 315 | } |
| 316 | |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 317 | /** |
Mady Mellor | e28fe10 | 2019-07-09 15:33:32 -0700 | [diff] [blame] | 318 | * Adds a group key indicating that the summary for this group should be suppressed. |
| 319 | * |
| 320 | * @param groupKey the group key of the group whose summary should be suppressed. |
| 321 | * @param notifKey the notification entry key of that summary. |
| 322 | */ |
| 323 | void addSummaryToSuppress(String groupKey, String notifKey) { |
| 324 | mSuppressedGroupKeys.put(groupKey, notifKey); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Retrieves the notif entry key of the summary associated with the provided group key. |
| 329 | * |
| 330 | * @param groupKey the group to look up |
| 331 | * @return the key for the {@link NotificationEntry} that is the summary of this group. |
| 332 | */ |
| 333 | String getSummaryKey(String groupKey) { |
| 334 | return mSuppressedGroupKeys.get(groupKey); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Removes a group key indicating that summary for this group should no longer be suppressed. |
| 339 | */ |
| 340 | void removeSuppressedSummary(String groupKey) { |
| 341 | mSuppressedGroupKeys.remove(groupKey); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Whether the summary for the provided group key is suppressed. |
| 346 | */ |
| 347 | boolean isSummarySuppressed(String groupKey) { |
| 348 | return mSuppressedGroupKeys.containsKey(groupKey); |
| 349 | } |
| 350 | |
| 351 | /** |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 352 | * Retrieves any bubbles that are part of the notification group represented by the provided |
| 353 | * group key. |
| 354 | */ |
| 355 | ArrayList<Bubble> getBubblesInGroup(@Nullable String groupKey) { |
| 356 | ArrayList<Bubble> bubbleChildren = new ArrayList<>(); |
| 357 | if (groupKey == null) { |
| 358 | return bubbleChildren; |
| 359 | } |
| 360 | for (Bubble b : mBubbles) { |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 361 | if (groupKey.equals(b.getEntry().getSbn().getGroupKey())) { |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 362 | bubbleChildren.add(b); |
| 363 | } |
| 364 | } |
| 365 | return bubbleChildren; |
| 366 | } |
| 367 | |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 368 | private void doAdd(Bubble bubble) { |
Issei Suzuki | a8d0731 | 2019-06-07 12:56:19 +0200 | [diff] [blame] | 369 | if (DEBUG_BUBBLE_DATA) { |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 370 | Log.d(TAG, "doAdd: " + bubble); |
| 371 | } |
| 372 | int minInsertPoint = 0; |
| 373 | boolean newGroup = !hasBubbleWithGroupId(bubble.getGroupId()); |
| 374 | if (isExpanded()) { |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 375 | // first bubble of a group goes to the beginning, otherwise within the existing group |
| 376 | minInsertPoint = newGroup ? 0 : findFirstIndexForGroup(bubble.getGroupId()); |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 377 | } |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 378 | if (insertBubble(minInsertPoint, bubble) < mBubbles.size() - 1) { |
Mark Renouf | 82a40e6 | 2019-05-23 16:16:24 -0400 | [diff] [blame] | 379 | mStateChange.orderChanged = true; |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 380 | } |
Mark Renouf | 82a40e6 | 2019-05-23 16:16:24 -0400 | [diff] [blame] | 381 | mStateChange.addedBubble = bubble; |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 382 | |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 383 | if (!isExpanded()) { |
Mark Renouf | 82a40e6 | 2019-05-23 16:16:24 -0400 | [diff] [blame] | 384 | mStateChange.orderChanged |= packGroup(findFirstIndexForGroup(bubble.getGroupId())); |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 385 | // Top bubble becomes selected. |
| 386 | setSelectedBubbleInternal(mBubbles.get(0)); |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 387 | } |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | private void trim() { |
Lyn Han | 3a7ce7a | 2019-12-10 18:16:35 -0800 | [diff] [blame] | 391 | if (mBubbles.size() > mMaxBubbles) { |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 392 | mBubbles.stream() |
| 393 | // sort oldest first (ascending lastActivity) |
| 394 | .sorted(Comparator.comparingLong(Bubble::getLastActivity)) |
| 395 | // skip the selected bubble |
| 396 | .filter((b) -> !b.equals(mSelectedBubble)) |
| 397 | .findFirst() |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 398 | .ifPresent((b) -> doRemove(b.getKey(), BubbleController.DISMISS_AGED)); |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
| 402 | private void doUpdate(Bubble bubble) { |
Issei Suzuki | a8d0731 | 2019-06-07 12:56:19 +0200 | [diff] [blame] | 403 | if (DEBUG_BUBBLE_DATA) { |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 404 | Log.d(TAG, "doUpdate: " + bubble); |
| 405 | } |
Mark Renouf | 82a40e6 | 2019-05-23 16:16:24 -0400 | [diff] [blame] | 406 | mStateChange.updatedBubble = bubble; |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 407 | if (!isExpanded()) { |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 408 | // while collapsed, update causes re-pack |
| 409 | int prevPos = mBubbles.indexOf(bubble); |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 410 | mBubbles.remove(bubble); |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 411 | int newPos = insertBubble(0, bubble); |
| 412 | if (prevPos != newPos) { |
| 413 | packGroup(newPos); |
Mark Renouf | 82a40e6 | 2019-05-23 16:16:24 -0400 | [diff] [blame] | 414 | mStateChange.orderChanged = true; |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 415 | } |
| 416 | setSelectedBubbleInternal(mBubbles.get(0)); |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 417 | } |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 418 | } |
| 419 | |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 420 | private void doRemove(String key, @DismissReason int reason) { |
Mady Mellor | 9ec3796 | 2020-01-10 10:21:03 -0800 | [diff] [blame] | 421 | // If it was pending remove it |
| 422 | for (int i = 0; i < mPendingBubbles.size(); i++) { |
| 423 | if (mPendingBubbles.get(i).getKey().equals(key)) { |
| 424 | mPendingBubbles.remove(mPendingBubbles.get(i)); |
| 425 | } |
| 426 | } |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 427 | int indexToRemove = indexForKey(key); |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 428 | if (indexToRemove == -1) { |
| 429 | return; |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 430 | } |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 431 | Bubble bubbleToRemove = mBubbles.get(indexToRemove); |
| 432 | if (mBubbles.size() == 1) { |
| 433 | // Going to become empty, handle specially. |
| 434 | setExpandedInternal(false); |
| 435 | setSelectedBubbleInternal(null); |
| 436 | } |
| 437 | if (indexToRemove < mBubbles.size() - 1) { |
| 438 | // Removing anything but the last bubble means positions will change. |
Mark Renouf | 82a40e6 | 2019-05-23 16:16:24 -0400 | [diff] [blame] | 439 | mStateChange.orderChanged = true; |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 440 | } |
| 441 | mBubbles.remove(indexToRemove); |
Mark Renouf | 82a40e6 | 2019-05-23 16:16:24 -0400 | [diff] [blame] | 442 | mStateChange.bubbleRemoved(bubbleToRemove, reason); |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 443 | if (!isExpanded()) { |
Mark Renouf | 82a40e6 | 2019-05-23 16:16:24 -0400 | [diff] [blame] | 444 | mStateChange.orderChanged |= repackAll(); |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 445 | } |
| 446 | |
Lyn Han | d698186 | 2020-02-18 18:01:43 -0800 | [diff] [blame] | 447 | overflowBubble(reason, bubbleToRemove); |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 448 | |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 449 | // Note: If mBubbles.isEmpty(), then mSelectedBubble is now null. |
| 450 | if (Objects.equals(mSelectedBubble, bubbleToRemove)) { |
| 451 | // Move selection to the new bubble at the same position. |
| 452 | int newIndex = Math.min(indexToRemove, mBubbles.size() - 1); |
| 453 | Bubble newSelected = mBubbles.get(newIndex); |
| 454 | setSelectedBubbleInternal(newSelected); |
| 455 | } |
Mady Mellor | ed99c27 | 2019-06-13 15:58:30 -0700 | [diff] [blame] | 456 | maybeSendDeleteIntent(reason, bubbleToRemove.getEntry()); |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 457 | } |
| 458 | |
Lyn Han | d698186 | 2020-02-18 18:01:43 -0800 | [diff] [blame] | 459 | void overflowBubble(@DismissReason int reason, Bubble bubble) { |
| 460 | if (reason == BubbleController.DISMISS_AGED |
| 461 | || reason == BubbleController.DISMISS_USER_GESTURE) { |
| 462 | if (DEBUG_BUBBLE_DATA) { |
| 463 | Log.d(TAG, "overflowing bubble: " + bubble); |
| 464 | } |
| 465 | mOverflowBubbles.add(0, bubble); |
| 466 | |
| 467 | if (mOverflowBubbles.size() == mMaxOverflowBubbles + 1) { |
| 468 | // Remove oldest bubble. |
| 469 | if (DEBUG_BUBBLE_DATA) { |
| 470 | Log.d(TAG, "Overflow full. Remove bubble: " + mOverflowBubbles.get( |
| 471 | mOverflowBubbles.size() - 1)); |
| 472 | } |
| 473 | mOverflowBubbles.remove(mOverflowBubbles.size() - 1); |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 478 | public void dismissAll(@DismissReason int reason) { |
Issei Suzuki | a8d0731 | 2019-06-07 12:56:19 +0200 | [diff] [blame] | 479 | if (DEBUG_BUBBLE_DATA) { |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 480 | Log.d(TAG, "dismissAll: reason=" + reason); |
| 481 | } |
| 482 | if (mBubbles.isEmpty()) { |
| 483 | return; |
| 484 | } |
| 485 | setExpandedInternal(false); |
| 486 | setSelectedBubbleInternal(null); |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 487 | while (!mBubbles.isEmpty()) { |
Lyn Han | d698186 | 2020-02-18 18:01:43 -0800 | [diff] [blame] | 488 | doRemove(mBubbles.get(0).getKey(), reason); |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 489 | } |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 490 | dispatchPendingChanges(); |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 491 | } |
| 492 | |
Mady Mellor | ca184aae | 2019-09-17 16:07:12 -0700 | [diff] [blame] | 493 | /** |
| 494 | * Indicates that the provided display is no longer in use and should be cleaned up. |
| 495 | * |
| 496 | * @param displayId the id of the display to clean up. |
| 497 | */ |
| 498 | void notifyDisplayEmpty(int displayId) { |
| 499 | for (Bubble b : mBubbles) { |
| 500 | if (b.getDisplayId() == displayId) { |
| 501 | if (b.getExpandedView() != null) { |
| 502 | b.getExpandedView().notifyDisplayEmpty(); |
| 503 | } |
| 504 | return; |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 509 | private void dispatchPendingChanges() { |
Mark Renouf | 82a40e6 | 2019-05-23 16:16:24 -0400 | [diff] [blame] | 510 | if (mListener != null && mStateChange.anythingChanged()) { |
| 511 | mListener.applyUpdate(mStateChange); |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 512 | } |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 513 | mStateChange = new Update(mBubbles, mOverflowBubbles); |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | /** |
Mark Renouf | 82a40e6 | 2019-05-23 16:16:24 -0400 | [diff] [blame] | 517 | * Requests a change to the selected bubble. |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 518 | * |
| 519 | * @param bubble the new selected bubble |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 520 | */ |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 521 | private void setSelectedBubbleInternal(@Nullable Bubble bubble) { |
Issei Suzuki | a8d0731 | 2019-06-07 12:56:19 +0200 | [diff] [blame] | 522 | if (DEBUG_BUBBLE_DATA) { |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 523 | Log.d(TAG, "setSelectedBubbleInternal: " + bubble); |
| 524 | } |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 525 | if (Objects.equals(bubble, mSelectedBubble)) { |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 526 | return; |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 527 | } |
Lyn Han | 1e19d7f | 2020-02-05 19:10:58 -0800 | [diff] [blame] | 528 | if (bubble != null && !mBubbles.contains(bubble) && !mOverflowBubbles.contains(bubble)) { |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 529 | Log.e(TAG, "Cannot select bubble which doesn't exist!" |
| 530 | + " (" + bubble + ") bubbles=" + mBubbles); |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 531 | return; |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 532 | } |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 533 | if (mExpanded && bubble != null) { |
Mady Mellor | 73310bc1 | 2019-05-01 20:47:49 -0700 | [diff] [blame] | 534 | bubble.markAsAccessedAt(mTimeSource.currentTimeMillis()); |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 535 | } |
| 536 | mSelectedBubble = bubble; |
Mark Renouf | 82a40e6 | 2019-05-23 16:16:24 -0400 | [diff] [blame] | 537 | mStateChange.selectedBubble = bubble; |
| 538 | mStateChange.selectionChanged = true; |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 539 | } |
| 540 | |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 541 | /** |
Mark Renouf | 82a40e6 | 2019-05-23 16:16:24 -0400 | [diff] [blame] | 542 | * Requests a change to the expanded state. |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 543 | * |
| 544 | * @param shouldExpand the new requested state |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 545 | */ |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 546 | private void setExpandedInternal(boolean shouldExpand) { |
Issei Suzuki | a8d0731 | 2019-06-07 12:56:19 +0200 | [diff] [blame] | 547 | if (DEBUG_BUBBLE_DATA) { |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 548 | Log.d(TAG, "setExpandedInternal: shouldExpand=" + shouldExpand); |
| 549 | } |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 550 | if (mExpanded == shouldExpand) { |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 551 | return; |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 552 | } |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 553 | if (shouldExpand) { |
| 554 | if (mBubbles.isEmpty()) { |
| 555 | Log.e(TAG, "Attempt to expand stack when empty!"); |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 556 | return; |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 557 | } |
| 558 | if (mSelectedBubble == null) { |
| 559 | Log.e(TAG, "Attempt to expand stack without selected bubble!"); |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 560 | return; |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 561 | } |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 562 | mSelectedBubble.markAsAccessedAt(mTimeSource.currentTimeMillis()); |
Mark Renouf | 82a40e6 | 2019-05-23 16:16:24 -0400 | [diff] [blame] | 563 | mStateChange.orderChanged |= repackAll(); |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 564 | } else if (!mBubbles.isEmpty()) { |
| 565 | // Apply ordering and grouping rules from expanded -> collapsed, then save |
| 566 | // the result. |
Mark Renouf | 82a40e6 | 2019-05-23 16:16:24 -0400 | [diff] [blame] | 567 | mStateChange.orderChanged |= repackAll(); |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 568 | // Save the state which should be returned to when expanded (with no other changes) |
| 569 | |
| 570 | if (mBubbles.indexOf(mSelectedBubble) > 0) { |
| 571 | // Move the selected bubble to the top while collapsed. |
| 572 | if (!mSelectedBubble.isOngoing() && mBubbles.get(0).isOngoing()) { |
| 573 | // The selected bubble cannot be raised to the first position because |
| 574 | // there is an ongoing bubble there. Instead, force the top ongoing bubble |
| 575 | // to become selected. |
| 576 | setSelectedBubbleInternal(mBubbles.get(0)); |
| 577 | } else { |
| 578 | // Raise the selected bubble (and it's group) up to the front so the selected |
| 579 | // bubble remains on top. |
| 580 | mBubbles.remove(mSelectedBubble); |
| 581 | mBubbles.add(0, mSelectedBubble); |
Mark Renouf | 6e4ddd7 | 2019-07-16 13:28:43 -0400 | [diff] [blame] | 582 | mStateChange.orderChanged |= packGroup(0); |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 583 | } |
| 584 | } |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 585 | } |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 586 | mExpanded = shouldExpand; |
Mark Renouf | 82a40e6 | 2019-05-23 16:16:24 -0400 | [diff] [blame] | 587 | mStateChange.expanded = shouldExpand; |
| 588 | mStateChange.expandedChanged = true; |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 589 | } |
| 590 | |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 591 | private static long sortKey(Bubble bubble) { |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 592 | long key = bubble.getLastUpdateTime(); |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 593 | if (bubble.isOngoing()) { |
| 594 | // Set 2nd highest bit (signed long int), to partition between ongoing and regular |
| 595 | key |= 0x4000000000000000L; |
| 596 | } |
| 597 | return key; |
| 598 | } |
| 599 | |
| 600 | /** |
| 601 | * Locates and inserts the bubble into a sorted position. The is inserted |
| 602 | * based on sort key, groupId is not considered. A call to {@link #packGroup(int)} may be |
| 603 | * required to keep grouping intact. |
| 604 | * |
| 605 | * @param minPosition the first insert point to consider |
Lyn Han | 72f5090 | 2019-10-25 15:55:49 -0700 | [diff] [blame] | 606 | * @param newBubble the bubble to insert |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 607 | * @return the position where the bubble was inserted |
| 608 | */ |
| 609 | private int insertBubble(int minPosition, Bubble newBubble) { |
| 610 | long newBubbleSortKey = sortKey(newBubble); |
| 611 | String previousGroupId = null; |
| 612 | |
| 613 | for (int pos = minPosition; pos < mBubbles.size(); pos++) { |
| 614 | Bubble bubbleAtPos = mBubbles.get(pos); |
| 615 | String groupIdAtPos = bubbleAtPos.getGroupId(); |
| 616 | boolean atStartOfGroup = !groupIdAtPos.equals(previousGroupId); |
| 617 | |
| 618 | if (atStartOfGroup && newBubbleSortKey > sortKey(bubbleAtPos)) { |
| 619 | // Insert before the start of first group which has older bubbles. |
| 620 | mBubbles.add(pos, newBubble); |
| 621 | return pos; |
| 622 | } |
| 623 | previousGroupId = groupIdAtPos; |
| 624 | } |
| 625 | mBubbles.add(newBubble); |
| 626 | return mBubbles.size() - 1; |
| 627 | } |
| 628 | |
| 629 | private boolean hasBubbleWithGroupId(String groupId) { |
| 630 | return mBubbles.stream().anyMatch(b -> b.getGroupId().equals(groupId)); |
| 631 | } |
| 632 | |
| 633 | private int findFirstIndexForGroup(String appId) { |
| 634 | for (int i = 0; i < mBubbles.size(); i++) { |
| 635 | Bubble bubbleAtPos = mBubbles.get(i); |
| 636 | if (bubbleAtPos.getGroupId().equals(appId)) { |
| 637 | return i; |
| 638 | } |
| 639 | } |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * Starting at the given position, moves all bubbles with the same group id to follow. Bubbles |
| 645 | * at positions lower than {@code position} are unchanged. Relative order within the group |
| 646 | * unchanged. Relative order of any other bubbles are also unchanged. |
| 647 | * |
| 648 | * @param position the position of the first bubble for the group |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 649 | * @return true if the position of any bubbles has changed as a result |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 650 | */ |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 651 | private boolean packGroup(int position) { |
Issei Suzuki | a8d0731 | 2019-06-07 12:56:19 +0200 | [diff] [blame] | 652 | if (DEBUG_BUBBLE_DATA) { |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 653 | Log.d(TAG, "packGroup: position=" + position); |
| 654 | } |
| 655 | Bubble groupStart = mBubbles.get(position); |
| 656 | final String groupAppId = groupStart.getGroupId(); |
| 657 | List<Bubble> moving = new ArrayList<>(); |
| 658 | |
| 659 | // Walk backward, collect bubbles within the group |
| 660 | for (int i = mBubbles.size() - 1; i > position; i--) { |
| 661 | if (mBubbles.get(i).getGroupId().equals(groupAppId)) { |
| 662 | moving.add(0, mBubbles.get(i)); |
| 663 | } |
| 664 | } |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 665 | if (moving.isEmpty()) { |
| 666 | return false; |
| 667 | } |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 668 | mBubbles.removeAll(moving); |
| 669 | mBubbles.addAll(position + 1, moving); |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 670 | return true; |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 671 | } |
| 672 | |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 673 | /** |
| 674 | * This applies a full sort and group pass to all existing bubbles. The bubbles are grouped |
| 675 | * by groupId. Each group is then sorted by the max(lastUpdated) time of it's bubbles. Bubbles |
| 676 | * within each group are then sorted by lastUpdated descending. |
| 677 | * |
| 678 | * @return true if the position of any bubbles changed as a result |
| 679 | */ |
| 680 | private boolean repackAll() { |
Issei Suzuki | a8d0731 | 2019-06-07 12:56:19 +0200 | [diff] [blame] | 681 | if (DEBUG_BUBBLE_DATA) { |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 682 | Log.d(TAG, "repackAll()"); |
| 683 | } |
| 684 | if (mBubbles.isEmpty()) { |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 685 | return false; |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 686 | } |
| 687 | Map<String, Long> groupLastActivity = new HashMap<>(); |
| 688 | for (Bubble bubble : mBubbles) { |
| 689 | long maxSortKeyForGroup = groupLastActivity.getOrDefault(bubble.getGroupId(), 0L); |
| 690 | long sortKeyForBubble = sortKey(bubble); |
| 691 | if (sortKeyForBubble > maxSortKeyForGroup) { |
| 692 | groupLastActivity.put(bubble.getGroupId(), sortKeyForBubble); |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | // Sort groups by their most recently active bubble |
| 697 | List<String> groupsByMostRecentActivity = |
| 698 | groupLastActivity.entrySet().stream() |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 699 | .sorted(GROUPS_BY_MAX_SORT_KEY_DESCENDING) |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 700 | .map(Map.Entry::getKey) |
| 701 | .collect(toList()); |
| 702 | |
| 703 | List<Bubble> repacked = new ArrayList<>(mBubbles.size()); |
| 704 | |
| 705 | // For each group, add bubbles, freshest to oldest |
| 706 | for (String appId : groupsByMostRecentActivity) { |
| 707 | mBubbles.stream() |
| 708 | .filter((b) -> b.getGroupId().equals(appId)) |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 709 | .sorted(BUBBLES_BY_SORT_KEY_DESCENDING) |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 710 | .forEachOrdered(repacked::add); |
| 711 | } |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 712 | if (repacked.equals(mBubbles)) { |
| 713 | return false; |
| 714 | } |
Mark Renouf | 82a40e6 | 2019-05-23 16:16:24 -0400 | [diff] [blame] | 715 | mBubbles.clear(); |
| 716 | mBubbles.addAll(repacked); |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 717 | return true; |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 718 | } |
| 719 | |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 720 | private void maybeSendDeleteIntent(@DismissReason int reason, NotificationEntry entry) { |
| 721 | if (reason == BubbleController.DISMISS_USER_GESTURE) { |
| 722 | Notification.BubbleMetadata bubbleMetadata = entry.getBubbleMetadata(); |
| 723 | PendingIntent deleteIntent = bubbleMetadata != null |
| 724 | ? bubbleMetadata.getDeleteIntent() |
| 725 | : null; |
| 726 | if (deleteIntent != null) { |
| 727 | try { |
| 728 | deleteIntent.send(); |
| 729 | } catch (PendingIntent.CanceledException e) { |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 730 | Log.w(TAG, "Failed to send delete intent for bubble with key: " |
| 731 | + entry.getKey()); |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | } |
| 735 | } |
| 736 | |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 737 | private int indexForKey(String key) { |
| 738 | for (int i = 0; i < mBubbles.size(); i++) { |
| 739 | Bubble bubble = mBubbles.get(i); |
| 740 | if (bubble.getKey().equals(key)) { |
| 741 | return i; |
| 742 | } |
| 743 | } |
| 744 | return -1; |
| 745 | } |
| 746 | |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 747 | /** |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 748 | * The set of bubbles in row. |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 749 | */ |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 750 | @VisibleForTesting(visibility = PRIVATE) |
| 751 | public List<Bubble> getBubbles() { |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 752 | return Collections.unmodifiableList(mBubbles); |
| 753 | } |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 754 | /** |
| 755 | * The set of bubbles in overflow. |
| 756 | */ |
| 757 | @VisibleForTesting(visibility = PRIVATE) |
| 758 | public List<Bubble> getOverflowBubbles() { |
| 759 | return Collections.unmodifiableList(mOverflowBubbles); |
| 760 | } |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 761 | |
| 762 | @VisibleForTesting(visibility = PRIVATE) |
| 763 | Bubble getBubbleWithKey(String key) { |
| 764 | for (int i = 0; i < mBubbles.size(); i++) { |
| 765 | Bubble bubble = mBubbles.get(i); |
| 766 | if (bubble.getKey().equals(key)) { |
| 767 | return bubble; |
| 768 | } |
| 769 | } |
| 770 | return null; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 771 | } |
Mark Renouf | 3bc5b36 | 2019-04-05 14:37:59 -0400 | [diff] [blame] | 772 | |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 773 | @VisibleForTesting(visibility = PRIVATE) |
| 774 | void setTimeSource(TimeSource timeSource) { |
| 775 | mTimeSource = timeSource; |
| 776 | } |
| 777 | |
Mark Renouf | 3bc5b36 | 2019-04-05 14:37:59 -0400 | [diff] [blame] | 778 | public void setListener(Listener listener) { |
| 779 | mListener = listener; |
| 780 | } |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 781 | |
Mady Mellor | 70cba7bb | 2019-07-02 15:06:07 -0700 | [diff] [blame] | 782 | /** |
| 783 | * Description of current bubble data state. |
| 784 | */ |
| 785 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Lyn Han | 72f5090 | 2019-10-25 15:55:49 -0700 | [diff] [blame] | 786 | pw.print("selected: "); |
| 787 | pw.println(mSelectedBubble != null |
Mady Mellor | 70cba7bb | 2019-07-02 15:06:07 -0700 | [diff] [blame] | 788 | ? mSelectedBubble.getKey() |
| 789 | : "null"); |
Lyn Han | 72f5090 | 2019-10-25 15:55:49 -0700 | [diff] [blame] | 790 | pw.print("expanded: "); |
| 791 | pw.println(mExpanded); |
| 792 | pw.print("count: "); |
| 793 | pw.println(mBubbles.size()); |
Mady Mellor | 70cba7bb | 2019-07-02 15:06:07 -0700 | [diff] [blame] | 794 | for (Bubble bubble : mBubbles) { |
| 795 | bubble.dump(fd, pw, args); |
| 796 | } |
Lyn Han | 72f5090 | 2019-10-25 15:55:49 -0700 | [diff] [blame] | 797 | pw.print("summaryKeys: "); |
| 798 | pw.println(mSuppressedGroupKeys.size()); |
Mady Mellor | e28fe10 | 2019-07-09 15:33:32 -0700 | [diff] [blame] | 799 | for (String key : mSuppressedGroupKeys.keySet()) { |
| 800 | pw.println(" suppressing: " + key); |
| 801 | } |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 802 | } |
Mark Renouf | c19b473 | 2019-06-26 12:08:33 -0400 | [diff] [blame] | 803 | } |