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