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