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