Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.statusbar; |
| 18 | |
| 19 | import android.content.Context; |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 20 | import android.content.res.ColorStateList; |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 21 | import android.graphics.Canvas; |
| 22 | import android.graphics.PorterDuff; |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 23 | import android.graphics.PorterDuffXfermode; |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 24 | import android.graphics.drawable.Drawable; |
Selim Cinek | 0fe0739 | 2017-11-09 13:26:34 -0800 | [diff] [blame] | 25 | import android.graphics.drawable.GradientDrawable; |
| 26 | import android.graphics.drawable.LayerDrawable; |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 27 | import android.graphics.drawable.RippleDrawable; |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 28 | import android.util.AttributeSet; |
| 29 | import android.view.View; |
| 30 | |
Selim Cinek | fe24fb7 | 2018-02-13 14:34:55 -0800 | [diff] [blame] | 31 | import com.android.internal.util.ArrayUtils; |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 32 | import com.android.systemui.Interpolators; |
Selim Cinek | 0fe0739 | 2017-11-09 13:26:34 -0800 | [diff] [blame] | 33 | import com.android.systemui.R; |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 34 | import com.android.systemui.statusbar.notification.ActivityLaunchAnimator; |
Selim Cinek | 0fe0739 | 2017-11-09 13:26:34 -0800 | [diff] [blame] | 35 | |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 36 | /** |
| 37 | * A view that can be used for both the dimmed and normal background of an notification. |
| 38 | */ |
| 39 | public class NotificationBackgroundView extends View { |
| 40 | |
Selim Cinek | 0fe0739 | 2017-11-09 13:26:34 -0800 | [diff] [blame] | 41 | private final boolean mDontModifyCorners; |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 42 | private Drawable mBackground; |
| 43 | private int mClipTopAmount; |
| 44 | private int mActualHeight; |
Selim Cinek | a686b2c | 2016-10-26 13:58:27 -0700 | [diff] [blame] | 45 | private int mClipBottomAmount; |
Selim Cinek | 588423a | 2017-08-17 16:42:51 -0700 | [diff] [blame] | 46 | private int mTintColor; |
Selim Cinek | 0fe0739 | 2017-11-09 13:26:34 -0800 | [diff] [blame] | 47 | private float[] mCornerRadii = new float[8]; |
Selim Cinek | 515b203 | 2017-11-15 10:20:19 -0800 | [diff] [blame] | 48 | private boolean mBottomIsRounded; |
| 49 | private int mBackgroundTop; |
Selim Cinek | 2871bef | 2017-11-22 08:40:00 -0800 | [diff] [blame] | 50 | private boolean mBottomAmountClips = true; |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 51 | private boolean mExpandAnimationRunning; |
| 52 | private float mActualWidth; |
| 53 | private int mDrawableAlpha = 255; |
Selim Cinek | fe24fb7 | 2018-02-13 14:34:55 -0800 | [diff] [blame] | 54 | private boolean mIsPressedAllowed; |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 55 | |
Tony Huang | c092c43 | 2018-05-18 17:38:54 +0800 | [diff] [blame] | 56 | private boolean mTopAmountRounded; |
| 57 | private float mDistanceToTopRoundness; |
| 58 | |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 59 | public NotificationBackgroundView(Context context, AttributeSet attrs) { |
| 60 | super(context, attrs); |
Selim Cinek | 0fe0739 | 2017-11-09 13:26:34 -0800 | [diff] [blame] | 61 | mDontModifyCorners = getResources().getBoolean( |
| 62 | R.bool.config_clipNotificationsToOutline); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 66 | protected void onDraw(Canvas canvas) { |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 67 | if (mClipTopAmount + mClipBottomAmount < mActualHeight - mBackgroundTop |
| 68 | || mExpandAnimationRunning) { |
Selim Cinek | 515b203 | 2017-11-15 10:20:19 -0800 | [diff] [blame] | 69 | canvas.save(); |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 70 | if (!mExpandAnimationRunning) { |
| 71 | canvas.clipRect(0, mClipTopAmount, getWidth(), mActualHeight - mClipBottomAmount); |
| 72 | } |
Selim Cinek | 515b203 | 2017-11-15 10:20:19 -0800 | [diff] [blame] | 73 | draw(canvas, mBackground); |
| 74 | canvas.restore(); |
| 75 | } |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | private void draw(Canvas canvas, Drawable drawable) { |
Selim Cinek | 515b203 | 2017-11-15 10:20:19 -0800 | [diff] [blame] | 79 | if (drawable != null) { |
Tony Huang | c092c43 | 2018-05-18 17:38:54 +0800 | [diff] [blame] | 80 | int top = mBackgroundTop; |
Selim Cinek | 515b203 | 2017-11-15 10:20:19 -0800 | [diff] [blame] | 81 | int bottom = mActualHeight; |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 82 | if (mBottomIsRounded && mBottomAmountClips && !mExpandAnimationRunning) { |
Selim Cinek | 515b203 | 2017-11-15 10:20:19 -0800 | [diff] [blame] | 83 | bottom -= mClipBottomAmount; |
| 84 | } |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 85 | int left = 0; |
| 86 | int right = getWidth(); |
| 87 | if (mExpandAnimationRunning) { |
| 88 | left = (int) ((getWidth() - mActualWidth) / 2.0f); |
| 89 | right = (int) (left + mActualWidth); |
| 90 | } |
Tony Huang | c092c43 | 2018-05-18 17:38:54 +0800 | [diff] [blame] | 91 | if (mTopAmountRounded) { |
| 92 | int clipTop = (int) (mClipTopAmount - mDistanceToTopRoundness); |
| 93 | top += clipTop; |
| 94 | if (clipTop >= 0) { |
| 95 | bottom += clipTop; |
| 96 | } |
| 97 | } |
| 98 | drawable.setBounds(left, top, right, bottom); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 99 | drawable.draw(canvas); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | protected boolean verifyDrawable(Drawable who) { |
| 105 | return super.verifyDrawable(who) || who == mBackground; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | protected void drawableStateChanged() { |
Selim Cinek | fe24fb7 | 2018-02-13 14:34:55 -0800 | [diff] [blame] | 110 | setState(getDrawableState()); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 113 | @Override |
| 114 | public void drawableHotspotChanged(float x, float y) { |
| 115 | if (mBackground != null) { |
| 116 | mBackground.setHotspot(x, y); |
| 117 | } |
| 118 | } |
| 119 | |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 120 | /** |
| 121 | * Sets a background drawable. As we need to change our bounds independently of layout, we need |
| 122 | * the notion of a background independently of the regular View background.. |
| 123 | */ |
| 124 | public void setCustomBackground(Drawable background) { |
| 125 | if (mBackground != null) { |
| 126 | mBackground.setCallback(null); |
| 127 | unscheduleDrawable(mBackground); |
| 128 | } |
| 129 | mBackground = background; |
Selim Cinek | 0fe0739 | 2017-11-09 13:26:34 -0800 | [diff] [blame] | 130 | mBackground.mutate(); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 131 | if (mBackground != null) { |
| 132 | mBackground.setCallback(this); |
Selim Cinek | 588423a | 2017-08-17 16:42:51 -0700 | [diff] [blame] | 133 | setTint(mTintColor); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 134 | } |
Jorim Jaggi | b7303a3 | 2015-08-19 16:51:36 -0700 | [diff] [blame] | 135 | if (mBackground instanceof RippleDrawable) { |
| 136 | ((RippleDrawable) mBackground).setForceSoftware(true); |
| 137 | } |
Selim Cinek | 0fe0739 | 2017-11-09 13:26:34 -0800 | [diff] [blame] | 138 | updateBackgroundRadii(); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 139 | invalidate(); |
| 140 | } |
| 141 | |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 142 | public void setCustomBackground(int drawableResId) { |
| 143 | final Drawable d = mContext.getDrawable(drawableResId); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 144 | setCustomBackground(d); |
| 145 | } |
| 146 | |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 147 | public void setTint(int tintColor) { |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 148 | if (tintColor != 0) { |
| 149 | mBackground.setColorFilter(tintColor, PorterDuff.Mode.SRC_ATOP); |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 150 | } else { |
| 151 | mBackground.clearColorFilter(); |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 152 | } |
Selim Cinek | 588423a | 2017-08-17 16:42:51 -0700 | [diff] [blame] | 153 | mTintColor = tintColor; |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 154 | invalidate(); |
| 155 | } |
| 156 | |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 157 | public void setActualHeight(int actualHeight) { |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 158 | if (mExpandAnimationRunning) { |
| 159 | return; |
| 160 | } |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 161 | mActualHeight = actualHeight; |
| 162 | invalidate(); |
| 163 | } |
| 164 | |
| 165 | public int getActualHeight() { |
| 166 | return mActualHeight; |
| 167 | } |
| 168 | |
| 169 | public void setClipTopAmount(int clipTopAmount) { |
| 170 | mClipTopAmount = clipTopAmount; |
| 171 | invalidate(); |
| 172 | } |
| 173 | |
Selim Cinek | a686b2c | 2016-10-26 13:58:27 -0700 | [diff] [blame] | 174 | public void setClipBottomAmount(int clipBottomAmount) { |
| 175 | mClipBottomAmount = clipBottomAmount; |
| 176 | invalidate(); |
| 177 | } |
| 178 | |
Tony Huang | c092c43 | 2018-05-18 17:38:54 +0800 | [diff] [blame] | 179 | public void setDistanceToTopRoundness(float distanceToTopRoundness) { |
| 180 | if (distanceToTopRoundness != mDistanceToTopRoundness) { |
| 181 | mTopAmountRounded = distanceToTopRoundness >= 0; |
| 182 | mDistanceToTopRoundness = distanceToTopRoundness; |
| 183 | invalidate(); |
| 184 | } |
| 185 | } |
| 186 | |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 187 | @Override |
| 188 | public boolean hasOverlappingRendering() { |
| 189 | |
| 190 | // Prevents this view from creating a layer when alpha is animating. |
| 191 | return false; |
| 192 | } |
Selim Cinek | b2da91b | 2014-09-02 17:35:20 +0200 | [diff] [blame] | 193 | |
| 194 | public void setState(int[] drawableState) { |
Selim Cinek | fe24fb7 | 2018-02-13 14:34:55 -0800 | [diff] [blame] | 195 | if (mBackground != null && mBackground.isStateful()) { |
| 196 | if (!mIsPressedAllowed) { |
| 197 | drawableState = ArrayUtils.removeInt(drawableState, |
| 198 | com.android.internal.R.attr.state_pressed); |
| 199 | } |
| 200 | mBackground.setState(drawableState); |
| 201 | } |
Selim Cinek | b2da91b | 2014-09-02 17:35:20 +0200 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | public void setRippleColor(int color) { |
| 205 | if (mBackground instanceof RippleDrawable) { |
| 206 | RippleDrawable ripple = (RippleDrawable) mBackground; |
| 207 | ripple.setColor(ColorStateList.valueOf(color)); |
| 208 | } |
| 209 | } |
Selim Cinek | ec29d34 | 2017-05-05 18:31:49 -0700 | [diff] [blame] | 210 | |
| 211 | public void setDrawableAlpha(int drawableAlpha) { |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 212 | mDrawableAlpha = drawableAlpha; |
| 213 | if (mExpandAnimationRunning) { |
| 214 | return; |
| 215 | } |
Selim Cinek | ec29d34 | 2017-05-05 18:31:49 -0700 | [diff] [blame] | 216 | mBackground.setAlpha(drawableAlpha); |
| 217 | } |
Selim Cinek | 0fe0739 | 2017-11-09 13:26:34 -0800 | [diff] [blame] | 218 | |
| 219 | public void setRoundness(float topRoundness, float bottomRoundNess) { |
Tony Huang | c092c43 | 2018-05-18 17:38:54 +0800 | [diff] [blame] | 220 | if (topRoundness == mCornerRadii[0] && bottomRoundNess == mCornerRadii[4]) { |
| 221 | return; |
| 222 | } |
Selim Cinek | 515b203 | 2017-11-15 10:20:19 -0800 | [diff] [blame] | 223 | mBottomIsRounded = bottomRoundNess != 0.0f; |
Selim Cinek | 0fe0739 | 2017-11-09 13:26:34 -0800 | [diff] [blame] | 224 | mCornerRadii[0] = topRoundness; |
| 225 | mCornerRadii[1] = topRoundness; |
| 226 | mCornerRadii[2] = topRoundness; |
| 227 | mCornerRadii[3] = topRoundness; |
| 228 | mCornerRadii[4] = bottomRoundNess; |
| 229 | mCornerRadii[5] = bottomRoundNess; |
| 230 | mCornerRadii[6] = bottomRoundNess; |
| 231 | mCornerRadii[7] = bottomRoundNess; |
| 232 | updateBackgroundRadii(); |
| 233 | } |
| 234 | |
Selim Cinek | 2871bef | 2017-11-22 08:40:00 -0800 | [diff] [blame] | 235 | public void setBottomAmountClips(boolean clips) { |
| 236 | if (clips != mBottomAmountClips) { |
| 237 | mBottomAmountClips = clips; |
| 238 | invalidate(); |
| 239 | } |
| 240 | } |
Selim Cinek | 0fe0739 | 2017-11-09 13:26:34 -0800 | [diff] [blame] | 241 | |
| 242 | private void updateBackgroundRadii() { |
| 243 | if (mDontModifyCorners) { |
| 244 | return; |
| 245 | } |
| 246 | if (mBackground instanceof LayerDrawable) { |
| 247 | GradientDrawable gradientDrawable = |
| 248 | (GradientDrawable) ((LayerDrawable) mBackground).getDrawable(0); |
| 249 | gradientDrawable.setCornerRadii(mCornerRadii); |
| 250 | } |
| 251 | } |
| 252 | |
Selim Cinek | 515b203 | 2017-11-15 10:20:19 -0800 | [diff] [blame] | 253 | public void setBackgroundTop(int backgroundTop) { |
| 254 | mBackgroundTop = backgroundTop; |
| 255 | invalidate(); |
| 256 | } |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 257 | |
| 258 | public void setExpandAnimationParams(ActivityLaunchAnimator.ExpandAnimationParameters params) { |
| 259 | mActualHeight = params.getHeight(); |
| 260 | mActualWidth = params.getWidth(); |
| 261 | float alphaProgress = Interpolators.ALPHA_IN.getInterpolation( |
| 262 | params.getProgress( |
| 263 | ActivityLaunchAnimator.ANIMATION_DURATION_FADE_CONTENT /* delay */, |
| 264 | ActivityLaunchAnimator.ANIMATION_DURATION_FADE_APP /* duration */)); |
| 265 | mBackground.setAlpha((int) (mDrawableAlpha * (1.0f - alphaProgress))); |
| 266 | invalidate(); |
| 267 | } |
| 268 | |
| 269 | public void setExpandAnimationRunning(boolean running) { |
| 270 | mExpandAnimationRunning = running; |
| 271 | if (mBackground instanceof LayerDrawable) { |
| 272 | GradientDrawable gradientDrawable = |
| 273 | (GradientDrawable) ((LayerDrawable) mBackground).getDrawable(0); |
| 274 | gradientDrawable.setXfermode( |
| 275 | running ? new PorterDuffXfermode(PorterDuff.Mode.SRC) : null); |
Stan Iliev | d192911e | 2018-02-06 16:22:01 -0500 | [diff] [blame] | 276 | // Speed optimization: disable AA if transfer mode is not SRC_OVER. AA is not easy to |
| 277 | // spot during animation anyways. |
| 278 | gradientDrawable.setAntiAlias(!running); |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 279 | } |
| 280 | if (!mExpandAnimationRunning) { |
| 281 | setDrawableAlpha(mDrawableAlpha); |
| 282 | } |
| 283 | invalidate(); |
| 284 | } |
Selim Cinek | fe24fb7 | 2018-02-13 14:34:55 -0800 | [diff] [blame] | 285 | |
| 286 | public void setPressedAllowed(boolean allowed) { |
| 287 | mIsPressedAllowed = allowed; |
| 288 | } |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 289 | } |