blob: 8d7325118139f8608040e0947828aa06019f2936 [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
Jorim Jaggife40f7d2014-04-28 15:20:04 +02002 * Copyright (C) 2014 The Android Open Source Project
Kenny Root15a4d2f2010-03-11 18:20:12 -08003 *
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
Jorim Jaggife40f7d2014-04-28 15:20:04 +020014 * limitations under the License
Kenny Root15a4d2f2010-03-11 18:20:12 -080015 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016
Rohan Shah20790b82018-07-02 17:21:04 -070017package com.android.systemui.statusbar.notification.row;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018
Jorim Jaggi4222d9a2014-04-23 16:13:15 +020019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.ObjectAnimator;
Jorim Jaggia8b48e12014-05-19 20:26:46 +020022import android.animation.ValueAnimator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.content.Context;
Selim Cinek8efa6dd2014-05-19 16:27:37 +020024import android.graphics.Canvas;
Selim Cinekec29d342017-05-05 18:31:49 -070025import android.graphics.Color;
Selim Cinek8efa6dd2014-05-19 16:27:37 +020026import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.util.AttributeSet;
Selim Cinek332c23f2018-03-16 17:37:50 -070028import android.util.MathUtils;
Jorim Jaggi251957d2014-04-09 04:24:09 +020029import android.view.MotionEvent;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070030import android.view.View;
ztenghui62f30e02014-06-05 09:55:04 -070031import android.view.ViewAnimationUtils;
Selim Cinekae1bbbb2016-12-13 16:06:02 -080032import android.view.accessibility.AccessibilityManager;
Jorim Jaggi4222d9a2014-04-23 16:13:15 +020033import android.view.animation.Interpolator;
Jorim Jaggia8b48e12014-05-19 20:26:46 +020034import android.view.animation.PathInterpolator;
ztenghui62f30e02014-06-05 09:55:04 -070035
Dave Mankoff781ef7e2019-06-28 16:33:25 -040036import com.android.systemui.Dependency;
Winsonc0d70582016-01-29 10:24:39 -080037import com.android.systemui.Interpolators;
Jorim Jaggia8b48e12014-05-19 20:26:46 +020038import com.android.systemui.R;
Dave Mankoff468d4f62019-05-08 14:56:29 -040039import com.android.systemui.plugins.FalsingManager;
Rohan Shah20790b82018-07-02 17:21:04 -070040import com.android.systemui.statusbar.NotificationShelf;
Selim Cinek33223572016-02-19 19:32:22 -080041import com.android.systemui.statusbar.notification.FakeShadowView;
Selim Cinekc3179332016-03-04 14:44:56 -080042import com.android.systemui.statusbar.notification.NotificationUtils;
Rohan Shah20790b82018-07-02 17:21:04 -070043import com.android.systemui.statusbar.notification.stack.NotificationStackScrollLayout;
44import com.android.systemui.statusbar.notification.stack.StackStateAnimator;
Gus Prevase2d6f042018-10-17 15:25:30 -040045import com.android.systemui.statusbar.phone.DoubleTapHelper;
Jorim Jaggi46739852014-04-15 09:58:24 +020046
Jorim Jaggife40f7d2014-04-28 15:20:04 +020047/**
Selim Cinek281c2022016-10-13 19:14:43 -070048 * Base class for both {@link ExpandableNotificationRow} and {@link NotificationShelf}
Jorim Jaggife40f7d2014-04-28 15:20:04 +020049 * to implement dimming/activating on Keyguard for the double-tap gesture
50 */
Jorim Jaggibe565df2014-04-28 17:51:23 +020051public abstract class ActivatableNotificationView extends ExpandableOutlineView {
Jorim Jaggi251957d2014-04-09 04:24:09 +020052
Jorim Jaggid552d9d2014-05-07 19:41:13 +020053 private static final int BACKGROUND_ANIMATION_LENGTH_MS = 220;
Jorim Jaggia8b48e12014-05-19 20:26:46 +020054 private static final int ACTIVATE_ANIMATION_LENGTH = 220;
55
Selim Cinek8efa6dd2014-05-19 16:27:37 +020056 /**
57 * The amount of width, which is kept in the end when performing a disappear animation (also
58 * the amount from which the horizontal appearing begins)
59 */
60 private static final float HORIZONTAL_COLLAPSED_REST_PARTIAL = 0.05f;
61
62 /**
63 * At which point from [0,1] does the horizontal collapse animation end (or start when
64 * expanding)? 1.0 meaning that it ends immediately and 0.0 that it is continuously animated.
65 */
66 private static final float HORIZONTAL_ANIMATION_END = 0.2f;
67
68 /**
69 * At which point from [0,1] does the alpha animation end (or start when
70 * expanding)? 1.0 meaning that it ends immediately and 0.0 that it is continuously animated.
71 */
72 private static final float ALPHA_ANIMATION_END = 0.0f;
73
74 /**
75 * At which point from [0,1] does the horizontal collapse animation start (or start when
76 * expanding)? 1.0 meaning that it starts immediately and 0.0 that it is animated at all.
77 */
78 private static final float HORIZONTAL_ANIMATION_START = 1.0f;
79
80 /**
81 * At which point from [0,1] does the vertical collapse animation start (or end when
82 * expanding) 1.0 meaning that it starts immediately and 0.0 that it is animated at all.
83 */
84 private static final float VERTICAL_ANIMATION_START = 1.0f;
85
Jorim Jaggi4e857f42014-11-17 19:14:04 +010086 /**
Selim Cinekf9bba0b2016-11-18 15:08:21 -080087 * A sentinel value when no color should be used. Can be used with {@link #setTintColor(int)}
88 * or {@link #setOverrideTintColor(int, float)}.
89 */
90 protected static final int NO_COLOR = 0;
91
Jorim Jaggia8b48e12014-05-19 20:26:46 +020092 private static final Interpolator ACTIVATE_INVERSE_INTERPOLATOR
93 = new PathInterpolator(0.6f, 0, 0.5f, 1);
94 private static final Interpolator ACTIVATE_INVERSE_ALPHA_INTERPOLATOR
95 = new PathInterpolator(0, 0, 0.5f, 1);
Lucas Dupinf03e7522018-06-25 16:21:13 -070096 private int mTintedRippleColor;
97 protected int mNormalRippleColor;
Selim Cinekae1bbbb2016-12-13 16:06:02 -080098 private final AccessibilityManager mAccessibilityManager;
Adrian Roos28a0de92017-02-21 21:38:12 +010099 private final DoubleTapHelper mDoubleTapHelper;
Jorim Jaggi251957d2014-04-09 04:24:09 +0200100
101 private boolean mDimmed;
Jorim Jaggi251957d2014-04-09 04:24:09 +0200102
Selim Cinek99104832017-01-25 14:47:33 -0800103 protected int mBgTint = NO_COLOR;
Mady Mellorc7d65b42016-05-04 11:44:57 -0400104 private float mBgAlpha = 1f;
Dan Sandlerfe266a32014-05-15 22:28:06 -0400105
Jorim Jaggi251957d2014-04-09 04:24:09 +0200106 /**
107 * Flag to indicate that the notification has been touched once and the second touch will
108 * click it.
109 */
110 private boolean mActivated;
111
Jorim Jaggic5dc0d02014-04-15 15:42:55 +0200112 private OnActivatedListener mOnActivatedListener;
Jorim Jaggi251957d2014-04-09 04:24:09 +0200113
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200114 private final Interpolator mSlowOutFastInInterpolator;
115 private final Interpolator mSlowOutLinearInInterpolator;
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200116 private Interpolator mCurrentAppearInterpolator;
117 private Interpolator mCurrentAlphaInterpolator;
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200118
Selim Cinek2627d722018-01-19 12:16:49 -0800119 protected NotificationBackgroundView mBackgroundNormal;
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200120 private NotificationBackgroundView mBackgroundDimmed;
121 private ObjectAnimator mBackgroundAnimator;
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200122 private RectF mAppearAnimationRect = new RectF();
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200123 private float mAnimationTranslationY;
124 private boolean mDrawingAppearAnimation;
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200125 private ValueAnimator mAppearAnimator;
Selim Cinekc3179332016-03-04 14:44:56 -0800126 private ValueAnimator mBackgroundColorAnimator;
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200127 private float mAppearAnimationFraction = -1.0f;
128 private float mAppearAnimationTranslation;
Lucas Dupinf03e7522018-06-25 16:21:13 -0700129 private int mNormalColor;
Gus Prevase2d6f042018-10-17 15:25:30 -0400130 private boolean mLastInSection;
131 private boolean mFirstInSection;
Selim Cinekdb167372016-11-17 15:41:17 -0800132 private boolean mIsBelowSpeedBump;
Dave Mankoff781ef7e2019-06-28 16:33:25 -0400133 private final FalsingManager mFalsingManager;
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200134
Selim Cinekd35c2792016-01-21 13:20:57 -0800135 private float mNormalBackgroundVisibilityAmount;
Selim Cinek7baaa9e2016-07-21 17:21:09 -0700136 private float mDimmedBackgroundFadeInAmount = -1;
Selim Cinekd35c2792016-01-21 13:20:57 -0800137 private ValueAnimator.AnimatorUpdateListener mBackgroundVisibilityUpdater
138 = new ValueAnimator.AnimatorUpdateListener() {
139 @Override
140 public void onAnimationUpdate(ValueAnimator animation) {
141 setNormalBackgroundVisibilityAmount(mBackgroundNormal.getAlpha());
Selim Cinek7baaa9e2016-07-21 17:21:09 -0700142 mDimmedBackgroundFadeInAmount = mBackgroundDimmed.getAlpha();
Selim Cinekd35c2792016-01-21 13:20:57 -0800143 }
144 };
Selim Cinek33223572016-02-19 19:32:22 -0800145 private FakeShadowView mFakeShadow;
Selim Cinekc3179332016-03-04 14:44:56 -0800146 private int mCurrentBackgroundTint;
147 private int mTargetTint;
148 private int mStartTint;
Selim Cinekf9bba0b2016-11-18 15:08:21 -0800149 private int mOverrideTint;
150 private float mOverrideAmount;
Selim Cinekfb6ee6d2016-12-29 16:49:26 +0100151 private boolean mShadowHidden;
Selim Cinek63edaf22017-04-24 22:18:48 -0700152 /**
153 * Similar to mDimmed but is also true if it's not dimmable but should be
154 */
155 private boolean mNeedsDimming;
Selim Cinekec29d342017-05-05 18:31:49 -0700156 private int mDimmedAlpha;
Selim Cinekc62635e2017-10-24 11:29:50 -0700157 private boolean mBlockNextTouch;
Selim Cinek332c23f2018-03-16 17:37:50 -0700158 private boolean mIsHeadsUpAnimation;
159 private int mHeadsUpAddStartLocation;
160 private float mHeadsUpLocation;
161 private boolean mIsAppearing;
Selim Cinekd35c2792016-01-21 13:20:57 -0800162
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200163 public ActivatableNotificationView(Context context, AttributeSet attrs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 super(context, attrs);
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200165 mSlowOutFastInInterpolator = new PathInterpolator(0.8f, 0.0f, 0.6f, 1.0f);
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200166 mSlowOutLinearInInterpolator = new PathInterpolator(0.8f, 0.0f, 1.0f, 1.0f);
Dave Mankoff781ef7e2019-06-28 16:33:25 -0400167 mFalsingManager = Dependency.get(FalsingManager.class); // TODO: inject into a controller.
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200168 setClipChildren(false);
169 setClipToPadding(false);
Lucas Dupinf03e7522018-06-25 16:21:13 -0700170 updateColors();
Selim Cinekae1bbbb2016-12-13 16:06:02 -0800171 mAccessibilityManager = AccessibilityManager.getInstance(mContext);
Adrian Roos28a0de92017-02-21 21:38:12 +0100172
173 mDoubleTapHelper = new DoubleTapHelper(this, (active) -> {
174 if (active) {
175 makeActive();
176 } else {
177 makeInactive(true /* animate */);
178 }
Selim Cinekc62635e2017-10-24 11:29:50 -0700179 }, super::performClick, this::handleSlideBack, mFalsingManager::onNotificationDoubleTap);
Selim Cinek332c23f2018-03-16 17:37:50 -0700180 initDimens();
181 }
182
Lucas Dupinf03e7522018-06-25 16:21:13 -0700183 private void updateColors() {
184 mNormalColor = mContext.getColor(R.color.notification_material_background_color);
185 mTintedRippleColor = mContext.getColor(
186 R.color.notification_ripple_tinted_color);
187 mNormalRippleColor = mContext.getColor(
188 R.color.notification_ripple_untinted_color);
189 mDimmedAlpha = Color.alpha(mContext.getColor(
190 R.color.notification_material_background_dimmed_color));
191 }
192
Selim Cinek332c23f2018-03-16 17:37:50 -0700193 private void initDimens() {
194 mHeadsUpAddStartLocation = getResources().getDimensionPixelSize(
195 com.android.internal.R.dimen.notification_content_margin_start);
196 }
197
198 @Override
199 public void onDensityOrFontScaleChanged() {
200 super.onDensityOrFontScaleChanged();
201 initDimens();
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200202 }
203
Selim Cinekab9c7b22018-12-11 18:15:47 -0800204 protected void updateBackgroundColors() {
Lucas Dupinf03e7522018-06-25 16:21:13 -0700205 updateColors();
206 initBackground();
207 updateBackgroundTint();
208 }
209
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200210 @Override
211 protected void onFinishInflate() {
212 super.onFinishInflate();
Alan Viverette51efddb2017-04-05 10:00:01 -0400213 mBackgroundNormal = findViewById(R.id.backgroundNormal);
214 mFakeShadow = findViewById(R.id.fake_shadow);
Selim Cinekfb6ee6d2016-12-29 16:49:26 +0100215 mShadowHidden = mFakeShadow.getVisibility() != VISIBLE;
Alan Viverette51efddb2017-04-05 10:00:01 -0400216 mBackgroundDimmed = findViewById(R.id.backgroundDimmed);
Anthony Chenad4d1582017-04-10 16:07:58 -0700217 initBackground();
Adrian Roosbcbb75a2014-05-27 16:38:11 +0200218 updateBackground();
Selim Cinek697178b2014-07-02 19:40:30 +0200219 updateBackgroundTint();
Selim Cinekd35c2792016-01-21 13:20:57 -0800220 updateOutlineAlpha();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 }
222
Anthony Chenad4d1582017-04-10 16:07:58 -0700223 /**
224 * Sets the custom backgrounds on {@link #mBackgroundNormal} and {@link #mBackgroundDimmed}.
225 * This method can also be used to reload the backgrounds on both of those views, which can
226 * be useful in a configuration change.
227 */
228 protected void initBackground() {
229 mBackgroundNormal.setCustomBackground(R.drawable.notification_material_bg);
230 mBackgroundDimmed.setCustomBackground(R.drawable.notification_material_bg_dim);
231 }
232
Jorim Jaggi251957d2014-04-09 04:24:09 +0200233 private final Runnable mTapTimeoutRunnable = new Runnable() {
234 @Override
235 public void run() {
Selim Cineka32ab602014-06-11 15:06:01 +0200236 makeInactive(true /* animate */);
Jorim Jaggi251957d2014-04-09 04:24:09 +0200237 }
238 };
239
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700240 @Override
Selim Cinek6183d122016-01-14 18:48:41 -0800241 public boolean onInterceptTouchEvent(MotionEvent ev) {
Selim Cinekc62635e2017-10-24 11:29:50 -0700242 if (mNeedsDimming && ev.getActionMasked() == MotionEvent.ACTION_DOWN
Selim Cinekae1bbbb2016-12-13 16:06:02 -0800243 && disallowSingleClick(ev) && !isTouchExplorationEnabled()) {
Selim Cinekc62635e2017-10-24 11:29:50 -0700244 if (!mActivated) {
245 return true;
246 } else if (!mDoubleTapHelper.isWithinDoubleTapSlop(ev)) {
247 mBlockNextTouch = true;
248 makeInactive(true /* animate */);
249 return true;
250 }
Jorim Jaggi251957d2014-04-09 04:24:09 +0200251 }
Selim Cinek6183d122016-01-14 18:48:41 -0800252 return super.onInterceptTouchEvent(ev);
253 }
254
Selim Cinekae1bbbb2016-12-13 16:06:02 -0800255 private boolean isTouchExplorationEnabled() {
256 return mAccessibilityManager.isTouchExplorationEnabled();
257 }
258
Selim Cinek6183d122016-01-14 18:48:41 -0800259 protected boolean disallowSingleClick(MotionEvent ev) {
260 return false;
Jorim Jaggi251957d2014-04-09 04:24:09 +0200261 }
262
Mady Mellorf0625802016-02-11 18:03:48 -0800263 protected boolean handleSlideBack() {
264 return false;
265 }
266
Selim Cinek697178b2014-07-02 19:40:30 +0200267 @Override
Selim Cinek570981d2015-12-01 11:37:01 -0800268 public boolean onTouchEvent(MotionEvent event) {
269 boolean result;
Selim Cinekc62635e2017-10-24 11:29:50 -0700270 if (mBlockNextTouch) {
271 mBlockNextTouch = false;
272 return false;
Selim Cinek63edaf22017-04-24 22:18:48 -0700273 }
Selim Cinekc62635e2017-10-24 11:29:50 -0700274 if (mNeedsDimming && !isTouchExplorationEnabled() && isInteractive()) {
Selim Cinek6183d122016-01-14 18:48:41 -0800275 boolean wasActivated = mActivated;
Selim Cinek570981d2015-12-01 11:37:01 -0800276 result = handleTouchEventDimmed(event);
Selim Cinek6183d122016-01-14 18:48:41 -0800277 if (wasActivated && result && event.getAction() == MotionEvent.ACTION_UP) {
Selim Cinek6183d122016-01-14 18:48:41 -0800278 removeCallbacks(mTapTimeoutRunnable);
279 }
Selim Cinek570981d2015-12-01 11:37:01 -0800280 } else {
281 result = super.onTouchEvent(event);
282 }
Selim Cinek570981d2015-12-01 11:37:01 -0800283 return result;
284 }
285
Selim Cinekc6813462017-01-13 17:10:38 -0800286 /**
287 * @return whether this view is interactive and can be double tapped
288 */
289 protected boolean isInteractive() {
290 return true;
291 }
292
Selim Cinek570981d2015-12-01 11:37:01 -0800293 @Override
Selim Cinek697178b2014-07-02 19:40:30 +0200294 public void drawableHotspotChanged(float x, float y) {
295 if (!mDimmed){
296 mBackgroundNormal.drawableHotspotChanged(x, y);
297 }
298 }
299
Selim Cinekb2da91b2014-09-02 17:35:20 +0200300 @Override
301 protected void drawableStateChanged() {
302 super.drawableStateChanged();
303 if (mDimmed) {
304 mBackgroundDimmed.setState(getDrawableState());
305 } else {
306 mBackgroundNormal.setState(getDrawableState());
307 }
308 }
309
Selim Cinekfe24fb72018-02-13 14:34:55 -0800310 public void setRippleAllowed(boolean allowed) {
311 mBackgroundNormal.setPressedAllowed(allowed);
312 }
313
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200314 private boolean handleTouchEventDimmed(MotionEvent event) {
Selim Cinek63edaf22017-04-24 22:18:48 -0700315 if (mNeedsDimming && !mDimmed) {
316 // We're actually dimmed, but our content isn't dimmable, let's ensure we have a ripple
317 super.onTouchEvent(event);
318 }
Adrian Roos28a0de92017-02-21 21:38:12 +0100319 return mDoubleTapHelper.onTouchEvent(event, getActualHeight());
Jorim Jaggi251957d2014-04-09 04:24:09 +0200320 }
321
Selim Cinek63edaf22017-04-24 22:18:48 -0700322 @Override
323 public boolean performClick() {
Selim Cinekc62635e2017-10-24 11:29:50 -0700324 if (!mNeedsDimming || isTouchExplorationEnabled()) {
Selim Cinek63edaf22017-04-24 22:18:48 -0700325 return super.performClick();
326 }
327 return false;
328 }
329
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200330 private void makeActive() {
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -0700331 mFalsingManager.onNotificationActive();
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200332 startActivateAnimation(false /* reverse */);
Jorim Jaggic5dc0d02014-04-15 15:42:55 +0200333 mActivated = true;
Jorim Jaggiecbab362014-04-23 16:13:15 +0200334 if (mOnActivatedListener != null) {
335 mOnActivatedListener.onActivated(this);
336 }
Jorim Jaggic5dc0d02014-04-15 15:42:55 +0200337 }
338
Selim Cinekd35c2792016-01-21 13:20:57 -0800339 private void startActivateAnimation(final boolean reverse) {
Selim Cinekf4d7fc32014-09-26 17:15:34 +0200340 if (!isAttachedToWindow()) {
341 return;
342 }
Selim Cinek63edaf22017-04-24 22:18:48 -0700343 if (!isDimmable()) {
344 return;
345 }
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200346 int widthHalf = mBackgroundNormal.getWidth()/2;
347 int heightHalf = mBackgroundNormal.getActualHeight()/2;
348 float radius = (float) Math.sqrt(widthHalf*widthHalf + heightHalf*heightHalf);
Selim Cinek6aaf2172014-08-25 19:29:49 +0200349 Animator animator;
350 if (reverse) {
351 animator = ViewAnimationUtils.createCircularReveal(mBackgroundNormal,
352 widthHalf, heightHalf, radius, 0);
353 } else {
354 animator = ViewAnimationUtils.createCircularReveal(mBackgroundNormal,
355 widthHalf, heightHalf, 0, radius);
356 }
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200357 mBackgroundNormal.setVisibility(View.VISIBLE);
358 Interpolator interpolator;
359 Interpolator alphaInterpolator;
360 if (!reverse) {
Selim Cinekc18010f2016-01-20 13:41:30 -0800361 interpolator = Interpolators.LINEAR_OUT_SLOW_IN;
362 alphaInterpolator = Interpolators.LINEAR_OUT_SLOW_IN;
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200363 } else {
364 interpolator = ACTIVATE_INVERSE_INTERPOLATOR;
365 alphaInterpolator = ACTIVATE_INVERSE_ALPHA_INTERPOLATOR;
366 }
367 animator.setInterpolator(interpolator);
368 animator.setDuration(ACTIVATE_ANIMATION_LENGTH);
369 if (reverse) {
370 mBackgroundNormal.setAlpha(1f);
371 animator.addListener(new AnimatorListenerAdapter() {
372 @Override
373 public void onAnimationEnd(Animator animation) {
Selim Cinek3ac08172016-03-09 10:48:36 -0800374 updateBackground();
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200375 }
376 });
Selim Cinek6aaf2172014-08-25 19:29:49 +0200377 animator.start();
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200378 } else {
379 mBackgroundNormal.setAlpha(0.4f);
380 animator.start();
381 }
382 mBackgroundNormal.animate()
383 .alpha(reverse ? 0f : 1f)
384 .setInterpolator(alphaInterpolator)
Selim Cinekd35c2792016-01-21 13:20:57 -0800385 .setUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
386 @Override
387 public void onAnimationUpdate(ValueAnimator animation) {
388 float animatedFraction = animation.getAnimatedFraction();
389 if (reverse) {
390 animatedFraction = 1.0f - animatedFraction;
391 }
392 setNormalBackgroundVisibilityAmount(animatedFraction);
393 }
394 })
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200395 .setDuration(ACTIVATE_ANIMATION_LENGTH);
396 }
397
Jorim Jaggi251957d2014-04-09 04:24:09 +0200398 /**
399 * Cancels the hotspot and makes the notification inactive.
400 */
Selim Cineka32ab602014-06-11 15:06:01 +0200401 public void makeInactive(boolean animate) {
Jorim Jaggic5dc0d02014-04-15 15:42:55 +0200402 if (mActivated) {
Selim Cinek3ac08172016-03-09 10:48:36 -0800403 mActivated = false;
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200404 if (mDimmed) {
Selim Cineka32ab602014-06-11 15:06:01 +0200405 if (animate) {
406 startActivateAnimation(true /* reverse */);
407 } else {
Selim Cinek3ac08172016-03-09 10:48:36 -0800408 updateBackground();
Selim Cineka32ab602014-06-11 15:06:01 +0200409 }
Jorim Jaggibccb9122014-05-08 14:55:48 +0200410 }
Jorim Jaggi251957d2014-04-09 04:24:09 +0200411 }
Jorim Jaggic5dc0d02014-04-15 15:42:55 +0200412 if (mOnActivatedListener != null) {
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200413 mOnActivatedListener.onActivationReset(this);
Jorim Jaggic5dc0d02014-04-15 15:42:55 +0200414 }
Jorim Jaggi251957d2014-04-09 04:24:09 +0200415 removeCallbacks(mTapTimeoutRunnable);
416 }
417
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200418 public void setDimmed(boolean dimmed, boolean fade) {
Selim Cinek63edaf22017-04-24 22:18:48 -0700419 mNeedsDimming = dimmed;
420 dimmed &= isDimmable();
Jorim Jaggi251957d2014-04-09 04:24:09 +0200421 if (mDimmed != dimmed) {
422 mDimmed = dimmed;
Mady Mellorc7d65b42016-05-04 11:44:57 -0400423 resetBackgroundAlpha();
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200424 if (fade) {
Jorim Jaggi4e857f42014-11-17 19:14:04 +0100425 fadeDimmedBackground();
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200426 } else {
Jorim Jaggi3c3c3fc2014-05-20 23:16:42 +0200427 updateBackground();
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200428 }
Jorim Jaggi251957d2014-04-09 04:24:09 +0200429 }
430 }
431
Selim Cinek63edaf22017-04-24 22:18:48 -0700432 public boolean isDimmable() {
433 return true;
434 }
435
Selim Cinekd35c2792016-01-21 13:20:57 -0800436 private void updateOutlineAlpha() {
Selim Cinekd35c2792016-01-21 13:20:57 -0800437 float alpha = NotificationStackScrollLayout.BACKGROUND_ALPHA_DIMMED;
438 alpha = (alpha + (1.0f - alpha) * mNormalBackgroundVisibilityAmount);
Selim Cinekd35c2792016-01-21 13:20:57 -0800439 setOutlineAlpha(alpha);
440 }
441
442 public void setNormalBackgroundVisibilityAmount(float normalBackgroundVisibilityAmount) {
443 mNormalBackgroundVisibilityAmount = normalBackgroundVisibilityAmount;
444 updateOutlineAlpha();
445 }
John Spurlockbf370992014-06-17 13:58:31 -0400446
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200447 @Override
Selim Cinekdb167372016-11-17 15:41:17 -0800448 public void setBelowSpeedBump(boolean below) {
449 super.setBelowSpeedBump(below);
450 if (below != mIsBelowSpeedBump) {
451 mIsBelowSpeedBump = below;
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200452 updateBackgroundTint();
Selim Cinekdb167372016-11-17 15:41:17 -0800453 onBelowSpeedBumpChanged();
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200454 }
455 }
456
Selim Cinekdb167372016-11-17 15:41:17 -0800457 protected void onBelowSpeedBumpChanged() {
458 }
459
460 /**
461 * @return whether we are below the speed bump
462 */
463 public boolean isBelowSpeedBump() {
464 return mIsBelowSpeedBump;
465 }
466
Selim Cinek697178b2014-07-02 19:40:30 +0200467 /**
468 * Sets the tint color of the background
469 */
470 public void setTintColor(int color) {
Selim Cinekc3179332016-03-04 14:44:56 -0800471 setTintColor(color, false);
472 }
473
474 /**
475 * Sets the tint color of the background
476 */
477 public void setTintColor(int color, boolean animated) {
Selim Cinek65d418e2016-11-29 15:42:34 -0800478 if (color != mBgTint) {
479 mBgTint = color;
480 updateBackgroundTint(animated);
481 }
Selim Cinek697178b2014-07-02 19:40:30 +0200482 }
483
Tony Huangc092c432018-05-18 17:38:54 +0800484 @Override
485 public void setDistanceToTopRoundness(float distanceToTopRoundness) {
486 super.setDistanceToTopRoundness(distanceToTopRoundness);
487 mBackgroundNormal.setDistanceToTopRoundness(distanceToTopRoundness);
488 mBackgroundDimmed.setDistanceToTopRoundness(distanceToTopRoundness);
489 }
490
Gus Prevase2d6f042018-10-17 15:25:30 -0400491 public boolean isLastInSection() {
492 return mLastInSection;
493 }
494
495 public boolean isFirstInSection() {
496 return mFirstInSection;
497 }
498
499 /** Sets whether this view is the last notification in a section. */
500 public void setLastInSection(boolean lastInSection) {
501 if (lastInSection != mLastInSection) {
502 mLastInSection = lastInSection;
503 mBackgroundNormal.setLastInSection(lastInSection);
504 mBackgroundDimmed.setLastInSection(lastInSection);
505 }
506 }
507
508 /** Sets whether this view is the first notification in a section. */
509 public void setFirstInSection(boolean firstInSection) {
510 if (firstInSection != mFirstInSection) {
511 mFirstInSection = firstInSection;
512 mBackgroundNormal.setFirstInSection(firstInSection);
513 mBackgroundDimmed.setFirstInSection(firstInSection);
514 }
515 }
516
Selim Cinekf9bba0b2016-11-18 15:08:21 -0800517 /**
518 * Set an override tint color that is used for the background.
519 *
520 * @param color the color that should be used to tint the background.
521 * This can be {@link #NO_COLOR} if the tint should be normally computed.
522 * @param overrideAmount a value from 0 to 1 how much the override tint should be used. The
523 * background color will then be the interpolation between this and the
524 * regular background color, where 1 means the overrideTintColor is fully
525 * used and the background color not at all.
526 */
527 public void setOverrideTintColor(int color, float overrideAmount) {
528 mOverrideTint = color;
529 mOverrideAmount = overrideAmount;
530 int newColor = calculateBgColor();
531 setBackgroundTintColor(newColor);
Selim Cinekec29d342017-05-05 18:31:49 -0700532 if (!isDimmable() && mNeedsDimming) {
533 mBackgroundNormal.setDrawableAlpha((int) NotificationUtils.interpolate(255,
534 mDimmedAlpha,
535 overrideAmount));
536 } else {
537 mBackgroundNormal.setDrawableAlpha(255);
538 }
Selim Cinekf9bba0b2016-11-18 15:08:21 -0800539 }
540
Selim Cineka6c6bfb2015-10-29 16:27:08 -0700541 protected void updateBackgroundTint() {
Selim Cinekc3179332016-03-04 14:44:56 -0800542 updateBackgroundTint(false /* animated */);
543 }
544
545 private void updateBackgroundTint(boolean animated) {
546 if (mBackgroundColorAnimator != null) {
547 mBackgroundColorAnimator.cancel();
548 }
Selim Cinekb2da91b2014-09-02 17:35:20 +0200549 int rippleColor = getRippleColor();
Selim Cinekc3179332016-03-04 14:44:56 -0800550 mBackgroundDimmed.setRippleColor(rippleColor);
551 mBackgroundNormal.setRippleColor(rippleColor);
552 int color = calculateBgColor();
553 if (!animated) {
554 setBackgroundTintColor(color);
555 } else if (color != mCurrentBackgroundTint) {
556 mStartTint = mCurrentBackgroundTint;
557 mTargetTint = color;
558 mBackgroundColorAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);
559 mBackgroundColorAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
560 @Override
561 public void onAnimationUpdate(ValueAnimator animation) {
562 int newColor = NotificationUtils.interpolateColors(mStartTint, mTargetTint,
563 animation.getAnimatedFraction());
564 setBackgroundTintColor(newColor);
565 }
566 });
567 mBackgroundColorAnimator.setDuration(StackStateAnimator.ANIMATION_DURATION_STANDARD);
568 mBackgroundColorAnimator.setInterpolator(Interpolators.LINEAR);
569 mBackgroundColorAnimator.addListener(new AnimatorListenerAdapter() {
570 @Override
571 public void onAnimationEnd(Animator animation) {
572 mBackgroundColorAnimator = null;
573 }
574 });
575 mBackgroundColorAnimator.start();
576 }
577 }
578
Kenny Guy14d035c2018-05-02 19:10:36 +0100579 protected void setBackgroundTintColor(int color) {
Selim Cinek65d418e2016-11-29 15:42:34 -0800580 if (color != mCurrentBackgroundTint) {
581 mCurrentBackgroundTint = color;
582 if (color == mNormalColor) {
583 // We don't need to tint a normal notification
584 color = 0;
585 }
586 mBackgroundDimmed.setTint(color);
587 mBackgroundNormal.setTint(color);
Selim Cinek697178b2014-07-02 19:40:30 +0200588 }
Dan Sandlerfe266a32014-05-15 22:28:06 -0400589 }
590
Jorim Jaggi4e857f42014-11-17 19:14:04 +0100591 /**
Jorim Jaggi4e857f42014-11-17 19:14:04 +0100592 * Fades the background when the dimmed state changes.
593 */
594 private void fadeDimmedBackground() {
595 mBackgroundDimmed.animate().cancel();
Selim Cinek59d97232014-10-09 15:54:40 -0700596 mBackgroundNormal.animate().cancel();
Selim Cinek3ac08172016-03-09 10:48:36 -0800597 if (mActivated) {
598 updateBackground();
599 return;
600 }
Selim Cinek34d93b02015-10-22 12:30:38 -0700601 if (!shouldHideBackground()) {
602 if (mDimmed) {
603 mBackgroundDimmed.setVisibility(View.VISIBLE);
604 } else {
605 mBackgroundNormal.setVisibility(View.VISIBLE);
606 }
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200607 }
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200608 float startAlpha = mDimmed ? 1f : 0;
609 float endAlpha = mDimmed ? 0 : 1f;
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200610 int duration = BACKGROUND_ANIMATION_LENGTH_MS;
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200611 // Check whether there is already a background animation running.
612 if (mBackgroundAnimator != null) {
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200613 startAlpha = (Float) mBackgroundAnimator.getAnimatedValue();
Jorim Jaggi98fb09c2014-05-01 22:40:56 +0200614 duration = (int) mBackgroundAnimator.getCurrentPlayTime();
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200615 mBackgroundAnimator.removeAllListeners();
616 mBackgroundAnimator.cancel();
Jorim Jaggi98fb09c2014-05-01 22:40:56 +0200617 if (duration <= 0) {
Jorim Jaggi3c3c3fc2014-05-20 23:16:42 +0200618 updateBackground();
Jorim Jaggi98fb09c2014-05-01 22:40:56 +0200619 return;
620 }
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200621 }
622 mBackgroundNormal.setAlpha(startAlpha);
623 mBackgroundAnimator =
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200624 ObjectAnimator.ofFloat(mBackgroundNormal, View.ALPHA, startAlpha, endAlpha);
Selim Cinekc18010f2016-01-20 13:41:30 -0800625 mBackgroundAnimator.setInterpolator(Interpolators.FAST_OUT_SLOW_IN);
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200626 mBackgroundAnimator.setDuration(duration);
627 mBackgroundAnimator.addListener(new AnimatorListenerAdapter() {
628 @Override
629 public void onAnimationEnd(Animator animation) {
Selim Cinek3ac08172016-03-09 10:48:36 -0800630 updateBackground();
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200631 mBackgroundAnimator = null;
Lucas Dupin00be88f2019-01-03 17:50:52 -0800632 mDimmedBackgroundFadeInAmount = -1;
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200633 }
634 });
Selim Cinekd35c2792016-01-21 13:20:57 -0800635 mBackgroundAnimator.addUpdateListener(mBackgroundVisibilityUpdater);
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200636 mBackgroundAnimator.start();
637 }
638
Mady Mellorc7d65b42016-05-04 11:44:57 -0400639 protected void updateBackgroundAlpha(float transformationAmount) {
Selim Cinek7baaa9e2016-07-21 17:21:09 -0700640 mBgAlpha = isChildInGroup() && mDimmed ? transformationAmount : 1f;
641 if (mDimmedBackgroundFadeInAmount != -1) {
642 mBgAlpha *= mDimmedBackgroundFadeInAmount;
643 }
Mady Mellorc7d65b42016-05-04 11:44:57 -0400644 mBackgroundDimmed.setAlpha(mBgAlpha);
645 }
646
647 protected void resetBackgroundAlpha() {
648 updateBackgroundAlpha(0f /* transformationAmount */);
649 }
650
Selim Cinek34d93b02015-10-22 12:30:38 -0700651 protected void updateBackground() {
Jorim Jaggi4e857f42014-11-17 19:14:04 +0100652 cancelFadeAnimations();
Selim Cinek34d93b02015-10-22 12:30:38 -0700653 if (shouldHideBackground()) {
Adrian Roos500263a2017-01-23 14:49:54 -0800654 mBackgroundDimmed.setVisibility(INVISIBLE);
655 mBackgroundNormal.setVisibility(mActivated ? VISIBLE : INVISIBLE);
Jorim Jaggi4e857f42014-11-17 19:14:04 +0100656 } else if (mDimmed) {
Mady Mellorc7d65b42016-05-04 11:44:57 -0400657 // When groups are animating to the expanded state from the lockscreen, show the
Lucas Dupin00be88f2019-01-03 17:50:52 -0800658 // normal background instead of the dimmed background.
Mady Mellorc7d65b42016-05-04 11:44:57 -0400659 final boolean dontShowDimmed = isGroupExpansionChanging() && isChildInGroup();
660 mBackgroundDimmed.setVisibility(dontShowDimmed ? View.INVISIBLE : View.VISIBLE);
661 mBackgroundNormal.setVisibility((mActivated || dontShowDimmed)
662 ? View.VISIBLE
663 : View.INVISIBLE);
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200664 } else {
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200665 mBackgroundDimmed.setVisibility(View.INVISIBLE);
666 mBackgroundNormal.setVisibility(View.VISIBLE);
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200667 mBackgroundNormal.setAlpha(1f);
Selim Cineka32ab602014-06-11 15:06:01 +0200668 removeCallbacks(mTapTimeoutRunnable);
Selim Cinek3ac08172016-03-09 10:48:36 -0800669 // make in inactive to avoid it sticking around active
670 makeInactive(false /* animate */);
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200671 }
Selim Cinekd35c2792016-01-21 13:20:57 -0800672 setNormalBackgroundVisibilityAmount(
673 mBackgroundNormal.getVisibility() == View.VISIBLE ? 1.0f : 0.0f);
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200674 }
675
Selim Cinek2871bef2017-11-22 08:40:00 -0800676 protected void updateBackgroundClipping() {
677 mBackgroundNormal.setBottomAmountClips(!isChildInGroup());
678 mBackgroundDimmed.setBottomAmountClips(!isChildInGroup());
679 }
680
Selim Cinek34d93b02015-10-22 12:30:38 -0700681 protected boolean shouldHideBackground() {
Lucas Dupin00be88f2019-01-03 17:50:52 -0800682 return false;
Selim Cinek34d93b02015-10-22 12:30:38 -0700683 }
684
Selim Cinek59d97232014-10-09 15:54:40 -0700685 private void cancelFadeAnimations() {
686 if (mBackgroundAnimator != null) {
687 mBackgroundAnimator.cancel();
688 }
Jorim Jaggi4e857f42014-11-17 19:14:04 +0100689 mBackgroundDimmed.animate().cancel();
Selim Cinek59d97232014-10-09 15:54:40 -0700690 mBackgroundNormal.animate().cancel();
691 }
692
Jorim Jaggibe565df2014-04-28 17:51:23 +0200693 @Override
694 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
695 super.onLayout(changed, left, top, right, bottom);
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200696 setPivotX(getWidth() / 2);
Jorim Jaggibe565df2014-04-28 17:51:23 +0200697 }
698
699 @Override
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200700 public void setActualHeight(int actualHeight, boolean notifyListeners) {
701 super.setActualHeight(actualHeight, notifyListeners);
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200702 setPivotY(actualHeight / 2);
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200703 mBackgroundNormal.setActualHeight(actualHeight);
704 mBackgroundDimmed.setActualHeight(actualHeight);
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200705 }
706
707 @Override
708 public void setClipTopAmount(int clipTopAmount) {
709 super.setClipTopAmount(clipTopAmount);
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200710 mBackgroundNormal.setClipTopAmount(clipTopAmount);
711 mBackgroundDimmed.setClipTopAmount(clipTopAmount);
Jorim Jaggi46739852014-04-15 09:58:24 +0200712 }
Jorim Jaggic5dc0d02014-04-15 15:42:55 +0200713
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200714 @Override
Selim Cineka686b2c2016-10-26 13:58:27 -0700715 public void setClipBottomAmount(int clipBottomAmount) {
716 super.setClipBottomAmount(clipBottomAmount);
717 mBackgroundNormal.setClipBottomAmount(clipBottomAmount);
718 mBackgroundDimmed.setClipBottomAmount(clipBottomAmount);
719 }
720
721 @Override
Gus Prevas211181532018-12-13 14:49:33 -0500722 public long performRemoveAnimation(long duration, long delay,
Selim Cinek332c23f2018-03-16 17:37:50 -0700723 float translationDirection, boolean isHeadsUpAnimation, float endLocation,
724 Runnable onFinishedRunnable, AnimatorListenerAdapter animationListener) {
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200725 enableAppearDrawing(true);
Selim Cinek332c23f2018-03-16 17:37:50 -0700726 mIsHeadsUpAnimation = isHeadsUpAnimation;
727 mHeadsUpLocation = endLocation;
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200728 if (mDrawingAppearAnimation) {
729 startAppearAnimation(false /* isAppearing */, translationDirection,
Selim Cinek332c23f2018-03-16 17:37:50 -0700730 delay, duration, onFinishedRunnable, animationListener);
Selim Cinek95ed5922014-08-28 14:30:12 +0200731 } else if (onFinishedRunnable != null) {
732 onFinishedRunnable.run();
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200733 }
Gus Prevas211181532018-12-13 14:49:33 -0500734 return 0;
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200735 }
736
737 @Override
Selim Cinek332c23f2018-03-16 17:37:50 -0700738 public void performAddAnimation(long delay, long duration, boolean isHeadsUpAppear) {
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200739 enableAppearDrawing(true);
Selim Cinek332c23f2018-03-16 17:37:50 -0700740 mIsHeadsUpAnimation = isHeadsUpAppear;
741 mHeadsUpLocation = mHeadsUpAddStartLocation;
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200742 if (mDrawingAppearAnimation) {
Selim Cinek332c23f2018-03-16 17:37:50 -0700743 startAppearAnimation(true /* isAppearing */, isHeadsUpAppear ? 0.0f : -1.0f, delay,
744 duration, null, null);
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200745 }
746 }
747
Jorim Jaggi60d07c52014-07-31 15:38:21 +0200748 private void startAppearAnimation(boolean isAppearing, float translationDirection, long delay,
Selim Cinek332c23f2018-03-16 17:37:50 -0700749 long duration, final Runnable onFinishedRunnable,
750 AnimatorListenerAdapter animationListener) {
Selim Cinek2cd45df2015-06-09 18:00:07 -0700751 cancelAppearAnimation();
Chris Wren310df312014-10-31 14:29:46 -0400752 mAnimationTranslationY = translationDirection * getActualHeight();
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200753 if (mAppearAnimationFraction == -1.0f) {
754 // not initialized yet, we start anew
755 if (isAppearing) {
756 mAppearAnimationFraction = 0.0f;
757 mAppearAnimationTranslation = mAnimationTranslationY;
758 } else {
759 mAppearAnimationFraction = 1.0f;
760 mAppearAnimationTranslation = 0;
761 }
762 }
Selim Cinek332c23f2018-03-16 17:37:50 -0700763 mIsAppearing = isAppearing;
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200764
765 float targetValue;
766 if (isAppearing) {
767 mCurrentAppearInterpolator = mSlowOutFastInInterpolator;
Selim Cinekc18010f2016-01-20 13:41:30 -0800768 mCurrentAlphaInterpolator = Interpolators.LINEAR_OUT_SLOW_IN;
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200769 targetValue = 1.0f;
770 } else {
Selim Cinekc18010f2016-01-20 13:41:30 -0800771 mCurrentAppearInterpolator = Interpolators.FAST_OUT_SLOW_IN;
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200772 mCurrentAlphaInterpolator = mSlowOutLinearInInterpolator;
773 targetValue = 0.0f;
774 }
775 mAppearAnimator = ValueAnimator.ofFloat(mAppearAnimationFraction,
776 targetValue);
Selim Cinekc18010f2016-01-20 13:41:30 -0800777 mAppearAnimator.setInterpolator(Interpolators.LINEAR);
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200778 mAppearAnimator.setDuration(
Jorim Jaggi60d07c52014-07-31 15:38:21 +0200779 (long) (duration * Math.abs(mAppearAnimationFraction - targetValue)));
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200780 mAppearAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
781 @Override
782 public void onAnimationUpdate(ValueAnimator animation) {
783 mAppearAnimationFraction = (float) animation.getAnimatedValue();
784 updateAppearAnimationAlpha();
785 updateAppearRect();
786 invalidate();
787 }
788 });
Selim Cinek332c23f2018-03-16 17:37:50 -0700789 if (animationListener != null) {
790 mAppearAnimator.addListener(animationListener);
791 }
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200792 if (delay > 0) {
793 // we need to apply the initial state already to avoid drawn frames in the wrong state
794 updateAppearAnimationAlpha();
795 updateAppearRect();
796 mAppearAnimator.setStartDelay(delay);
797 }
798 mAppearAnimator.addListener(new AnimatorListenerAdapter() {
799 private boolean mWasCancelled;
800
801 @Override
802 public void onAnimationEnd(Animator animation) {
803 if (onFinishedRunnable != null) {
804 onFinishedRunnable.run();
805 }
806 if (!mWasCancelled) {
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200807 enableAppearDrawing(false);
Selim Cinekaa3901a2016-08-05 11:04:37 -0700808 onAppearAnimationFinished(isAppearing);
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200809 }
810 }
811
812 @Override
813 public void onAnimationStart(Animator animation) {
814 mWasCancelled = false;
815 }
816
817 @Override
818 public void onAnimationCancel(Animator animation) {
819 mWasCancelled = true;
820 }
821 });
822 mAppearAnimator.start();
823 }
824
Selim Cinekaa3901a2016-08-05 11:04:37 -0700825 protected void onAppearAnimationFinished(boolean wasAppearing) {
826 }
827
Selim Cinek2cd45df2015-06-09 18:00:07 -0700828 private void cancelAppearAnimation() {
829 if (mAppearAnimator != null) {
830 mAppearAnimator.cancel();
Selim Cinekb65c6db2015-12-28 12:48:15 +0100831 mAppearAnimator = null;
Selim Cinek2cd45df2015-06-09 18:00:07 -0700832 }
833 }
834
835 public void cancelAppearDrawing() {
836 cancelAppearAnimation();
837 enableAppearDrawing(false);
838 }
839
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200840 private void updateAppearRect() {
841 float inverseFraction = (1.0f - mAppearAnimationFraction);
842 float translationFraction = mCurrentAppearInterpolator.getInterpolation(inverseFraction);
843 float translateYTotalAmount = translationFraction * mAnimationTranslationY;
844 mAppearAnimationTranslation = translateYTotalAmount;
845
846 // handle width animation
847 float widthFraction = (inverseFraction - (1.0f - HORIZONTAL_ANIMATION_START))
848 / (HORIZONTAL_ANIMATION_START - HORIZONTAL_ANIMATION_END);
849 widthFraction = Math.min(1.0f, Math.max(0.0f, widthFraction));
850 widthFraction = mCurrentAppearInterpolator.getInterpolation(widthFraction);
Selim Cinek332c23f2018-03-16 17:37:50 -0700851 float startWidthFraction = HORIZONTAL_COLLAPSED_REST_PARTIAL;
852 if (mIsHeadsUpAnimation && !mIsAppearing) {
853 startWidthFraction = 0;
854 }
855 float width = MathUtils.lerp(startWidthFraction, 1.0f, 1.0f - widthFraction)
856 * getWidth();
857 float left;
858 float right;
859 if (mIsHeadsUpAnimation) {
860 left = MathUtils.lerp(mHeadsUpLocation, 0, 1.0f - widthFraction);
861 right = left + width;
862 } else {
863 left = getWidth() * 0.5f - width / 2.0f;
864 right = getWidth() - left;
865 }
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200866
867 // handle top animation
868 float heightFraction = (inverseFraction - (1.0f - VERTICAL_ANIMATION_START)) /
869 VERTICAL_ANIMATION_START;
870 heightFraction = Math.max(0.0f, heightFraction);
871 heightFraction = mCurrentAppearInterpolator.getInterpolation(heightFraction);
872
873 float top;
874 float bottom;
Chris Wren310df312014-10-31 14:29:46 -0400875 final int actualHeight = getActualHeight();
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200876 if (mAnimationTranslationY > 0.0f) {
Chris Wren310df312014-10-31 14:29:46 -0400877 bottom = actualHeight - heightFraction * mAnimationTranslationY * 0.1f
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200878 - translateYTotalAmount;
879 top = bottom * heightFraction;
880 } else {
Chris Wren310df312014-10-31 14:29:46 -0400881 top = heightFraction * (actualHeight + mAnimationTranslationY) * 0.1f -
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200882 translateYTotalAmount;
Chris Wren310df312014-10-31 14:29:46 -0400883 bottom = actualHeight * (1 - heightFraction) + top * heightFraction;
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200884 }
885 mAppearAnimationRect.set(left, top, right, bottom);
886 setOutlineRect(left, top + mAppearAnimationTranslation, right,
887 bottom + mAppearAnimationTranslation);
888 }
889
890 private void updateAppearAnimationAlpha() {
Selim Cinek560e64d2015-06-09 19:58:11 -0700891 float contentAlphaProgress = mAppearAnimationFraction;
892 contentAlphaProgress = contentAlphaProgress / (1.0f - ALPHA_ANIMATION_END);
893 contentAlphaProgress = Math.min(1.0f, contentAlphaProgress);
894 contentAlphaProgress = mCurrentAlphaInterpolator.getInterpolation(contentAlphaProgress);
895 setContentAlpha(contentAlphaProgress);
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200896 }
897
Selim Cinek560e64d2015-06-09 19:58:11 -0700898 private void setContentAlpha(float contentAlpha) {
Selim Cinek560e64d2015-06-09 19:58:11 -0700899 View contentView = getContentView();
Jorim Jaggi1f98c622015-07-20 11:43:27 -0700900 if (contentView.hasOverlappingRendering()) {
901 int layerType = contentAlpha == 0.0f || contentAlpha == 1.0f ? LAYER_TYPE_NONE
902 : LAYER_TYPE_HARDWARE;
903 int currentLayerType = contentView.getLayerType();
904 if (currentLayerType != layerType) {
905 contentView.setLayerType(layerType, null);
906 }
Selim Cinek560e64d2015-06-09 19:58:11 -0700907 }
908 contentView.setAlpha(contentAlpha);
909 }
910
Selim Cinek0fe07392017-11-09 13:26:34 -0800911 @Override
912 protected void applyRoundness() {
913 super.applyRoundness();
Selim Cinekd9b7dd42017-11-10 17:53:47 -0800914 applyBackgroundRoundness(getCurrentBackgroundRadiusTop(),
915 getCurrentBackgroundRadiusBottom());
Selim Cinek0fe07392017-11-09 13:26:34 -0800916 }
917
918 protected void applyBackgroundRoundness(float topRadius, float bottomRadius) {
919 mBackgroundDimmed.setRoundness(topRadius, bottomRadius);
920 mBackgroundNormal.setRoundness(topRadius, bottomRadius);
921 }
922
Selim Cinek515b2032017-11-15 10:20:19 -0800923 @Override
924 protected void setBackgroundTop(int backgroundTop) {
925 mBackgroundDimmed.setBackgroundTop(backgroundTop);
926 mBackgroundNormal.setBackgroundTop(backgroundTop);
927 }
928
Selim Cinek560e64d2015-06-09 19:58:11 -0700929 protected abstract View getContentView();
930
Selim Cinekc3179332016-03-04 14:44:56 -0800931 public int calculateBgColor() {
Selim Cinekf9bba0b2016-11-18 15:08:21 -0800932 return calculateBgColor(true /* withTint */, true /* withOverRide */);
Selim Cinekc3179332016-03-04 14:44:56 -0800933 }
934
Selim Cinek515b2032017-11-15 10:20:19 -0800935 @Override
Selim Cinek515b2032017-11-15 10:20:19 -0800936 protected boolean childNeedsClipping(View child) {
937 if (child instanceof NotificationBackgroundView && isClippingNeeded()) {
938 return true;
939 }
940 return super.childNeedsClipping(child);
941 }
942
Selim Cinekf9bba0b2016-11-18 15:08:21 -0800943 /**
944 * @param withTint should a possible tint be factored in?
Lucas Dupin00be88f2019-01-03 17:50:52 -0800945 * @param withOverride should the value be interpolated with {@link #mOverrideTint}
Selim Cinekf9bba0b2016-11-18 15:08:21 -0800946 * @return the calculated background color
947 */
Lucas Dupin00be88f2019-01-03 17:50:52 -0800948 private int calculateBgColor(boolean withTint, boolean withOverride) {
949 if (withOverride && mOverrideTint != NO_COLOR) {
Selim Cinekf9bba0b2016-11-18 15:08:21 -0800950 int defaultTint = calculateBgColor(withTint, false);
951 return NotificationUtils.interpolateColors(defaultTint, mOverrideTint, mOverrideAmount);
952 }
953 if (withTint && mBgTint != NO_COLOR) {
Selim Cinek697178b2014-07-02 19:40:30 +0200954 return mBgTint;
Selim Cinek697178b2014-07-02 19:40:30 +0200955 } else {
956 return mNormalColor;
957 }
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200958 }
959
Selim Cinekb5605e52015-02-20 18:21:41 +0100960 protected int getRippleColor() {
Selim Cinekb2da91b2014-09-02 17:35:20 +0200961 if (mBgTint != 0) {
962 return mTintedRippleColor;
Selim Cinekb2da91b2014-09-02 17:35:20 +0200963 } else {
964 return mNormalRippleColor;
965 }
966 }
967
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200968 /**
969 * When we draw the appear animation, we render the view in a bitmap and render this bitmap
970 * as a shader of a rect. This call creates the Bitmap and switches the drawing mode,
971 * such that the normal drawing of the views does not happen anymore.
972 *
973 * @param enable Should it be enabled.
974 */
975 private void enableAppearDrawing(boolean enable) {
976 if (enable != mDrawingAppearAnimation) {
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200977 mDrawingAppearAnimation = enable;
Selim Cinek560e64d2015-06-09 19:58:11 -0700978 if (!enable) {
979 setContentAlpha(1.0f);
Selim Cinekb65c6db2015-12-28 12:48:15 +0100980 mAppearAnimationFraction = -1;
981 setOutlineRect(null);
Selim Cinek560e64d2015-06-09 19:58:11 -0700982 }
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200983 invalidate();
984 }
985 }
986
Selim Cinekf38d6c32017-06-28 15:44:02 +0200987 public boolean isDrawingAppearAnimation() {
988 return mDrawingAppearAnimation;
989 }
990
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200991 @Override
992 protected void dispatchDraw(Canvas canvas) {
Selim Cinek560e64d2015-06-09 19:58:11 -0700993 if (mDrawingAppearAnimation) {
994 canvas.save();
995 canvas.translate(0, mAppearAnimationTranslation);
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200996 }
Selim Cinek560e64d2015-06-09 19:58:11 -0700997 super.dispatchDraw(canvas);
998 if (mDrawingAppearAnimation) {
999 canvas.restore();
1000 }
Selim Cinek8efa6dd2014-05-19 16:27:37 +02001001 }
1002
Jorim Jaggic5dc0d02014-04-15 15:42:55 +02001003 public void setOnActivatedListener(OnActivatedListener onActivatedListener) {
1004 mOnActivatedListener = onActivatedListener;
1005 }
1006
Selim Cinek34d93b02015-10-22 12:30:38 -07001007 public boolean hasSameBgColor(ActivatableNotificationView otherView) {
Selim Cinekc3179332016-03-04 14:44:56 -08001008 return calculateBgColor() == otherView.calculateBgColor();
Selim Cinek34d93b02015-10-22 12:30:38 -07001009 }
1010
Selim Cinek277a8aa2016-01-22 12:12:37 -08001011 @Override
Selim Cinek33223572016-02-19 19:32:22 -08001012 public void setFakeShadowIntensity(float shadowIntensity, float outlineAlpha, int shadowYEnd,
1013 int outlineTranslation) {
Selim Cinekfb6ee6d2016-12-29 16:49:26 +01001014 boolean hiddenBefore = mShadowHidden;
1015 mShadowHidden = shadowIntensity == 0.0f;
1016 if (!mShadowHidden || !hiddenBefore) {
1017 mFakeShadow.setFakeShadowTranslationZ(shadowIntensity * (getTranslationZ()
1018 + FakeShadowView.SHADOW_SIBLING_TRESHOLD), outlineAlpha, shadowYEnd,
1019 outlineTranslation);
1020 }
Selim Cinek33223572016-02-19 19:32:22 -08001021 }
1022
Selim Cinekc3179332016-03-04 14:44:56 -08001023 public int getBackgroundColorWithoutTint() {
Selim Cinekf9bba0b2016-11-18 15:08:21 -08001024 return calculateBgColor(false /* withTint */, false /* withOverride */);
Selim Cinekc3179332016-03-04 14:44:56 -08001025 }
1026
Gustav Sennton1d199a62019-05-23 12:57:03 +01001027 public int getCurrentBackgroundTint() {
1028 return mCurrentBackgroundTint;
1029 }
1030
Selim Cinek29aab962018-02-27 17:05:45 -08001031 public boolean isPinned() {
1032 return false;
1033 }
1034
1035 public boolean isHeadsUpAnimatingAway() {
1036 return false;
1037 }
1038
Selim Cinekc3fec682019-06-06 18:11:07 -07001039 public boolean isHeadsUp() {
1040 return false;
1041 }
1042
Selim Cinekc7e4cb52019-06-20 15:41:45 -07001043 public int getHeadsUpHeightWithoutHeader() {
1044 return getHeight();
1045 }
1046
Jorim Jaggic5dc0d02014-04-15 15:42:55 +02001047 public interface OnActivatedListener {
Selim Cineka32ab602014-06-11 15:06:01 +02001048 void onActivated(ActivatableNotificationView view);
1049 void onActivationReset(ActivatableNotificationView view);
Jorim Jaggic5dc0d02014-04-15 15:42:55 +02001050 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051}