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