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