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