Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 19 | import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 20 | import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; |
| 21 | |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 22 | import android.app.Notification; |
| 23 | import android.app.PendingIntent; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.res.Resources; |
Joshua Tsuji | 580c0bf | 2019-01-28 13:28:21 -0500 | [diff] [blame] | 26 | import android.graphics.Outline; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 27 | import android.graphics.Point; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 28 | import android.graphics.PointF; |
| 29 | import android.graphics.Rect; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 30 | import android.graphics.RectF; |
Mady Mellor | 217b2e9 | 2019-02-27 11:44:16 -0800 | [diff] [blame] | 31 | import android.os.Bundle; |
Steven Wu | a254dab | 2019-01-29 11:30:39 -0500 | [diff] [blame] | 32 | import android.service.notification.StatusBarNotification; |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 33 | import android.util.Log; |
Steven Wu | a254dab | 2019-01-29 11:30:39 -0500 | [diff] [blame] | 34 | import android.util.StatsLog; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 35 | import android.view.LayoutInflater; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 36 | import android.view.MotionEvent; |
| 37 | import android.view.View; |
Joshua Tsuji | 580c0bf | 2019-01-28 13:28:21 -0500 | [diff] [blame] | 38 | import android.view.ViewOutlineProvider; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 39 | import android.view.ViewTreeObserver; |
Joshua Tsuji | 0fee768 | 2019-01-25 11:37:49 -0500 | [diff] [blame] | 40 | import android.view.WindowInsets; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 41 | import android.view.WindowManager; |
Mady Mellor | 217b2e9 | 2019-02-27 11:44:16 -0800 | [diff] [blame] | 42 | import android.view.accessibility.AccessibilityNodeInfo; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 43 | import android.widget.FrameLayout; |
| 44 | |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 45 | import androidx.annotation.MainThread; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 46 | import androidx.annotation.Nullable; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 47 | import androidx.dynamicanimation.animation.DynamicAnimation; |
| 48 | import androidx.dynamicanimation.animation.SpringAnimation; |
| 49 | import androidx.dynamicanimation.animation.SpringForce; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 50 | |
Mady Mellor | edd4ee1 | 2019-01-18 10:45:11 -0800 | [diff] [blame] | 51 | import com.android.internal.annotations.VisibleForTesting; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 52 | import com.android.internal.widget.ViewClippingUtil; |
| 53 | import com.android.systemui.R; |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 54 | import com.android.systemui.bubbles.BubbleController.DismissReason; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 55 | import com.android.systemui.bubbles.animation.ExpandedAnimationController; |
| 56 | import com.android.systemui.bubbles.animation.PhysicsAnimationLayout; |
| 57 | import com.android.systemui.bubbles.animation.StackAnimationController; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 58 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 59 | |
Steven Wu | a254dab | 2019-01-29 11:30:39 -0500 | [diff] [blame] | 60 | import java.math.BigDecimal; |
| 61 | import java.math.RoundingMode; |
| 62 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 63 | /** |
| 64 | * Renders bubbles in a stack and handles animating expanded and collapsed states. |
| 65 | */ |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 66 | public class BubbleStackView extends FrameLayout { |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 67 | private static final String TAG = "BubbleStackView"; |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 68 | private static final boolean DEBUG = false; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 69 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 70 | private Point mDisplaySize; |
| 71 | |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 72 | private final SpringAnimation mExpandedViewXAnim; |
| 73 | private final SpringAnimation mExpandedViewYAnim; |
Mady Mellor | cfd06c1 | 2019-02-13 14:32:12 -0800 | [diff] [blame] | 74 | private final BubbleData mBubbleData; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 75 | |
| 76 | private PhysicsAnimationLayout mBubbleContainer; |
| 77 | private StackAnimationController mStackAnimationController; |
| 78 | private ExpandedAnimationController mExpandedAnimationController; |
| 79 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 80 | private FrameLayout mExpandedViewContainer; |
| 81 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 82 | |
| 83 | private int mBubbleSize; |
| 84 | private int mBubblePadding; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 85 | private int mExpandedAnimateXDistance; |
| 86 | private int mExpandedAnimateYDistance; |
Joshua Tsuji | f44347f | 2019-02-12 14:28:06 -0500 | [diff] [blame] | 87 | private int mStatusBarHeight; |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 88 | private int mPipDismissHeight; |
Joshua Tsuji | a19515f | 2019-02-13 18:02:29 -0500 | [diff] [blame] | 89 | private int mImeOffset; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 90 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 91 | private Bubble mExpandedBubble; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 92 | private boolean mIsExpanded; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 93 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 94 | private BubbleTouchHandler mTouchHandler; |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 95 | private BubbleController.BubbleExpandListener mExpandListener; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 96 | private BubbleExpandedView.OnBubbleBlockedListener mBlockedListener; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 97 | |
| 98 | private boolean mViewUpdatedRequested = false; |
| 99 | private boolean mIsAnimating = false; |
| 100 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 101 | private LayoutInflater mInflater; |
| 102 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 103 | // Used for determining view / touch intersection |
| 104 | int[] mTempLoc = new int[2]; |
| 105 | RectF mTempRect = new RectF(); |
| 106 | |
| 107 | private ViewTreeObserver.OnPreDrawListener mViewUpdater = |
| 108 | new ViewTreeObserver.OnPreDrawListener() { |
| 109 | @Override |
| 110 | public boolean onPreDraw() { |
| 111 | getViewTreeObserver().removeOnPreDrawListener(mViewUpdater); |
| 112 | applyCurrentState(); |
| 113 | mViewUpdatedRequested = false; |
| 114 | return true; |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | private ViewClippingUtil.ClippingParameters mClippingParameters = |
| 119 | new ViewClippingUtil.ClippingParameters() { |
| 120 | |
| 121 | @Override |
| 122 | public boolean shouldFinish(View view) { |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public boolean isClippingEnablingAllowed(View view) { |
| 128 | return !mIsExpanded; |
| 129 | } |
| 130 | }; |
| 131 | |
Mady Mellor | cfd06c1 | 2019-02-13 14:32:12 -0800 | [diff] [blame] | 132 | public BubbleStackView(Context context, BubbleData data) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 133 | super(context); |
| 134 | |
Mady Mellor | cfd06c1 | 2019-02-13 14:32:12 -0800 | [diff] [blame] | 135 | mBubbleData = data; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 136 | mInflater = LayoutInflater.from(context); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 137 | mTouchHandler = new BubbleTouchHandler(context, this); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 138 | setOnTouchListener(mTouchHandler); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 139 | mInflater = LayoutInflater.from(context); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 140 | |
| 141 | Resources res = getResources(); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 142 | mBubbleSize = res.getDimensionPixelSize(R.dimen.individual_bubble_size); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 143 | mBubblePadding = res.getDimensionPixelSize(R.dimen.bubble_padding); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 144 | mExpandedAnimateXDistance = |
| 145 | res.getDimensionPixelSize(R.dimen.bubble_expanded_animate_x_distance); |
| 146 | mExpandedAnimateYDistance = |
| 147 | res.getDimensionPixelSize(R.dimen.bubble_expanded_animate_y_distance); |
Joshua Tsuji | f44347f | 2019-02-12 14:28:06 -0500 | [diff] [blame] | 148 | mStatusBarHeight = |
| 149 | res.getDimensionPixelSize(com.android.internal.R.dimen.status_bar_height); |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 150 | mPipDismissHeight = mContext.getResources().getDimensionPixelSize( |
| 151 | R.dimen.pip_dismiss_gradient_height); |
Joshua Tsuji | a19515f | 2019-02-13 18:02:29 -0500 | [diff] [blame] | 152 | mImeOffset = res.getDimensionPixelSize(R.dimen.pip_ime_offset); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 153 | |
| 154 | mDisplaySize = new Point(); |
| 155 | WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
| 156 | wm.getDefaultDisplay().getSize(mDisplaySize); |
| 157 | |
| 158 | int padding = res.getDimensionPixelSize(R.dimen.bubble_expanded_view_padding); |
| 159 | int elevation = res.getDimensionPixelSize(R.dimen.bubble_elevation); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 160 | |
| 161 | mStackAnimationController = new StackAnimationController(); |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 162 | mExpandedAnimationController = new ExpandedAnimationController(mDisplaySize); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 163 | |
| 164 | mBubbleContainer = new PhysicsAnimationLayout(context); |
| 165 | mBubbleContainer.setMaxRenderedChildren( |
| 166 | getResources().getInteger(R.integer.bubbles_max_rendered)); |
| 167 | mBubbleContainer.setController(mStackAnimationController); |
| 168 | mBubbleContainer.setElevation(elevation); |
| 169 | mBubbleContainer.setPadding(padding, 0, padding, 0); |
| 170 | mBubbleContainer.setClipChildren(false); |
| 171 | addView(mBubbleContainer, new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)); |
| 172 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 173 | mExpandedViewContainer = new FrameLayout(context); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 174 | mExpandedViewContainer.setElevation(elevation); |
| 175 | mExpandedViewContainer.setPadding(padding, padding, padding, padding); |
| 176 | mExpandedViewContainer.setClipChildren(false); |
| 177 | addView(mExpandedViewContainer); |
| 178 | |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 179 | mExpandedViewXAnim = |
| 180 | new SpringAnimation(mExpandedViewContainer, DynamicAnimation.TRANSLATION_X); |
| 181 | mExpandedViewXAnim.setSpring( |
| 182 | new SpringForce() |
| 183 | .setStiffness(SpringForce.STIFFNESS_LOW) |
| 184 | .setDampingRatio(SpringForce.DAMPING_RATIO_LOW_BOUNCY)); |
| 185 | |
| 186 | mExpandedViewYAnim = |
| 187 | new SpringAnimation(mExpandedViewContainer, DynamicAnimation.TRANSLATION_Y); |
| 188 | mExpandedViewYAnim.setSpring( |
| 189 | new SpringForce() |
| 190 | .setStiffness(SpringForce.STIFFNESS_LOW) |
| 191 | .setDampingRatio(SpringForce.DAMPING_RATIO_LOW_BOUNCY)); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 192 | |
| 193 | setClipChildren(false); |
Mady Mellor | 217b2e9 | 2019-02-27 11:44:16 -0800 | [diff] [blame] | 194 | setFocusable(true); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 195 | mBubbleContainer.bringToFront(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | @Override |
Mady Mellor | 217b2e9 | 2019-02-27 11:44:16 -0800 | [diff] [blame] | 199 | public void getBoundsOnScreen(Rect outRect, boolean clipToParent) { |
| 200 | getBoundsOnScreen(outRect); |
| 201 | } |
| 202 | |
| 203 | @Override |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 204 | protected void onDetachedFromWindow() { |
| 205 | super.onDetachedFromWindow(); |
| 206 | getViewTreeObserver().removeOnPreDrawListener(mViewUpdater); |
| 207 | } |
| 208 | |
| 209 | @Override |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 210 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 211 | float x = ev.getRawX(); |
| 212 | float y = ev.getRawY(); |
| 213 | // If we're expanded only intercept if the tap is outside of the widget container |
| 214 | if (mIsExpanded && isIntersecting(mExpandedViewContainer, x, y)) { |
| 215 | return false; |
| 216 | } else { |
| 217 | return isIntersecting(mBubbleContainer, x, y); |
| 218 | } |
| 219 | } |
| 220 | |
Mady Mellor | 217b2e9 | 2019-02-27 11:44:16 -0800 | [diff] [blame] | 221 | @Override |
| 222 | public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { |
| 223 | super.onInitializeAccessibilityNodeInfoInternal(info); |
| 224 | info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_DISMISS); |
| 225 | if (mIsExpanded) { |
| 226 | info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_COLLAPSE); |
| 227 | } else { |
| 228 | info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_EXPAND); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public boolean performAccessibilityActionInternal(int action, Bundle arguments) { |
| 234 | if (super.performAccessibilityActionInternal(action, arguments)) { |
| 235 | return true; |
| 236 | } |
| 237 | switch (action) { |
| 238 | case AccessibilityNodeInfo.ACTION_DISMISS: |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 239 | stackDismissed(BubbleController.DISMISS_ACCESSIBILITY_ACTION); |
Mady Mellor | 217b2e9 | 2019-02-27 11:44:16 -0800 | [diff] [blame] | 240 | return true; |
| 241 | case AccessibilityNodeInfo.ACTION_COLLAPSE: |
| 242 | collapseStack(); |
| 243 | return true; |
| 244 | case AccessibilityNodeInfo.ACTION_EXPAND: |
| 245 | expandStack(); |
| 246 | return true; |
| 247 | } |
| 248 | return false; |
| 249 | } |
| 250 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 251 | /** |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 252 | * Updates the visibility of the 'dot' indicating an update on the bubble. |
| 253 | * @param key the {@link NotificationEntry#key} associated with the bubble. |
| 254 | */ |
| 255 | public void updateDotVisibility(String key) { |
| 256 | Bubble b = mBubbleData.getBubble(key); |
| 257 | if (b != null) { |
| 258 | b.iconView.updateDotVisibility(); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /** |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 263 | * Sets the listener to notify when the bubble stack is expanded. |
| 264 | */ |
| 265 | public void setExpandListener(BubbleController.BubbleExpandListener listener) { |
| 266 | mExpandListener = listener; |
| 267 | } |
| 268 | |
| 269 | /** |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 270 | * Sets the listener to notify when a bubble is blocked. |
| 271 | */ |
Mady Mellor | 3d82e68 | 2019-02-05 13:34:48 -0800 | [diff] [blame] | 272 | public void setOnBlockedListener(BubbleExpandedView.OnBubbleBlockedListener listener) { |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 273 | mBlockedListener = listener; |
| 274 | for (Bubble b : mBubbleData.getBubbles()) { |
| 275 | b.expandedView.setOnBlockedListener(mBlockedListener); |
| 276 | } |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /** |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 280 | * Whether the stack of bubbles is expanded or not. |
| 281 | */ |
| 282 | public boolean isExpanded() { |
| 283 | return mIsExpanded; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * The {@link BubbleView} that is expanded, null if one does not exist. |
| 288 | */ |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 289 | BubbleView getExpandedBubbleView() { |
| 290 | return mExpandedBubble != null ? mExpandedBubble.iconView : null; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * The {@link Bubble} that is expanded, null if one does not exist. |
| 295 | */ |
| 296 | Bubble getExpandedBubble() { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 297 | return mExpandedBubble; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Sets the bubble that should be expanded and expands if needed. |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 302 | * |
| 303 | * @param key the {@link NotificationEntry#key} associated with the bubble to expand. |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 304 | */ |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 305 | void setExpandedBubble(String key) { |
| 306 | Bubble bubbleToExpand = mBubbleData.getBubble(key); |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 307 | if (mIsExpanded && !bubbleToExpand.equals(mExpandedBubble)) { |
| 308 | // Previously expanded, notify that this bubble is no longer expanded |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 309 | notifyExpansionChanged(mExpandedBubble.entry, false /* expanded */); |
Mady Mellor | acb1215 | 2019-01-29 15:24:48 -0800 | [diff] [blame] | 310 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 311 | Bubble prevBubble = mExpandedBubble; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 312 | mExpandedBubble = bubbleToExpand; |
Mady Mellor | de2d4d2 | 2019-01-29 14:15:34 -0800 | [diff] [blame] | 313 | if (!mIsExpanded) { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 314 | // If we weren't previously expanded we should animate open. |
| 315 | animateExpansion(true /* expand */); |
Steven Wu | a254dab | 2019-01-29 11:30:39 -0500 | [diff] [blame] | 316 | logBubbleEvent(mExpandedBubble, StatsLog.BUBBLE_UICHANGED__ACTION__EXPANDED); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 317 | } else { |
Mady Mellor | de2d4d2 | 2019-01-29 14:15:34 -0800 | [diff] [blame] | 318 | // Otherwise just update the views |
| 319 | // TODO: probably animate / page to expanded one |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 320 | updateExpandedBubble(); |
Mady Mellor | de2d4d2 | 2019-01-29 14:15:34 -0800 | [diff] [blame] | 321 | updatePointerPosition(); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 322 | requestUpdate(); |
Steven Wu | a254dab | 2019-01-29 11:30:39 -0500 | [diff] [blame] | 323 | logBubbleEvent(prevBubble, StatsLog.BUBBLE_UICHANGED__ACTION__COLLAPSED); |
| 324 | logBubbleEvent(mExpandedBubble, StatsLog.BUBBLE_UICHANGED__ACTION__EXPANDED); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 325 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 326 | mExpandedBubble.entry.setShowInShadeWhenBubble(false); |
| 327 | notifyExpansionChanged(mExpandedBubble.entry, true /* expanded */); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /** |
Mady Mellor | edd4ee1 | 2019-01-18 10:45:11 -0800 | [diff] [blame] | 331 | * Sets the entry that should be expanded and expands if needed. |
| 332 | */ |
| 333 | @VisibleForTesting |
| 334 | public void setExpandedBubble(NotificationEntry entry) { |
| 335 | for (int i = 0; i < mBubbleContainer.getChildCount(); i++) { |
| 336 | BubbleView bv = (BubbleView) mBubbleContainer.getChildAt(i); |
| 337 | if (entry.equals(bv.getEntry())) { |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 338 | setExpandedBubble(entry.key); |
Mady Mellor | edd4ee1 | 2019-01-18 10:45:11 -0800 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | /** |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 344 | * Adds a bubble to the top of the stack. |
| 345 | * |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 346 | * @param entry the notification to add to the stack of bubbles. |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 347 | */ |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 348 | public void addBubble(NotificationEntry entry) { |
| 349 | Bubble b = new Bubble(entry, mInflater, this /* stackView */, mBlockedListener); |
| 350 | mBubbleData.addBubble(b); |
| 351 | |
| 352 | mBubbleContainer.addView(b.iconView, 0, |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 353 | new FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 354 | ViewClippingUtil.setClippingDeactivated(b.iconView, true, mClippingParameters); |
| 355 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 356 | requestUpdate(); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 357 | logBubbleEvent(b, StatsLog.BUBBLE_UICHANGED__ACTION__POSTED); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Remove a bubble from the stack. |
| 362 | */ |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 363 | public void removeBubble(String key, int reason) { |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 364 | Bubble b = mBubbleData.removeBubble(key); |
| 365 | if (b == null) { |
| 366 | return; |
| 367 | } |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 368 | int removedIndex = dismissBubble(b, reason); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 369 | int bubbleCount = mBubbleContainer.getChildCount(); |
Mark Renouf | 658c6bc | 2019-01-30 10:26:54 -0500 | [diff] [blame] | 370 | if (bubbleCount == 0) { |
| 371 | // If no bubbles remain, collapse the entire stack. |
| 372 | collapseStack(); |
| 373 | return; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 374 | } else if (b.equals(mExpandedBubble)) { |
Mark Renouf | 658c6bc | 2019-01-30 10:26:54 -0500 | [diff] [blame] | 375 | // Was the current bubble just removed? |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 376 | // If we have other bubbles and are expanded go to the next one or previous |
| 377 | // if the bubble removed was last |
| 378 | int nextIndex = bubbleCount > removedIndex ? removedIndex : bubbleCount - 1; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 379 | BubbleView expandedBubble = (BubbleView) mBubbleContainer.getChildAt(nextIndex); |
Mark Renouf | 658c6bc | 2019-01-30 10:26:54 -0500 | [diff] [blame] | 380 | if (mIsExpanded) { |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 381 | setExpandedBubble(expandedBubble.getKey()); |
Mark Renouf | 658c6bc | 2019-01-30 10:26:54 -0500 | [diff] [blame] | 382 | } else { |
| 383 | mExpandedBubble = null; |
| 384 | } |
Mady Mellor | cd9b130 | 2018-11-06 18:08:04 -0800 | [diff] [blame] | 385 | } |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 386 | // TODO: consider logging reason code |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 387 | logBubbleEvent(b, StatsLog.BUBBLE_UICHANGED__ACTION__DISMISSED); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Dismiss the stack of bubbles. |
| 392 | */ |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 393 | public void stackDismissed(int reason) { |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 394 | for (Bubble bubble : mBubbleData.getBubbles()) { |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 395 | dismissBubble(bubble, reason); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 396 | } |
| 397 | mBubbleData.clear(); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 398 | collapseStack(); |
| 399 | mBubbleContainer.removeAllViews(); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 400 | mExpandedViewContainer.removeAllViews(); |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 401 | // TODO: consider logging reason code |
Steven Wu | a254dab | 2019-01-29 11:30:39 -0500 | [diff] [blame] | 402 | logBubbleEvent(null /* no bubble associated with bubble stack dismiss */, |
| 403 | StatsLog.BUBBLE_UICHANGED__ACTION__STACK_DISMISSED); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /** |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 407 | * Marks the notification entry as dismissed, cleans up Bubble icon and expanded view UI |
| 408 | * elements and calls deleteIntent if necessary. |
| 409 | * |
| 410 | * <p>Note: This does not remove the Bubble from BubbleData. |
| 411 | * |
| 412 | * @param bubble the Bubble being dismissed |
| 413 | * @param reason code for the reason the dismiss was triggered |
| 414 | * @see BubbleController.DismissReason |
| 415 | */ |
| 416 | private int dismissBubble(Bubble bubble, @DismissReason int reason) { |
| 417 | if (DEBUG) { |
| 418 | Log.d(TAG, "dismissBubble: " + bubble + " reason=" + reason); |
| 419 | } |
| 420 | bubble.entry.setBubbleDismissed(true); |
| 421 | bubble.expandedView.cleanUpExpandedState(); |
| 422 | |
| 423 | // Remove it from the views |
| 424 | int removedIndex = mBubbleContainer.indexOfChild(bubble.iconView); |
| 425 | mBubbleContainer.removeViewAt(removedIndex); |
| 426 | |
| 427 | if (reason == BubbleController.DISMISS_USER_GESTURE) { |
| 428 | Notification.BubbleMetadata bubbleMetadata = bubble.entry.getBubbleMetadata(); |
| 429 | PendingIntent deleteIntent = bubbleMetadata.getDeleteIntent(); |
| 430 | if (deleteIntent != null) { |
| 431 | try { |
| 432 | deleteIntent.send(); |
| 433 | } catch (PendingIntent.CanceledException e) { |
| 434 | Log.w(TAG, "Failed to send delete intent for bubble with key: " |
| 435 | + (bubble.entry != null ? bubble.entry.key : " null entry")); |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | return removedIndex; |
| 440 | } |
| 441 | |
| 442 | /** |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 443 | * Updates a bubble in the stack. |
| 444 | * |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 445 | * @param entry the entry to update in the stack. |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 446 | * @param updatePosition whether this bubble should be moved to top of the stack. |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 447 | */ |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 448 | public void updateBubble(NotificationEntry entry, boolean updatePosition) { |
| 449 | Bubble b = mBubbleData.getBubble(entry.key); |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 450 | mBubbleData.updateBubble(entry.key, entry); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 451 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 452 | if (updatePosition && !mIsExpanded) { |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 453 | // If alerting it gets promoted to top of the stack. |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 454 | if (mBubbleContainer.indexOfChild(b.iconView) != 0) { |
| 455 | mBubbleContainer.moveViewTo(b.iconView, 0); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 456 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 457 | requestUpdate(); |
| 458 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 459 | if (mIsExpanded && entry.equals(mExpandedBubble.entry)) { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 460 | entry.setShowInShadeWhenBubble(false); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 461 | requestUpdate(); |
| 462 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 463 | logBubbleEvent(b, StatsLog.BUBBLE_UICHANGED__ACTION__UPDATED); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /** |
| 467 | * @return the view the touch event is on |
| 468 | */ |
| 469 | @Nullable |
| 470 | public View getTargetView(MotionEvent event) { |
| 471 | float x = event.getRawX(); |
| 472 | float y = event.getRawY(); |
| 473 | if (mIsExpanded) { |
| 474 | if (isIntersecting(mBubbleContainer, x, y)) { |
| 475 | for (int i = 0; i < mBubbleContainer.getChildCount(); i++) { |
| 476 | BubbleView view = (BubbleView) mBubbleContainer.getChildAt(i); |
| 477 | if (isIntersecting(view, x, y)) { |
| 478 | return view; |
| 479 | } |
| 480 | } |
| 481 | } else if (isIntersecting(mExpandedViewContainer, x, y)) { |
| 482 | return mExpandedViewContainer; |
| 483 | } |
| 484 | // Outside parts of view we care about. |
| 485 | return null; |
| 486 | } |
| 487 | // If we're collapsed, the stack is always the target. |
| 488 | return this; |
| 489 | } |
| 490 | |
| 491 | /** |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 492 | * Collapses the stack of bubbles. |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 493 | * <p> |
| 494 | * Must be called from the main thread. |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 495 | */ |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 496 | @MainThread |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 497 | public void collapseStack() { |
| 498 | if (mIsExpanded) { |
| 499 | // TODO: Save opened bubble & move it to top of stack |
| 500 | animateExpansion(false /* shouldExpand */); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 501 | notifyExpansionChanged(mExpandedBubble.entry, mIsExpanded); |
Steven Wu | a254dab | 2019-01-29 11:30:39 -0500 | [diff] [blame] | 502 | logBubbleEvent(mExpandedBubble, StatsLog.BUBBLE_UICHANGED__ACTION__COLLAPSED); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 506 | void collapseStack(Runnable endRunnable) { |
| 507 | collapseStack(); |
| 508 | // TODO - use the runnable at end of animation |
| 509 | endRunnable.run(); |
| 510 | } |
| 511 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 512 | /** |
Mady Mellor | 94d94a7 | 2019-03-05 18:16:59 -0800 | [diff] [blame] | 513 | * Expands the stack of bubbles. |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 514 | * <p> |
| 515 | * Must be called from the main thread. |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 516 | */ |
Mark Renouf | cecc77b | 2019-01-30 16:32:24 -0500 | [diff] [blame] | 517 | @MainThread |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 518 | public void expandStack() { |
| 519 | if (!mIsExpanded) { |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 520 | String expandedBubbleKey = getBubbleAt(0).getKey(); |
| 521 | setExpandedBubble(expandedBubbleKey); |
Steven Wu | a254dab | 2019-01-29 11:30:39 -0500 | [diff] [blame] | 522 | logBubbleEvent(mExpandedBubble, StatsLog.BUBBLE_UICHANGED__ACTION__STACK_EXPANDED); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
| 526 | /** |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 527 | * Tell the stack to animate to collapsed or expanded state. |
| 528 | */ |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 529 | private void animateExpansion(boolean shouldExpand) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 530 | if (mIsExpanded != shouldExpand) { |
| 531 | mIsExpanded = shouldExpand; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 532 | updateExpandedBubble(); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 533 | applyCurrentState(); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 534 | |
| 535 | mIsAnimating = true; |
| 536 | |
| 537 | Runnable updateAfter = () -> { |
| 538 | applyCurrentState(); |
| 539 | mIsAnimating = false; |
| 540 | requestUpdate(); |
| 541 | }; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 542 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 543 | if (shouldExpand) { |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 544 | mBubbleContainer.setController(mExpandedAnimationController); |
| 545 | mExpandedAnimationController.expandFromStack( |
Joshua Tsuji | 3829caa | 2019-03-05 18:09:13 -0500 | [diff] [blame] | 546 | /* collapseTo */ |
| 547 | mStackAnimationController.getStackPositionAlongNearestHorizontalEdge(), |
| 548 | /* after */ |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 549 | () -> { |
| 550 | updatePointerPosition(); |
| 551 | updateAfter.run(); |
Mady Mellor | de2d4d2 | 2019-01-29 14:15:34 -0800 | [diff] [blame] | 552 | }); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 553 | } else { |
| 554 | mBubbleContainer.cancelAllAnimations(); |
| 555 | mExpandedAnimationController.collapseBackToStack( |
| 556 | () -> { |
| 557 | mBubbleContainer.setController(mStackAnimationController); |
| 558 | updateAfter.run(); |
| 559 | }); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 560 | } |
| 561 | |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 562 | final float xStart = |
| 563 | mStackAnimationController.getStackPosition().x < getWidth() / 2 |
| 564 | ? -mExpandedAnimateXDistance |
| 565 | : mExpandedAnimateXDistance; |
| 566 | |
| 567 | final float yStart = Math.min( |
| 568 | mStackAnimationController.getStackPosition().y, |
| 569 | mExpandedAnimateYDistance); |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 570 | final float yDest = getYPositionForExpandedView(); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 571 | |
| 572 | if (shouldExpand) { |
| 573 | mExpandedViewContainer.setTranslationX(xStart); |
| 574 | mExpandedViewContainer.setTranslationY(yStart); |
| 575 | mExpandedViewContainer.setAlpha(0f); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 576 | } |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 577 | |
| 578 | mExpandedViewXAnim.animateToFinalPosition(shouldExpand ? 0f : xStart); |
| 579 | mExpandedViewYAnim.animateToFinalPosition(shouldExpand ? yDest : yStart); |
| 580 | mExpandedViewContainer.animate() |
| 581 | .setDuration(100) |
| 582 | .alpha(shouldExpand ? 1f : 0f); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * The width of the collapsed stack of bubbles. |
| 588 | */ |
| 589 | public int getStackWidth() { |
| 590 | return mBubblePadding * (mBubbleContainer.getChildCount() - 1) |
| 591 | + mBubbleSize + mBubbleContainer.getPaddingEnd() |
| 592 | + mBubbleContainer.getPaddingStart(); |
| 593 | } |
| 594 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 595 | private void notifyExpansionChanged(NotificationEntry entry, boolean expanded) { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 596 | if (mExpandListener != null) { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 597 | mExpandListener.onBubbleExpandChanged(expanded, entry != null ? entry.key : null); |
| 598 | } |
| 599 | } |
| 600 | |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 601 | /** Return the BubbleView at the given index from the bubble container. */ |
| 602 | public BubbleView getBubbleAt(int i) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 603 | return mBubbleContainer.getChildCount() > i |
| 604 | ? (BubbleView) mBubbleContainer.getChildAt(i) |
| 605 | : null; |
| 606 | } |
| 607 | |
Joshua Tsuji | a19515f | 2019-02-13 18:02:29 -0500 | [diff] [blame] | 608 | /** Moves the bubbles out of the way if they're going to be over the keyboard. */ |
| 609 | public void onImeVisibilityChanged(boolean visible, int height) { |
| 610 | if (!mIsExpanded) { |
| 611 | if (visible) { |
| 612 | mStackAnimationController.updateBoundsForVisibleImeAndAnimate(height + mImeOffset); |
| 613 | } else { |
| 614 | mStackAnimationController.updateBoundsForInvisibleImeAndAnimate(); |
| 615 | } |
| 616 | } |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | /** Called when a drag operation on an individual bubble has started. */ |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 620 | public void onBubbleDragStart(View bubble) { |
| 621 | mExpandedAnimationController.prepareForBubbleDrag(bubble); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /** Called with the coordinates to which an individual bubble has been dragged. */ |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 625 | public void onBubbleDragged(View bubble, float x, float y) { |
| 626 | if (!mIsExpanded || mIsAnimating) { |
| 627 | return; |
| 628 | } |
| 629 | |
| 630 | mExpandedAnimationController.dragBubbleOut(bubble, x, y); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /** Called when a drag operation on an individual bubble has finished. */ |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 634 | public void onBubbleDragFinish( |
| 635 | View bubble, float x, float y, float velX, float velY, boolean dismissed) { |
| 636 | if (!mIsExpanded || mIsAnimating) { |
| 637 | return; |
| 638 | } |
| 639 | |
| 640 | if (dismissed) { |
| 641 | mExpandedAnimationController.prepareForDismissalWithVelocity(bubble, velX, velY); |
| 642 | } else { |
| 643 | mExpandedAnimationController.snapBubbleBack(bubble, velX, velY); |
| 644 | } |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | void onDragStart() { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 648 | if (mIsExpanded || mIsAnimating) { |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 649 | return; |
| 650 | } |
| 651 | |
| 652 | mStackAnimationController.cancelStackPositionAnimations(); |
| 653 | mBubbleContainer.setController(mStackAnimationController); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | void onDragged(float x, float y) { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 657 | if (mIsExpanded || mIsAnimating) { |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 658 | return; |
| 659 | } |
| 660 | |
| 661 | mStackAnimationController.moveFirstBubbleWithStackFollowing(x, y); |
| 662 | } |
| 663 | |
| 664 | void onDragFinish(float x, float y, float velX, float velY) { |
| 665 | // TODO: Add fling to bottom to dismiss. |
| 666 | |
| 667 | if (mIsExpanded || mIsAnimating) { |
| 668 | return; |
| 669 | } |
| 670 | |
Joshua Tsuji | e13b4fc | 2019-02-28 18:39:57 -0500 | [diff] [blame] | 671 | mStackAnimationController.flingStackThenSpringToEdge(x, velX, velY); |
Steven Wu | a254dab | 2019-01-29 11:30:39 -0500 | [diff] [blame] | 672 | logBubbleEvent(null /* no bubble associated with bubble stack move */, |
| 673 | StatsLog.BUBBLE_UICHANGED__ACTION__STACK_MOVED); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /** |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 677 | * Calculates how large the expanded view of the bubble can be. This takes into account the |
| 678 | * y position when the bubbles are expanded as well as the bounds of the dismiss target. |
| 679 | */ |
| 680 | int getMaxExpandedHeight() { |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 681 | boolean showOnTop = BubbleController.showBubblesAtTop(getContext()); |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 682 | int expandedY = (int) mExpandedAnimationController.getExpandedY(); |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 683 | if (showOnTop) { |
| 684 | // PIP dismiss view uses FLAG_LAYOUT_IN_SCREEN so we need to subtract the bottom inset |
| 685 | int pipDismissHeight = mPipDismissHeight - getBottomInset(); |
| 686 | return mDisplaySize.y - expandedY - mBubbleSize - pipDismissHeight; |
| 687 | } else { |
| 688 | return expandedY - getStatusBarHeight(); |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | /** |
| 693 | * Calculates the y position of the expanded view when it is expanded. |
| 694 | */ |
| 695 | float getYPositionForExpandedView() { |
| 696 | boolean showOnTop = BubbleController.showBubblesAtTop(getContext()); |
| 697 | if (showOnTop) { |
| 698 | return getStatusBarHeight() + mBubbleSize + mBubblePadding; |
| 699 | } else { |
| 700 | return mExpandedAnimationController.getExpandedY() |
| 701 | - mExpandedBubble.expandedView.getExpandedSize() - mBubblePadding; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * Called when the height of the currently expanded view has changed (not via an |
| 707 | * update to the bubble's desired height but for some other reason, e.g. permission view |
| 708 | * goes away). |
| 709 | */ |
| 710 | void onExpandedHeightChanged() { |
| 711 | if (mIsExpanded) { |
| 712 | requestUpdate(); |
| 713 | } |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 714 | } |
| 715 | |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 716 | @Override |
| 717 | public void getBoundsOnScreen(Rect outRect) { |
| 718 | if (!mIsExpanded) { |
Mady Mellor | 217b2e9 | 2019-02-27 11:44:16 -0800 | [diff] [blame] | 719 | if (mBubbleContainer.getChildCount() > 0) { |
| 720 | mBubbleContainer.getChildAt(0).getBoundsOnScreen(outRect); |
| 721 | } |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 722 | } else { |
| 723 | mBubbleContainer.getBoundsOnScreen(outRect); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | private int getStatusBarHeight() { |
| 728 | if (getRootWindowInsets() != null) { |
Joshua Tsuji | 0fee768 | 2019-01-25 11:37:49 -0500 | [diff] [blame] | 729 | WindowInsets insets = getRootWindowInsets(); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 730 | return Math.max( |
Joshua Tsuji | f44347f | 2019-02-12 14:28:06 -0500 | [diff] [blame] | 731 | mStatusBarHeight, |
Joshua Tsuji | 0fee768 | 2019-01-25 11:37:49 -0500 | [diff] [blame] | 732 | insets.getDisplayCutout() != null |
| 733 | ? insets.getDisplayCutout().getSafeInsetTop() |
| 734 | : 0); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | return 0; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 738 | } |
| 739 | |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 740 | private int getBottomInset() { |
| 741 | if (getRootWindowInsets() != null) { |
| 742 | WindowInsets insets = getRootWindowInsets(); |
| 743 | return insets.getSystemWindowInsetBottom(); |
| 744 | } |
| 745 | return 0; |
| 746 | } |
| 747 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 748 | private boolean isIntersecting(View view, float x, float y) { |
| 749 | mTempLoc = view.getLocationOnScreen(); |
| 750 | mTempRect.set(mTempLoc[0], mTempLoc[1], mTempLoc[0] + view.getWidth(), |
| 751 | mTempLoc[1] + view.getHeight()); |
| 752 | return mTempRect.contains(x, y); |
| 753 | } |
| 754 | |
| 755 | private void requestUpdate() { |
| 756 | if (mViewUpdatedRequested || mIsAnimating) { |
| 757 | return; |
| 758 | } |
| 759 | mViewUpdatedRequested = true; |
| 760 | getViewTreeObserver().addOnPreDrawListener(mViewUpdater); |
| 761 | invalidate(); |
| 762 | } |
| 763 | |
| 764 | private void updateExpandedBubble() { |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 765 | mExpandedViewContainer.removeAllViews(); |
| 766 | if (mExpandedBubble != null && mIsExpanded) { |
| 767 | mExpandedViewContainer.addView(mExpandedBubble.expandedView); |
Mady Mellor | 5029fa6 | 2019-03-05 12:16:21 -0800 | [diff] [blame] | 768 | mExpandedBubble.expandedView.populateExpandedView(); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 769 | mExpandedViewContainer.setVisibility(mIsExpanded ? VISIBLE : GONE); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | |
| 773 | private void applyCurrentState() { |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 774 | Log.d(TAG, "applyCurrentState: mIsExpanded=" + mIsExpanded); |
| 775 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 776 | mExpandedViewContainer.setVisibility(mIsExpanded ? VISIBLE : GONE); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 777 | if (mIsExpanded) { |
Mady Mellor | 5029fa6 | 2019-03-05 12:16:21 -0800 | [diff] [blame] | 778 | // First update the view so that it calculates a new height (ensuring the y position |
| 779 | // calculation is correct) |
| 780 | mExpandedBubble.expandedView.updateView(); |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 781 | final float y = getYPositionForExpandedView(); |
| 782 | mExpandedViewContainer.setTranslationY(y); |
Mady Mellor | 5029fa6 | 2019-03-05 12:16:21 -0800 | [diff] [blame] | 783 | // Then update the view so that ActivityView knows we translated |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 784 | mExpandedBubble.expandedView.updateView(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 785 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 786 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 787 | int bubbsCount = mBubbleContainer.getChildCount(); |
| 788 | for (int i = 0; i < bubbsCount; i++) { |
| 789 | BubbleView bv = (BubbleView) mBubbleContainer.getChildAt(i); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 790 | bv.updateDotVisibility(); |
| 791 | bv.setZ(bubbsCount - i); |
Joshua Tsuji | 580c0bf | 2019-01-28 13:28:21 -0500 | [diff] [blame] | 792 | |
| 793 | // Draw the shadow around the circle inscribed within the bubble's bounds. This |
| 794 | // (intentionally) does not draw a shadow behind the update dot, which should be drawing |
| 795 | // its own shadow since it's on a different (higher) plane. |
| 796 | bv.setOutlineProvider(new ViewOutlineProvider() { |
| 797 | @Override |
| 798 | public void getOutline(View view, Outline outline) { |
| 799 | outline.setOval(0, 0, mBubbleSize, mBubbleSize); |
| 800 | } |
| 801 | }); |
| 802 | bv.setClipToOutline(false); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 803 | } |
| 804 | } |
| 805 | |
Mady Mellor | de2d4d2 | 2019-01-29 14:15:34 -0800 | [diff] [blame] | 806 | private void updatePointerPosition() { |
| 807 | if (mExpandedBubble != null) { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 808 | float pointerPosition = mExpandedBubble.iconView.getTranslationX() |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 809 | + (mExpandedBubble.iconView.getWidth() / 2f); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 810 | mExpandedBubble.expandedView.setPointerPosition((int) pointerPosition); |
Mady Mellor | de2d4d2 | 2019-01-29 14:15:34 -0800 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | |
Steven Wu | a254dab | 2019-01-29 11:30:39 -0500 | [diff] [blame] | 814 | /** |
| 815 | * @return the number of bubbles in the stack view. |
| 816 | */ |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 817 | public int getBubbleCount() { |
Steven Wu | a254dab | 2019-01-29 11:30:39 -0500 | [diff] [blame] | 818 | return mBubbleContainer.getChildCount(); |
| 819 | } |
| 820 | |
| 821 | /** |
| 822 | * Finds the bubble index within the stack. |
| 823 | * |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 824 | * @param bubble the bubble to look up. |
Steven Wu | a254dab | 2019-01-29 11:30:39 -0500 | [diff] [blame] | 825 | * @return the index of the bubble view within the bubble stack. The range of the position |
| 826 | * is between 0 and the bubble count minus 1. |
| 827 | */ |
Steven Wu | a62cb6a | 2019-02-15 17:12:51 -0500 | [diff] [blame] | 828 | int getBubbleIndex(@Nullable Bubble bubble) { |
| 829 | if (bubble == null) { |
| 830 | return 0; |
| 831 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 832 | return mBubbleContainer.indexOfChild(bubble.iconView); |
Steven Wu | a254dab | 2019-01-29 11:30:39 -0500 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | /** |
| 836 | * @return the normalized x-axis position of the bubble stack rounded to 4 decimal places. |
| 837 | */ |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 838 | public float getNormalizedXPosition() { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 839 | return new BigDecimal(getStackPosition().x / mDisplaySize.x) |
Steven Wu | a254dab | 2019-01-29 11:30:39 -0500 | [diff] [blame] | 840 | .setScale(4, RoundingMode.CEILING.HALF_UP) |
| 841 | .floatValue(); |
| 842 | } |
| 843 | |
| 844 | /** |
| 845 | * @return the normalized y-axis position of the bubble stack rounded to 4 decimal places. |
| 846 | */ |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 847 | public float getNormalizedYPosition() { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 848 | return new BigDecimal(getStackPosition().y / mDisplaySize.y) |
Steven Wu | a254dab | 2019-01-29 11:30:39 -0500 | [diff] [blame] | 849 | .setScale(4, RoundingMode.CEILING.HALF_UP) |
| 850 | .floatValue(); |
| 851 | } |
| 852 | |
Joshua Tsuji | a19515f | 2019-02-13 18:02:29 -0500 | [diff] [blame] | 853 | public PointF getStackPosition() { |
| 854 | return mStackAnimationController.getStackPosition(); |
| 855 | } |
| 856 | |
Steven Wu | a254dab | 2019-01-29 11:30:39 -0500 | [diff] [blame] | 857 | /** |
| 858 | * Logs the bubble UI event. |
| 859 | * |
| 860 | * @param bubble the bubble that is being interacted on. Null value indicates that |
| 861 | * the user interaction is not specific to one bubble. |
| 862 | * @param action the user interaction enum. |
| 863 | */ |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 864 | private void logBubbleEvent(@Nullable Bubble bubble, int action) { |
Steven Wu | a254dab | 2019-01-29 11:30:39 -0500 | [diff] [blame] | 865 | if (bubble == null) { |
| 866 | StatsLog.write(StatsLog.BUBBLE_UI_CHANGED, |
| 867 | null /* package name */, |
| 868 | null /* notification channel */, |
| 869 | 0 /* notification ID */, |
| 870 | 0 /* bubble position */, |
| 871 | getBubbleCount(), |
| 872 | action, |
| 873 | getNormalizedXPosition(), |
| 874 | getNormalizedYPosition()); |
| 875 | } else { |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 876 | StatusBarNotification notification = bubble.entry.notification; |
Steven Wu | a254dab | 2019-01-29 11:30:39 -0500 | [diff] [blame] | 877 | StatsLog.write(StatsLog.BUBBLE_UI_CHANGED, |
| 878 | notification.getPackageName(), |
| 879 | notification.getNotification().getChannelId(), |
| 880 | notification.getId(), |
| 881 | getBubbleIndex(bubble), |
| 882 | getBubbleCount(), |
| 883 | action, |
| 884 | getNormalizedXPosition(), |
| 885 | getNormalizedYPosition()); |
| 886 | } |
| 887 | } |
Mark Renouf | 041d726 | 2019-02-06 12:09:41 -0500 | [diff] [blame] | 888 | |
| 889 | /** |
| 890 | * Called when a back gesture should be directed to the Bubbles stack. When expanded, |
| 891 | * a back key down/up event pair is forwarded to the bubble Activity. |
| 892 | */ |
| 893 | boolean performBackPressIfNeeded() { |
| 894 | if (!isExpanded()) { |
| 895 | return false; |
| 896 | } |
| 897 | return mExpandedBubble.expandedView.performBackPressIfNeeded(); |
| 898 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 899 | } |