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