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; |
Selim Cinek | 588423a | 2017-08-17 16:42:51 -0700 | [diff] [blame] | 22 | import android.graphics.ColorFilter; |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 23 | import android.graphics.PorterDuff; |
| 24 | import android.graphics.drawable.Drawable; |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 25 | import android.graphics.drawable.RippleDrawable; |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 26 | import android.util.AttributeSet; |
| 27 | import android.view.View; |
| 28 | |
| 29 | /** |
| 30 | * A view that can be used for both the dimmed and normal background of an notification. |
| 31 | */ |
| 32 | public class NotificationBackgroundView extends View { |
| 33 | |
| 34 | private Drawable mBackground; |
| 35 | private int mClipTopAmount; |
| 36 | private int mActualHeight; |
Selim Cinek | a686b2c | 2016-10-26 13:58:27 -0700 | [diff] [blame] | 37 | private int mClipBottomAmount; |
Selim Cinek | 588423a | 2017-08-17 16:42:51 -0700 | [diff] [blame] | 38 | private int mTintColor; |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 39 | |
| 40 | public NotificationBackgroundView(Context context, AttributeSet attrs) { |
| 41 | super(context, attrs); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | @Override |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 45 | protected void onDraw(Canvas canvas) { |
| 46 | draw(canvas, mBackground); |
| 47 | } |
| 48 | |
| 49 | private void draw(Canvas canvas, Drawable drawable) { |
Selim Cinek | a686b2c | 2016-10-26 13:58:27 -0700 | [diff] [blame] | 50 | int bottom = mActualHeight - mClipBottomAmount; |
| 51 | if (drawable != null && bottom > mClipTopAmount) { |
| 52 | drawable.setBounds(0, mClipTopAmount, getWidth(), bottom); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 53 | drawable.draw(canvas); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | protected boolean verifyDrawable(Drawable who) { |
| 59 | return super.verifyDrawable(who) || who == mBackground; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | protected void drawableStateChanged() { |
| 64 | drawableStateChanged(mBackground); |
| 65 | } |
| 66 | |
| 67 | private void drawableStateChanged(Drawable d) { |
| 68 | if (d != null && d.isStateful()) { |
| 69 | d.setState(getDrawableState()); |
| 70 | } |
| 71 | } |
| 72 | |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 73 | @Override |
| 74 | public void drawableHotspotChanged(float x, float y) { |
| 75 | if (mBackground != null) { |
| 76 | mBackground.setHotspot(x, y); |
| 77 | } |
| 78 | } |
| 79 | |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 80 | /** |
| 81 | * Sets a background drawable. As we need to change our bounds independently of layout, we need |
| 82 | * the notion of a background independently of the regular View background.. |
| 83 | */ |
| 84 | public void setCustomBackground(Drawable background) { |
| 85 | if (mBackground != null) { |
| 86 | mBackground.setCallback(null); |
| 87 | unscheduleDrawable(mBackground); |
| 88 | } |
| 89 | mBackground = background; |
| 90 | if (mBackground != null) { |
| 91 | mBackground.setCallback(this); |
Selim Cinek | 588423a | 2017-08-17 16:42:51 -0700 | [diff] [blame] | 92 | setTint(mTintColor); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 93 | } |
Jorim Jaggi | b7303a3 | 2015-08-19 16:51:36 -0700 | [diff] [blame] | 94 | if (mBackground instanceof RippleDrawable) { |
| 95 | ((RippleDrawable) mBackground).setForceSoftware(true); |
| 96 | } |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 97 | invalidate(); |
| 98 | } |
| 99 | |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 100 | public void setCustomBackground(int drawableResId) { |
| 101 | final Drawable d = mContext.getDrawable(drawableResId); |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 102 | setCustomBackground(d); |
| 103 | } |
| 104 | |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 105 | public void setTint(int tintColor) { |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 106 | if (tintColor != 0) { |
| 107 | mBackground.setColorFilter(tintColor, PorterDuff.Mode.SRC_ATOP); |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 108 | } else { |
| 109 | mBackground.clearColorFilter(); |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 110 | } |
Selim Cinek | 588423a | 2017-08-17 16:42:51 -0700 | [diff] [blame] | 111 | mTintColor = tintColor; |
Selim Cinek | 697178b | 2014-07-02 19:40:30 +0200 | [diff] [blame] | 112 | invalidate(); |
| 113 | } |
| 114 | |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 115 | public void setActualHeight(int actualHeight) { |
| 116 | mActualHeight = actualHeight; |
| 117 | invalidate(); |
| 118 | } |
| 119 | |
| 120 | public int getActualHeight() { |
| 121 | return mActualHeight; |
| 122 | } |
| 123 | |
| 124 | public void setClipTopAmount(int clipTopAmount) { |
| 125 | mClipTopAmount = clipTopAmount; |
| 126 | invalidate(); |
| 127 | } |
| 128 | |
Selim Cinek | a686b2c | 2016-10-26 13:58:27 -0700 | [diff] [blame] | 129 | public void setClipBottomAmount(int clipBottomAmount) { |
| 130 | mClipBottomAmount = clipBottomAmount; |
| 131 | invalidate(); |
| 132 | } |
| 133 | |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 134 | @Override |
| 135 | public boolean hasOverlappingRendering() { |
| 136 | |
| 137 | // Prevents this view from creating a layer when alpha is animating. |
| 138 | return false; |
| 139 | } |
Selim Cinek | b2da91b | 2014-09-02 17:35:20 +0200 | [diff] [blame] | 140 | |
| 141 | public void setState(int[] drawableState) { |
| 142 | mBackground.setState(drawableState); |
| 143 | } |
| 144 | |
| 145 | public void setRippleColor(int color) { |
| 146 | if (mBackground instanceof RippleDrawable) { |
| 147 | RippleDrawable ripple = (RippleDrawable) mBackground; |
| 148 | ripple.setColor(ColorStateList.valueOf(color)); |
| 149 | } |
| 150 | } |
Selim Cinek | ec29d34 | 2017-05-05 18:31:49 -0700 | [diff] [blame] | 151 | |
| 152 | public void setDrawableAlpha(int drawableAlpha) { |
| 153 | mBackground.setAlpha(drawableAlpha); |
| 154 | } |
Jorim Jaggi | a8b48e1 | 2014-05-19 20:26:46 +0200 | [diff] [blame] | 155 | } |