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