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 | |
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; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 49 | import com.android.systemui.statusbar.StatusBarStateController; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 50 | import com.android.systemui.statusbar.notification.NotificationEntryListener; |
| 51 | import com.android.systemui.statusbar.notification.NotificationEntryManager; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 52 | import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 53 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 54 | import com.android.systemui.statusbar.notification.row.NotificationInflater; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 55 | import com.android.systemui.statusbar.phone.StatusBarWindowController; |
| 56 | |
| 57 | import java.util.HashMap; |
| 58 | import java.util.Map; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 59 | import java.util.Set; |
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 | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 71 | public class BubbleController { |
| 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; |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 91 | private BubbleStateChangeListener mStateChangeListener; |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 92 | private BubbleExpandListener mExpandListener; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 93 | private LayoutInflater mInflater; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 94 | |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 95 | private final Map<String, BubbleView> mBubbles = new HashMap<>(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 96 | private BubbleStackView mStackView; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 97 | private final Point mDisplaySize; |
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; |
| 156 | WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
| 157 | mDisplaySize = new Point(); |
| 158 | wm.getDefaultDisplay().getSize(mDisplaySize); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 159 | mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 160 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 161 | mNotificationEntryManager = Dependency.get(NotificationEntryManager.class); |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 162 | mNotificationEntryManager.addNotificationEntryListener(mEntryListener); |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 163 | |
| 164 | try { |
| 165 | mNotificationManagerService = INotificationManager.Stub.asInterface( |
| 166 | ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE)); |
| 167 | } catch (ServiceManager.ServiceNotFoundException e) { |
| 168 | e.printStackTrace(); |
| 169 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 170 | |
| 171 | mStatusBarWindowController = statusBarWindowController; |
| 172 | mStatusBarStateListener = new StatusBarStateListener(); |
| 173 | Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener); |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 177 | * Set a listener to be notified when some states of the bubbles change. |
| 178 | */ |
| 179 | public void setBubbleStateChangeListener(BubbleStateChangeListener listener) { |
| 180 | mStateChangeListener = listener; |
| 181 | } |
| 182 | |
| 183 | /** |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 184 | * Set a listener to be notified of bubble expand events. |
| 185 | */ |
| 186 | public void setExpandListener(BubbleExpandListener listener) { |
| 187 | mExpandListener = listener; |
| 188 | if (mStackView != null) { |
| 189 | mStackView.setExpandListener(mExpandListener); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 194 | * Whether or not there are bubbles present, regardless of them being visible on the |
| 195 | * screen (e.g. if on AOD). |
| 196 | */ |
| 197 | public boolean hasBubbles() { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 198 | for (BubbleView bv : mBubbles.values()) { |
| 199 | if (!bv.getEntry().isBubbleDismissed()) { |
| 200 | return true; |
| 201 | } |
| 202 | } |
| 203 | return false; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Whether the stack of bubbles is expanded or not. |
| 208 | */ |
| 209 | public boolean isStackExpanded() { |
| 210 | return mStackView != null && mStackView.isExpanded(); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Tell the stack of bubbles to collapse. |
| 215 | */ |
| 216 | public void collapseStack() { |
| 217 | if (mStackView != null) { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 218 | mStackView.collapseStack(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack. |
| 224 | */ |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 225 | void dismissStack() { |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 226 | if (mStackView == null) { |
| 227 | return; |
| 228 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 229 | Set<String> keys = mBubbles.keySet(); |
| 230 | for (String key: keys) { |
| 231 | mBubbles.get(key).getEntry().setBubbleDismissed(true); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 232 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 233 | mStackView.stackDismissed(); |
| 234 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 235 | updateVisibility(); |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 236 | mNotificationEntryManager.updateNotifications(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /** |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 240 | * Adds or updates a bubble associated with the provided notification entry. |
| 241 | * |
| 242 | * @param notif the notification associated with this bubble. |
| 243 | * @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] | 244 | */ |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 245 | public void updateBubble(NotificationEntry notif, boolean updatePosition) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 246 | if (mBubbles.containsKey(notif.key)) { |
| 247 | // It's an update |
| 248 | BubbleView bubble = mBubbles.get(notif.key); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 249 | mStackView.updateBubble(bubble, notif, updatePosition); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 250 | } else { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 251 | if (mStackView == null) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 252 | mStackView = new BubbleStackView(mContext); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 253 | ViewGroup sbv = mStatusBarWindowController.getStatusBarView(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 254 | // XXX: Bug when you expand the shade on top of expanded bubble, there is no scrim |
| 255 | // between bubble and the shade |
| 256 | int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1; |
| 257 | sbv.addView(mStackView, bubblePosition, |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 258 | new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)); |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 259 | if (mExpandListener != null) { |
| 260 | mStackView.setExpandListener(mExpandListener); |
| 261 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 262 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 263 | // It's new |
| 264 | BubbleView bubble = (BubbleView) mInflater.inflate( |
| 265 | R.layout.bubble_view, mStackView, false /* attachToRoot */); |
| 266 | bubble.setNotif(notif); |
Mady Mellor | edd4ee1 | 2019-01-18 10:45:11 -0800 | [diff] [blame] | 267 | PendingIntent bubbleIntent = getValidBubbleIntent(notif); |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame^] | 268 | if (bubbleIntent != null) { |
| 269 | bubble.setBubbleIntent(bubbleIntent); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 270 | } |
| 271 | mBubbles.put(bubble.getKey(), bubble); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 272 | mStackView.addBubble(bubble); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 273 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 274 | updateVisibility(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Removes the bubble associated with the {@param uri}. |
| 279 | */ |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 280 | void removeBubble(String key) { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 281 | BubbleView bv = mBubbles.remove(key); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 282 | if (mStackView != null && bv != null) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 283 | mStackView.removeBubble(bv); |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 284 | bv.destroyActivityView(mStackView); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 285 | } |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 286 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 287 | NotificationEntry entry = bv != null ? bv.getEntry() : null; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 288 | if (entry != null) { |
| 289 | entry.setBubbleDismissed(true); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 290 | mNotificationEntryManager.updateNotifications(); |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 291 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 292 | updateVisibility(); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 295 | @SuppressWarnings("FieldCanBeLocal") |
| 296 | private final NotificationEntryListener mEntryListener = new NotificationEntryListener() { |
| 297 | @Override |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 298 | public void onPendingEntryAdded(NotificationEntry entry) { |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame^] | 299 | if (!areBubblesEnabled(mContext)) { |
| 300 | return; |
| 301 | } |
| 302 | if (shouldAutoBubbleForFlags(mContext, entry) || shouldBubble(entry)) { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 303 | // TODO: handle group summaries |
| 304 | // 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] | 305 | entry.setIsBubble(true); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 306 | entry.setShowInShadeWhenBubble(true); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | @Override |
| 311 | public void onEntryInflated(NotificationEntry entry, |
| 312 | @NotificationInflater.InflationFlag int inflatedFlags) { |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame^] | 313 | if (!areBubblesEnabled(mContext)) { |
| 314 | return; |
| 315 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 316 | if (entry.isBubble() && mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) { |
| 317 | updateBubble(entry, true /* updatePosition */); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public void onPreEntryUpdated(NotificationEntry entry) { |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame^] | 323 | if (!areBubblesEnabled(mContext)) { |
| 324 | return; |
| 325 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 326 | if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry) |
| 327 | && alertAgain(entry, entry.notification.getNotification())) { |
| 328 | entry.setShowInShadeWhenBubble(true); |
| 329 | entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed |
| 330 | if (mBubbles.containsKey(entry.key)) { |
| 331 | mBubbles.get(entry.key).updateDotVisibility(); |
| 332 | } |
| 333 | updateBubble(entry, true /* updatePosition */); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | @Override |
| 338 | public void onEntryRemoved(NotificationEntry entry, |
| 339 | @Nullable NotificationVisibility visibility, |
| 340 | boolean removedByUser) { |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame^] | 341 | if (!areBubblesEnabled(mContext)) { |
| 342 | return; |
| 343 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 344 | entry.setShowInShadeWhenBubble(false); |
| 345 | if (mBubbles.containsKey(entry.key)) { |
| 346 | mBubbles.get(entry.key).updateDotVisibility(); |
| 347 | } |
| 348 | if (!removedByUser) { |
| 349 | // This was a cancel so we should remove the bubble |
| 350 | removeBubble(entry.key); |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | }; |
| 354 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 355 | /** |
| 356 | * Lets any listeners know if bubble state has changed. |
| 357 | */ |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 358 | private void updateBubblesShowing() { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 359 | if (mStackView == null) { |
| 360 | return; |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 361 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 362 | |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 363 | boolean hadBubbles = mStatusBarWindowController.getBubblesShowing(); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 364 | boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE; |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 365 | mStatusBarWindowController.setBubblesShowing(hasBubblesShowing); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 366 | if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) { |
| 367 | mStateChangeListener.onHasBubblesChanged(hasBubblesShowing); |
| 368 | } |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /** |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 372 | * Updates the visibility of the bubbles based on current state. |
| 373 | * Does not un-bubble, just hides or un-hides. Will notify any |
| 374 | * {@link BubbleStateChangeListener}s if visibility changes. |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 375 | */ |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 376 | public void updateVisibility() { |
| 377 | if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) { |
| 378 | // Bubbles only appear in unlocked shade |
| 379 | mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE); |
| 380 | } else if (mStackView != null) { |
| 381 | mStackView.setVisibility(INVISIBLE); |
| 382 | collapseStack(); |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 383 | } |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 384 | updateBubblesShowing(); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Rect indicating the touchable region for the bubble stack / expanded stack. |
| 389 | */ |
| 390 | public Rect getTouchableRegion() { |
| 391 | if (mStackView == null || mStackView.getVisibility() != VISIBLE) { |
| 392 | return null; |
| 393 | } |
| 394 | mStackView.getBoundsOnScreen(mTempRect); |
| 395 | return mTempRect; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 396 | } |
| 397 | |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame^] | 398 | @VisibleForTesting |
| 399 | BubbleStackView getStackView() { |
| 400 | return mStackView; |
| 401 | } |
| 402 | |
| 403 | @Nullable |
| 404 | private PendingIntent getValidBubbleIntent(NotificationEntry notif) { |
| 405 | Notification notification = notif.notification.getNotification(); |
| 406 | Notification.BubbleMetadata data = notif.getBubbleMetadata(); |
| 407 | if (data != null && canLaunchInActivityView(data.getIntent())) { |
| 408 | return data.getIntent(); |
| 409 | } else if (shouldUseContentIntent(mContext) |
| 410 | && canLaunchInActivityView(notification.contentIntent)) { |
| 411 | Log.d(TAG, "[addBubble " + notif.key |
| 412 | + "]: No appOverlayIntent, using contentIntent."); |
| 413 | return notification.contentIntent; |
| 414 | } |
| 415 | Log.d(TAG, "[addBubble " + notif.key + "]: No supported intent for ActivityView."); |
| 416 | return null; |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Whether an intent is properly configured to display in an {@link android.app.ActivityView}. |
| 421 | */ |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 422 | private boolean canLaunchInActivityView(PendingIntent intent) { |
| 423 | if (intent == null) { |
| 424 | return false; |
| 425 | } |
| 426 | ActivityInfo info = |
| 427 | intent.getIntent().resolveActivityInfo(mContext.getPackageManager(), 0); |
| 428 | return info != null |
| 429 | && ActivityInfo.isResizeableMode(info.resizeMode) |
| 430 | && (info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) != 0; |
| 431 | } |
| 432 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 433 | /** |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 434 | * Whether the notification has been developer configured to bubble and is allowed by the user. |
| 435 | */ |
Mady Mellor | c18ba96 | 2019-01-29 11:11:56 -0800 | [diff] [blame] | 436 | @VisibleForTesting |
| 437 | protected boolean shouldBubble(NotificationEntry entry) { |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 438 | StatusBarNotification n = entry.notification; |
| 439 | boolean canAppOverlay = false; |
| 440 | try { |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 441 | canAppOverlay = mNotificationManagerService.areBubblesAllowedForPackage( |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 442 | n.getPackageName(), n.getUid()); |
| 443 | } catch (RemoteException e) { |
| 444 | Log.w(TAG, "Error calling NoMan to determine if app can overlay", e); |
| 445 | } |
| 446 | |
Mady Mellor | c18ba96 | 2019-01-29 11:11:56 -0800 | [diff] [blame] | 447 | NotificationChannel channel = mNotificationEntryManager.getNotificationData().getChannel( |
| 448 | entry.key); |
| 449 | boolean canChannelOverlay = channel != null && channel.canBubble(); |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 450 | boolean hasOverlayIntent = n.getNotification().getBubbleMetadata() != null |
| 451 | && n.getNotification().getBubbleMetadata().getIntent() != null; |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame^] | 452 | return hasOverlayIntent && canChannelOverlay && canAppOverlay; |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /** |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame^] | 456 | * 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] | 457 | */ |
Mady Mellor | 9bad224 | 2019-01-28 11:21:51 -0800 | [diff] [blame] | 458 | @VisibleForTesting |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame^] | 459 | protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) { |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 460 | if (entry.isBubbleDismissed()) { |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 461 | return false; |
| 462 | } |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 463 | StatusBarNotification n = entry.notification; |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 464 | |
| 465 | boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE; |
| 466 | boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE; |
| 467 | boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE; |
| 468 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 469 | boolean hasRemoteInput = false; |
| 470 | if (n.getNotification().actions != null) { |
| 471 | for (Notification.Action action : n.getNotification().actions) { |
| 472 | if (action.getRemoteInputs() != null) { |
| 473 | hasRemoteInput = true; |
| 474 | break; |
| 475 | } |
| 476 | } |
| 477 | } |
Mady Mellor | 711f956 | 2018-12-05 14:53:46 -0800 | [diff] [blame] | 478 | boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category) |
| 479 | && n.isOngoing(); |
| 480 | boolean isMusic = n.getNotification().hasMediaSession(); |
| 481 | boolean isImportantOngoing = isMusic || isCall; |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 482 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 483 | Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle(); |
Mady Mellor | e317537 | 2018-12-04 17:05:11 -0800 | [diff] [blame] | 484 | boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category); |
| 485 | boolean isMessageStyle = Notification.MessagingStyle.class.equals(style); |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame^] | 486 | return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages) |
Mady Mellor | 711f956 | 2018-12-05 14:53:46 -0800 | [diff] [blame] | 487 | || (isImportantOngoing && autoBubbleOngoing) |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 488 | || autoBubbleAll; |
| 489 | } |
| 490 | |
| 491 | private static boolean shouldAutoBubbleMessages(Context context) { |
| 492 | return Settings.Secure.getInt(context.getContentResolver(), |
| 493 | ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0; |
| 494 | } |
| 495 | |
| 496 | private static boolean shouldAutoBubbleOngoing(Context context) { |
| 497 | return Settings.Secure.getInt(context.getContentResolver(), |
| 498 | ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0; |
| 499 | } |
| 500 | |
| 501 | private static boolean shouldAutoBubbleAll(Context context) { |
| 502 | return Settings.Secure.getInt(context.getContentResolver(), |
| 503 | ENABLE_AUTO_BUBBLE_ALL, 0) != 0; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 504 | } |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 505 | |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 506 | private static boolean shouldUseContentIntent(Context context) { |
| 507 | return Settings.Secure.getInt(context.getContentResolver(), |
| 508 | ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0; |
| 509 | } |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame^] | 510 | |
| 511 | private static boolean areBubblesEnabled(Context context) { |
| 512 | return Settings.Secure.getInt(context.getContentResolver(), |
| 513 | ENABLE_BUBBLES, 1) != 0; |
| 514 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 515 | } |