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