Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.bubbles; |
| 18 | |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 19 | import static android.view.View.INVISIBLE; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 20 | import static android.view.View.VISIBLE; |
| 21 | import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; |
| 22 | |
| 23 | import static com.android.systemui.bubbles.BubbleMovementHelper.EDGE_OVERLAP; |
| 24 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 25 | import android.app.Notification; |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 26 | import android.app.PendingIntent; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 27 | import android.content.Context; |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 28 | import android.content.pm.ActivityInfo; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 29 | import android.graphics.Point; |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 30 | import android.graphics.Rect; |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 31 | import android.provider.Settings; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 32 | import android.service.notification.StatusBarNotification; |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 33 | import android.util.Log; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 34 | import android.view.ViewGroup; |
| 35 | import android.view.WindowManager; |
| 36 | import android.widget.FrameLayout; |
| 37 | |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 38 | import androidx.annotation.Nullable; |
| 39 | |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 40 | import com.android.internal.annotations.VisibleForTesting; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 41 | import com.android.systemui.Dependency; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 42 | import com.android.systemui.R; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 43 | import com.android.systemui.statusbar.notification.NotificationEntryListener; |
| 44 | import com.android.systemui.statusbar.notification.NotificationEntryManager; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 45 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 46 | import com.android.systemui.statusbar.phone.StatusBarWindowController; |
| 47 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 48 | import java.util.ArrayList; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 49 | import java.util.HashMap; |
| 50 | import java.util.Map; |
| 51 | |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 52 | import javax.inject.Inject; |
| 53 | import javax.inject.Singleton; |
| 54 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 55 | /** |
| 56 | * Bubbles are a special type of content that can "float" on top of other apps or System UI. |
| 57 | * Bubbles can be expanded to show more content. |
| 58 | * |
| 59 | * The controller manages addition, removal, and visible state of bubbles on screen. |
| 60 | */ |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 61 | @Singleton |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 62 | public class BubbleController { |
| 63 | private static final int MAX_BUBBLES = 5; // TODO: actually enforce this |
| 64 | |
| 65 | private static final String TAG = "BubbleController"; |
| 66 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 67 | // Enables some subset of notifs to automatically become bubbles |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 68 | private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 69 | // When a bubble is dismissed, recreate it as a notification |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 70 | private static final boolean DEBUG_DEMOTE_TO_NOTIF = false; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 71 | |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 72 | // Secure settings |
| 73 | private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging"; |
| 74 | private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing"; |
| 75 | private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all"; |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 76 | private static final String ENABLE_BUBBLE_ACTIVITY_VIEW = "experiment_bubble_activity_view"; |
| 77 | private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent"; |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 78 | |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 79 | private final Context mContext; |
| 80 | private final NotificationEntryManager mNotificationEntryManager; |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 81 | private BubbleStateChangeListener mStateChangeListener; |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 82 | private BubbleExpandListener mExpandListener; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 83 | |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 84 | private final Map<String, BubbleView> mBubbles = new HashMap<>(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 85 | private BubbleStackView mStackView; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 86 | private final Point mDisplaySize; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 87 | |
| 88 | // Bubbles get added to the status bar view |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 89 | private final StatusBarWindowController mStatusBarWindowController; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 90 | |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 91 | // Used for determining view rect for touch interaction |
| 92 | private Rect mTempRect = new Rect(); |
| 93 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 94 | /** |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 95 | * Listener to be notified when some states of the bubbles change. |
| 96 | */ |
| 97 | public interface BubbleStateChangeListener { |
| 98 | /** |
| 99 | * Called when the stack has bubbles or no longer has bubbles. |
| 100 | */ |
| 101 | void onHasBubblesChanged(boolean hasBubbles); |
| 102 | } |
| 103 | |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 104 | /** |
| 105 | * Listener to find out about stack expansion / collapse events. |
| 106 | */ |
| 107 | public interface BubbleExpandListener { |
| 108 | /** |
| 109 | * Called when the expansion state of the bubble stack changes. |
| 110 | * |
| 111 | * @param isExpanding whether it's expanding or collapsing |
| 112 | * @param amount fraction of how expanded or collapsed it is, 1 being fully, 0 at the start |
| 113 | */ |
| 114 | void onBubbleExpandChanged(boolean isExpanding, float amount); |
| 115 | } |
| 116 | |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 117 | @Inject |
Jason Monk | 92d5c24 | 2018-12-21 14:37:34 -0500 | [diff] [blame] | 118 | public BubbleController(Context context, StatusBarWindowController statusBarWindowController) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 119 | mContext = context; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 120 | mNotificationEntryManager = Dependency.get(NotificationEntryManager.class); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 121 | WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
| 122 | mDisplaySize = new Point(); |
| 123 | wm.getDefaultDisplay().getSize(mDisplaySize); |
Jason Monk | 92d5c24 | 2018-12-21 14:37:34 -0500 | [diff] [blame] | 124 | mStatusBarWindowController = statusBarWindowController; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 125 | |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 126 | mNotificationEntryManager.addNotificationEntryListener(mEntryListener); |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 130 | * Set a listener to be notified when some states of the bubbles change. |
| 131 | */ |
| 132 | public void setBubbleStateChangeListener(BubbleStateChangeListener listener) { |
| 133 | mStateChangeListener = listener; |
| 134 | } |
| 135 | |
| 136 | /** |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 137 | * Set a listener to be notified of bubble expand events. |
| 138 | */ |
| 139 | public void setExpandListener(BubbleExpandListener listener) { |
| 140 | mExpandListener = listener; |
| 141 | if (mStackView != null) { |
| 142 | mStackView.setExpandListener(mExpandListener); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 147 | * Whether or not there are bubbles present, regardless of them being visible on the |
| 148 | * screen (e.g. if on AOD). |
| 149 | */ |
| 150 | public boolean hasBubbles() { |
| 151 | return mBubbles.size() > 0; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Whether the stack of bubbles is expanded or not. |
| 156 | */ |
| 157 | public boolean isStackExpanded() { |
| 158 | return mStackView != null && mStackView.isExpanded(); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Tell the stack of bubbles to collapse. |
| 163 | */ |
| 164 | public void collapseStack() { |
| 165 | if (mStackView != null) { |
| 166 | mStackView.animateExpansion(false); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack. |
| 172 | */ |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 173 | void dismissStack() { |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 174 | if (mStackView == null) { |
| 175 | return; |
| 176 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 177 | Point startPoint = getStartPoint(mStackView.getStackWidth(), mDisplaySize); |
| 178 | // Reset the position of the stack (TODO - or should we save / respect last user position?) |
| 179 | mStackView.setPosition(startPoint.x, startPoint.y); |
| 180 | for (String key: mBubbles.keySet()) { |
| 181 | removeBubble(key); |
| 182 | } |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 183 | mNotificationEntryManager.updateNotifications(); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 184 | updateBubblesShowing(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Adds a bubble associated with the provided notification entry or updates it if it exists. |
| 189 | */ |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 190 | public void addBubble(NotificationEntry notif) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 191 | if (mBubbles.containsKey(notif.key)) { |
| 192 | // It's an update |
| 193 | BubbleView bubble = mBubbles.get(notif.key); |
| 194 | mStackView.updateBubble(bubble, notif); |
| 195 | } else { |
| 196 | // It's new |
| 197 | BubbleView bubble = new BubbleView(mContext); |
| 198 | bubble.setNotif(notif); |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 199 | if (shouldUseActivityView(mContext)) { |
| 200 | bubble.setAppOverlayIntent(getAppOverlayIntent(notif)); |
| 201 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 202 | mBubbles.put(bubble.getKey(), bubble); |
| 203 | |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 204 | boolean setPosition = mStackView != null && mStackView.getVisibility() != VISIBLE; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 205 | if (mStackView == null) { |
| 206 | setPosition = true; |
| 207 | mStackView = new BubbleStackView(mContext); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 208 | ViewGroup sbv = mStatusBarWindowController.getStatusBarView(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 209 | // XXX: Bug when you expand the shade on top of expanded bubble, there is no scrim |
| 210 | // between bubble and the shade |
| 211 | int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1; |
| 212 | sbv.addView(mStackView, bubblePosition, |
| 213 | new FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)); |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 214 | if (mExpandListener != null) { |
| 215 | mStackView.setExpandListener(mExpandListener); |
| 216 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 217 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 218 | mStackView.addBubble(bubble); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 219 | if (setPosition) { |
| 220 | // Need to add the bubble to the stack before we can know the width |
| 221 | Point startPoint = getStartPoint(mStackView.getStackWidth(), mDisplaySize); |
| 222 | mStackView.setPosition(startPoint.x, startPoint.y); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 223 | mStackView.setVisibility(VISIBLE); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 224 | } |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 225 | updateBubblesShowing(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 229 | @Nullable |
| 230 | private PendingIntent getAppOverlayIntent(NotificationEntry notif) { |
| 231 | Notification notification = notif.notification.getNotification(); |
| 232 | if (canLaunchInActivityView(notification.getAppOverlayIntent())) { |
| 233 | return notification.getAppOverlayIntent(); |
| 234 | } else if (shouldUseContentIntent(mContext) |
| 235 | && canLaunchInActivityView(notification.contentIntent)) { |
| 236 | Log.d(TAG, "[addBubble " + notif.key |
| 237 | + "]: No appOverlayIntent, using contentIntent."); |
| 238 | return notification.contentIntent; |
| 239 | } |
| 240 | Log.d(TAG, "[addBubble " + notif.key + "]: No supported intent for ActivityView."); |
| 241 | return null; |
| 242 | } |
| 243 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 244 | /** |
| 245 | * Removes the bubble associated with the {@param uri}. |
| 246 | */ |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 247 | void removeBubble(String key) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 248 | BubbleView bv = mBubbles.get(key); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 249 | if (mStackView != null && bv != null) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 250 | mStackView.removeBubble(bv); |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 251 | bv.destroyActivityView(mStackView); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 252 | bv.getEntry().setBubbleDismissed(true); |
| 253 | } |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 254 | |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 255 | NotificationEntry entry = mNotificationEntryManager.getNotificationData().get(key); |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 256 | if (entry != null) { |
| 257 | entry.setBubbleDismissed(true); |
| 258 | if (!DEBUG_DEMOTE_TO_NOTIF) { |
| 259 | mNotificationEntryManager.performRemoveNotification(entry.notification); |
| 260 | } |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 261 | } |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 262 | mNotificationEntryManager.updateNotifications(); |
| 263 | |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 264 | updateBubblesShowing(); |
| 265 | } |
| 266 | |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 267 | @SuppressWarnings("FieldCanBeLocal") |
| 268 | private final NotificationEntryListener mEntryListener = new NotificationEntryListener() { |
| 269 | @Override |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 270 | public void onPendingEntryAdded(NotificationEntry entry) { |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 271 | if (shouldAutoBubble(mContext, entry)) { |
| 272 | entry.setIsBubble(true); |
| 273 | } |
| 274 | } |
| 275 | }; |
| 276 | |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 277 | private void updateBubblesShowing() { |
| 278 | boolean hasBubblesShowing = false; |
| 279 | for (BubbleView bv : mBubbles.values()) { |
| 280 | if (!bv.getEntry().isBubbleDismissed()) { |
| 281 | hasBubblesShowing = true; |
| 282 | break; |
| 283 | } |
| 284 | } |
| 285 | boolean hadBubbles = mStatusBarWindowController.getBubblesShowing(); |
| 286 | mStatusBarWindowController.setBubblesShowing(hasBubblesShowing); |
| 287 | if (mStackView != null && !hasBubblesShowing) { |
| 288 | mStackView.setVisibility(INVISIBLE); |
| 289 | } |
| 290 | if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) { |
| 291 | mStateChangeListener.onHasBubblesChanged(hasBubblesShowing); |
| 292 | } |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Sets the visibility of the bubbles, doesn't un-bubble them, just changes visibility. |
| 297 | */ |
| 298 | public void updateVisibility(boolean visible) { |
| 299 | if (mStackView == null) { |
| 300 | return; |
| 301 | } |
| 302 | ArrayList<BubbleView> viewsToRemove = new ArrayList<>(); |
| 303 | for (BubbleView bv : mBubbles.values()) { |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 304 | NotificationEntry entry = bv.getEntry(); |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 305 | if (entry != null) { |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 306 | if (entry.isRowRemoved() || entry.isBubbleDismissed() || entry.isRowDismissed()) { |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 307 | viewsToRemove.add(bv); |
| 308 | } |
| 309 | } |
| 310 | } |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 311 | for (BubbleView bubbleView : viewsToRemove) { |
| 312 | mBubbles.remove(bubbleView.getKey()); |
| 313 | mStackView.removeBubble(bubbleView); |
| 314 | bubbleView.destroyActivityView(mStackView); |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 315 | } |
| 316 | if (mStackView != null) { |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 317 | mStackView.setVisibility(visible ? VISIBLE : INVISIBLE); |
| 318 | if (!visible) { |
| 319 | collapseStack(); |
| 320 | } |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 321 | } |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 322 | updateBubblesShowing(); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Rect indicating the touchable region for the bubble stack / expanded stack. |
| 327 | */ |
| 328 | public Rect getTouchableRegion() { |
| 329 | if (mStackView == null || mStackView.getVisibility() != VISIBLE) { |
| 330 | return null; |
| 331 | } |
| 332 | mStackView.getBoundsOnScreen(mTempRect); |
| 333 | return mTempRect; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 336 | private boolean canLaunchInActivityView(PendingIntent intent) { |
| 337 | if (intent == null) { |
| 338 | return false; |
| 339 | } |
| 340 | ActivityInfo info = |
| 341 | intent.getIntent().resolveActivityInfo(mContext.getPackageManager(), 0); |
| 342 | return info != null |
| 343 | && ActivityInfo.isResizeableMode(info.resizeMode) |
| 344 | && (info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) != 0; |
| 345 | } |
| 346 | |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 347 | @VisibleForTesting |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 348 | BubbleStackView getStackView() { |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 349 | return mStackView; |
| 350 | } |
| 351 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 352 | // TODO: factor in PIP location / maybe last place user had it |
| 353 | /** |
| 354 | * Gets an appropriate starting point to position the bubble stack. |
| 355 | */ |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 356 | private static Point getStartPoint(int size, Point displaySize) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 357 | final int x = displaySize.x - size + EDGE_OVERLAP; |
| 358 | final int y = displaySize.y / 4; |
| 359 | return new Point(x, y); |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Gets an appropriate position for the bubble when the stack is expanded. |
| 364 | */ |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 365 | static Point getExpandPoint(BubbleStackView view, int size, Point displaySize) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 366 | // Same place for now.. |
| 367 | return new Point(EDGE_OVERLAP, size); |
| 368 | } |
| 369 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 370 | /** |
| 371 | * Whether the notification should bubble or not. |
| 372 | */ |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 373 | private static boolean shouldAutoBubble(Context context, NotificationEntry entry) { |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 374 | if (entry.isBubbleDismissed()) { |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 375 | return false; |
| 376 | } |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 377 | |
| 378 | boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE; |
| 379 | boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE; |
| 380 | boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE; |
| 381 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 382 | StatusBarNotification n = entry.notification; |
| 383 | boolean hasRemoteInput = false; |
| 384 | if (n.getNotification().actions != null) { |
| 385 | for (Notification.Action action : n.getNotification().actions) { |
| 386 | if (action.getRemoteInputs() != null) { |
| 387 | hasRemoteInput = true; |
| 388 | break; |
| 389 | } |
| 390 | } |
| 391 | } |
Mady Mellor | 711f956 | 2018-12-05 14:53:46 -0800 | [diff] [blame] | 392 | boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category) |
| 393 | && n.isOngoing(); |
| 394 | boolean isMusic = n.getNotification().hasMediaSession(); |
| 395 | boolean isImportantOngoing = isMusic || isCall; |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 396 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 397 | Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle(); |
Mady Mellor | e317537 | 2018-12-04 17:05:11 -0800 | [diff] [blame] | 398 | boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category); |
| 399 | boolean isMessageStyle = Notification.MessagingStyle.class.equals(style); |
| 400 | return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages) |
Mady Mellor | 711f956 | 2018-12-05 14:53:46 -0800 | [diff] [blame] | 401 | || (isImportantOngoing && autoBubbleOngoing) |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 402 | || autoBubbleAll; |
| 403 | } |
| 404 | |
| 405 | private static boolean shouldAutoBubbleMessages(Context context) { |
| 406 | return Settings.Secure.getInt(context.getContentResolver(), |
| 407 | ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0; |
| 408 | } |
| 409 | |
| 410 | private static boolean shouldAutoBubbleOngoing(Context context) { |
| 411 | return Settings.Secure.getInt(context.getContentResolver(), |
| 412 | ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0; |
| 413 | } |
| 414 | |
| 415 | private static boolean shouldAutoBubbleAll(Context context) { |
| 416 | return Settings.Secure.getInt(context.getContentResolver(), |
| 417 | ENABLE_AUTO_BUBBLE_ALL, 0) != 0; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 418 | } |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 419 | |
| 420 | private static boolean shouldUseActivityView(Context context) { |
| 421 | return Settings.Secure.getInt(context.getContentResolver(), |
| 422 | ENABLE_BUBBLE_ACTIVITY_VIEW, 0) != 0; |
| 423 | } |
| 424 | |
| 425 | private static boolean shouldUseContentIntent(Context context) { |
| 426 | return Settings.Secure.getInt(context.getContentResolver(), |
| 427 | ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0; |
| 428 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 429 | } |