Kenny Root | 15a4d2f | 2010-03-11 18:20:12 -0800 | [diff] [blame] | 1 | /* |
Jorim Jaggi | fe40f7d | 2014-04-28 15:20:04 +0200 | [diff] [blame] | 2 | * Copyright (C) 2014 The Android Open Source Project |
Kenny Root | 15a4d2f | 2010-03-11 18:20:12 -0800 | [diff] [blame] | 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 |
Jorim Jaggi | fe40f7d | 2014-04-28 15:20:04 +0200 | [diff] [blame] | 14 | * limitations under the License |
Kenny Root | 15a4d2f | 2010-03-11 18:20:12 -0800 | [diff] [blame] | 15 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 16 | |
Joe Onorato | 79de0c5 | 2010-05-26 17:03:26 -0400 | [diff] [blame] | 17 | package com.android.systemui.statusbar; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
| 21 | import android.animation.ObjectAnimator; |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 22 | import android.animation.ValueAnimator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.content.Context; |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 24 | import android.graphics.Bitmap; |
| 25 | import android.graphics.BitmapShader; |
| 26 | import android.graphics.Canvas; |
| 27 | import android.graphics.Color; |
| 28 | import android.graphics.Paint; |
| 29 | import android.graphics.PorterDuff; |
| 30 | import android.graphics.PorterDuffColorFilter; |
| 31 | import android.graphics.RectF; |
| 32 | import android.graphics.Shader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.util.AttributeSet; |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 34 | import android.view.MotionEvent; |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 35 | import android.view.View; |
ztenghui | 62f30e0 | 2014-06-05 09:55:04 -0700 | [diff] [blame] | 36 | import android.view.ViewAnimationUtils; |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 37 | import android.view.ViewConfiguration; |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 38 | import android.view.animation.AnimationUtils; |
| 39 | import android.view.animation.Interpolator; |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 40 | import android.view.animation.LinearInterpolator; |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 41 | import android.view.animation.PathInterpolator; |
ztenghui | 62f30e0 | 2014-06-05 09:55:04 -0700 | [diff] [blame] | 42 | |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 43 | import com.android.systemui.R; |
Jorim Jaggi | 4673985 | 2014-04-15 09:58:24 +0200 | [diff] [blame] | 44 | |
Jorim Jaggi | fe40f7d | 2014-04-28 15:20:04 +0200 | [diff] [blame] | 45 | /** |
| 46 | * Base class for both {@link ExpandableNotificationRow} and {@link NotificationOverflowContainer} |
| 47 | * to implement dimming/activating on Keyguard for the double-tap gesture |
| 48 | */ |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 49 | public abstract class ActivatableNotificationView extends ExpandableOutlineView { |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 50 | |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 51 | private static final long DOUBLETAP_TIMEOUT_MS = 1200; |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 52 | private static final int BACKGROUND_ANIMATION_LENGTH_MS = 220; |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 53 | private static final int ACTIVATE_ANIMATION_LENGTH = 220; |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 54 | private static final int DARK_ANIMATION_LENGTH = 170; |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 55 | |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 56 | /** |
| 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 Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 86 | /** |
| 87 | * Scale for the background to animate from when exiting dark mode. |
| 88 | */ |
| 89 | private static final float DARK_EXIT_SCALE_START = 0.93f; |
| 90 | |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 91 | private static final Interpolator ACTIVATE_INVERSE_INTERPOLATOR |
| 92 | = new PathInterpolator(0.6f, 0, 0.5f, 1); |
| 93 | private static final Interpolator ACTIVATE_INVERSE_ALPHA_INTERPOLATOR |
| 94 | = new PathInterpolator(0, 0, 0.5f, 1); |
Selim Cinek | b2da91b | 2014-09-02 17:35:20 +0200 | [diff] [blame] | 95 | private final int mTintedRippleColor; |
| 96 | private final int mLowPriorityRippleColor; |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 97 | protected final int mNormalRippleColor; |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 98 | |
| 99 | private boolean mDimmed; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 100 | private boolean mDark; |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 101 | |
Dan Sandler | fe266a3 | 2014-05-15 22:28:06 -0400 | [diff] [blame] | 102 | private int mBgTint = 0; |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 103 | private final int mRoundedRectCornerRadius; |
Dan Sandler | fe266a3 | 2014-05-15 22:28:06 -0400 | [diff] [blame] | 104 | |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 105 | /** |
| 106 | * Flag to indicate that the notification has been touched once and the second touch will |
| 107 | * click it. |
| 108 | */ |
| 109 | private boolean mActivated; |
| 110 | |
| 111 | private float mDownX; |
| 112 | private float mDownY; |
| 113 | private final float mTouchSlop; |
Jorim Jaggi | c5dc0d0 | 2014-04-15 15:42:55 +0200 | [diff] [blame] | 114 | |
| 115 | private OnActivatedListener mOnActivatedListener; |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 116 | |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 117 | private final Interpolator mLinearOutSlowInInterpolator; |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 118 | protected final Interpolator mFastOutSlowInInterpolator; |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 119 | private final Interpolator mSlowOutFastInInterpolator; |
| 120 | private final Interpolator mSlowOutLinearInInterpolator; |
| 121 | private final Interpolator mLinearInterpolator; |
| 122 | private Interpolator mCurrentAppearInterpolator; |
| 123 | private Interpolator mCurrentAlphaInterpolator; |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 124 | |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 125 | private NotificationBackgroundView mBackgroundNormal; |
| 126 | private NotificationBackgroundView mBackgroundDimmed; |
| 127 | private ObjectAnimator mBackgroundAnimator; |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 128 | private RectF mAppearAnimationRect = new RectF(); |
| 129 | private PorterDuffColorFilter mAppearAnimationFilter; |
| 130 | private float mAnimationTranslationY; |
| 131 | private boolean mDrawingAppearAnimation; |
| 132 | private Paint mAppearPaint = new Paint(); |
| 133 | private ValueAnimator mAppearAnimator; |
| 134 | private float mAppearAnimationFraction = -1.0f; |
| 135 | private float mAppearAnimationTranslation; |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 136 | private boolean mShowingLegacyBackground; |
| 137 | private final int mLegacyColor; |
| 138 | private final int mNormalColor; |
Selim Cinek | 3d2b94bf | 2014-07-02 22:12:47 +0200 | [diff] [blame] | 139 | private final int mLowPriorityColor; |
| 140 | private boolean mIsBelowSpeedBump; |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 141 | |
Jorim Jaggi | fe40f7d | 2014-04-28 15:20:04 +0200 | [diff] [blame] | 142 | public ActivatableNotificationView(Context context, AttributeSet attrs) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | super(context, attrs); |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 144 | mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 145 | mFastOutSlowInInterpolator = |
| 146 | AnimationUtils.loadInterpolator(context, android.R.interpolator.fast_out_slow_in); |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 147 | mSlowOutFastInInterpolator = new PathInterpolator(0.8f, 0.0f, 0.6f, 1.0f); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 148 | mLinearOutSlowInInterpolator = |
| 149 | AnimationUtils.loadInterpolator(context, android.R.interpolator.linear_out_slow_in); |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 150 | mSlowOutLinearInInterpolator = new PathInterpolator(0.8f, 0.0f, 1.0f, 1.0f); |
| 151 | mLinearInterpolator = new LinearInterpolator(); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 152 | setClipChildren(false); |
| 153 | setClipToPadding(false); |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 154 | mAppearAnimationFilter = new PorterDuffColorFilter(0, PorterDuff.Mode.SRC_ATOP); |
| 155 | mRoundedRectCornerRadius = getResources().getDimensionPixelSize( |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 156 | R.dimen.notification_material_rounded_rect_radius); |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 157 | mLegacyColor = context.getColor(R.color.notification_legacy_background_color); |
| 158 | mNormalColor = context.getColor(R.color.notification_material_background_color); |
| 159 | mLowPriorityColor = context.getColor( |
Selim Cinek | 3d2b94bf | 2014-07-02 22:12:47 +0200 | [diff] [blame] | 160 | R.color.notification_material_background_low_priority_color); |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 161 | mTintedRippleColor = context.getColor( |
Selim Cinek | b2da91b | 2014-09-02 17:35:20 +0200 | [diff] [blame] | 162 | R.color.notification_ripple_tinted_color); |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 163 | mLowPriorityRippleColor = context.getColor( |
Selim Cinek | b2da91b | 2014-09-02 17:35:20 +0200 | [diff] [blame] | 164 | R.color.notification_ripple_color_low_priority); |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 165 | mNormalRippleColor = context.getColor( |
Selim Cinek | b2da91b | 2014-09-02 17:35:20 +0200 | [diff] [blame] | 166 | R.color.notification_ripple_untinted_color); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Override |
| 170 | protected void onFinishInflate() { |
| 171 | super.onFinishInflate(); |
| 172 | mBackgroundNormal = (NotificationBackgroundView) findViewById(R.id.backgroundNormal); |
| 173 | mBackgroundDimmed = (NotificationBackgroundView) findViewById(R.id.backgroundDimmed); |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 174 | mBackgroundNormal.setCustomBackground(R.drawable.notification_material_bg); |
| 175 | mBackgroundDimmed.setCustomBackground(R.drawable.notification_material_bg_dim); |
Adrian Roos | bcbb75a | 2014-05-27 16:38:11 +0200 | [diff] [blame] | 176 | updateBackground(); |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 177 | updateBackgroundTint(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 180 | private final Runnable mTapTimeoutRunnable = new Runnable() { |
| 181 | @Override |
| 182 | public void run() { |
Selim Cinek | a32ab60 | 2014-06-11 15:06:01 +0200 | [diff] [blame] | 183 | makeInactive(true /* animate */); |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 184 | } |
| 185 | }; |
| 186 | |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 187 | @Override |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 188 | public boolean onTouchEvent(MotionEvent event) { |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 189 | if (mDimmed) { |
| 190 | return handleTouchEventDimmed(event); |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 191 | } else { |
| 192 | return super.onTouchEvent(event); |
| 193 | } |
| 194 | } |
| 195 | |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 196 | @Override |
| 197 | public void drawableHotspotChanged(float x, float y) { |
| 198 | if (!mDimmed){ |
| 199 | mBackgroundNormal.drawableHotspotChanged(x, y); |
| 200 | } |
| 201 | } |
| 202 | |
Selim Cinek | b2da91b | 2014-09-02 17:35:20 +0200 | [diff] [blame] | 203 | @Override |
| 204 | protected void drawableStateChanged() { |
| 205 | super.drawableStateChanged(); |
| 206 | if (mDimmed) { |
| 207 | mBackgroundDimmed.setState(getDrawableState()); |
| 208 | } else { |
| 209 | mBackgroundNormal.setState(getDrawableState()); |
| 210 | } |
| 211 | } |
| 212 | |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 213 | private boolean handleTouchEventDimmed(MotionEvent event) { |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 214 | int action = event.getActionMasked(); |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 215 | switch (action) { |
| 216 | case MotionEvent.ACTION_DOWN: |
| 217 | mDownX = event.getX(); |
| 218 | mDownY = event.getY(); |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 219 | if (mDownY > getActualHeight()) { |
| 220 | return false; |
| 221 | } |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 222 | break; |
| 223 | case MotionEvent.ACTION_MOVE: |
| 224 | if (!isWithinTouchSlop(event)) { |
Selim Cinek | a32ab60 | 2014-06-11 15:06:01 +0200 | [diff] [blame] | 225 | makeInactive(true /* animate */); |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 226 | return false; |
| 227 | } |
| 228 | break; |
| 229 | case MotionEvent.ACTION_UP: |
| 230 | if (isWithinTouchSlop(event)) { |
| 231 | if (!mActivated) { |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 232 | makeActive(); |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 233 | postDelayed(mTapTimeoutRunnable, DOUBLETAP_TIMEOUT_MS); |
| 234 | } else { |
Selim Cinek | a32ab60 | 2014-06-11 15:06:01 +0200 | [diff] [blame] | 235 | boolean performed = performClick(); |
| 236 | if (performed) { |
| 237 | removeCallbacks(mTapTimeoutRunnable); |
| 238 | } |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 239 | } |
| 240 | } else { |
Selim Cinek | a32ab60 | 2014-06-11 15:06:01 +0200 | [diff] [blame] | 241 | makeInactive(true /* animate */); |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 242 | } |
| 243 | break; |
| 244 | case MotionEvent.ACTION_CANCEL: |
Selim Cinek | a32ab60 | 2014-06-11 15:06:01 +0200 | [diff] [blame] | 245 | makeInactive(true /* animate */); |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 246 | break; |
| 247 | default: |
| 248 | break; |
| 249 | } |
| 250 | return true; |
| 251 | } |
| 252 | |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 253 | private void makeActive() { |
| 254 | startActivateAnimation(false /* reverse */); |
Jorim Jaggi | c5dc0d0 | 2014-04-15 15:42:55 +0200 | [diff] [blame] | 255 | mActivated = true; |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 256 | if (mOnActivatedListener != null) { |
| 257 | mOnActivatedListener.onActivated(this); |
| 258 | } |
Jorim Jaggi | c5dc0d0 | 2014-04-15 15:42:55 +0200 | [diff] [blame] | 259 | } |
| 260 | |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 261 | private void startActivateAnimation(boolean reverse) { |
Selim Cinek | f4d7fc3 | 2014-09-26 17:15:34 +0200 | [diff] [blame] | 262 | if (!isAttachedToWindow()) { |
| 263 | return; |
| 264 | } |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 265 | int widthHalf = mBackgroundNormal.getWidth()/2; |
| 266 | int heightHalf = mBackgroundNormal.getActualHeight()/2; |
| 267 | float radius = (float) Math.sqrt(widthHalf*widthHalf + heightHalf*heightHalf); |
Selim Cinek | 6aaf217 | 2014-08-25 19:29:49 +0200 | [diff] [blame] | 268 | Animator animator; |
| 269 | if (reverse) { |
| 270 | animator = ViewAnimationUtils.createCircularReveal(mBackgroundNormal, |
| 271 | widthHalf, heightHalf, radius, 0); |
| 272 | } else { |
| 273 | animator = ViewAnimationUtils.createCircularReveal(mBackgroundNormal, |
| 274 | widthHalf, heightHalf, 0, radius); |
| 275 | } |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 276 | mBackgroundNormal.setVisibility(View.VISIBLE); |
| 277 | Interpolator interpolator; |
| 278 | Interpolator alphaInterpolator; |
| 279 | if (!reverse) { |
| 280 | interpolator = mLinearOutSlowInInterpolator; |
| 281 | alphaInterpolator = mLinearOutSlowInInterpolator; |
| 282 | } else { |
| 283 | interpolator = ACTIVATE_INVERSE_INTERPOLATOR; |
| 284 | alphaInterpolator = ACTIVATE_INVERSE_ALPHA_INTERPOLATOR; |
| 285 | } |
| 286 | animator.setInterpolator(interpolator); |
| 287 | animator.setDuration(ACTIVATE_ANIMATION_LENGTH); |
| 288 | if (reverse) { |
| 289 | mBackgroundNormal.setAlpha(1f); |
| 290 | animator.addListener(new AnimatorListenerAdapter() { |
| 291 | @Override |
| 292 | public void onAnimationEnd(Animator animation) { |
Selim Cinek | 23e6acb | 2014-08-12 16:01:41 +0200 | [diff] [blame] | 293 | if (mDimmed) { |
| 294 | mBackgroundNormal.setVisibility(View.INVISIBLE); |
| 295 | } |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 296 | } |
| 297 | }); |
Selim Cinek | 6aaf217 | 2014-08-25 19:29:49 +0200 | [diff] [blame] | 298 | animator.start(); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 299 | } else { |
| 300 | mBackgroundNormal.setAlpha(0.4f); |
| 301 | animator.start(); |
| 302 | } |
| 303 | mBackgroundNormal.animate() |
| 304 | .alpha(reverse ? 0f : 1f) |
| 305 | .setInterpolator(alphaInterpolator) |
| 306 | .setDuration(ACTIVATE_ANIMATION_LENGTH); |
| 307 | } |
| 308 | |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 309 | /** |
| 310 | * Cancels the hotspot and makes the notification inactive. |
| 311 | */ |
Selim Cinek | a32ab60 | 2014-06-11 15:06:01 +0200 | [diff] [blame] | 312 | public void makeInactive(boolean animate) { |
Jorim Jaggi | c5dc0d0 | 2014-04-15 15:42:55 +0200 | [diff] [blame] | 313 | if (mActivated) { |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 314 | if (mDimmed) { |
Selim Cinek | a32ab60 | 2014-06-11 15:06:01 +0200 | [diff] [blame] | 315 | if (animate) { |
| 316 | startActivateAnimation(true /* reverse */); |
| 317 | } else { |
| 318 | mBackgroundNormal.setVisibility(View.INVISIBLE); |
| 319 | } |
Jorim Jaggi | bccb912 | 2014-05-08 14:55:48 +0200 | [diff] [blame] | 320 | } |
Jorim Jaggi | c5dc0d0 | 2014-04-15 15:42:55 +0200 | [diff] [blame] | 321 | mActivated = false; |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 322 | } |
Jorim Jaggi | c5dc0d0 | 2014-04-15 15:42:55 +0200 | [diff] [blame] | 323 | if (mOnActivatedListener != null) { |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 324 | mOnActivatedListener.onActivationReset(this); |
Jorim Jaggi | c5dc0d0 | 2014-04-15 15:42:55 +0200 | [diff] [blame] | 325 | } |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 326 | removeCallbacks(mTapTimeoutRunnable); |
| 327 | } |
| 328 | |
| 329 | private boolean isWithinTouchSlop(MotionEvent event) { |
| 330 | return Math.abs(event.getX() - mDownX) < mTouchSlop |
| 331 | && Math.abs(event.getY() - mDownY) < mTouchSlop; |
| 332 | } |
| 333 | |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 334 | public void setDimmed(boolean dimmed, boolean fade) { |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 335 | if (mDimmed != dimmed) { |
| 336 | mDimmed = dimmed; |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 337 | if (fade) { |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 338 | fadeDimmedBackground(); |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 339 | } else { |
Jorim Jaggi | 3c3c3fc | 2014-05-20 23:16:42 +0200 | [diff] [blame] | 340 | updateBackground(); |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 341 | } |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 345 | public void setDark(boolean dark, boolean fade, long delay) { |
| 346 | super.setDark(dark, fade, delay); |
| 347 | if (mDark == dark) { |
| 348 | return; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 349 | } |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 350 | mDark = dark; |
| 351 | if (!dark && fade) { |
| 352 | if (mActivated) { |
| 353 | mBackgroundDimmed.setVisibility(View.VISIBLE); |
| 354 | mBackgroundNormal.setVisibility(View.VISIBLE); |
Selim Cinek | c430a5b | 2014-12-10 16:29:01 +0100 | [diff] [blame] | 355 | } else if (mDimmed) { |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 356 | mBackgroundDimmed.setVisibility(View.VISIBLE); |
| 357 | mBackgroundNormal.setVisibility(View.INVISIBLE); |
Selim Cinek | c430a5b | 2014-12-10 16:29:01 +0100 | [diff] [blame] | 358 | } else { |
| 359 | mBackgroundDimmed.setVisibility(View.INVISIBLE); |
| 360 | mBackgroundNormal.setVisibility(View.VISIBLE); |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 361 | } |
Selim Cinek | c430a5b | 2014-12-10 16:29:01 +0100 | [diff] [blame] | 362 | fadeInFromDark(delay); |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 363 | } else { |
| 364 | updateBackground(); |
| 365 | } |
| 366 | } |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 367 | |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 368 | public void setShowingLegacyBackground(boolean showing) { |
| 369 | mShowingLegacyBackground = showing; |
| 370 | updateBackgroundTint(); |
Jorim Jaggi | 4673985 | 2014-04-15 09:58:24 +0200 | [diff] [blame] | 371 | } |
| 372 | |
Selim Cinek | 3d2b94bf | 2014-07-02 22:12:47 +0200 | [diff] [blame] | 373 | @Override |
| 374 | public void setBelowSpeedBump(boolean below) { |
| 375 | super.setBelowSpeedBump(below); |
| 376 | if (below != mIsBelowSpeedBump) { |
| 377 | mIsBelowSpeedBump = below; |
| 378 | updateBackgroundTint(); |
| 379 | } |
| 380 | } |
| 381 | |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 382 | /** |
| 383 | * Sets the tint color of the background |
| 384 | */ |
| 385 | public void setTintColor(int color) { |
| 386 | mBgTint = color; |
| 387 | updateBackgroundTint(); |
| 388 | } |
| 389 | |
| 390 | private void updateBackgroundTint() { |
Chet Haase | 5058c38 | 2015-01-06 06:29:35 -0800 | [diff] [blame] | 391 | int color = getBgColor(); |
Selim Cinek | b2da91b | 2014-09-02 17:35:20 +0200 | [diff] [blame] | 392 | int rippleColor = getRippleColor(); |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 393 | if (color == mNormalColor) { |
| 394 | // We don't need to tint a normal notification |
| 395 | color = 0; |
| 396 | } |
| 397 | mBackgroundDimmed.setTint(color); |
| 398 | mBackgroundNormal.setTint(color); |
Selim Cinek | b2da91b | 2014-09-02 17:35:20 +0200 | [diff] [blame] | 399 | mBackgroundDimmed.setRippleColor(rippleColor); |
| 400 | mBackgroundNormal.setRippleColor(rippleColor); |
Dan Sandler | fe266a3 | 2014-05-15 22:28:06 -0400 | [diff] [blame] | 401 | } |
| 402 | |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 403 | /** |
Selim Cinek | c430a5b | 2014-12-10 16:29:01 +0100 | [diff] [blame] | 404 | * Fades in the background when exiting dark mode. |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 405 | */ |
Selim Cinek | c430a5b | 2014-12-10 16:29:01 +0100 | [diff] [blame] | 406 | private void fadeInFromDark(long delay) { |
| 407 | final View background = mDimmed ? mBackgroundDimmed : mBackgroundNormal; |
| 408 | background.setAlpha(0f); |
| 409 | background.setPivotX(mBackgroundDimmed.getWidth() / 2f); |
| 410 | background.setPivotY(getActualHeight() / 2f); |
| 411 | background.setScaleX(DARK_EXIT_SCALE_START); |
| 412 | background.setScaleY(DARK_EXIT_SCALE_START); |
| 413 | background.animate() |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 414 | .alpha(1f) |
| 415 | .scaleX(1f) |
| 416 | .scaleY(1f) |
| 417 | .setDuration(DARK_ANIMATION_LENGTH) |
| 418 | .setStartDelay(delay) |
| 419 | .setInterpolator(mLinearOutSlowInInterpolator) |
| 420 | .setListener(new AnimatorListenerAdapter() { |
| 421 | @Override |
| 422 | public void onAnimationCancel(Animator animation) { |
| 423 | // Jump state if we are cancelled |
Selim Cinek | c430a5b | 2014-12-10 16:29:01 +0100 | [diff] [blame] | 424 | background.setScaleX(1f); |
| 425 | background.setScaleY(1f); |
| 426 | background.setAlpha(1f); |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 427 | } |
| 428 | }) |
| 429 | .start(); |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Fades the background when the dimmed state changes. |
| 434 | */ |
| 435 | private void fadeDimmedBackground() { |
| 436 | mBackgroundDimmed.animate().cancel(); |
Selim Cinek | 59d9723 | 2014-10-09 15:54:40 -0700 | [diff] [blame] | 437 | mBackgroundNormal.animate().cancel(); |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 438 | if (mDimmed) { |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 439 | mBackgroundDimmed.setVisibility(View.VISIBLE); |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 440 | } else { |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 441 | mBackgroundNormal.setVisibility(View.VISIBLE); |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 442 | } |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 443 | float startAlpha = mDimmed ? 1f : 0; |
| 444 | float endAlpha = mDimmed ? 0 : 1f; |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 445 | int duration = BACKGROUND_ANIMATION_LENGTH_MS; |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 446 | // Check whether there is already a background animation running. |
| 447 | if (mBackgroundAnimator != null) { |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 448 | startAlpha = (Float) mBackgroundAnimator.getAnimatedValue(); |
Jorim Jaggi | 98fb09c | 2014-05-01 22:40:56 +0200 | [diff] [blame] | 449 | duration = (int) mBackgroundAnimator.getCurrentPlayTime(); |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 450 | mBackgroundAnimator.removeAllListeners(); |
| 451 | mBackgroundAnimator.cancel(); |
Jorim Jaggi | 98fb09c | 2014-05-01 22:40:56 +0200 | [diff] [blame] | 452 | if (duration <= 0) { |
Jorim Jaggi | 3c3c3fc | 2014-05-20 23:16:42 +0200 | [diff] [blame] | 453 | updateBackground(); |
Jorim Jaggi | 98fb09c | 2014-05-01 22:40:56 +0200 | [diff] [blame] | 454 | return; |
| 455 | } |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 456 | } |
| 457 | mBackgroundNormal.setAlpha(startAlpha); |
| 458 | mBackgroundAnimator = |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 459 | ObjectAnimator.ofFloat(mBackgroundNormal, View.ALPHA, startAlpha, endAlpha); |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 460 | mBackgroundAnimator.setInterpolator(mFastOutSlowInInterpolator); |
| 461 | mBackgroundAnimator.setDuration(duration); |
| 462 | mBackgroundAnimator.addListener(new AnimatorListenerAdapter() { |
| 463 | @Override |
| 464 | public void onAnimationEnd(Animator animation) { |
| 465 | if (mDimmed) { |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 466 | mBackgroundNormal.setVisibility(View.INVISIBLE); |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 467 | } else { |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 468 | mBackgroundDimmed.setVisibility(View.INVISIBLE); |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 469 | } |
| 470 | mBackgroundAnimator = null; |
| 471 | } |
| 472 | }); |
| 473 | mBackgroundAnimator.start(); |
| 474 | } |
| 475 | |
Jorim Jaggi | 3c3c3fc | 2014-05-20 23:16:42 +0200 | [diff] [blame] | 476 | private void updateBackground() { |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 477 | cancelFadeAnimations(); |
| 478 | if (mDark) { |
| 479 | mBackgroundDimmed.setVisibility(View.INVISIBLE); |
| 480 | mBackgroundNormal.setVisibility(View.INVISIBLE); |
| 481 | } else if (mDimmed) { |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 482 | mBackgroundDimmed.setVisibility(View.VISIBLE); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 483 | mBackgroundNormal.setVisibility(View.INVISIBLE); |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 484 | } else { |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 485 | mBackgroundDimmed.setVisibility(View.INVISIBLE); |
| 486 | mBackgroundNormal.setVisibility(View.VISIBLE); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 487 | mBackgroundNormal.setAlpha(1f); |
Selim Cinek | a32ab60 | 2014-06-11 15:06:01 +0200 | [diff] [blame] | 488 | removeCallbacks(mTapTimeoutRunnable); |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
Selim Cinek | 59d9723 | 2014-10-09 15:54:40 -0700 | [diff] [blame] | 492 | private void cancelFadeAnimations() { |
| 493 | if (mBackgroundAnimator != null) { |
| 494 | mBackgroundAnimator.cancel(); |
| 495 | } |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 496 | mBackgroundDimmed.animate().cancel(); |
Selim Cinek | 59d9723 | 2014-10-09 15:54:40 -0700 | [diff] [blame] | 497 | mBackgroundNormal.animate().cancel(); |
| 498 | } |
| 499 | |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 500 | @Override |
| 501 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 502 | super.onLayout(changed, left, top, right, bottom); |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 503 | setPivotX(getWidth() / 2); |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | @Override |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 507 | public void setActualHeight(int actualHeight, boolean notifyListeners) { |
| 508 | super.setActualHeight(actualHeight, notifyListeners); |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 509 | setPivotY(actualHeight / 2); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 510 | mBackgroundNormal.setActualHeight(actualHeight); |
| 511 | mBackgroundDimmed.setActualHeight(actualHeight); |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | @Override |
| 515 | public void setClipTopAmount(int clipTopAmount) { |
| 516 | super.setClipTopAmount(clipTopAmount); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 517 | mBackgroundNormal.setClipTopAmount(clipTopAmount); |
| 518 | mBackgroundDimmed.setClipTopAmount(clipTopAmount); |
Jorim Jaggi | 4673985 | 2014-04-15 09:58:24 +0200 | [diff] [blame] | 519 | } |
Jorim Jaggi | c5dc0d0 | 2014-04-15 15:42:55 +0200 | [diff] [blame] | 520 | |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 521 | @Override |
Jorim Jaggi | 60d07c5 | 2014-07-31 15:38:21 +0200 | [diff] [blame] | 522 | public void performRemoveAnimation(long duration, float translationDirection, |
| 523 | Runnable onFinishedRunnable) { |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 524 | enableAppearDrawing(true); |
| 525 | if (mDrawingAppearAnimation) { |
| 526 | startAppearAnimation(false /* isAppearing */, translationDirection, |
Jorim Jaggi | 60d07c5 | 2014-07-31 15:38:21 +0200 | [diff] [blame] | 527 | 0, duration, onFinishedRunnable); |
Selim Cinek | 95ed592 | 2014-08-28 14:30:12 +0200 | [diff] [blame] | 528 | } else if (onFinishedRunnable != null) { |
| 529 | onFinishedRunnable.run(); |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
| 533 | @Override |
Jorim Jaggi | 60d07c5 | 2014-07-31 15:38:21 +0200 | [diff] [blame] | 534 | public void performAddAnimation(long delay, long duration) { |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 535 | enableAppearDrawing(true); |
| 536 | if (mDrawingAppearAnimation) { |
Jorim Jaggi | 60d07c5 | 2014-07-31 15:38:21 +0200 | [diff] [blame] | 537 | startAppearAnimation(true /* isAppearing */, -1.0f, delay, duration, null); |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
Jorim Jaggi | 60d07c5 | 2014-07-31 15:38:21 +0200 | [diff] [blame] | 541 | private void startAppearAnimation(boolean isAppearing, float translationDirection, long delay, |
| 542 | long duration, final Runnable onFinishedRunnable) { |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 543 | if (mAppearAnimator != null) { |
| 544 | mAppearAnimator.cancel(); |
| 545 | } |
Chris Wren | 310df31 | 2014-10-31 14:29:46 -0400 | [diff] [blame] | 546 | mAnimationTranslationY = translationDirection * getActualHeight(); |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 547 | if (mAppearAnimationFraction == -1.0f) { |
| 548 | // not initialized yet, we start anew |
| 549 | if (isAppearing) { |
| 550 | mAppearAnimationFraction = 0.0f; |
| 551 | mAppearAnimationTranslation = mAnimationTranslationY; |
| 552 | } else { |
| 553 | mAppearAnimationFraction = 1.0f; |
| 554 | mAppearAnimationTranslation = 0; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | float targetValue; |
| 559 | if (isAppearing) { |
| 560 | mCurrentAppearInterpolator = mSlowOutFastInInterpolator; |
| 561 | mCurrentAlphaInterpolator = mLinearOutSlowInInterpolator; |
| 562 | targetValue = 1.0f; |
| 563 | } else { |
| 564 | mCurrentAppearInterpolator = mFastOutSlowInInterpolator; |
| 565 | mCurrentAlphaInterpolator = mSlowOutLinearInInterpolator; |
| 566 | targetValue = 0.0f; |
| 567 | } |
| 568 | mAppearAnimator = ValueAnimator.ofFloat(mAppearAnimationFraction, |
| 569 | targetValue); |
| 570 | mAppearAnimator.setInterpolator(mLinearInterpolator); |
| 571 | mAppearAnimator.setDuration( |
Jorim Jaggi | 60d07c5 | 2014-07-31 15:38:21 +0200 | [diff] [blame] | 572 | (long) (duration * Math.abs(mAppearAnimationFraction - targetValue))); |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 573 | mAppearAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 574 | @Override |
| 575 | public void onAnimationUpdate(ValueAnimator animation) { |
| 576 | mAppearAnimationFraction = (float) animation.getAnimatedValue(); |
| 577 | updateAppearAnimationAlpha(); |
| 578 | updateAppearRect(); |
| 579 | invalidate(); |
| 580 | } |
| 581 | }); |
| 582 | if (delay > 0) { |
| 583 | // we need to apply the initial state already to avoid drawn frames in the wrong state |
| 584 | updateAppearAnimationAlpha(); |
| 585 | updateAppearRect(); |
| 586 | mAppearAnimator.setStartDelay(delay); |
| 587 | } |
| 588 | mAppearAnimator.addListener(new AnimatorListenerAdapter() { |
| 589 | private boolean mWasCancelled; |
| 590 | |
| 591 | @Override |
| 592 | public void onAnimationEnd(Animator animation) { |
| 593 | if (onFinishedRunnable != null) { |
| 594 | onFinishedRunnable.run(); |
| 595 | } |
| 596 | if (!mWasCancelled) { |
| 597 | mAppearAnimationFraction = -1; |
| 598 | setOutlineRect(null); |
| 599 | enableAppearDrawing(false); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | @Override |
| 604 | public void onAnimationStart(Animator animation) { |
| 605 | mWasCancelled = false; |
| 606 | } |
| 607 | |
| 608 | @Override |
| 609 | public void onAnimationCancel(Animator animation) { |
| 610 | mWasCancelled = true; |
| 611 | } |
| 612 | }); |
| 613 | mAppearAnimator.start(); |
| 614 | } |
| 615 | |
| 616 | private void updateAppearRect() { |
| 617 | float inverseFraction = (1.0f - mAppearAnimationFraction); |
| 618 | float translationFraction = mCurrentAppearInterpolator.getInterpolation(inverseFraction); |
| 619 | float translateYTotalAmount = translationFraction * mAnimationTranslationY; |
| 620 | mAppearAnimationTranslation = translateYTotalAmount; |
| 621 | |
| 622 | // handle width animation |
| 623 | float widthFraction = (inverseFraction - (1.0f - HORIZONTAL_ANIMATION_START)) |
| 624 | / (HORIZONTAL_ANIMATION_START - HORIZONTAL_ANIMATION_END); |
| 625 | widthFraction = Math.min(1.0f, Math.max(0.0f, widthFraction)); |
| 626 | widthFraction = mCurrentAppearInterpolator.getInterpolation(widthFraction); |
| 627 | float left = (getWidth() * (0.5f - HORIZONTAL_COLLAPSED_REST_PARTIAL / 2.0f) * |
| 628 | widthFraction); |
| 629 | float right = getWidth() - left; |
| 630 | |
| 631 | // handle top animation |
| 632 | float heightFraction = (inverseFraction - (1.0f - VERTICAL_ANIMATION_START)) / |
| 633 | VERTICAL_ANIMATION_START; |
| 634 | heightFraction = Math.max(0.0f, heightFraction); |
| 635 | heightFraction = mCurrentAppearInterpolator.getInterpolation(heightFraction); |
| 636 | |
| 637 | float top; |
| 638 | float bottom; |
Chris Wren | 310df31 | 2014-10-31 14:29:46 -0400 | [diff] [blame] | 639 | final int actualHeight = getActualHeight(); |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 640 | if (mAnimationTranslationY > 0.0f) { |
Chris Wren | 310df31 | 2014-10-31 14:29:46 -0400 | [diff] [blame] | 641 | bottom = actualHeight - heightFraction * mAnimationTranslationY * 0.1f |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 642 | - translateYTotalAmount; |
| 643 | top = bottom * heightFraction; |
| 644 | } else { |
Chris Wren | 310df31 | 2014-10-31 14:29:46 -0400 | [diff] [blame] | 645 | top = heightFraction * (actualHeight + mAnimationTranslationY) * 0.1f - |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 646 | translateYTotalAmount; |
Chris Wren | 310df31 | 2014-10-31 14:29:46 -0400 | [diff] [blame] | 647 | bottom = actualHeight * (1 - heightFraction) + top * heightFraction; |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 648 | } |
| 649 | mAppearAnimationRect.set(left, top, right, bottom); |
| 650 | setOutlineRect(left, top + mAppearAnimationTranslation, right, |
| 651 | bottom + mAppearAnimationTranslation); |
| 652 | } |
| 653 | |
| 654 | private void updateAppearAnimationAlpha() { |
Chet Haase | 5058c38 | 2015-01-06 06:29:35 -0800 | [diff] [blame] | 655 | int backgroundColor = getBgColor(); |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 656 | if (backgroundColor != -1) { |
| 657 | float contentAlphaProgress = mAppearAnimationFraction; |
| 658 | contentAlphaProgress = contentAlphaProgress / (1.0f - ALPHA_ANIMATION_END); |
| 659 | contentAlphaProgress = Math.min(1.0f, contentAlphaProgress); |
| 660 | contentAlphaProgress = mCurrentAlphaInterpolator.getInterpolation(contentAlphaProgress); |
| 661 | int sourceColor = Color.argb((int) (255 * (1.0f - contentAlphaProgress)), |
| 662 | Color.red(backgroundColor), Color.green(backgroundColor), |
| 663 | Color.blue(backgroundColor)); |
| 664 | mAppearAnimationFilter.setColor(sourceColor); |
| 665 | mAppearPaint.setColorFilter(mAppearAnimationFilter); |
| 666 | } |
| 667 | } |
| 668 | |
Chet Haase | 5058c38 | 2015-01-06 06:29:35 -0800 | [diff] [blame] | 669 | private int getBgColor() { |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 670 | if (mBgTint != 0) { |
| 671 | return mBgTint; |
| 672 | } else if (mShowingLegacyBackground) { |
| 673 | return mLegacyColor; |
Selim Cinek | 3d2b94bf | 2014-07-02 22:12:47 +0200 | [diff] [blame] | 674 | } else if (mIsBelowSpeedBump) { |
| 675 | return mLowPriorityColor; |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 676 | } else { |
| 677 | return mNormalColor; |
| 678 | } |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 679 | } |
| 680 | |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 681 | protected int getRippleColor() { |
Selim Cinek | b2da91b | 2014-09-02 17:35:20 +0200 | [diff] [blame] | 682 | if (mBgTint != 0) { |
| 683 | return mTintedRippleColor; |
| 684 | } else if (mShowingLegacyBackground) { |
| 685 | return mTintedRippleColor; |
| 686 | } else if (mIsBelowSpeedBump) { |
| 687 | return mLowPriorityRippleColor; |
| 688 | } else { |
| 689 | return mNormalRippleColor; |
| 690 | } |
| 691 | } |
| 692 | |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 693 | /** |
| 694 | * When we draw the appear animation, we render the view in a bitmap and render this bitmap |
| 695 | * as a shader of a rect. This call creates the Bitmap and switches the drawing mode, |
| 696 | * such that the normal drawing of the views does not happen anymore. |
| 697 | * |
| 698 | * @param enable Should it be enabled. |
| 699 | */ |
| 700 | private void enableAppearDrawing(boolean enable) { |
| 701 | if (enable != mDrawingAppearAnimation) { |
| 702 | if (enable) { |
| 703 | if (getWidth() == 0 || getActualHeight() == 0) { |
| 704 | // TODO: This should not happen, but it can during expansion. Needs |
| 705 | // investigation |
| 706 | return; |
| 707 | } |
| 708 | Bitmap bitmap = Bitmap.createBitmap(getWidth(), getActualHeight(), |
| 709 | Bitmap.Config.ARGB_8888); |
| 710 | Canvas canvas = new Canvas(bitmap); |
| 711 | draw(canvas); |
| 712 | mAppearPaint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, |
| 713 | Shader.TileMode.CLAMP)); |
| 714 | } else { |
| 715 | mAppearPaint.setShader(null); |
| 716 | } |
| 717 | mDrawingAppearAnimation = enable; |
| 718 | invalidate(); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | @Override |
| 723 | protected void dispatchDraw(Canvas canvas) { |
| 724 | if (!mDrawingAppearAnimation) { |
| 725 | super.dispatchDraw(canvas); |
| 726 | } else { |
| 727 | drawAppearRect(canvas); |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | private void drawAppearRect(Canvas canvas) { |
| 732 | canvas.save(); |
| 733 | canvas.translate(0, mAppearAnimationTranslation); |
| 734 | canvas.drawRoundRect(mAppearAnimationRect, mRoundedRectCornerRadius, |
| 735 | mRoundedRectCornerRadius, mAppearPaint); |
| 736 | canvas.restore(); |
| 737 | } |
| 738 | |
Jorim Jaggi | c5dc0d0 | 2014-04-15 15:42:55 +0200 | [diff] [blame] | 739 | public void setOnActivatedListener(OnActivatedListener onActivatedListener) { |
| 740 | mOnActivatedListener = onActivatedListener; |
| 741 | } |
| 742 | |
Christoph Studer | 22f2ee5 | 2014-07-29 22:57:21 +0200 | [diff] [blame] | 743 | public void reset() { |
| 744 | setTintColor(0); |
| 745 | setShowingLegacyBackground(false); |
| 746 | setBelowSpeedBump(false); |
| 747 | } |
| 748 | |
Jorim Jaggi | c5dc0d0 | 2014-04-15 15:42:55 +0200 | [diff] [blame] | 749 | public interface OnActivatedListener { |
Selim Cinek | a32ab60 | 2014-06-11 15:06:01 +0200 | [diff] [blame] | 750 | void onActivated(ActivatableNotificationView view); |
| 751 | void onActivationReset(ActivatableNotificationView view); |
Jorim Jaggi | c5dc0d0 | 2014-04-15 15:42:55 +0200 | [diff] [blame] | 752 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 753 | } |