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