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