blob: 81a99bc1e70c4b212ea22e9667d28b397611b99e [file] [log] [blame]
Jorim Jaggia8b48e12014-05-19 20:26:46 +02001/*
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
17package com.android.systemui.statusbar;
18
19import android.content.Context;
Selim Cinek697178b2014-07-02 19:40:30 +020020import android.content.res.ColorStateList;
Jorim Jaggia8b48e12014-05-19 20:26:46 +020021import android.graphics.Canvas;
Selim Cinek588423a2017-08-17 16:42:51 -070022import android.graphics.ColorFilter;
Jorim Jaggia8b48e12014-05-19 20:26:46 +020023import android.graphics.PorterDuff;
24import android.graphics.drawable.Drawable;
Selim Cinek697178b2014-07-02 19:40:30 +020025import android.graphics.drawable.RippleDrawable;
Jorim Jaggia8b48e12014-05-19 20:26:46 +020026import android.util.AttributeSet;
27import android.view.View;
28
29/**
30 * A view that can be used for both the dimmed and normal background of an notification.
31 */
32public class NotificationBackgroundView extends View {
33
34 private Drawable mBackground;
35 private int mClipTopAmount;
36 private int mActualHeight;
Selim Cineka686b2c2016-10-26 13:58:27 -070037 private int mClipBottomAmount;
Selim Cinek588423a2017-08-17 16:42:51 -070038 private int mTintColor;
Jorim Jaggia8b48e12014-05-19 20:26:46 +020039
40 public NotificationBackgroundView(Context context, AttributeSet attrs) {
41 super(context, attrs);
Jorim Jaggia8b48e12014-05-19 20:26:46 +020042 }
43
44 @Override
Jorim Jaggia8b48e12014-05-19 20:26:46 +020045 protected void onDraw(Canvas canvas) {
46 draw(canvas, mBackground);
47 }
48
49 private void draw(Canvas canvas, Drawable drawable) {
Selim Cineka686b2c2016-10-26 13:58:27 -070050 int bottom = mActualHeight - mClipBottomAmount;
51 if (drawable != null && bottom > mClipTopAmount) {
52 drawable.setBounds(0, mClipTopAmount, getWidth(), bottom);
Jorim Jaggia8b48e12014-05-19 20:26:46 +020053 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 Cinek697178b2014-07-02 19:40:30 +020073 @Override
74 public void drawableHotspotChanged(float x, float y) {
75 if (mBackground != null) {
76 mBackground.setHotspot(x, y);
77 }
78 }
79
Jorim Jaggia8b48e12014-05-19 20:26:46 +020080 /**
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 Cinek588423a2017-08-17 16:42:51 -070092 setTint(mTintColor);
Jorim Jaggia8b48e12014-05-19 20:26:46 +020093 }
Jorim Jaggib7303a32015-08-19 16:51:36 -070094 if (mBackground instanceof RippleDrawable) {
95 ((RippleDrawable) mBackground).setForceSoftware(true);
96 }
Jorim Jaggia8b48e12014-05-19 20:26:46 +020097 invalidate();
98 }
99
Selim Cinek697178b2014-07-02 19:40:30 +0200100 public void setCustomBackground(int drawableResId) {
101 final Drawable d = mContext.getDrawable(drawableResId);
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200102 setCustomBackground(d);
103 }
104
Selim Cinek697178b2014-07-02 19:40:30 +0200105 public void setTint(int tintColor) {
Selim Cinek697178b2014-07-02 19:40:30 +0200106 if (tintColor != 0) {
107 mBackground.setColorFilter(tintColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek697178b2014-07-02 19:40:30 +0200108 } else {
109 mBackground.clearColorFilter();
Selim Cinek697178b2014-07-02 19:40:30 +0200110 }
Selim Cinek588423a2017-08-17 16:42:51 -0700111 mTintColor = tintColor;
Selim Cinek697178b2014-07-02 19:40:30 +0200112 invalidate();
113 }
114
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200115 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 Cineka686b2c2016-10-26 13:58:27 -0700129 public void setClipBottomAmount(int clipBottomAmount) {
130 mClipBottomAmount = clipBottomAmount;
131 invalidate();
132 }
133
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200134 @Override
135 public boolean hasOverlappingRendering() {
136
137 // Prevents this view from creating a layer when alpha is animating.
138 return false;
139 }
Selim Cinekb2da91b2014-09-02 17:35:20 +0200140
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 Cinekec29d342017-05-05 18:31:49 -0700151
152 public void setDrawableAlpha(int drawableAlpha) {
153 mBackground.setAlpha(drawableAlpha);
154 }
Jorim Jaggia8b48e12014-05-19 20:26:46 +0200155}