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