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; |
| 21 | import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; |
| 22 | |
| 23 | import static com.android.systemui.bubbles.BubbleMovementHelper.EDGE_OVERLAP; |
| 24 | |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame^] | 25 | import android.annotation.Nullable; |
| 26 | import android.app.INotificationManager; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 27 | import android.app.Notification; |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 28 | import android.app.PendingIntent; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 29 | import android.content.Context; |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 30 | import android.content.pm.ActivityInfo; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 31 | import android.graphics.Point; |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 32 | import android.graphics.Rect; |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame^] | 33 | import android.os.RemoteException; |
| 34 | import android.os.ServiceManager; |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 35 | import android.provider.Settings; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 36 | import android.service.notification.StatusBarNotification; |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 37 | import android.util.Log; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 38 | import android.view.ViewGroup; |
| 39 | import android.view.WindowManager; |
| 40 | import android.widget.FrameLayout; |
| 41 | |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 42 | import com.android.internal.annotations.VisibleForTesting; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 43 | import com.android.systemui.Dependency; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 44 | import com.android.systemui.R; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 45 | import com.android.systemui.statusbar.notification.NotificationEntryListener; |
| 46 | import com.android.systemui.statusbar.notification.NotificationEntryManager; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 47 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 48 | import com.android.systemui.statusbar.phone.StatusBarWindowController; |
| 49 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 50 | import java.util.ArrayList; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 51 | import java.util.HashMap; |
| 52 | import java.util.Map; |
| 53 | |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 54 | import javax.inject.Inject; |
| 55 | import javax.inject.Singleton; |
| 56 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 57 | /** |
| 58 | * Bubbles are a special type of content that can "float" on top of other apps or System UI. |
| 59 | * Bubbles can be expanded to show more content. |
| 60 | * |
| 61 | * The controller manages addition, removal, and visible state of bubbles on screen. |
| 62 | */ |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 63 | @Singleton |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 64 | public class BubbleController { |
| 65 | private static final int MAX_BUBBLES = 5; // TODO: actually enforce this |
| 66 | |
| 67 | private static final String TAG = "BubbleController"; |
| 68 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 69 | // Enables some subset of notifs to automatically become bubbles |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 70 | private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 71 | // When a bubble is dismissed, recreate it as a notification |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 72 | private static final boolean DEBUG_DEMOTE_TO_NOTIF = false; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 73 | |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 74 | // Secure settings |
| 75 | private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging"; |
| 76 | private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing"; |
| 77 | private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all"; |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 78 | private static final String ENABLE_BUBBLE_ACTIVITY_VIEW = "experiment_bubble_activity_view"; |
| 79 | private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent"; |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 80 | |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 81 | private final Context mContext; |
| 82 | private final NotificationEntryManager mNotificationEntryManager; |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 83 | private BubbleStateChangeListener mStateChangeListener; |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 84 | private BubbleExpandListener mExpandListener; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 85 | |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 86 | private final Map<String, BubbleView> mBubbles = new HashMap<>(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 87 | private BubbleStackView mStackView; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 88 | private final Point mDisplaySize; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 89 | |
| 90 | // Bubbles get added to the status bar view |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 91 | private final StatusBarWindowController mStatusBarWindowController; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 92 | |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame^] | 93 | private INotificationManager mNotificationManagerService; |
| 94 | |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 95 | // Used for determining view rect for touch interaction |
| 96 | private Rect mTempRect = new Rect(); |
| 97 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 98 | /** |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 99 | * Listener to be notified when some states of the bubbles change. |
| 100 | */ |
| 101 | public interface BubbleStateChangeListener { |
| 102 | /** |
| 103 | * Called when the stack has bubbles or no longer has bubbles. |
| 104 | */ |
| 105 | void onHasBubblesChanged(boolean hasBubbles); |
| 106 | } |
| 107 | |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 108 | /** |
| 109 | * Listener to find out about stack expansion / collapse events. |
| 110 | */ |
| 111 | public interface BubbleExpandListener { |
| 112 | /** |
| 113 | * Called when the expansion state of the bubble stack changes. |
| 114 | * |
| 115 | * @param isExpanding whether it's expanding or collapsing |
| 116 | * @param amount fraction of how expanded or collapsed it is, 1 being fully, 0 at the start |
| 117 | */ |
| 118 | void onBubbleExpandChanged(boolean isExpanding, float amount); |
| 119 | } |
| 120 | |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 121 | @Inject |
Jason Monk | 92d5c24 | 2018-12-21 14:37:34 -0500 | [diff] [blame] | 122 | public BubbleController(Context context, StatusBarWindowController statusBarWindowController) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 123 | mContext = context; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 124 | mNotificationEntryManager = Dependency.get(NotificationEntryManager.class); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 125 | WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
| 126 | mDisplaySize = new Point(); |
| 127 | wm.getDefaultDisplay().getSize(mDisplaySize); |
Jason Monk | 92d5c24 | 2018-12-21 14:37:34 -0500 | [diff] [blame] | 128 | mStatusBarWindowController = statusBarWindowController; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 129 | |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 130 | mNotificationEntryManager.addNotificationEntryListener(mEntryListener); |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame^] | 131 | |
| 132 | try { |
| 133 | mNotificationManagerService = INotificationManager.Stub.asInterface( |
| 134 | ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE)); |
| 135 | } catch (ServiceManager.ServiceNotFoundException e) { |
| 136 | e.printStackTrace(); |
| 137 | } |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 141 | * Set a listener to be notified when some states of the bubbles change. |
| 142 | */ |
| 143 | public void setBubbleStateChangeListener(BubbleStateChangeListener listener) { |
| 144 | mStateChangeListener = listener; |
| 145 | } |
| 146 | |
| 147 | /** |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 148 | * Set a listener to be notified of bubble expand events. |
| 149 | */ |
| 150 | public void setExpandListener(BubbleExpandListener listener) { |
| 151 | mExpandListener = listener; |
| 152 | if (mStackView != null) { |
| 153 | mStackView.setExpandListener(mExpandListener); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 158 | * Whether or not there are bubbles present, regardless of them being visible on the |
| 159 | * screen (e.g. if on AOD). |
| 160 | */ |
| 161 | public boolean hasBubbles() { |
| 162 | return mBubbles.size() > 0; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Whether the stack of bubbles is expanded or not. |
| 167 | */ |
| 168 | public boolean isStackExpanded() { |
| 169 | return mStackView != null && mStackView.isExpanded(); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Tell the stack of bubbles to collapse. |
| 174 | */ |
| 175 | public void collapseStack() { |
| 176 | if (mStackView != null) { |
| 177 | mStackView.animateExpansion(false); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack. |
| 183 | */ |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 184 | void dismissStack() { |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 185 | if (mStackView == null) { |
| 186 | return; |
| 187 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 188 | Point startPoint = getStartPoint(mStackView.getStackWidth(), mDisplaySize); |
| 189 | // Reset the position of the stack (TODO - or should we save / respect last user position?) |
| 190 | mStackView.setPosition(startPoint.x, startPoint.y); |
| 191 | for (String key: mBubbles.keySet()) { |
| 192 | removeBubble(key); |
| 193 | } |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 194 | mNotificationEntryManager.updateNotifications(); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 195 | updateBubblesShowing(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Adds a bubble associated with the provided notification entry or updates it if it exists. |
| 200 | */ |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 201 | public void addBubble(NotificationEntry notif) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 202 | if (mBubbles.containsKey(notif.key)) { |
| 203 | // It's an update |
| 204 | BubbleView bubble = mBubbles.get(notif.key); |
| 205 | mStackView.updateBubble(bubble, notif); |
| 206 | } else { |
| 207 | // It's new |
| 208 | BubbleView bubble = new BubbleView(mContext); |
| 209 | bubble.setNotif(notif); |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 210 | if (shouldUseActivityView(mContext)) { |
| 211 | bubble.setAppOverlayIntent(getAppOverlayIntent(notif)); |
| 212 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 213 | mBubbles.put(bubble.getKey(), bubble); |
| 214 | |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 215 | boolean setPosition = mStackView != null && mStackView.getVisibility() != VISIBLE; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 216 | if (mStackView == null) { |
| 217 | setPosition = true; |
| 218 | mStackView = new BubbleStackView(mContext); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 219 | ViewGroup sbv = mStatusBarWindowController.getStatusBarView(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 220 | // XXX: Bug when you expand the shade on top of expanded bubble, there is no scrim |
| 221 | // between bubble and the shade |
| 222 | int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1; |
| 223 | sbv.addView(mStackView, bubblePosition, |
| 224 | new FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)); |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 225 | if (mExpandListener != null) { |
| 226 | mStackView.setExpandListener(mExpandListener); |
| 227 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 228 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 229 | mStackView.addBubble(bubble); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 230 | if (setPosition) { |
| 231 | // Need to add the bubble to the stack before we can know the width |
| 232 | Point startPoint = getStartPoint(mStackView.getStackWidth(), mDisplaySize); |
| 233 | mStackView.setPosition(startPoint.x, startPoint.y); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 234 | mStackView.setVisibility(VISIBLE); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 235 | } |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 236 | updateBubblesShowing(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 240 | @Nullable |
| 241 | private PendingIntent getAppOverlayIntent(NotificationEntry notif) { |
| 242 | Notification notification = notif.notification.getNotification(); |
| 243 | if (canLaunchInActivityView(notification.getAppOverlayIntent())) { |
| 244 | return notification.getAppOverlayIntent(); |
| 245 | } else if (shouldUseContentIntent(mContext) |
| 246 | && canLaunchInActivityView(notification.contentIntent)) { |
| 247 | Log.d(TAG, "[addBubble " + notif.key |
| 248 | + "]: No appOverlayIntent, using contentIntent."); |
| 249 | return notification.contentIntent; |
| 250 | } |
| 251 | Log.d(TAG, "[addBubble " + notif.key + "]: No supported intent for ActivityView."); |
| 252 | return null; |
| 253 | } |
| 254 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 255 | /** |
| 256 | * Removes the bubble associated with the {@param uri}. |
| 257 | */ |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 258 | void removeBubble(String key) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 259 | BubbleView bv = mBubbles.get(key); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 260 | if (mStackView != null && bv != null) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 261 | mStackView.removeBubble(bv); |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 262 | bv.destroyActivityView(mStackView); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 263 | bv.getEntry().setBubbleDismissed(true); |
| 264 | } |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 265 | |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 266 | NotificationEntry entry = mNotificationEntryManager.getNotificationData().get(key); |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 267 | if (entry != null) { |
| 268 | entry.setBubbleDismissed(true); |
| 269 | if (!DEBUG_DEMOTE_TO_NOTIF) { |
| 270 | mNotificationEntryManager.performRemoveNotification(entry.notification); |
| 271 | } |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 272 | } |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 273 | mNotificationEntryManager.updateNotifications(); |
| 274 | |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 275 | updateBubblesShowing(); |
| 276 | } |
| 277 | |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 278 | @SuppressWarnings("FieldCanBeLocal") |
| 279 | private final NotificationEntryListener mEntryListener = new NotificationEntryListener() { |
| 280 | @Override |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 281 | public void onPendingEntryAdded(NotificationEntry entry) { |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame^] | 282 | if (shouldAutoBubble(mContext, entry) || shouldBubble(entry)) { |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 283 | entry.setIsBubble(true); |
| 284 | } |
| 285 | } |
| 286 | }; |
| 287 | |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 288 | private void updateBubblesShowing() { |
| 289 | boolean hasBubblesShowing = false; |
| 290 | for (BubbleView bv : mBubbles.values()) { |
| 291 | if (!bv.getEntry().isBubbleDismissed()) { |
| 292 | hasBubblesShowing = true; |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | boolean hadBubbles = mStatusBarWindowController.getBubblesShowing(); |
| 297 | mStatusBarWindowController.setBubblesShowing(hasBubblesShowing); |
| 298 | if (mStackView != null && !hasBubblesShowing) { |
| 299 | mStackView.setVisibility(INVISIBLE); |
| 300 | } |
| 301 | if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) { |
| 302 | mStateChangeListener.onHasBubblesChanged(hasBubblesShowing); |
| 303 | } |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Sets the visibility of the bubbles, doesn't un-bubble them, just changes visibility. |
| 308 | */ |
| 309 | public void updateVisibility(boolean visible) { |
| 310 | if (mStackView == null) { |
| 311 | return; |
| 312 | } |
| 313 | ArrayList<BubbleView> viewsToRemove = new ArrayList<>(); |
| 314 | for (BubbleView bv : mBubbles.values()) { |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 315 | NotificationEntry entry = bv.getEntry(); |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 316 | if (entry != null) { |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 317 | if (entry.isRowRemoved() || entry.isBubbleDismissed() || entry.isRowDismissed()) { |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 318 | viewsToRemove.add(bv); |
| 319 | } |
| 320 | } |
| 321 | } |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 322 | for (BubbleView bubbleView : viewsToRemove) { |
| 323 | mBubbles.remove(bubbleView.getKey()); |
| 324 | mStackView.removeBubble(bubbleView); |
| 325 | bubbleView.destroyActivityView(mStackView); |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 326 | } |
| 327 | if (mStackView != null) { |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 328 | mStackView.setVisibility(visible ? VISIBLE : INVISIBLE); |
| 329 | if (!visible) { |
| 330 | collapseStack(); |
| 331 | } |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 332 | } |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 333 | updateBubblesShowing(); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Rect indicating the touchable region for the bubble stack / expanded stack. |
| 338 | */ |
| 339 | public Rect getTouchableRegion() { |
| 340 | if (mStackView == null || mStackView.getVisibility() != VISIBLE) { |
| 341 | return null; |
| 342 | } |
| 343 | mStackView.getBoundsOnScreen(mTempRect); |
| 344 | return mTempRect; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 345 | } |
| 346 | |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 347 | private boolean canLaunchInActivityView(PendingIntent intent) { |
| 348 | if (intent == null) { |
| 349 | return false; |
| 350 | } |
| 351 | ActivityInfo info = |
| 352 | intent.getIntent().resolveActivityInfo(mContext.getPackageManager(), 0); |
| 353 | return info != null |
| 354 | && ActivityInfo.isResizeableMode(info.resizeMode) |
| 355 | && (info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) != 0; |
| 356 | } |
| 357 | |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 358 | @VisibleForTesting |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 359 | BubbleStackView getStackView() { |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 360 | return mStackView; |
| 361 | } |
| 362 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 363 | // TODO: factor in PIP location / maybe last place user had it |
| 364 | /** |
| 365 | * Gets an appropriate starting point to position the bubble stack. |
| 366 | */ |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 367 | private static Point getStartPoint(int size, Point displaySize) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 368 | final int x = displaySize.x - size + EDGE_OVERLAP; |
| 369 | final int y = displaySize.y / 4; |
| 370 | return new Point(x, y); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Gets an appropriate position for the bubble when the stack is expanded. |
| 375 | */ |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 376 | static Point getExpandPoint(BubbleStackView view, int size, Point displaySize) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 377 | // Same place for now.. |
| 378 | return new Point(EDGE_OVERLAP, size); |
| 379 | } |
| 380 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 381 | /** |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame^] | 382 | * Whether the notification has been developer configured to bubble and is allowed by the user. |
| 383 | */ |
| 384 | private boolean shouldBubble(NotificationEntry entry) { |
| 385 | StatusBarNotification n = entry.notification; |
| 386 | boolean canAppOverlay = false; |
| 387 | try { |
| 388 | canAppOverlay = mNotificationManagerService.areAppOverlaysAllowedForPackage( |
| 389 | n.getPackageName(), n.getUid()); |
| 390 | } catch (RemoteException e) { |
| 391 | Log.w(TAG, "Error calling NoMan to determine if app can overlay", e); |
| 392 | } |
| 393 | |
| 394 | boolean canChannelOverlay = mNotificationEntryManager.getNotificationData().getChannel( |
| 395 | entry.key).canOverlayApps(); |
| 396 | boolean hasOverlayIntent = n.getNotification().getAppOverlayIntent() != null; |
| 397 | return hasOverlayIntent && canChannelOverlay && canAppOverlay; |
| 398 | } |
| 399 | |
| 400 | /** |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 401 | * Whether the notification should bubble or not. |
| 402 | */ |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame^] | 403 | private boolean shouldAutoBubble(Context context, NotificationEntry entry) { |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 404 | if (entry.isBubbleDismissed()) { |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 405 | return false; |
| 406 | } |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame^] | 407 | StatusBarNotification n = entry.notification; |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 408 | |
| 409 | boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE; |
| 410 | boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE; |
| 411 | boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE; |
| 412 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 413 | boolean hasRemoteInput = false; |
| 414 | if (n.getNotification().actions != null) { |
| 415 | for (Notification.Action action : n.getNotification().actions) { |
| 416 | if (action.getRemoteInputs() != null) { |
| 417 | hasRemoteInput = true; |
| 418 | break; |
| 419 | } |
| 420 | } |
| 421 | } |
Mady Mellor | 711f956 | 2018-12-05 14:53:46 -0800 | [diff] [blame] | 422 | boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category) |
| 423 | && n.isOngoing(); |
| 424 | boolean isMusic = n.getNotification().hasMediaSession(); |
| 425 | boolean isImportantOngoing = isMusic || isCall; |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 426 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 427 | Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle(); |
Mady Mellor | e317537 | 2018-12-04 17:05:11 -0800 | [diff] [blame] | 428 | boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category); |
| 429 | boolean isMessageStyle = Notification.MessagingStyle.class.equals(style); |
| 430 | return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages) |
Mady Mellor | 711f956 | 2018-12-05 14:53:46 -0800 | [diff] [blame] | 431 | || (isImportantOngoing && autoBubbleOngoing) |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 432 | || autoBubbleAll; |
| 433 | } |
| 434 | |
| 435 | private static boolean shouldAutoBubbleMessages(Context context) { |
| 436 | return Settings.Secure.getInt(context.getContentResolver(), |
| 437 | ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0; |
| 438 | } |
| 439 | |
| 440 | private static boolean shouldAutoBubbleOngoing(Context context) { |
| 441 | return Settings.Secure.getInt(context.getContentResolver(), |
| 442 | ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0; |
| 443 | } |
| 444 | |
| 445 | private static boolean shouldAutoBubbleAll(Context context) { |
| 446 | return Settings.Secure.getInt(context.getContentResolver(), |
| 447 | ENABLE_AUTO_BUBBLE_ALL, 0) != 0; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 448 | } |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 449 | |
| 450 | private static boolean shouldUseActivityView(Context context) { |
| 451 | return Settings.Secure.getInt(context.getContentResolver(), |
| 452 | ENABLE_BUBBLE_ACTIVITY_VIEW, 0) != 0; |
| 453 | } |
| 454 | |
| 455 | private static boolean shouldUseContentIntent(Context context) { |
| 456 | return Settings.Secure.getInt(context.getContentResolver(), |
| 457 | ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0; |
| 458 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 459 | } |