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