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