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 | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 19 | import static android.view.Display.DEFAULT_DISPLAY; |
| 20 | import static android.view.Display.INVALID_DISPLAY; |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 21 | import static android.view.View.INVISIBLE; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 22 | import static android.view.View.VISIBLE; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 23 | import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 24 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 25 | import static com.android.systemui.statusbar.StatusBarState.SHADE; |
| 26 | import static com.android.systemui.statusbar.notification.NotificationAlertingManager.alertAgain; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 27 | |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 28 | import static java.lang.annotation.RetentionPolicy.SOURCE; |
| 29 | |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 30 | import android.annotation.Nullable; |
Mady Mellor | e80930e | 2019-03-21 16:00:45 -0700 | [diff] [blame] | 31 | import android.app.ActivityManager; |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 32 | import android.app.ActivityManager.RunningTaskInfo; |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 33 | import android.app.ActivityTaskManager; |
| 34 | import android.app.IActivityTaskManager; |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 35 | import android.app.INotificationManager; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 36 | import android.app.Notification; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 37 | import android.content.Context; |
Joshua Tsuji | a19515f | 2019-02-13 18:02:29 -0500 | [diff] [blame] | 38 | import android.content.pm.ParceledListSlice; |
Joshua Tsuji | f418f9e | 2019-04-04 17:09:53 -0400 | [diff] [blame] | 39 | import android.content.res.Configuration; |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 40 | import android.graphics.Rect; |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 41 | import android.os.RemoteException; |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 42 | import android.os.ServiceManager; |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 43 | import android.provider.Settings; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 44 | import android.service.notification.StatusBarNotification; |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 45 | import android.view.Display; |
Joshua Tsuji | a19515f | 2019-02-13 18:02:29 -0500 | [diff] [blame] | 46 | import android.view.IPinnedStackController; |
| 47 | import android.view.IPinnedStackListener; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 48 | import android.view.ViewGroup; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 49 | import android.widget.FrameLayout; |
| 50 | |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 51 | import androidx.annotation.IntDef; |
Mark Renouf | 658c6bc | 2019-01-30 10:26:54 -0500 | [diff] [blame] | 52 | import androidx.annotation.MainThread; |
| 53 | |
Mady Mellor | ebdbbb9 | 2018-11-15 14:36:48 -0800 | [diff] [blame] | 54 | import com.android.internal.annotations.VisibleForTesting; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 55 | import com.android.internal.statusbar.NotificationVisibility; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 56 | import com.android.systemui.Dependency; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 57 | import com.android.systemui.R; |
Beverly | 8fdb533 | 2019-02-04 14:29:49 -0500 | [diff] [blame] | 58 | import com.android.systemui.plugins.statusbar.StatusBarStateController; |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 59 | import com.android.systemui.shared.system.ActivityManagerWrapper; |
| 60 | import com.android.systemui.shared.system.TaskStackChangeListener; |
Joshua Tsuji | a19515f | 2019-02-13 18:02:29 -0500 | [diff] [blame] | 61 | import com.android.systemui.shared.system.WindowManagerWrapper; |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 62 | import com.android.systemui.statusbar.notification.NotificationEntryListener; |
| 63 | import com.android.systemui.statusbar.notification.NotificationEntryManager; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 64 | import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 65 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 66 | import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 67 | import com.android.systemui.statusbar.phone.StatusBarWindowController; |
Lyn Han | f1c9b8b | 2019-03-14 16:49:48 -0700 | [diff] [blame] | 68 | import com.android.systemui.statusbar.policy.ConfigurationController; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 69 | |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 70 | import java.lang.annotation.Retention; |
Mady Mellor | e80930e | 2019-03-21 16:00:45 -0700 | [diff] [blame] | 71 | import java.util.List; |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 72 | |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 73 | import javax.inject.Inject; |
| 74 | import javax.inject.Singleton; |
| 75 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 76 | /** |
| 77 | * Bubbles are a special type of content that can "float" on top of other apps or System UI. |
| 78 | * Bubbles can be expanded to show more content. |
| 79 | * |
| 80 | * The controller manages addition, removal, and visible state of bubbles on screen. |
| 81 | */ |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 82 | @Singleton |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 83 | public class BubbleController implements ConfigurationController.ConfigurationListener { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 84 | |
| 85 | private static final String TAG = "BubbleController"; |
| 86 | |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 87 | @Retention(SOURCE) |
| 88 | @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED, |
| 89 | DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION}) |
| 90 | @interface DismissReason {} |
Lyn Han | f1c9b8b | 2019-03-14 16:49:48 -0700 | [diff] [blame] | 91 | |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 92 | static final int DISMISS_USER_GESTURE = 1; |
| 93 | static final int DISMISS_AGED = 2; |
| 94 | static final int DISMISS_TASK_FINISHED = 3; |
| 95 | static final int DISMISS_BLOCKED = 4; |
| 96 | static final int DISMISS_NOTIF_CANCEL = 5; |
| 97 | static final int DISMISS_ACCESSIBILITY_ACTION = 6; |
| 98 | |
Joshua Tsuji | 25a4b7b | 2019-03-22 14:11:06 -0400 | [diff] [blame] | 99 | static final int MAX_BUBBLES = 5; // TODO: actually enforce this |
| 100 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 101 | // Enables some subset of notifs to automatically become bubbles |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 102 | private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 103 | |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 104 | /** Flag to enable or disable the entire feature */ |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 105 | private static final String ENABLE_BUBBLES = "experiment_enable_bubbles"; |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 106 | /** Auto bubble flags set whether different notif types should be presented as a bubble */ |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 107 | private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging"; |
| 108 | private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing"; |
| 109 | private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all"; |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 110 | |
| 111 | /** Use an activityView for an auto-bubbled notifs if it has an appropriate content intent */ |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 112 | private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent"; |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 113 | |
Joshua Tsuji | 010c2b1 | 2019-02-25 18:11:25 -0500 | [diff] [blame] | 114 | private static final String BUBBLE_STIFFNESS = "experiment_bubble_stiffness"; |
| 115 | private static final String BUBBLE_BOUNCINESS = "experiment_bubble_bounciness"; |
| 116 | |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 117 | private final Context mContext; |
| 118 | private final NotificationEntryManager mNotificationEntryManager; |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 119 | private final IActivityTaskManager mActivityTaskManager; |
| 120 | private final BubbleTaskStackListener mTaskStackListener; |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 121 | private BubbleStateChangeListener mStateChangeListener; |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 122 | private BubbleExpandListener mExpandListener; |
Issei Suzuki | c038754 | 2019-03-08 17:31:14 +0100 | [diff] [blame] | 123 | @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 124 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 125 | private BubbleData mBubbleData; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 126 | private BubbleStackView mStackView; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 127 | |
| 128 | // Bubbles get added to the status bar view |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 129 | private final StatusBarWindowController mStatusBarWindowController; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 130 | private StatusBarStateListener mStatusBarStateListener; |
| 131 | |
| 132 | private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider = |
| 133 | Dependency.get(NotificationInterruptionStateProvider.class); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 134 | |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 135 | private INotificationManager mNotificationManagerService; |
| 136 | |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 137 | // Used for determining view rect for touch interaction |
| 138 | private Rect mTempRect = new Rect(); |
| 139 | |
Joshua Tsuji | f418f9e | 2019-04-04 17:09:53 -0400 | [diff] [blame] | 140 | /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */ |
| 141 | private int mOrientation = Configuration.ORIENTATION_UNDEFINED; |
| 142 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 143 | /** |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 144 | * Listener to be notified when some states of the bubbles change. |
| 145 | */ |
| 146 | public interface BubbleStateChangeListener { |
| 147 | /** |
| 148 | * Called when the stack has bubbles or no longer has bubbles. |
| 149 | */ |
| 150 | void onHasBubblesChanged(boolean hasBubbles); |
| 151 | } |
| 152 | |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 153 | /** |
| 154 | * Listener to find out about stack expansion / collapse events. |
| 155 | */ |
| 156 | public interface BubbleExpandListener { |
| 157 | /** |
| 158 | * Called when the expansion state of the bubble stack changes. |
Lyn Han | f1c9b8b | 2019-03-14 16:49:48 -0700 | [diff] [blame] | 159 | * |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 160 | * @param isExpanding whether it's expanding or collapsing |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 161 | * @param key the notification key associated with bubble being expanded |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 162 | */ |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 163 | void onBubbleExpandChanged(boolean isExpanding, String key); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Listens for the current state of the status bar and updates the visibility state |
| 168 | * of bubbles as needed. |
| 169 | */ |
| 170 | private class StatusBarStateListener implements StatusBarStateController.StateListener { |
| 171 | private int mState; |
| 172 | /** |
| 173 | * Returns the current status bar state. |
| 174 | */ |
| 175 | public int getCurrentState() { |
| 176 | return mState; |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public void onStateChanged(int newState) { |
| 181 | mState = newState; |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 182 | boolean shouldCollapse = (mState != SHADE); |
| 183 | if (shouldCollapse) { |
| 184 | collapseStack(); |
| 185 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 186 | updateVisibility(); |
| 187 | } |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 190 | @Inject |
Mady Mellor | cfd06c1 | 2019-02-13 14:32:12 -0800 | [diff] [blame] | 191 | public BubbleController(Context context, StatusBarWindowController statusBarWindowController, |
Lyn Han | f1c9b8b | 2019-03-14 16:49:48 -0700 | [diff] [blame] | 192 | BubbleData data, ConfigurationController configurationController) { |
| 193 | this(context, statusBarWindowController, data, null /* synchronizer */, |
| 194 | configurationController); |
Issei Suzuki | c038754 | 2019-03-08 17:31:14 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | public BubbleController(Context context, StatusBarWindowController statusBarWindowController, |
Lyn Han | f1c9b8b | 2019-03-14 16:49:48 -0700 | [diff] [blame] | 198 | BubbleData data, @Nullable BubbleStackView.SurfaceSynchronizer synchronizer, |
| 199 | ConfigurationController configurationController) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 200 | mContext = context; |
Lyn Han | f1c9b8b | 2019-03-14 16:49:48 -0700 | [diff] [blame] | 201 | configurationController.addCallback(this /* configurationListener */); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 202 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 203 | mNotificationEntryManager = Dependency.get(NotificationEntryManager.class); |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 204 | mNotificationEntryManager.addNotificationEntryListener(mEntryListener); |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 205 | |
| 206 | try { |
| 207 | mNotificationManagerService = INotificationManager.Stub.asInterface( |
| 208 | ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE)); |
| 209 | } catch (ServiceManager.ServiceNotFoundException e) { |
| 210 | e.printStackTrace(); |
| 211 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 212 | |
| 213 | mStatusBarWindowController = statusBarWindowController; |
| 214 | mStatusBarStateListener = new StatusBarStateListener(); |
| 215 | Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener); |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 216 | |
| 217 | mActivityTaskManager = ActivityTaskManager.getService(); |
| 218 | mTaskStackListener = new BubbleTaskStackListener(); |
| 219 | ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 220 | |
Joshua Tsuji | a19515f | 2019-02-13 18:02:29 -0500 | [diff] [blame] | 221 | try { |
| 222 | WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener()); |
| 223 | } catch (RemoteException e) { |
| 224 | e.printStackTrace(); |
| 225 | } |
| 226 | |
Mady Mellor | cfd06c1 | 2019-02-13 14:32:12 -0800 | [diff] [blame] | 227 | mBubbleData = data; |
Mark Renouf | 3bc5b36 | 2019-04-05 14:37:59 -0400 | [diff] [blame] | 228 | mBubbleData.setListener(mBubbleDataListener); |
Issei Suzuki | c038754 | 2019-03-08 17:31:14 +0100 | [diff] [blame] | 229 | mSurfaceSynchronizer = synchronizer; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Mark Renouf | 8b6a3c6 | 2019-04-09 10:17:40 -0400 | [diff] [blame] | 232 | /** |
| 233 | * BubbleStackView is lazily created by this method the first time a Bubble is added. This |
| 234 | * method initializes the stack view and adds it to the StatusBar just above the scrim. |
| 235 | */ |
| 236 | private void ensureStackViewCreated() { |
| 237 | if (mStackView == null) { |
| 238 | mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer); |
| 239 | ViewGroup sbv = mStatusBarWindowController.getStatusBarView(); |
| 240 | // TODO(b/130237686): When you expand the shade on top of expanded bubble, there is no |
| 241 | // scrim between bubble and the shade |
| 242 | int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1; |
| 243 | sbv.addView(mStackView, bubblePosition, |
| 244 | new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)); |
| 245 | if (mExpandListener != null) { |
| 246 | mStackView.setExpandListener(mExpandListener); |
| 247 | } |
Mark Renouf | 8b6a3c6 | 2019-04-09 10:17:40 -0400 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
Lyn Han | f1c9b8b | 2019-03-14 16:49:48 -0700 | [diff] [blame] | 251 | @Override |
| 252 | public void onUiModeChanged() { |
| 253 | if (mStackView != null) { |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 254 | mStackView.onThemeChanged(); |
Lyn Han | f1c9b8b | 2019-03-14 16:49:48 -0700 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public void onOverlayChanged() { |
| 260 | if (mStackView != null) { |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 261 | mStackView.onThemeChanged(); |
Lyn Han | f1c9b8b | 2019-03-14 16:49:48 -0700 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
Joshua Tsuji | f418f9e | 2019-04-04 17:09:53 -0400 | [diff] [blame] | 265 | @Override |
| 266 | public void onConfigChanged(Configuration newConfig) { |
| 267 | if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) { |
| 268 | mStackView.onOrientationChanged(); |
| 269 | mOrientation = newConfig.orientation; |
| 270 | } |
| 271 | } |
| 272 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 273 | /** |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 274 | * Set a listener to be notified when some states of the bubbles change. |
| 275 | */ |
| 276 | public void setBubbleStateChangeListener(BubbleStateChangeListener listener) { |
| 277 | mStateChangeListener = listener; |
| 278 | } |
| 279 | |
| 280 | /** |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 281 | * Set a listener to be notified of bubble expand events. |
| 282 | */ |
| 283 | public void setExpandListener(BubbleExpandListener listener) { |
Issei Suzuki | ac9fcb7 | 2019-02-04 17:45:57 +0100 | [diff] [blame] | 284 | mExpandListener = ((isExpanding, key) -> { |
| 285 | if (listener != null) { |
| 286 | listener.onBubbleExpandChanged(isExpanding, key); |
| 287 | } |
| 288 | mStatusBarWindowController.setBubbleExpanded(isExpanding); |
| 289 | }); |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 290 | if (mStackView != null) { |
| 291 | mStackView.setExpandListener(mExpandListener); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /** |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 296 | * Whether or not there are bubbles present, regardless of them being visible on the |
| 297 | * screen (e.g. if on AOD). |
| 298 | */ |
| 299 | public boolean hasBubbles() { |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 300 | if (mStackView == null) { |
| 301 | return false; |
| 302 | } |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 303 | return mBubbleData.hasBubbles(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Whether the stack of bubbles is expanded or not. |
| 308 | */ |
| 309 | public boolean isStackExpanded() { |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 310 | return mBubbleData.isExpanded(); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Tell the stack of bubbles to expand. |
| 315 | */ |
| 316 | public void expandStack() { |
| 317 | mBubbleData.setExpanded(true); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Tell the stack of bubbles to collapse. |
| 322 | */ |
| 323 | public void collapseStack() { |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 324 | mBubbleData.setExpanded(false /* expanded */); |
| 325 | } |
| 326 | |
| 327 | void selectBubble(Bubble bubble) { |
| 328 | mBubbleData.setSelectedBubble(bubble); |
| 329 | } |
| 330 | |
| 331 | @VisibleForTesting |
| 332 | void selectBubble(String key) { |
| 333 | Bubble bubble = mBubbleData.getBubbleWithKey(key); |
| 334 | selectBubble(bubble); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | /** |
Mark Renouf | fec45da | 2019-03-13 13:24:27 -0400 | [diff] [blame] | 338 | * Request the stack expand if needed, then select the specified Bubble as current. |
| 339 | * |
| 340 | * @param notificationKey the notification key for the bubble to be selected |
| 341 | */ |
| 342 | public void expandStackAndSelectBubble(String notificationKey) { |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 343 | Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey); |
| 344 | if (bubble != null) { |
| 345 | mBubbleData.setSelectedBubble(bubble); |
| 346 | mBubbleData.setExpanded(true); |
Mark Renouf | fec45da | 2019-03-13 13:24:27 -0400 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
| 350 | /** |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 351 | * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack. |
| 352 | */ |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 353 | void dismissStack(@DismissReason int reason) { |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 354 | mBubbleData.dismissAll(reason); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /** |
Mark Renouf | 041d726 | 2019-02-06 12:09:41 -0500 | [diff] [blame] | 358 | * Directs a back gesture at the bubble stack. When opened, the current expanded bubble |
| 359 | * is forwarded a back key down/up pair. |
| 360 | */ |
| 361 | public void performBackPressIfNeeded() { |
| 362 | if (mStackView != null) { |
| 363 | mStackView.performBackPressIfNeeded(); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | /** |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 368 | * Adds or updates a bubble associated with the provided notification entry. |
| 369 | * |
Mark Renouf | 8b6a3c6 | 2019-04-09 10:17:40 -0400 | [diff] [blame] | 370 | * @param notif the notification associated with this bubble. |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 371 | */ |
Mark Renouf | f97ed46 | 2019-04-05 13:46:24 -0400 | [diff] [blame] | 372 | void updateBubble(NotificationEntry notif) { |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 373 | mBubbleData.notificationEntryUpdated(notif); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Removes the bubble associated with the {@param uri}. |
Mark Renouf | 658c6bc | 2019-01-30 10:26:54 -0500 | [diff] [blame] | 378 | * <p> |
| 379 | * Must be called from the main thread. |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 380 | */ |
Mark Renouf | 658c6bc | 2019-01-30 10:26:54 -0500 | [diff] [blame] | 381 | @MainThread |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 382 | void removeBubble(String key, int reason) { |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 383 | // TEMP: refactor to change this to pass entry |
| 384 | Bubble bubble = mBubbleData.getBubbleWithKey(key); |
| 385 | if (bubble != null) { |
| 386 | mBubbleData.notificationEntryRemoved(bubble.entry, reason); |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 390 | @SuppressWarnings("FieldCanBeLocal") |
| 391 | private final NotificationEntryListener mEntryListener = new NotificationEntryListener() { |
| 392 | @Override |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 393 | public void onPendingEntryAdded(NotificationEntry entry) { |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 394 | if (!areBubblesEnabled(mContext)) { |
| 395 | return; |
| 396 | } |
| 397 | if (shouldAutoBubbleForFlags(mContext, entry) || shouldBubble(entry)) { |
Mady Mellor | 8a1f025 | 2019-04-01 11:31:34 -0700 | [diff] [blame] | 398 | // TODO: handle group summaries? |
| 399 | updateShowInShadeForSuppressNotification(entry); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
| 403 | @Override |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 404 | public void onEntryInflated(NotificationEntry entry, @InflationFlag int inflatedFlags) { |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 405 | if (!areBubblesEnabled(mContext)) { |
| 406 | return; |
| 407 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 408 | if (entry.isBubble() && mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) { |
Mark Renouf | f97ed46 | 2019-04-05 13:46:24 -0400 | [diff] [blame] | 409 | updateBubble(entry); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
| 413 | @Override |
| 414 | public void onPreEntryUpdated(NotificationEntry entry) { |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 415 | if (!areBubblesEnabled(mContext)) { |
| 416 | return; |
| 417 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 418 | if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry) |
| 419 | && alertAgain(entry, entry.notification.getNotification())) { |
Mady Mellor | 8a1f025 | 2019-04-01 11:31:34 -0700 | [diff] [blame] | 420 | updateShowInShadeForSuppressNotification(entry); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 421 | entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed |
Mark Renouf | f97ed46 | 2019-04-05 13:46:24 -0400 | [diff] [blame] | 422 | updateBubble(entry); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
| 426 | @Override |
| 427 | public void onEntryRemoved(NotificationEntry entry, |
| 428 | @Nullable NotificationVisibility visibility, |
| 429 | boolean removedByUser) { |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 430 | if (!areBubblesEnabled(mContext)) { |
| 431 | return; |
| 432 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 433 | entry.setShowInShadeWhenBubble(false); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 434 | if (mStackView != null) { |
| 435 | mStackView.updateDotVisibility(entry.key); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 436 | } |
| 437 | if (!removedByUser) { |
| 438 | // This was a cancel so we should remove the bubble |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 439 | removeBubble(entry.key, DISMISS_NOTIF_CANCEL); |
Ned Burns | 01e3821 | 2019-01-03 16:32:52 -0500 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | }; |
| 443 | |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 444 | @SuppressWarnings("FieldCanBeLocal") |
Mark Renouf | 3bc5b36 | 2019-04-05 14:37:59 -0400 | [diff] [blame] | 445 | private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() { |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 446 | |
Mark Renouf | 3bc5b36 | 2019-04-05 14:37:59 -0400 | [diff] [blame] | 447 | @Override |
| 448 | public void onBubbleAdded(Bubble bubble) { |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 449 | ensureStackViewCreated(); |
| 450 | mStackView.addBubble(bubble); |
Mark Renouf | 3bc5b36 | 2019-04-05 14:37:59 -0400 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | @Override |
| 454 | public void onBubbleRemoved(Bubble bubble, int reason) { |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 455 | if (mStackView != null) { |
| 456 | mStackView.removeBubble(bubble); |
| 457 | } |
Mark Renouf | 3bc5b36 | 2019-04-05 14:37:59 -0400 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | public void onBubbleUpdated(Bubble bubble) { |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 461 | if (mStackView != null) { |
| 462 | mStackView.updateBubble(bubble); |
| 463 | } |
Mark Renouf | 3bc5b36 | 2019-04-05 14:37:59 -0400 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | @Override |
| 467 | public void onOrderChanged(List<Bubble> bubbles) { |
Mark Renouf | 3bc5b36 | 2019-04-05 14:37:59 -0400 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | @Override |
| 471 | public void onSelectionChanged(Bubble selectedBubble) { |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 472 | if (mStackView != null) { |
| 473 | mStackView.setSelectedBubble(selectedBubble); |
| 474 | } |
Mark Renouf | 3bc5b36 | 2019-04-05 14:37:59 -0400 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | @Override |
| 478 | public void onExpandedChanged(boolean expanded) { |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 479 | if (mStackView != null) { |
| 480 | mStackView.setExpanded(expanded); |
| 481 | } |
Mark Renouf | 3bc5b36 | 2019-04-05 14:37:59 -0400 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | @Override |
| 485 | public void showFlyoutText(Bubble bubble, String text) { |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 486 | if (mStackView != null) { |
| 487 | mStackView.animateInFlyoutForBubble(bubble); |
| 488 | } |
Mark Renouf | 3bc5b36 | 2019-04-05 14:37:59 -0400 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | @Override |
| 492 | public void apply() { |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 493 | mNotificationEntryManager.updateNotifications(); |
| 494 | updateVisibility(); |
Mark Renouf | 3bc5b36 | 2019-04-05 14:37:59 -0400 | [diff] [blame] | 495 | } |
| 496 | }; |
| 497 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 498 | /** |
| 499 | * Lets any listeners know if bubble state has changed. |
| 500 | */ |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 501 | private void updateBubblesShowing() { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 502 | if (mStackView == null) { |
| 503 | return; |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 504 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 505 | |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 506 | boolean hadBubbles = mStatusBarWindowController.getBubblesShowing(); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 507 | boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE; |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 508 | mStatusBarWindowController.setBubblesShowing(hasBubblesShowing); |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 509 | if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) { |
| 510 | mStateChangeListener.onHasBubblesChanged(hasBubblesShowing); |
| 511 | } |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | /** |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 515 | * Updates the visibility of the bubbles based on current state. |
| 516 | * Does not un-bubble, just hides or un-hides. Will notify any |
| 517 | * {@link BubbleStateChangeListener}s if visibility changes. |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 518 | */ |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 519 | public void updateVisibility() { |
| 520 | if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) { |
| 521 | // Bubbles only appear in unlocked shade |
| 522 | mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE); |
| 523 | } else if (mStackView != null) { |
| 524 | mStackView.setVisibility(INVISIBLE); |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 525 | } |
Mady Mellor | d1c78b26 | 2018-11-06 18:04:40 -0800 | [diff] [blame] | 526 | updateBubblesShowing(); |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * Rect indicating the touchable region for the bubble stack / expanded stack. |
| 531 | */ |
| 532 | public Rect getTouchableRegion() { |
| 533 | if (mStackView == null || mStackView.getVisibility() != VISIBLE) { |
| 534 | return null; |
| 535 | } |
| 536 | mStackView.getBoundsOnScreen(mTempRect); |
| 537 | return mTempRect; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 538 | } |
| 539 | |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 540 | /** |
| 541 | * The display id of the expanded view, if the stack is expanded and not occluded by the |
| 542 | * status bar, otherwise returns {@link Display#INVALID_DISPLAY}. |
| 543 | */ |
| 544 | public int getExpandedDisplayId(Context context) { |
Joel Galenson | 4071ddb | 2019-04-18 13:30:45 -0700 | [diff] [blame^] | 545 | if (mStackView == null) { |
| 546 | return INVALID_DISPLAY; |
| 547 | } |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 548 | boolean defaultDisplay = context.getDisplay() != null |
| 549 | && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY; |
| 550 | Bubble b = mStackView.getExpandedBubble(); |
| 551 | if (defaultDisplay && b != null && isStackExpanded() |
| 552 | && !mStatusBarWindowController.getPanelExpanded()) { |
| 553 | return b.expandedView.getVirtualDisplayId(); |
| 554 | } |
| 555 | return INVALID_DISPLAY; |
| 556 | } |
| 557 | |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 558 | @VisibleForTesting |
| 559 | BubbleStackView getStackView() { |
| 560 | return mStackView; |
| 561 | } |
| 562 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 563 | /** |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 564 | * Whether the notification has been developer configured to bubble and is allowed by the user. |
| 565 | */ |
Mady Mellor | c18ba96 | 2019-01-29 11:11:56 -0800 | [diff] [blame] | 566 | @VisibleForTesting |
| 567 | protected boolean shouldBubble(NotificationEntry entry) { |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 568 | StatusBarNotification n = entry.notification; |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 569 | boolean hasOverlayIntent = n.getNotification().getBubbleMetadata() != null |
| 570 | && n.getNotification().getBubbleMetadata().getIntent() != null; |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 571 | return hasOverlayIntent && entry.canBubble; |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /** |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 575 | * 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] | 576 | */ |
Mady Mellor | 9bad224 | 2019-01-28 11:21:51 -0800 | [diff] [blame] | 577 | @VisibleForTesting |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 578 | protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) { |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 579 | if (entry.isBubbleDismissed()) { |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 580 | return false; |
| 581 | } |
Mady Mellor | b4991e6 | 2019-01-10 15:14:51 -0800 | [diff] [blame] | 582 | StatusBarNotification n = entry.notification; |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 583 | |
| 584 | boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE; |
| 585 | boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE; |
| 586 | boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE; |
| 587 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 588 | boolean hasRemoteInput = false; |
| 589 | if (n.getNotification().actions != null) { |
| 590 | for (Notification.Action action : n.getNotification().actions) { |
| 591 | if (action.getRemoteInputs() != null) { |
| 592 | hasRemoteInput = true; |
| 593 | break; |
| 594 | } |
| 595 | } |
| 596 | } |
Mady Mellor | 711f956 | 2018-12-05 14:53:46 -0800 | [diff] [blame] | 597 | boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category) |
| 598 | && n.isOngoing(); |
| 599 | boolean isMusic = n.getNotification().hasMediaSession(); |
| 600 | boolean isImportantOngoing = isMusic || isCall; |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 601 | |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 602 | Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle(); |
Mady Mellor | e317537 | 2018-12-04 17:05:11 -0800 | [diff] [blame] | 603 | boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category); |
| 604 | boolean isMessageStyle = Notification.MessagingStyle.class.equals(style); |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 605 | return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages) |
Mady Mellor | 711f956 | 2018-12-05 14:53:46 -0800 | [diff] [blame] | 606 | || (isImportantOngoing && autoBubbleOngoing) |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 607 | || autoBubbleAll; |
| 608 | } |
| 609 | |
Mady Mellor | e80930e | 2019-03-21 16:00:45 -0700 | [diff] [blame] | 610 | private boolean shouldAutoExpand(NotificationEntry entry) { |
| 611 | Notification.BubbleMetadata metadata = entry.getBubbleMetadata(); |
| 612 | return metadata != null && metadata.getAutoExpandBubble() |
Steven Wu | 8ba8ca9 | 2019-04-11 10:47:42 -0400 | [diff] [blame] | 613 | && isForegroundApp(mContext, entry.notification.getPackageName()); |
Mady Mellor | e80930e | 2019-03-21 16:00:45 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Mady Mellor | 8a1f025 | 2019-04-01 11:31:34 -0700 | [diff] [blame] | 616 | private void updateShowInShadeForSuppressNotification(NotificationEntry entry) { |
| 617 | boolean suppressNotification = entry.getBubbleMetadata() != null |
Mady Mellor | c529d6d | 2019-04-16 14:22:52 -0700 | [diff] [blame] | 618 | && entry.getBubbleMetadata().isNotificationSuppressed() |
Steven Wu | 8ba8ca9 | 2019-04-11 10:47:42 -0400 | [diff] [blame] | 619 | && isForegroundApp(mContext, entry.notification.getPackageName()); |
Mady Mellor | 8a1f025 | 2019-04-01 11:31:34 -0700 | [diff] [blame] | 620 | entry.setShowInShadeWhenBubble(!suppressNotification); |
| 621 | } |
| 622 | |
Mady Mellor | e80930e | 2019-03-21 16:00:45 -0700 | [diff] [blame] | 623 | /** |
| 624 | * Return true if the applications with the package name is running in foreground. |
| 625 | * |
Steven Wu | 8ba8ca9 | 2019-04-11 10:47:42 -0400 | [diff] [blame] | 626 | * @param context application context. |
Mady Mellor | e80930e | 2019-03-21 16:00:45 -0700 | [diff] [blame] | 627 | * @param pkgName application package name. |
| 628 | */ |
Steven Wu | 8ba8ca9 | 2019-04-11 10:47:42 -0400 | [diff] [blame] | 629 | public static boolean isForegroundApp(Context context, String pkgName) { |
| 630 | ActivityManager am = context.getSystemService(ActivityManager.class); |
Mady Mellor | e80930e | 2019-03-21 16:00:45 -0700 | [diff] [blame] | 631 | List<RunningTaskInfo> tasks = am.getRunningTasks(1 /* maxNum */); |
| 632 | return !tasks.isEmpty() && pkgName.equals(tasks.get(0).topActivity.getPackageName()); |
| 633 | } |
| 634 | |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 635 | /** |
| 636 | * This task stack listener is responsible for responding to tasks moved to the front |
| 637 | * which are on the default (main) display. When this happens, expanded bubbles must be |
| 638 | * collapsed so the user may interact with the app which was just moved to the front. |
| 639 | * <p> |
| 640 | * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches |
| 641 | * these calls via a main thread Handler. |
| 642 | */ |
| 643 | @MainThread |
| 644 | private class BubbleTaskStackListener extends TaskStackChangeListener { |
| 645 | |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 646 | @Override |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 647 | public void onTaskMovedToFront(RunningTaskInfo taskInfo) { |
| 648 | if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) { |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 649 | mBubbleData.setExpanded(false); |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 653 | @Override |
Mark Renouf | a12e876 | 2019-03-07 15:43:01 -0500 | [diff] [blame] | 654 | public void onActivityLaunchOnSecondaryDisplayRerouted() { |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 655 | if (mStackView != null) { |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 656 | mBubbleData.setExpanded(false); |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | } |
| 660 | |
Mady Mellor | ceced17 | 2018-11-27 11:18:39 -0800 | [diff] [blame] | 661 | private static boolean shouldAutoBubbleMessages(Context context) { |
| 662 | return Settings.Secure.getInt(context.getContentResolver(), |
| 663 | ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0; |
| 664 | } |
| 665 | |
| 666 | private static boolean shouldAutoBubbleOngoing(Context context) { |
| 667 | return Settings.Secure.getInt(context.getContentResolver(), |
| 668 | ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0; |
| 669 | } |
| 670 | |
| 671 | private static boolean shouldAutoBubbleAll(Context context) { |
| 672 | return Settings.Secure.getInt(context.getContentResolver(), |
| 673 | ENABLE_AUTO_BUBBLE_ALL, 0) != 0; |
Mady Mellor | 5549dd2 | 2018-11-06 18:07:34 -0800 | [diff] [blame] | 674 | } |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 675 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 676 | static boolean shouldUseContentIntent(Context context) { |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 677 | return Settings.Secure.getInt(context.getContentResolver(), |
| 678 | ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0; |
| 679 | } |
Mady Mellor | f6e3ac0 | 2019-01-29 10:37:52 -0800 | [diff] [blame] | 680 | |
| 681 | private static boolean areBubblesEnabled(Context context) { |
| 682 | return Settings.Secure.getInt(context.getContentResolver(), |
| 683 | ENABLE_BUBBLES, 1) != 0; |
| 684 | } |
Joshua Tsuji | a19515f | 2019-02-13 18:02:29 -0500 | [diff] [blame] | 685 | |
Joshua Tsuji | 010c2b1 | 2019-02-25 18:11:25 -0500 | [diff] [blame] | 686 | /** Default stiffness to use for bubble physics animations. */ |
| 687 | public static int getBubbleStiffness(Context context, int defaultStiffness) { |
| 688 | return Settings.Secure.getInt( |
| 689 | context.getContentResolver(), BUBBLE_STIFFNESS, defaultStiffness); |
| 690 | } |
| 691 | |
| 692 | /** Default bounciness/damping ratio to use for bubble physics animations. */ |
| 693 | public static float getBubbleBounciness(Context context, float defaultBounciness) { |
| 694 | return Settings.Secure.getInt( |
| 695 | context.getContentResolver(), |
| 696 | BUBBLE_BOUNCINESS, |
| 697 | (int) (defaultBounciness * 100)) / 100f; |
| 698 | } |
| 699 | |
Joshua Tsuji | a19515f | 2019-02-13 18:02:29 -0500 | [diff] [blame] | 700 | /** PinnedStackListener that dispatches IME visibility updates to the stack. */ |
| 701 | private class BubblesImeListener extends IPinnedStackListener.Stub { |
| 702 | |
| 703 | @Override |
| 704 | public void onListenerRegistered(IPinnedStackController controller) throws RemoteException { |
| 705 | } |
| 706 | |
| 707 | @Override |
| 708 | public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds, |
| 709 | Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment, |
| 710 | int displayRotation) throws RemoteException {} |
| 711 | |
| 712 | @Override |
Joshua Tsuji | d942283 | 2019-03-05 13:32:37 -0500 | [diff] [blame] | 713 | public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) { |
| 714 | if (mStackView != null && mStackView.getBubbleCount() > 0) { |
| 715 | mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight)); |
Joshua Tsuji | a19515f | 2019-02-13 18:02:29 -0500 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | |
| 719 | @Override |
| 720 | public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight) |
| 721 | throws RemoteException {} |
| 722 | |
| 723 | @Override |
| 724 | public void onMinimizedStateChanged(boolean isMinimized) throws RemoteException {} |
| 725 | |
| 726 | @Override |
| 727 | public void onActionsChanged(ParceledListSlice actions) throws RemoteException {} |
| 728 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 729 | } |