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 | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 73 | public class BubbleController { |
| 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 | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 269 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 270 | // It's new |
| 271 | BubbleView bubble = (BubbleView) mInflater.inflate( |
| 272 | R.layout.bubble_view, mStackView, false /* attachToRoot */); |
| 273 | bubble.setNotif(notif); |
Mady Mellor | edd4ee1 | 2019-01-18 10:45:11 -0800 | [diff] [blame] | 274 | PendingIntent bubbleIntent = getValidBubbleIntent(notif); |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 275 | if (bubbleIntent != null) { |
| 276 | bubble.setBubbleIntent(bubbleIntent); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 277 | } |
| 278 | mBubbles.put(bubble.getKey(), bubble); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 279 | mStackView.addBubble(bubble); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 280 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 281 | updateVisibility(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Removes the bubble associated with the {@param uri}. |
Mark Renouf | 658c6bc | 2019-01-30 10:26:54 -0500 | [diff] [blame] | 286 | * <p> |
| 287 | * Must be called from the main thread. |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 288 | */ |
Mark Renouf | 658c6bc | 2019-01-30 10:26:54 -0500 | [diff] [blame] | 289 | @MainThread |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 290 | void removeBubble(String key) { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 291 | BubbleView bv = mBubbles.remove(key); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 292 | if (mStackView != null && bv != null) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 293 | mStackView.removeBubble(bv); |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 294 | bv.destroyActivityView(mStackView); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 295 | } |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 296 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 297 | NotificationEntry entry = bv != null ? bv.getEntry() : null; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 298 | if (entry != null) { |
| 299 | entry.setBubbleDismissed(true); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 300 | mNotificationEntryManager.updateNotifications(); |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 301 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 302 | updateVisibility(); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 305 | @SuppressWarnings("FieldCanBeLocal") |
| 306 | private final NotificationEntryListener mEntryListener = new NotificationEntryListener() { |
| 307 | @Override |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 308 | public void onPendingEntryAdded(NotificationEntry entry) { |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 309 | if (!areBubblesEnabled(mContext)) { |
| 310 | return; |
| 311 | } |
| 312 | if (shouldAutoBubbleForFlags(mContext, entry) || shouldBubble(entry)) { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 313 | // TODO: handle group summaries |
| 314 | // 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] | 315 | entry.setIsBubble(true); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 316 | entry.setShowInShadeWhenBubble(true); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | @Override |
| 321 | public void onEntryInflated(NotificationEntry entry, |
| 322 | @NotificationInflater.InflationFlag int inflatedFlags) { |
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 (entry.isBubble() && mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) { |
| 327 | updateBubble(entry, true /* updatePosition */); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | public void onPreEntryUpdated(NotificationEntry entry) { |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 333 | if (!areBubblesEnabled(mContext)) { |
| 334 | return; |
| 335 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 336 | if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry) |
| 337 | && alertAgain(entry, entry.notification.getNotification())) { |
| 338 | entry.setShowInShadeWhenBubble(true); |
| 339 | entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed |
| 340 | if (mBubbles.containsKey(entry.key)) { |
| 341 | mBubbles.get(entry.key).updateDotVisibility(); |
| 342 | } |
| 343 | updateBubble(entry, true /* updatePosition */); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public void onEntryRemoved(NotificationEntry entry, |
| 349 | @Nullable NotificationVisibility visibility, |
| 350 | boolean removedByUser) { |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 351 | if (!areBubblesEnabled(mContext)) { |
| 352 | return; |
| 353 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 354 | entry.setShowInShadeWhenBubble(false); |
| 355 | if (mBubbles.containsKey(entry.key)) { |
| 356 | mBubbles.get(entry.key).updateDotVisibility(); |
| 357 | } |
| 358 | if (!removedByUser) { |
| 359 | // This was a cancel so we should remove the bubble |
| 360 | removeBubble(entry.key); |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | }; |
| 364 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 365 | /** |
| 366 | * Lets any listeners know if bubble state has changed. |
| 367 | */ |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 368 | private void updateBubblesShowing() { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 369 | if (mStackView == null) { |
| 370 | return; |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 371 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 372 | |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 373 | boolean hadBubbles = mStatusBarWindowController.getBubblesShowing(); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 374 | boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE; |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 375 | mStatusBarWindowController.setBubblesShowing(hasBubblesShowing); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 376 | if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) { |
| 377 | mStateChangeListener.onHasBubblesChanged(hasBubblesShowing); |
| 378 | } |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /** |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 382 | * Updates the visibility of the bubbles based on current state. |
| 383 | * Does not un-bubble, just hides or un-hides. Will notify any |
| 384 | * {@link BubbleStateChangeListener}s if visibility changes. |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 385 | */ |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 386 | public void updateVisibility() { |
| 387 | if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) { |
| 388 | // Bubbles only appear in unlocked shade |
| 389 | mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE); |
| 390 | } else if (mStackView != null) { |
| 391 | mStackView.setVisibility(INVISIBLE); |
| 392 | collapseStack(); |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 393 | } |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 394 | updateBubblesShowing(); |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Rect indicating the touchable region for the bubble stack / expanded stack. |
| 399 | */ |
| 400 | public Rect getTouchableRegion() { |
| 401 | if (mStackView == null || mStackView.getVisibility() != VISIBLE) { |
| 402 | return null; |
| 403 | } |
| 404 | mStackView.getBoundsOnScreen(mTempRect); |
| 405 | return mTempRect; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 408 | @VisibleForTesting |
| 409 | BubbleStackView getStackView() { |
| 410 | return mStackView; |
| 411 | } |
| 412 | |
| 413 | @Nullable |
| 414 | private PendingIntent getValidBubbleIntent(NotificationEntry notif) { |
| 415 | Notification notification = notif.notification.getNotification(); |
| 416 | Notification.BubbleMetadata data = notif.getBubbleMetadata(); |
| 417 | if (data != null && canLaunchInActivityView(data.getIntent())) { |
| 418 | return data.getIntent(); |
| 419 | } else if (shouldUseContentIntent(mContext) |
| 420 | && canLaunchInActivityView(notification.contentIntent)) { |
| 421 | Log.d(TAG, "[addBubble " + notif.key |
| 422 | + "]: No appOverlayIntent, using contentIntent."); |
| 423 | return notification.contentIntent; |
| 424 | } |
| 425 | Log.d(TAG, "[addBubble " + notif.key + "]: No supported intent for ActivityView."); |
| 426 | return null; |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Whether an intent is properly configured to display in an {@link android.app.ActivityView}. |
| 431 | */ |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 432 | private boolean canLaunchInActivityView(PendingIntent intent) { |
| 433 | if (intent == null) { |
| 434 | return false; |
| 435 | } |
| 436 | ActivityInfo info = |
| 437 | intent.getIntent().resolveActivityInfo(mContext.getPackageManager(), 0); |
| 438 | return info != null |
| 439 | && ActivityInfo.isResizeableMode(info.resizeMode) |
| 440 | && (info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) != 0; |
| 441 | } |
| 442 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 443 | /** |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 444 | * Whether the notification has been developer configured to bubble and is allowed by the user. |
| 445 | */ |
Mady Mellor | c18ba96 | 2019-01-29 11:11:56 -0800 | [diff] [blame] | 446 | @VisibleForTesting |
| 447 | protected boolean shouldBubble(NotificationEntry entry) { |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 448 | StatusBarNotification n = entry.notification; |
| 449 | boolean canAppOverlay = false; |
| 450 | try { |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 451 | canAppOverlay = mNotificationManagerService.areBubblesAllowedForPackage( |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 452 | n.getPackageName(), n.getUid()); |
| 453 | } catch (RemoteException e) { |
| 454 | Log.w(TAG, "Error calling NoMan to determine if app can overlay", e); |
| 455 | } |
| 456 | |
Mady Mellor | c18ba96 | 2019-01-29 11:11:56 -0800 | [diff] [blame] | 457 | NotificationChannel channel = mNotificationEntryManager.getNotificationData().getChannel( |
| 458 | entry.key); |
| 459 | boolean canChannelOverlay = channel != null && channel.canBubble(); |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 460 | boolean hasOverlayIntent = n.getNotification().getBubbleMetadata() != null |
| 461 | && n.getNotification().getBubbleMetadata().getIntent() != null; |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 462 | return hasOverlayIntent && canChannelOverlay && canAppOverlay; |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /** |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 466 | * 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] | 467 | */ |
Mady Mellor | 9bad224 | 2019-01-28 11:21:51 -0800 | [diff] [blame] | 468 | @VisibleForTesting |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 469 | protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) { |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 470 | if (entry.isBubbleDismissed()) { |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 471 | return false; |
| 472 | } |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 473 | StatusBarNotification n = entry.notification; |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 474 | |
| 475 | boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE; |
| 476 | boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE; |
| 477 | boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE; |
| 478 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 479 | boolean hasRemoteInput = false; |
| 480 | if (n.getNotification().actions != null) { |
| 481 | for (Notification.Action action : n.getNotification().actions) { |
| 482 | if (action.getRemoteInputs() != null) { |
| 483 | hasRemoteInput = true; |
| 484 | break; |
| 485 | } |
| 486 | } |
| 487 | } |
Mady Mellor | 711f956 | 2018-12-05 14:53:46 -0800 | [diff] [blame] | 488 | boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category) |
| 489 | && n.isOngoing(); |
| 490 | boolean isMusic = n.getNotification().hasMediaSession(); |
| 491 | boolean isImportantOngoing = isMusic || isCall; |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 492 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 493 | Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle(); |
Mady Mellor | e317537 | 2018-12-04 17:05:11 -0800 | [diff] [blame] | 494 | boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category); |
| 495 | boolean isMessageStyle = Notification.MessagingStyle.class.equals(style); |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 496 | return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages) |
Mady Mellor | 711f956 | 2018-12-05 14:53:46 -0800 | [diff] [blame] | 497 | || (isImportantOngoing && autoBubbleOngoing) |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 498 | || autoBubbleAll; |
| 499 | } |
| 500 | |
| 501 | private static boolean shouldAutoBubbleMessages(Context context) { |
| 502 | return Settings.Secure.getInt(context.getContentResolver(), |
| 503 | ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0; |
| 504 | } |
| 505 | |
| 506 | private static boolean shouldAutoBubbleOngoing(Context context) { |
| 507 | return Settings.Secure.getInt(context.getContentResolver(), |
| 508 | ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0; |
| 509 | } |
| 510 | |
| 511 | private static boolean shouldAutoBubbleAll(Context context) { |
| 512 | return Settings.Secure.getInt(context.getContentResolver(), |
| 513 | ENABLE_AUTO_BUBBLE_ALL, 0) != 0; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 514 | } |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 515 | |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 516 | private static boolean shouldUseContentIntent(Context context) { |
| 517 | return Settings.Secure.getInt(context.getContentResolver(), |
| 518 | ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0; |
| 519 | } |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 520 | |
| 521 | private static boolean areBubblesEnabled(Context context) { |
| 522 | return Settings.Secure.getInt(context.getContentResolver(), |
| 523 | ENABLE_BUBBLES, 1) != 0; |
| 524 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 525 | } |