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; |
| 26 | import android.app.NotificationManager; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 27 | import android.content.Context; |
| 28 | import android.graphics.Point; |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 29 | import android.graphics.Rect; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 30 | import android.service.notification.StatusBarNotification; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 31 | import android.view.ViewGroup; |
| 32 | import android.view.WindowManager; |
| 33 | import android.widget.FrameLayout; |
| 34 | |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame^] | 35 | import com.android.internal.annotations.VisibleForTesting; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 36 | import com.android.systemui.Dependency; |
| 37 | import com.android.systemui.R; |
| 38 | import com.android.systemui.statusbar.notification.NotificationData; |
| 39 | import com.android.systemui.statusbar.phone.StatusBarWindowController; |
| 40 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 41 | import java.util.ArrayList; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 42 | import java.util.HashMap; |
| 43 | import java.util.Map; |
| 44 | |
| 45 | /** |
| 46 | * Bubbles are a special type of content that can "float" on top of other apps or System UI. |
| 47 | * Bubbles can be expanded to show more content. |
| 48 | * |
| 49 | * The controller manages addition, removal, and visible state of bubbles on screen. |
| 50 | */ |
| 51 | public class BubbleController { |
| 52 | private static final int MAX_BUBBLES = 5; // TODO: actually enforce this |
| 53 | |
| 54 | private static final String TAG = "BubbleController"; |
| 55 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 56 | // Enables some subset of notifs to automatically become bubbles |
| 57 | public static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false; |
| 58 | // When a bubble is dismissed, recreate it as a notification |
| 59 | public static final boolean DEBUG_DEMOTE_TO_NOTIF = false; |
| 60 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 61 | private Context mContext; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 62 | private BubbleDismissListener mDismissListener; |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 63 | private BubbleStateChangeListener mStateChangeListener; |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 64 | private BubbleExpandListener mExpandListener; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 65 | |
| 66 | private Map<String, BubbleView> mBubbles = new HashMap<>(); |
| 67 | private BubbleStackView mStackView; |
| 68 | private Point mDisplaySize; |
| 69 | |
| 70 | // Bubbles get added to the status bar view |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame^] | 71 | @VisibleForTesting |
| 72 | protected StatusBarWindowController mStatusBarWindowController; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 73 | |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 74 | // Used for determining view rect for touch interaction |
| 75 | private Rect mTempRect = new Rect(); |
| 76 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 77 | /** |
| 78 | * Listener to find out about bubble / bubble stack dismissal events. |
| 79 | */ |
| 80 | public interface BubbleDismissListener { |
| 81 | /** |
| 82 | * Called when the entire stack of bubbles is dismissed by the user. |
| 83 | */ |
| 84 | void onStackDismissed(); |
| 85 | |
| 86 | /** |
| 87 | * Called when a specific bubble is dismissed by the user. |
| 88 | */ |
| 89 | void onBubbleDismissed(String key); |
| 90 | } |
| 91 | |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 92 | /** |
| 93 | * Listener to be notified when some states of the bubbles change. |
| 94 | */ |
| 95 | public interface BubbleStateChangeListener { |
| 96 | /** |
| 97 | * Called when the stack has bubbles or no longer has bubbles. |
| 98 | */ |
| 99 | void onHasBubblesChanged(boolean hasBubbles); |
| 100 | } |
| 101 | |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 102 | /** |
| 103 | * Listener to find out about stack expansion / collapse events. |
| 104 | */ |
| 105 | public interface BubbleExpandListener { |
| 106 | /** |
| 107 | * Called when the expansion state of the bubble stack changes. |
| 108 | * |
| 109 | * @param isExpanding whether it's expanding or collapsing |
| 110 | * @param amount fraction of how expanded or collapsed it is, 1 being fully, 0 at the start |
| 111 | */ |
| 112 | void onBubbleExpandChanged(boolean isExpanding, float amount); |
| 113 | } |
| 114 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 115 | public BubbleController(Context context) { |
| 116 | mContext = context; |
| 117 | WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
| 118 | mDisplaySize = new Point(); |
| 119 | wm.getDefaultDisplay().getSize(mDisplaySize); |
| 120 | mStatusBarWindowController = Dependency.get(StatusBarWindowController.class); |
| 121 | } |
| 122 | |
| 123 | /** |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 124 | * Set a listener to be notified of bubble dismissal events. |
| 125 | */ |
| 126 | public void setDismissListener(BubbleDismissListener listener) { |
| 127 | mDismissListener = listener; |
| 128 | } |
| 129 | |
| 130 | /** |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 131 | * Set a listener to be notified when some states of the bubbles change. |
| 132 | */ |
| 133 | public void setBubbleStateChangeListener(BubbleStateChangeListener listener) { |
| 134 | mStateChangeListener = listener; |
| 135 | } |
| 136 | |
| 137 | /** |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 138 | * Set a listener to be notified of bubble expand events. |
| 139 | */ |
| 140 | public void setExpandListener(BubbleExpandListener listener) { |
| 141 | mExpandListener = listener; |
| 142 | if (mStackView != null) { |
| 143 | mStackView.setExpandListener(mExpandListener); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 148 | * Whether or not there are bubbles present, regardless of them being visible on the |
| 149 | * screen (e.g. if on AOD). |
| 150 | */ |
| 151 | public boolean hasBubbles() { |
| 152 | return mBubbles.size() > 0; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Whether the stack of bubbles is expanded or not. |
| 157 | */ |
| 158 | public boolean isStackExpanded() { |
| 159 | return mStackView != null && mStackView.isExpanded(); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Tell the stack of bubbles to collapse. |
| 164 | */ |
| 165 | public void collapseStack() { |
| 166 | if (mStackView != null) { |
| 167 | mStackView.animateExpansion(false); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack. |
| 173 | */ |
| 174 | public void dismissStack() { |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 175 | if (mStackView == null) { |
| 176 | return; |
| 177 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 178 | Point startPoint = getStartPoint(mStackView.getStackWidth(), mDisplaySize); |
| 179 | // Reset the position of the stack (TODO - or should we save / respect last user position?) |
| 180 | mStackView.setPosition(startPoint.x, startPoint.y); |
| 181 | for (String key: mBubbles.keySet()) { |
| 182 | removeBubble(key); |
| 183 | } |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 184 | if (mDismissListener != null) { |
| 185 | mDismissListener.onStackDismissed(); |
| 186 | } |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 187 | updateBubblesShowing(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Adds a bubble associated with the provided notification entry or updates it if it exists. |
| 192 | */ |
| 193 | public void addBubble(NotificationData.Entry notif) { |
| 194 | if (mBubbles.containsKey(notif.key)) { |
| 195 | // It's an update |
| 196 | BubbleView bubble = mBubbles.get(notif.key); |
| 197 | mStackView.updateBubble(bubble, notif); |
| 198 | } else { |
| 199 | // It's new |
| 200 | BubbleView bubble = new BubbleView(mContext); |
| 201 | bubble.setNotif(notif); |
| 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 | |
| 229 | /** |
| 230 | * Removes the bubble associated with the {@param uri}. |
| 231 | */ |
| 232 | public void removeBubble(String key) { |
| 233 | BubbleView bv = mBubbles.get(key); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 234 | if (mStackView != null && bv != null) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 235 | mStackView.removeBubble(bv); |
| 236 | bv.getEntry().setBubbleDismissed(true); |
| 237 | } |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 238 | if (mDismissListener != null) { |
| 239 | mDismissListener.onBubbleDismissed(key); |
| 240 | } |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 241 | updateBubblesShowing(); |
| 242 | } |
| 243 | |
| 244 | private void updateBubblesShowing() { |
| 245 | boolean hasBubblesShowing = false; |
| 246 | for (BubbleView bv : mBubbles.values()) { |
| 247 | if (!bv.getEntry().isBubbleDismissed()) { |
| 248 | hasBubblesShowing = true; |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | boolean hadBubbles = mStatusBarWindowController.getBubblesShowing(); |
| 253 | mStatusBarWindowController.setBubblesShowing(hasBubblesShowing); |
| 254 | if (mStackView != null && !hasBubblesShowing) { |
| 255 | mStackView.setVisibility(INVISIBLE); |
| 256 | } |
| 257 | if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) { |
| 258 | mStateChangeListener.onHasBubblesChanged(hasBubblesShowing); |
| 259 | } |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Sets the visibility of the bubbles, doesn't un-bubble them, just changes visibility. |
| 264 | */ |
| 265 | public void updateVisibility(boolean visible) { |
| 266 | if (mStackView == null) { |
| 267 | return; |
| 268 | } |
| 269 | ArrayList<BubbleView> viewsToRemove = new ArrayList<>(); |
| 270 | for (BubbleView bv : mBubbles.values()) { |
| 271 | NotificationData.Entry entry = bv.getEntry(); |
| 272 | if (entry != null) { |
| 273 | if (entry.row.isRemoved() || entry.isBubbleDismissed() || entry.row.isDismissed()) { |
| 274 | viewsToRemove.add(bv); |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | for (BubbleView view : viewsToRemove) { |
| 279 | mBubbles.remove(view.getKey()); |
| 280 | mStackView.removeBubble(view); |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 281 | } |
| 282 | if (mStackView != null) { |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 283 | mStackView.setVisibility(visible ? VISIBLE : INVISIBLE); |
| 284 | if (!visible) { |
| 285 | collapseStack(); |
| 286 | } |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 287 | } |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 288 | updateBubblesShowing(); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Rect indicating the touchable region for the bubble stack / expanded stack. |
| 293 | */ |
| 294 | public Rect getTouchableRegion() { |
| 295 | if (mStackView == null || mStackView.getVisibility() != VISIBLE) { |
| 296 | return null; |
| 297 | } |
| 298 | mStackView.getBoundsOnScreen(mTempRect); |
| 299 | return mTempRect; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame^] | 302 | @VisibleForTesting |
| 303 | public BubbleStackView getStackView() { |
| 304 | return mStackView; |
| 305 | } |
| 306 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 307 | // TODO: factor in PIP location / maybe last place user had it |
| 308 | /** |
| 309 | * Gets an appropriate starting point to position the bubble stack. |
| 310 | */ |
| 311 | public static Point getStartPoint(int size, Point displaySize) { |
| 312 | final int x = displaySize.x - size + EDGE_OVERLAP; |
| 313 | final int y = displaySize.y / 4; |
| 314 | return new Point(x, y); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Gets an appropriate position for the bubble when the stack is expanded. |
| 319 | */ |
| 320 | public static Point getExpandPoint(BubbleStackView view, int size, Point displaySize) { |
| 321 | // Same place for now.. |
| 322 | return new Point(EDGE_OVERLAP, size); |
| 323 | } |
| 324 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 325 | /** |
| 326 | * Whether the notification should bubble or not. |
| 327 | */ |
| 328 | public static boolean shouldAutoBubble(NotificationData.Entry entry, int priority, |
| 329 | boolean canAppOverlay) { |
| 330 | if (!DEBUG_ENABLE_AUTO_BUBBLE || entry.isBubbleDismissed()) { |
| 331 | return false; |
| 332 | } |
| 333 | StatusBarNotification n = entry.notification; |
| 334 | boolean hasRemoteInput = false; |
| 335 | if (n.getNotification().actions != null) { |
| 336 | for (Notification.Action action : n.getNotification().actions) { |
| 337 | if (action.getRemoteInputs() != null) { |
| 338 | hasRemoteInput = true; |
| 339 | break; |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle(); |
| 344 | boolean shouldBubble = priority >= NotificationManager.IMPORTANCE_HIGH |
| 345 | || Notification.MessagingStyle.class.equals(style) |
| 346 | || Notification.CATEGORY_MESSAGE.equals(n.getNotification().category) |
| 347 | || hasRemoteInput |
| 348 | || canAppOverlay; |
| 349 | return shouldBubble && !entry.isBubbleDismissed(); |
| 350 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 351 | } |