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; |
| 27 | import android.app.INotificationManager; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 28 | import android.app.Notification; |
Mady Mellor | c18ba96 | 2019-01-29 11:11:56 -0800 | [diff] [blame] | 29 | import android.app.NotificationChannel; |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 30 | import android.app.PendingIntent; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 31 | import android.content.Context; |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 32 | import android.content.pm.ActivityInfo; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 33 | import android.graphics.Point; |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 34 | import android.graphics.Rect; |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 35 | import android.os.RemoteException; |
| 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 | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 39 | import android.util.Log; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 40 | import android.view.LayoutInflater; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 41 | import android.view.ViewGroup; |
| 42 | import android.view.WindowManager; |
| 43 | import android.widget.FrameLayout; |
| 44 | |
Mark Renouf | 658c6bc | 2019-01-30 10:26:54 -0500 | [diff] [blame] | 45 | import androidx.annotation.MainThread; |
| 46 | |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 47 | import com.android.internal.annotations.VisibleForTesting; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 48 | import com.android.internal.statusbar.NotificationVisibility; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 49 | import com.android.systemui.Dependency; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 50 | import com.android.systemui.R; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 51 | import com.android.systemui.statusbar.StatusBarStateController; |
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 | |
| 59 | import java.util.HashMap; |
| 60 | import java.util.Map; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 61 | import java.util.Set; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 62 | |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 63 | import javax.inject.Inject; |
| 64 | import javax.inject.Singleton; |
| 65 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 66 | /** |
| 67 | * Bubbles are a special type of content that can "float" on top of other apps or System UI. |
| 68 | * Bubbles can be expanded to show more content. |
| 69 | * |
| 70 | * The controller manages addition, removal, and visible state of bubbles on screen. |
| 71 | */ |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 72 | @Singleton |
Mady Mellor | 3d82e68 | 2019-02-05 13:34:48 -0800 | [diff] [blame^] | 73 | public class BubbleController implements BubbleExpandedView.OnBubbleBlockedListener { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 74 | private static final int MAX_BUBBLES = 5; // TODO: actually enforce this |
| 75 | |
| 76 | private static final String TAG = "BubbleController"; |
| 77 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 78 | // Enables some subset of notifs to automatically become bubbles |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 79 | private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 80 | |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 81 | // Secure settings flags |
| 82 | // Feature level flag |
| 83 | private static final String ENABLE_BUBBLES = "experiment_enable_bubbles"; |
| 84 | // 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] | 85 | private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging"; |
| 86 | private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing"; |
| 87 | private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all"; |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 88 | // 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] | 89 | private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent"; |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 90 | |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 91 | private final Context mContext; |
| 92 | private final NotificationEntryManager mNotificationEntryManager; |
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 | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 95 | private LayoutInflater mInflater; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 96 | |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 97 | private final Map<String, BubbleView> mBubbles = new HashMap<>(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 98 | private BubbleStackView mStackView; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 99 | private final Point mDisplaySize; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 100 | |
| 101 | // Bubbles get added to the status bar view |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 102 | private final StatusBarWindowController mStatusBarWindowController; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 103 | private StatusBarStateListener mStatusBarStateListener; |
| 104 | |
| 105 | private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider = |
| 106 | Dependency.get(NotificationInterruptionStateProvider.class); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 107 | |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 108 | private INotificationManager mNotificationManagerService; |
| 109 | |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 110 | // Used for determining view rect for touch interaction |
| 111 | private Rect mTempRect = new Rect(); |
| 112 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 113 | /** |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 114 | * Listener to be notified when some states of the bubbles change. |
| 115 | */ |
| 116 | public interface BubbleStateChangeListener { |
| 117 | /** |
| 118 | * Called when the stack has bubbles or no longer has bubbles. |
| 119 | */ |
| 120 | void onHasBubblesChanged(boolean hasBubbles); |
| 121 | } |
| 122 | |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 123 | /** |
| 124 | * Listener to find out about stack expansion / collapse events. |
| 125 | */ |
| 126 | public interface BubbleExpandListener { |
| 127 | /** |
| 128 | * Called when the expansion state of the bubble stack changes. |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 129 | * @param isExpanding whether it's expanding or collapsing |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 130 | * @param key the notification key associated with bubble being expanded |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 131 | */ |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 132 | void onBubbleExpandChanged(boolean isExpanding, String key); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Listens for the current state of the status bar and updates the visibility state |
| 137 | * of bubbles as needed. |
| 138 | */ |
| 139 | private class StatusBarStateListener implements StatusBarStateController.StateListener { |
| 140 | private int mState; |
| 141 | /** |
| 142 | * Returns the current status bar state. |
| 143 | */ |
| 144 | public int getCurrentState() { |
| 145 | return mState; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void onStateChanged(int newState) { |
| 150 | mState = newState; |
| 151 | updateVisibility(); |
| 152 | } |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 155 | @Inject |
Jason Monk | 92d5c24 | 2018-12-21 14:37:34 -0500 | [diff] [blame] | 156 | public BubbleController(Context context, StatusBarWindowController statusBarWindowController) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 157 | mContext = context; |
| 158 | WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
| 159 | mDisplaySize = new Point(); |
| 160 | wm.getDefaultDisplay().getSize(mDisplaySize); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 161 | mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 162 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 163 | mNotificationEntryManager = Dependency.get(NotificationEntryManager.class); |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 164 | mNotificationEntryManager.addNotificationEntryListener(mEntryListener); |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 165 | |
| 166 | try { |
| 167 | mNotificationManagerService = INotificationManager.Stub.asInterface( |
| 168 | ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE)); |
| 169 | } catch (ServiceManager.ServiceNotFoundException e) { |
| 170 | e.printStackTrace(); |
| 171 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 172 | |
| 173 | mStatusBarWindowController = statusBarWindowController; |
| 174 | mStatusBarStateListener = new StatusBarStateListener(); |
| 175 | Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener); |
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 | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 205 | for (BubbleView bv : mBubbles.values()) { |
| 206 | if (!bv.getEntry().isBubbleDismissed()) { |
| 207 | return true; |
| 208 | } |
| 209 | } |
| 210 | return false; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Whether the stack of bubbles is expanded or not. |
| 215 | */ |
| 216 | public boolean isStackExpanded() { |
| 217 | return mStackView != null && mStackView.isExpanded(); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Tell the stack of bubbles to collapse. |
| 222 | */ |
| 223 | public void collapseStack() { |
| 224 | if (mStackView != null) { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 225 | mStackView.collapseStack(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack. |
| 231 | */ |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 232 | void dismissStack() { |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 233 | if (mStackView == null) { |
| 234 | return; |
| 235 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 236 | Set<String> keys = mBubbles.keySet(); |
| 237 | for (String key: keys) { |
| 238 | mBubbles.get(key).getEntry().setBubbleDismissed(true); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 239 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 240 | mStackView.stackDismissed(); |
| 241 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 242 | updateVisibility(); |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 243 | mNotificationEntryManager.updateNotifications(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /** |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 247 | * Adds or updates a bubble associated with the provided notification entry. |
| 248 | * |
| 249 | * @param notif the notification associated with this bubble. |
| 250 | * @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] | 251 | */ |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 252 | public void updateBubble(NotificationEntry notif, boolean updatePosition) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 253 | if (mBubbles.containsKey(notif.key)) { |
| 254 | // It's an update |
| 255 | BubbleView bubble = mBubbles.get(notif.key); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 256 | mStackView.updateBubble(bubble, notif, updatePosition); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 257 | } else { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 258 | if (mStackView == null) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 259 | mStackView = new BubbleStackView(mContext); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 260 | ViewGroup sbv = mStatusBarWindowController.getStatusBarView(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 261 | // XXX: Bug when you expand the shade on top of expanded bubble, there is no scrim |
| 262 | // between bubble and the shade |
| 263 | int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1; |
| 264 | sbv.addView(mStackView, bubblePosition, |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 265 | new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)); |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 266 | if (mExpandListener != null) { |
| 267 | mStackView.setExpandListener(mExpandListener); |
| 268 | } |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 269 | mStackView.setOnBlockedListener(this); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 270 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 271 | // It's new |
| 272 | BubbleView bubble = (BubbleView) mInflater.inflate( |
| 273 | R.layout.bubble_view, mStackView, false /* attachToRoot */); |
| 274 | bubble.setNotif(notif); |
Mady Mellor | edd4ee1 | 2019-01-18 10:45:11 -0800 | [diff] [blame] | 275 | PendingIntent bubbleIntent = getValidBubbleIntent(notif); |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 276 | if (bubbleIntent != null) { |
| 277 | bubble.setBubbleIntent(bubbleIntent); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 278 | } |
| 279 | mBubbles.put(bubble.getKey(), bubble); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 280 | mStackView.addBubble(bubble); |
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 | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 292 | BubbleView bv = mBubbles.remove(key); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 293 | if (mStackView != null && bv != null) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 294 | mStackView.removeBubble(bv); |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 295 | bv.destroyActivityView(mStackView); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 296 | } |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 297 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 298 | NotificationEntry entry = bv != null ? bv.getEntry() : null; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 299 | if (entry != null) { |
| 300 | entry.setBubbleDismissed(true); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 301 | mNotificationEntryManager.updateNotifications(); |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 302 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 303 | updateVisibility(); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 306 | @Override |
| 307 | public void onBubbleBlocked(NotificationEntry entry) { |
| 308 | Object[] bubbles = mBubbles.values().toArray(); |
| 309 | for (int i = 0; i < bubbles.length; i++) { |
| 310 | NotificationEntry e = ((BubbleView) bubbles[i]).getEntry(); |
| 311 | boolean samePackage = entry.notification.getPackageName().equals( |
| 312 | e.notification.getPackageName()); |
| 313 | if (samePackage) { |
| 314 | removeBubble(entry.key); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 319 | @SuppressWarnings("FieldCanBeLocal") |
| 320 | private final NotificationEntryListener mEntryListener = new NotificationEntryListener() { |
| 321 | @Override |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 322 | public void onPendingEntryAdded(NotificationEntry entry) { |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 323 | if (!areBubblesEnabled(mContext)) { |
| 324 | return; |
| 325 | } |
| 326 | if (shouldAutoBubbleForFlags(mContext, entry) || shouldBubble(entry)) { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 327 | // TODO: handle group summaries |
| 328 | // 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] | 329 | entry.setIsBubble(true); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 330 | entry.setShowInShadeWhenBubble(true); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | @Override |
| 335 | public void onEntryInflated(NotificationEntry entry, |
| 336 | @NotificationInflater.InflationFlag int inflatedFlags) { |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 337 | if (!areBubblesEnabled(mContext)) { |
| 338 | return; |
| 339 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 340 | if (entry.isBubble() && mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) { |
| 341 | updateBubble(entry, true /* updatePosition */); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public void onPreEntryUpdated(NotificationEntry entry) { |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 347 | if (!areBubblesEnabled(mContext)) { |
| 348 | return; |
| 349 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 350 | if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry) |
| 351 | && alertAgain(entry, entry.notification.getNotification())) { |
| 352 | entry.setShowInShadeWhenBubble(true); |
| 353 | entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed |
| 354 | if (mBubbles.containsKey(entry.key)) { |
| 355 | mBubbles.get(entry.key).updateDotVisibility(); |
| 356 | } |
| 357 | updateBubble(entry, true /* updatePosition */); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | @Override |
| 362 | public void onEntryRemoved(NotificationEntry entry, |
| 363 | @Nullable NotificationVisibility visibility, |
| 364 | boolean removedByUser) { |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 365 | if (!areBubblesEnabled(mContext)) { |
| 366 | return; |
| 367 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 368 | entry.setShowInShadeWhenBubble(false); |
| 369 | if (mBubbles.containsKey(entry.key)) { |
| 370 | mBubbles.get(entry.key).updateDotVisibility(); |
| 371 | } |
| 372 | if (!removedByUser) { |
| 373 | // This was a cancel so we should remove the bubble |
| 374 | removeBubble(entry.key); |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | }; |
| 378 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 379 | /** |
| 380 | * Lets any listeners know if bubble state has changed. |
| 381 | */ |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 382 | private void updateBubblesShowing() { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 383 | if (mStackView == null) { |
| 384 | return; |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 385 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 386 | |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 387 | boolean hadBubbles = mStatusBarWindowController.getBubblesShowing(); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 388 | boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE; |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 389 | mStatusBarWindowController.setBubblesShowing(hasBubblesShowing); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 390 | if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) { |
| 391 | mStateChangeListener.onHasBubblesChanged(hasBubblesShowing); |
| 392 | } |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /** |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 396 | * Updates the visibility of the bubbles based on current state. |
| 397 | * Does not un-bubble, just hides or un-hides. Will notify any |
| 398 | * {@link BubbleStateChangeListener}s if visibility changes. |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 399 | */ |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 400 | public void updateVisibility() { |
| 401 | if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) { |
| 402 | // Bubbles only appear in unlocked shade |
| 403 | mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE); |
| 404 | } else if (mStackView != null) { |
| 405 | mStackView.setVisibility(INVISIBLE); |
| 406 | collapseStack(); |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 407 | } |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 408 | updateBubblesShowing(); |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Rect indicating the touchable region for the bubble stack / expanded stack. |
| 413 | */ |
| 414 | public Rect getTouchableRegion() { |
| 415 | if (mStackView == null || mStackView.getVisibility() != VISIBLE) { |
| 416 | return null; |
| 417 | } |
| 418 | mStackView.getBoundsOnScreen(mTempRect); |
| 419 | return mTempRect; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 422 | @VisibleForTesting |
| 423 | BubbleStackView getStackView() { |
| 424 | return mStackView; |
| 425 | } |
| 426 | |
| 427 | @Nullable |
| 428 | private PendingIntent getValidBubbleIntent(NotificationEntry notif) { |
| 429 | Notification notification = notif.notification.getNotification(); |
| 430 | Notification.BubbleMetadata data = notif.getBubbleMetadata(); |
| 431 | if (data != null && canLaunchInActivityView(data.getIntent())) { |
| 432 | return data.getIntent(); |
| 433 | } else if (shouldUseContentIntent(mContext) |
| 434 | && canLaunchInActivityView(notification.contentIntent)) { |
| 435 | Log.d(TAG, "[addBubble " + notif.key |
| 436 | + "]: No appOverlayIntent, using contentIntent."); |
| 437 | return notification.contentIntent; |
| 438 | } |
| 439 | Log.d(TAG, "[addBubble " + notif.key + "]: No supported intent for ActivityView."); |
| 440 | return null; |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Whether an intent is properly configured to display in an {@link android.app.ActivityView}. |
| 445 | */ |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 446 | private boolean canLaunchInActivityView(PendingIntent intent) { |
| 447 | if (intent == null) { |
| 448 | return false; |
| 449 | } |
| 450 | ActivityInfo info = |
| 451 | intent.getIntent().resolveActivityInfo(mContext.getPackageManager(), 0); |
| 452 | return info != null |
| 453 | && ActivityInfo.isResizeableMode(info.resizeMode) |
| 454 | && (info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) != 0; |
| 455 | } |
| 456 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 457 | /** |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 458 | * Whether the notification has been developer configured to bubble and is allowed by the user. |
| 459 | */ |
Mady Mellor | c18ba96 | 2019-01-29 11:11:56 -0800 | [diff] [blame] | 460 | @VisibleForTesting |
| 461 | protected boolean shouldBubble(NotificationEntry entry) { |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 462 | StatusBarNotification n = entry.notification; |
| 463 | boolean canAppOverlay = false; |
| 464 | try { |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 465 | canAppOverlay = mNotificationManagerService.areBubblesAllowedForPackage( |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 466 | n.getPackageName(), n.getUid()); |
| 467 | } catch (RemoteException e) { |
| 468 | Log.w(TAG, "Error calling NoMan to determine if app can overlay", e); |
| 469 | } |
| 470 | |
Mady Mellor | c18ba96 | 2019-01-29 11:11:56 -0800 | [diff] [blame] | 471 | NotificationChannel channel = mNotificationEntryManager.getNotificationData().getChannel( |
| 472 | entry.key); |
| 473 | boolean canChannelOverlay = channel != null && channel.canBubble(); |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 474 | boolean hasOverlayIntent = n.getNotification().getBubbleMetadata() != null |
| 475 | && n.getNotification().getBubbleMetadata().getIntent() != null; |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 476 | return hasOverlayIntent && canChannelOverlay && canAppOverlay; |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /** |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 480 | * 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] | 481 | */ |
Mady Mellor | 9bad224 | 2019-01-28 11:21:51 -0800 | [diff] [blame] | 482 | @VisibleForTesting |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 483 | protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) { |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 484 | if (entry.isBubbleDismissed()) { |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 485 | return false; |
| 486 | } |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 487 | StatusBarNotification n = entry.notification; |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 488 | |
| 489 | boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE; |
| 490 | boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE; |
| 491 | boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE; |
| 492 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 493 | boolean hasRemoteInput = false; |
| 494 | if (n.getNotification().actions != null) { |
| 495 | for (Notification.Action action : n.getNotification().actions) { |
| 496 | if (action.getRemoteInputs() != null) { |
| 497 | hasRemoteInput = true; |
| 498 | break; |
| 499 | } |
| 500 | } |
| 501 | } |
Mady Mellor | 711f956 | 2018-12-05 14:53:46 -0800 | [diff] [blame] | 502 | boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category) |
| 503 | && n.isOngoing(); |
| 504 | boolean isMusic = n.getNotification().hasMediaSession(); |
| 505 | boolean isImportantOngoing = isMusic || isCall; |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 506 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 507 | Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle(); |
Mady Mellor | e317537 | 2018-12-04 17:05:11 -0800 | [diff] [blame] | 508 | boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category); |
| 509 | boolean isMessageStyle = Notification.MessagingStyle.class.equals(style); |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 510 | return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages) |
Mady Mellor | 711f956 | 2018-12-05 14:53:46 -0800 | [diff] [blame] | 511 | || (isImportantOngoing && autoBubbleOngoing) |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 512 | || autoBubbleAll; |
| 513 | } |
| 514 | |
| 515 | private static boolean shouldAutoBubbleMessages(Context context) { |
| 516 | return Settings.Secure.getInt(context.getContentResolver(), |
| 517 | ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0; |
| 518 | } |
| 519 | |
| 520 | private static boolean shouldAutoBubbleOngoing(Context context) { |
| 521 | return Settings.Secure.getInt(context.getContentResolver(), |
| 522 | ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0; |
| 523 | } |
| 524 | |
| 525 | private static boolean shouldAutoBubbleAll(Context context) { |
| 526 | return Settings.Secure.getInt(context.getContentResolver(), |
| 527 | ENABLE_AUTO_BUBBLE_ALL, 0) != 0; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 528 | } |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 529 | |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 530 | private static boolean shouldUseContentIntent(Context context) { |
| 531 | return Settings.Secure.getInt(context.getContentResolver(), |
| 532 | ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0; |
| 533 | } |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 534 | |
| 535 | private static boolean areBubblesEnabled(Context context) { |
| 536 | return Settings.Secure.getInt(context.getContentResolver(), |
| 537 | ENABLE_BUBBLES, 1) != 0; |
| 538 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 539 | } |