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