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; |
| 23 | import android.graphics.drawable.Drawable; |
Selim Cinek | 0fe0739 | 2017-11-09 13:26:34 -0800 | [diff] [blame] | 24 | import android.graphics.drawable.GradientDrawable; |
| 25 | import android.graphics.drawable.LayerDrawable; |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 26 | import android.graphics.drawable.RippleDrawable; |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 27 | import android.util.AttributeSet; |
| 28 | import android.view.View; |
| 29 | |
Selim Cinek | 0fe0739 | 2017-11-09 13:26:34 -0800 | [diff] [blame] | 30 | import com.android.systemui.R; |
| 31 | |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 32 | /** |
| 33 | * A view that can be used for both the dimmed and normal background of an notification. |
| 34 | */ |
| 35 | public class NotificationBackgroundView extends View { |
| 36 | |
Selim Cinek | 0fe0739 | 2017-11-09 13:26:34 -0800 | [diff] [blame] | 37 | private final boolean mDontModifyCorners; |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 38 | private Drawable mBackground; |
| 39 | private int mClipTopAmount; |
| 40 | private int mActualHeight; |
Selim Cinek | a686b2c | 2016-10-26 13:58:27 -0700 | [diff] [blame] | 41 | private int mClipBottomAmount; |
Selim Cinek | 588423a | 2017-08-17 16:42:51 -0700 | [diff] [blame] | 42 | private int mTintColor; |
Selim Cinek | 0fe0739 | 2017-11-09 13:26:34 -0800 | [diff] [blame] | 43 | private float[] mCornerRadii = new float[8]; |
Selim Cinek | 515b203 | 2017-11-15 10:20:19 -0800 | [diff] [blame] | 44 | private boolean mBottomIsRounded; |
| 45 | private int mBackgroundTop; |
Selim Cinek | 2871bef | 2017-11-22 08:40:00 -0800 | [diff] [blame] | 46 | private boolean mBottomAmountClips = true; |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 47 | |
| 48 | public NotificationBackgroundView(Context context, AttributeSet attrs) { |
| 49 | super(context, attrs); |
Selim Cinek | 0fe0739 | 2017-11-09 13:26:34 -0800 | [diff] [blame] | 50 | mDontModifyCorners = getResources().getBoolean( |
| 51 | R.bool.config_clipNotificationsToOutline); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 55 | protected void onDraw(Canvas canvas) { |
Selim Cinek | 515b203 | 2017-11-15 10:20:19 -0800 | [diff] [blame] | 56 | if (mClipTopAmount + mClipBottomAmount < mActualHeight - mBackgroundTop) { |
| 57 | canvas.save(); |
| 58 | canvas.clipRect(0, mClipTopAmount, getWidth(), mActualHeight - mClipBottomAmount); |
| 59 | draw(canvas, mBackground); |
| 60 | canvas.restore(); |
| 61 | } |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | private void draw(Canvas canvas, Drawable drawable) { |
Selim Cinek | 515b203 | 2017-11-15 10:20:19 -0800 | [diff] [blame] | 65 | if (drawable != null) { |
| 66 | int bottom = mActualHeight; |
Selim Cinek | 2871bef | 2017-11-22 08:40:00 -0800 | [diff] [blame] | 67 | if (mBottomIsRounded && mBottomAmountClips) { |
Selim Cinek | 515b203 | 2017-11-15 10:20:19 -0800 | [diff] [blame] | 68 | bottom -= mClipBottomAmount; |
| 69 | } |
Selim Cinek | b95fd18 | 2017-12-21 13:03:32 -0800 | [diff] [blame] | 70 | drawable.setBounds(0, mBackgroundTop, getWidth(), bottom); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 71 | drawable.draw(canvas); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | protected boolean verifyDrawable(Drawable who) { |
| 77 | return super.verifyDrawable(who) || who == mBackground; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | protected void drawableStateChanged() { |
| 82 | drawableStateChanged(mBackground); |
| 83 | } |
| 84 | |
| 85 | private void drawableStateChanged(Drawable d) { |
| 86 | if (d != null && d.isStateful()) { |
| 87 | d.setState(getDrawableState()); |
| 88 | } |
| 89 | } |
| 90 | |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 91 | @Override |
| 92 | public void drawableHotspotChanged(float x, float y) { |
| 93 | if (mBackground != null) { |
| 94 | mBackground.setHotspot(x, y); |
| 95 | } |
| 96 | } |
| 97 | |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 98 | /** |
| 99 | * Sets a background drawable. As we need to change our bounds independently of layout, we need |
| 100 | * the notion of a background independently of the regular View background.. |
| 101 | */ |
| 102 | public void setCustomBackground(Drawable background) { |
| 103 | if (mBackground != null) { |
| 104 | mBackground.setCallback(null); |
| 105 | unscheduleDrawable(mBackground); |
| 106 | } |
| 107 | mBackground = background; |
Selim Cinek | 0fe0739 | 2017-11-09 13:26:34 -0800 | [diff] [blame] | 108 | mBackground.mutate(); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 109 | if (mBackground != null) { |
| 110 | mBackground.setCallback(this); |
Selim Cinek | 588423a | 2017-08-17 16:42:51 -0700 | [diff] [blame] | 111 | setTint(mTintColor); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 112 | } |
Jorim Jaggi | b7303a3 | 2015-08-19 16:51:36 -0700 | [diff] [blame] | 113 | if (mBackground instanceof RippleDrawable) { |
| 114 | ((RippleDrawable) mBackground).setForceSoftware(true); |
| 115 | } |
Selim Cinek | 0fe0739 | 2017-11-09 13:26:34 -0800 | [diff] [blame] | 116 | updateBackgroundRadii(); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 117 | invalidate(); |
| 118 | } |
| 119 | |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 120 | public void setCustomBackground(int drawableResId) { |
| 121 | final Drawable d = mContext.getDrawable(drawableResId); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 122 | setCustomBackground(d); |
| 123 | } |
| 124 | |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 125 | public void setTint(int tintColor) { |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 126 | if (tintColor != 0) { |
| 127 | mBackground.setColorFilter(tintColor, PorterDuff.Mode.SRC_ATOP); |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 128 | } else { |
| 129 | mBackground.clearColorFilter(); |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 130 | } |
Selim Cinek | 588423a | 2017-08-17 16:42:51 -0700 | [diff] [blame] | 131 | mTintColor = tintColor; |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 132 | invalidate(); |
| 133 | } |
| 134 | |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 135 | public void setActualHeight(int actualHeight) { |
| 136 | mActualHeight = actualHeight; |
| 137 | invalidate(); |
| 138 | } |
| 139 | |
| 140 | public int getActualHeight() { |
| 141 | return mActualHeight; |
| 142 | } |
| 143 | |
| 144 | public void setClipTopAmount(int clipTopAmount) { |
| 145 | mClipTopAmount = clipTopAmount; |
| 146 | invalidate(); |
| 147 | } |
| 148 | |
Selim Cinek | a686b2c | 2016-10-26 13:58:27 -0700 | [diff] [blame] | 149 | public void setClipBottomAmount(int clipBottomAmount) { |
| 150 | mClipBottomAmount = clipBottomAmount; |
| 151 | invalidate(); |
| 152 | } |
| 153 | |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 154 | @Override |
| 155 | public boolean hasOverlappingRendering() { |
| 156 | |
| 157 | // Prevents this view from creating a layer when alpha is animating. |
| 158 | return false; |
| 159 | } |
Selim Cinek | b2da91b | 2014-09-02 17:35:20 +0200 | [diff] [blame] | 160 | |
| 161 | public void setState(int[] drawableState) { |
| 162 | mBackground.setState(drawableState); |
| 163 | } |
| 164 | |
| 165 | public void setRippleColor(int color) { |
| 166 | if (mBackground instanceof RippleDrawable) { |
| 167 | RippleDrawable ripple = (RippleDrawable) mBackground; |
| 168 | ripple.setColor(ColorStateList.valueOf(color)); |
| 169 | } |
| 170 | } |
Selim Cinek | ec29d34 | 2017-05-05 18:31:49 -0700 | [diff] [blame] | 171 | |
| 172 | public void setDrawableAlpha(int drawableAlpha) { |
| 173 | mBackground.setAlpha(drawableAlpha); |
| 174 | } |
Selim Cinek | 0fe0739 | 2017-11-09 13:26:34 -0800 | [diff] [blame] | 175 | |
| 176 | public void setRoundness(float topRoundness, float bottomRoundNess) { |
Selim Cinek | 515b203 | 2017-11-15 10:20:19 -0800 | [diff] [blame] | 177 | mBottomIsRounded = bottomRoundNess != 0.0f; |
Selim Cinek | 0fe0739 | 2017-11-09 13:26:34 -0800 | [diff] [blame] | 178 | mCornerRadii[0] = topRoundness; |
| 179 | mCornerRadii[1] = topRoundness; |
| 180 | mCornerRadii[2] = topRoundness; |
| 181 | mCornerRadii[3] = topRoundness; |
| 182 | mCornerRadii[4] = bottomRoundNess; |
| 183 | mCornerRadii[5] = bottomRoundNess; |
| 184 | mCornerRadii[6] = bottomRoundNess; |
| 185 | mCornerRadii[7] = bottomRoundNess; |
| 186 | updateBackgroundRadii(); |
| 187 | } |
| 188 | |
Selim Cinek | 2871bef | 2017-11-22 08:40:00 -0800 | [diff] [blame] | 189 | public void setBottomAmountClips(boolean clips) { |
| 190 | if (clips != mBottomAmountClips) { |
| 191 | mBottomAmountClips = clips; |
| 192 | invalidate(); |
| 193 | } |
| 194 | } |
Selim Cinek | 0fe0739 | 2017-11-09 13:26:34 -0800 | [diff] [blame] | 195 | |
| 196 | private void updateBackgroundRadii() { |
| 197 | if (mDontModifyCorners) { |
| 198 | return; |
| 199 | } |
| 200 | if (mBackground instanceof LayerDrawable) { |
| 201 | GradientDrawable gradientDrawable = |
| 202 | (GradientDrawable) ((LayerDrawable) mBackground).getDrawable(0); |
| 203 | gradientDrawable.setCornerRadii(mCornerRadii); |
| 204 | } |
| 205 | } |
| 206 | |
Selim Cinek | 515b203 | 2017-11-15 10:20:19 -0800 | [diff] [blame] | 207 | public void setBackgroundTop(int backgroundTop) { |
| 208 | mBackgroundTop = backgroundTop; |
| 209 | invalidate(); |
| 210 | } |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 211 | } |