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; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 21 | import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 22 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 23 | import static com.android.systemui.statusbar.StatusBarState.SHADE; |
| 24 | import static com.android.systemui.statusbar.notification.NotificationAlertingManager.alertAgain; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 25 | |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 26 | import android.annotation.Nullable; |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 27 | import android.app.ActivityManager; |
| 28 | import android.app.ActivityTaskManager; |
| 29 | import android.app.IActivityTaskManager; |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 30 | import android.app.INotificationManager; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 31 | import android.app.Notification; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 32 | import android.content.Context; |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 33 | import android.graphics.Rect; |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 34 | import android.os.RemoteException; |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 35 | import android.os.ServiceManager; |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 36 | import android.provider.Settings; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 37 | import android.service.notification.StatusBarNotification; |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 38 | import android.view.Display; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 39 | import android.view.ViewGroup; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 40 | import android.widget.FrameLayout; |
| 41 | |
Mark Renouf | 658c6bc | 2019-01-30 10:26:54 -0500 | [diff] [blame] | 42 | import androidx.annotation.MainThread; |
| 43 | |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 44 | import com.android.internal.annotations.VisibleForTesting; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 45 | import com.android.internal.statusbar.NotificationVisibility; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 46 | import com.android.systemui.Dependency; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 47 | import com.android.systemui.R; |
Beverly | 8fdb533 | 2019-02-04 14:29:49 -0500 | [diff] [blame] | 48 | import com.android.systemui.plugins.statusbar.StatusBarStateController; |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 49 | import com.android.systemui.shared.system.ActivityManagerWrapper; |
| 50 | import com.android.systemui.shared.system.TaskStackChangeListener; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 51 | import com.android.systemui.statusbar.notification.NotificationEntryListener; |
| 52 | import com.android.systemui.statusbar.notification.NotificationEntryManager; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 53 | import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 54 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 55 | import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 56 | import com.android.systemui.statusbar.phone.StatusBarWindowController; |
| 57 | |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 58 | import java.util.List; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 59 | |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 60 | import javax.inject.Inject; |
| 61 | import javax.inject.Singleton; |
| 62 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 63 | /** |
| 64 | * Bubbles are a special type of content that can "float" on top of other apps or System UI. |
| 65 | * Bubbles can be expanded to show more content. |
| 66 | * |
| 67 | * The controller manages addition, removal, and visible state of bubbles on screen. |
| 68 | */ |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 69 | @Singleton |
Mady Mellor | 3d82e68 | 2019-02-05 13:34:48 -0800 | [diff] [blame] | 70 | public class BubbleController implements BubbleExpandedView.OnBubbleBlockedListener { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 71 | private static final int MAX_BUBBLES = 5; // TODO: actually enforce this |
| 72 | |
| 73 | private static final String TAG = "BubbleController"; |
| 74 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 75 | // Enables some subset of notifs to automatically become bubbles |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 76 | private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 77 | |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 78 | // Secure settings flags |
| 79 | // Feature level flag |
| 80 | private static final String ENABLE_BUBBLES = "experiment_enable_bubbles"; |
| 81 | // Auto bubble flags set whether different notification types should be presented as a bubble |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 82 | private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging"; |
| 83 | private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing"; |
| 84 | private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all"; |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 85 | // Use an activity view for an auto-bubbled notification if it has an appropriate content intent |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 86 | private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent"; |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 87 | |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 88 | private final Context mContext; |
| 89 | private final NotificationEntryManager mNotificationEntryManager; |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 90 | private final IActivityTaskManager mActivityTaskManager; |
| 91 | private final BubbleTaskStackListener mTaskStackListener; |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 92 | private BubbleStateChangeListener mStateChangeListener; |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 93 | private BubbleExpandListener mExpandListener; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 94 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 95 | private BubbleData mBubbleData; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 96 | private BubbleStackView mStackView; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 97 | |
| 98 | // Bubbles get added to the status bar view |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 99 | private final StatusBarWindowController mStatusBarWindowController; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 100 | private StatusBarStateListener mStatusBarStateListener; |
| 101 | |
| 102 | private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider = |
| 103 | Dependency.get(NotificationInterruptionStateProvider.class); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 104 | |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 105 | private INotificationManager mNotificationManagerService; |
| 106 | |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 107 | // Used for determining view rect for touch interaction |
| 108 | private Rect mTempRect = new Rect(); |
| 109 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 110 | /** |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 111 | * Listener to be notified when some states of the bubbles change. |
| 112 | */ |
| 113 | public interface BubbleStateChangeListener { |
| 114 | /** |
| 115 | * Called when the stack has bubbles or no longer has bubbles. |
| 116 | */ |
| 117 | void onHasBubblesChanged(boolean hasBubbles); |
| 118 | } |
| 119 | |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 120 | /** |
| 121 | * Listener to find out about stack expansion / collapse events. |
| 122 | */ |
| 123 | public interface BubbleExpandListener { |
| 124 | /** |
| 125 | * Called when the expansion state of the bubble stack changes. |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 126 | * @param isExpanding whether it's expanding or collapsing |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 127 | * @param key the notification key associated with bubble being expanded |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 128 | */ |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 129 | void onBubbleExpandChanged(boolean isExpanding, String key); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Listens for the current state of the status bar and updates the visibility state |
| 134 | * of bubbles as needed. |
| 135 | */ |
| 136 | private class StatusBarStateListener implements StatusBarStateController.StateListener { |
| 137 | private int mState; |
| 138 | /** |
| 139 | * Returns the current status bar state. |
| 140 | */ |
| 141 | public int getCurrentState() { |
| 142 | return mState; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public void onStateChanged(int newState) { |
| 147 | mState = newState; |
| 148 | updateVisibility(); |
| 149 | } |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 152 | @Inject |
Mady Mellor | cfd06c1 | 2019-02-13 14:32:12 -0800 | [diff] [blame] | 153 | public BubbleController(Context context, StatusBarWindowController statusBarWindowController, |
| 154 | BubbleData data) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 155 | mContext = context; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 156 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 157 | mNotificationEntryManager = Dependency.get(NotificationEntryManager.class); |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 158 | mNotificationEntryManager.addNotificationEntryListener(mEntryListener); |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 159 | |
| 160 | try { |
| 161 | mNotificationManagerService = INotificationManager.Stub.asInterface( |
| 162 | ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE)); |
| 163 | } catch (ServiceManager.ServiceNotFoundException e) { |
| 164 | e.printStackTrace(); |
| 165 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 166 | |
| 167 | mStatusBarWindowController = statusBarWindowController; |
| 168 | mStatusBarStateListener = new StatusBarStateListener(); |
| 169 | Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener); |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 170 | |
| 171 | mActivityTaskManager = ActivityTaskManager.getService(); |
| 172 | mTaskStackListener = new BubbleTaskStackListener(); |
| 173 | ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 174 | |
Mady Mellor | cfd06c1 | 2019-02-13 14:32:12 -0800 | [diff] [blame] | 175 | mBubbleData = data; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /** |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 179 | * Set a listener to be notified when some states of the bubbles change. |
| 180 | */ |
| 181 | public void setBubbleStateChangeListener(BubbleStateChangeListener listener) { |
| 182 | mStateChangeListener = listener; |
| 183 | } |
| 184 | |
| 185 | /** |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 186 | * Set a listener to be notified of bubble expand events. |
| 187 | */ |
| 188 | public void setExpandListener(BubbleExpandListener listener) { |
Issei Suzuki | ac9fcb7 | 2019-02-04 17:45:57 +0100 | [diff] [blame] | 189 | mExpandListener = ((isExpanding, key) -> { |
| 190 | if (listener != null) { |
| 191 | listener.onBubbleExpandChanged(isExpanding, key); |
| 192 | } |
| 193 | mStatusBarWindowController.setBubbleExpanded(isExpanding); |
| 194 | }); |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 195 | if (mStackView != null) { |
| 196 | mStackView.setExpandListener(mExpandListener); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 201 | * Whether or not there are bubbles present, regardless of them being visible on the |
| 202 | * screen (e.g. if on AOD). |
| 203 | */ |
| 204 | public boolean hasBubbles() { |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 205 | if (mStackView == null) { |
| 206 | return false; |
| 207 | } |
| 208 | for (Bubble bubble : mBubbleData.getBubbles()) { |
| 209 | if (!bubble.entry.isBubbleDismissed()) { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 210 | return true; |
| 211 | } |
| 212 | } |
| 213 | return false; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Whether the stack of bubbles is expanded or not. |
| 218 | */ |
| 219 | public boolean isStackExpanded() { |
| 220 | return mStackView != null && mStackView.isExpanded(); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Tell the stack of bubbles to collapse. |
| 225 | */ |
| 226 | public void collapseStack() { |
| 227 | if (mStackView != null) { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 228 | mStackView.collapseStack(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack. |
| 234 | */ |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 235 | void dismissStack() { |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 236 | if (mStackView == null) { |
| 237 | return; |
| 238 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 239 | mStackView.stackDismissed(); |
| 240 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 241 | updateVisibility(); |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 242 | mNotificationEntryManager.updateNotifications(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /** |
Mark Renouf | 041d726 | 2019-02-06 12:09:41 -0500 | [diff] [blame] | 246 | * Directs a back gesture at the bubble stack. When opened, the current expanded bubble |
| 247 | * is forwarded a back key down/up pair. |
| 248 | */ |
| 249 | public void performBackPressIfNeeded() { |
| 250 | if (mStackView != null) { |
| 251 | mStackView.performBackPressIfNeeded(); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /** |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 256 | * Adds or updates a bubble associated with the provided notification entry. |
| 257 | * |
| 258 | * @param notif the notification associated with this bubble. |
| 259 | * @param updatePosition whether this update should promote the bubble to the top of the stack. |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 260 | */ |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 261 | public void updateBubble(NotificationEntry notif, boolean updatePosition) { |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 262 | if (mStackView != null && mBubbleData.getBubble(notif.key) != null) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 263 | // It's an update |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 264 | mStackView.updateBubble(notif, updatePosition); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 265 | } else { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 266 | if (mStackView == null) { |
Mady Mellor | cfd06c1 | 2019-02-13 14:32:12 -0800 | [diff] [blame] | 267 | mStackView = new BubbleStackView(mContext, mBubbleData); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 268 | ViewGroup sbv = mStatusBarWindowController.getStatusBarView(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 269 | // XXX: Bug when you expand the shade on top of expanded bubble, there is no scrim |
| 270 | // between bubble and the shade |
| 271 | int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1; |
| 272 | sbv.addView(mStackView, bubblePosition, |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 273 | new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)); |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 274 | if (mExpandListener != null) { |
| 275 | mStackView.setExpandListener(mExpandListener); |
| 276 | } |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 277 | mStackView.setOnBlockedListener(this); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 278 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 279 | // It's new |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 280 | mStackView.addBubble(notif); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 281 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 282 | updateVisibility(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Removes the bubble associated with the {@param uri}. |
Mark Renouf | 658c6bc | 2019-01-30 10:26:54 -0500 | [diff] [blame] | 287 | * <p> |
| 288 | * Must be called from the main thread. |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 289 | */ |
Mark Renouf | 658c6bc | 2019-01-30 10:26:54 -0500 | [diff] [blame] | 290 | @MainThread |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 291 | void removeBubble(String key) { |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 292 | if (mStackView != null) { |
| 293 | mStackView.removeBubble(key); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 294 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 295 | mNotificationEntryManager.updateNotifications(); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 296 | updateVisibility(); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 299 | @Override |
| 300 | public void onBubbleBlocked(NotificationEntry entry) { |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 301 | Object[] bubbles = mBubbleData.getBubbles().toArray(); |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 302 | for (int i = 0; i < bubbles.length; i++) { |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 303 | NotificationEntry e = ((Bubble) bubbles[i]).entry; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 304 | boolean samePackage = entry.notification.getPackageName().equals( |
| 305 | e.notification.getPackageName()); |
| 306 | if (samePackage) { |
| 307 | removeBubble(entry.key); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 312 | @SuppressWarnings("FieldCanBeLocal") |
| 313 | private final NotificationEntryListener mEntryListener = new NotificationEntryListener() { |
| 314 | @Override |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 315 | public void onPendingEntryAdded(NotificationEntry entry) { |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 316 | if (!areBubblesEnabled(mContext)) { |
| 317 | return; |
| 318 | } |
| 319 | if (shouldAutoBubbleForFlags(mContext, entry) || shouldBubble(entry)) { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 320 | // TODO: handle group summaries |
| 321 | // It's a new notif, it shows in the shade and as a bubble |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 322 | entry.setIsBubble(true); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 323 | entry.setShowInShadeWhenBubble(true); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | @Override |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 328 | public void onEntryInflated(NotificationEntry entry, @InflationFlag int inflatedFlags) { |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 329 | if (!areBubblesEnabled(mContext)) { |
| 330 | return; |
| 331 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 332 | if (entry.isBubble() && mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) { |
| 333 | updateBubble(entry, true /* updatePosition */); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | @Override |
| 338 | public void onPreEntryUpdated(NotificationEntry entry) { |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 339 | if (!areBubblesEnabled(mContext)) { |
| 340 | return; |
| 341 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 342 | if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry) |
| 343 | && alertAgain(entry, entry.notification.getNotification())) { |
| 344 | entry.setShowInShadeWhenBubble(true); |
| 345 | entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 346 | updateBubble(entry, true /* updatePosition */); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 347 | mStackView.updateDotVisibility(entry.key); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
| 351 | @Override |
| 352 | public void onEntryRemoved(NotificationEntry entry, |
| 353 | @Nullable NotificationVisibility visibility, |
| 354 | boolean removedByUser) { |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 355 | if (!areBubblesEnabled(mContext)) { |
| 356 | return; |
| 357 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 358 | entry.setShowInShadeWhenBubble(false); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 359 | if (mStackView != null) { |
| 360 | mStackView.updateDotVisibility(entry.key); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 361 | } |
| 362 | if (!removedByUser) { |
| 363 | // This was a cancel so we should remove the bubble |
| 364 | removeBubble(entry.key); |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | }; |
| 368 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 369 | /** |
| 370 | * Lets any listeners know if bubble state has changed. |
| 371 | */ |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 372 | private void updateBubblesShowing() { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 373 | if (mStackView == null) { |
| 374 | return; |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 375 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 376 | |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 377 | boolean hadBubbles = mStatusBarWindowController.getBubblesShowing(); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 378 | boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE; |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 379 | mStatusBarWindowController.setBubblesShowing(hasBubblesShowing); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 380 | if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) { |
| 381 | mStateChangeListener.onHasBubblesChanged(hasBubblesShowing); |
| 382 | } |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /** |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 386 | * Updates the visibility of the bubbles based on current state. |
| 387 | * Does not un-bubble, just hides or un-hides. Will notify any |
| 388 | * {@link BubbleStateChangeListener}s if visibility changes. |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 389 | */ |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 390 | public void updateVisibility() { |
| 391 | if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) { |
| 392 | // Bubbles only appear in unlocked shade |
| 393 | mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE); |
| 394 | } else if (mStackView != null) { |
| 395 | mStackView.setVisibility(INVISIBLE); |
| 396 | collapseStack(); |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 397 | } |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 398 | updateBubblesShowing(); |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Rect indicating the touchable region for the bubble stack / expanded stack. |
| 403 | */ |
| 404 | public Rect getTouchableRegion() { |
| 405 | if (mStackView == null || mStackView.getVisibility() != VISIBLE) { |
| 406 | return null; |
| 407 | } |
| 408 | mStackView.getBoundsOnScreen(mTempRect); |
| 409 | return mTempRect; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 412 | @VisibleForTesting |
| 413 | BubbleStackView getStackView() { |
| 414 | return mStackView; |
| 415 | } |
| 416 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 417 | /** |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 418 | * Whether the notification has been developer configured to bubble and is allowed by the user. |
| 419 | */ |
Mady Mellor | c18ba96 | 2019-01-29 11:11:56 -0800 | [diff] [blame] | 420 | @VisibleForTesting |
| 421 | protected boolean shouldBubble(NotificationEntry entry) { |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 422 | StatusBarNotification n = entry.notification; |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 423 | boolean hasOverlayIntent = n.getNotification().getBubbleMetadata() != null |
| 424 | && n.getNotification().getBubbleMetadata().getIntent() != null; |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 425 | return hasOverlayIntent && entry.canBubble; |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /** |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 429 | * Whether the notification should automatically bubble or not. Gated by secure settings flags. |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 430 | */ |
Mady Mellor | 9bad224 | 2019-01-28 11:21:51 -0800 | [diff] [blame] | 431 | @VisibleForTesting |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 432 | protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) { |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 433 | if (entry.isBubbleDismissed()) { |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 434 | return false; |
| 435 | } |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 436 | StatusBarNotification n = entry.notification; |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 437 | |
| 438 | boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE; |
| 439 | boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE; |
| 440 | boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE; |
| 441 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 442 | boolean hasRemoteInput = false; |
| 443 | if (n.getNotification().actions != null) { |
| 444 | for (Notification.Action action : n.getNotification().actions) { |
| 445 | if (action.getRemoteInputs() != null) { |
| 446 | hasRemoteInput = true; |
| 447 | break; |
| 448 | } |
| 449 | } |
| 450 | } |
Mady Mellor | 711f956 | 2018-12-05 14:53:46 -0800 | [diff] [blame] | 451 | boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category) |
| 452 | && n.isOngoing(); |
| 453 | boolean isMusic = n.getNotification().hasMediaSession(); |
| 454 | boolean isImportantOngoing = isMusic || isCall; |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 455 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 456 | Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle(); |
Mady Mellor | e317537 | 2018-12-04 17:05:11 -0800 | [diff] [blame] | 457 | boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category); |
| 458 | boolean isMessageStyle = Notification.MessagingStyle.class.equals(style); |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 459 | return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages) |
Mady Mellor | 711f956 | 2018-12-05 14:53:46 -0800 | [diff] [blame] | 460 | || (isImportantOngoing && autoBubbleOngoing) |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 461 | || autoBubbleAll; |
| 462 | } |
| 463 | |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 464 | /** |
| 465 | * This task stack listener is responsible for responding to tasks moved to the front |
| 466 | * which are on the default (main) display. When this happens, expanded bubbles must be |
| 467 | * collapsed so the user may interact with the app which was just moved to the front. |
| 468 | * <p> |
| 469 | * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches |
| 470 | * these calls via a main thread Handler. |
| 471 | */ |
| 472 | @MainThread |
| 473 | private class BubbleTaskStackListener extends TaskStackChangeListener { |
| 474 | |
| 475 | @Nullable |
| 476 | private ActivityManager.StackInfo findStackInfo(int taskId) throws RemoteException { |
| 477 | final List<ActivityManager.StackInfo> stackInfoList = |
| 478 | mActivityTaskManager.getAllStackInfos(); |
| 479 | // Iterate through stacks from top to bottom. |
| 480 | final int stackCount = stackInfoList.size(); |
| 481 | for (int stackIndex = 0; stackIndex < stackCount; stackIndex++) { |
| 482 | final ActivityManager.StackInfo stackInfo = stackInfoList.get(stackIndex); |
| 483 | // Iterate through tasks from top to bottom. |
| 484 | for (int taskIndex = stackInfo.taskIds.length - 1; taskIndex >= 0; taskIndex--) { |
| 485 | if (stackInfo.taskIds[taskIndex] == taskId) { |
| 486 | return stackInfo; |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | return null; |
| 491 | } |
| 492 | |
| 493 | @Override |
| 494 | public void onTaskMovedToFront(int taskId) { |
| 495 | ActivityManager.StackInfo stackInfo = null; |
| 496 | try { |
| 497 | stackInfo = findStackInfo(taskId); |
| 498 | } catch (RemoteException e) { |
| 499 | e.rethrowAsRuntimeException(); |
| 500 | } |
| 501 | if (stackInfo != null && stackInfo.displayId == Display.DEFAULT_DISPLAY |
| 502 | && mStackView != null) { |
| 503 | mStackView.collapseStack(); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * This is a workaround for the case when the activity had to be created in a new task. |
| 509 | * Existing code in ActivityStackSupervisor checks the display where the activity |
| 510 | * ultimately ended up, displays an error message toast, and calls this method instead of |
| 511 | * onTaskMovedToFront. |
| 512 | */ |
| 513 | // TODO(b/124058588): add requestedDisplayId to this callback, ignore unless matches |
| 514 | @Override |
| 515 | public void onActivityLaunchOnSecondaryDisplayFailed() { |
| 516 | if (mStackView != null) { |
| 517 | mStackView.collapseStack(); |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 522 | private static boolean shouldAutoBubbleMessages(Context context) { |
| 523 | return Settings.Secure.getInt(context.getContentResolver(), |
| 524 | ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0; |
| 525 | } |
| 526 | |
| 527 | private static boolean shouldAutoBubbleOngoing(Context context) { |
| 528 | return Settings.Secure.getInt(context.getContentResolver(), |
| 529 | ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0; |
| 530 | } |
| 531 | |
| 532 | private static boolean shouldAutoBubbleAll(Context context) { |
| 533 | return Settings.Secure.getInt(context.getContentResolver(), |
| 534 | ENABLE_AUTO_BUBBLE_ALL, 0) != 0; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 535 | } |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 536 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 537 | static boolean shouldUseContentIntent(Context context) { |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 538 | return Settings.Secure.getInt(context.getContentResolver(), |
| 539 | ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0; |
| 540 | } |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 541 | |
| 542 | private static boolean areBubblesEnabled(Context context) { |
| 543 | return Settings.Secure.getInt(context.getContentResolver(), |
| 544 | ENABLE_BUBBLES, 1) != 0; |
| 545 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 546 | } |