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