• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
20 import android.content.res.ColorStateList;
21 import android.graphics.Canvas;
22 import android.graphics.PorterDuff;
23 import android.graphics.drawable.Drawable;
24 import android.graphics.drawable.RippleDrawable;
25 import android.util.AttributeSet;
26 import android.view.MotionEvent;
27 import android.view.View;
28 import com.android.systemui.R;
29 
30 /**
31  * A view that can be used for both the dimmed and normal background of an notification.
32  */
33 public class NotificationBackgroundView extends View {
34 
35     private Drawable mBackground;
36     private int mClipTopAmount;
37     private int mActualHeight;
38 
NotificationBackgroundView(Context context, AttributeSet attrs)39     public NotificationBackgroundView(Context context, AttributeSet attrs) {
40         super(context, attrs);
41     }
42 
43     @Override
onDraw(Canvas canvas)44     protected void onDraw(Canvas canvas) {
45         draw(canvas, mBackground);
46     }
47 
draw(Canvas canvas, Drawable drawable)48     private void draw(Canvas canvas, Drawable drawable) {
49         if (drawable != null) {
50             drawable.setBounds(0, mClipTopAmount, getWidth(), mActualHeight);
51             drawable.draw(canvas);
52         }
53     }
54 
55     @Override
verifyDrawable(Drawable who)56     protected boolean verifyDrawable(Drawable who) {
57         return super.verifyDrawable(who) || who == mBackground;
58     }
59 
60     @Override
drawableStateChanged()61     protected void drawableStateChanged() {
62         drawableStateChanged(mBackground);
63     }
64 
drawableStateChanged(Drawable d)65     private void drawableStateChanged(Drawable d) {
66         if (d != null && d.isStateful()) {
67             d.setState(getDrawableState());
68         }
69     }
70 
71     @Override
drawableHotspotChanged(float x, float y)72     public void drawableHotspotChanged(float x, float y) {
73         if (mBackground != null) {
74             mBackground.setHotspot(x, y);
75         }
76     }
77 
78     /**
79      * Sets a background drawable. As we need to change our bounds independently of layout, we need
80      * the notion of a background independently of the regular View background..
81      */
setCustomBackground(Drawable background)82     public void setCustomBackground(Drawable background) {
83         if (mBackground != null) {
84             mBackground.setCallback(null);
85             unscheduleDrawable(mBackground);
86         }
87         mBackground = background;
88         if (mBackground != null) {
89             mBackground.setCallback(this);
90         }
91         invalidate();
92     }
93 
setCustomBackground(int drawableResId)94     public void setCustomBackground(int drawableResId) {
95         final Drawable d = mContext.getDrawable(drawableResId);
96         setCustomBackground(d);
97     }
98 
setTint(int tintColor)99     public void setTint(int tintColor) {
100         if (tintColor != 0) {
101             mBackground.setColorFilter(tintColor, PorterDuff.Mode.SRC_ATOP);
102         } else {
103             mBackground.clearColorFilter();
104         }
105         invalidate();
106     }
107 
setActualHeight(int actualHeight)108     public void setActualHeight(int actualHeight) {
109         mActualHeight = actualHeight;
110         invalidate();
111     }
112 
getActualHeight()113     public int getActualHeight() {
114         return mActualHeight;
115     }
116 
setClipTopAmount(int clipTopAmount)117     public void setClipTopAmount(int clipTopAmount) {
118         mClipTopAmount = clipTopAmount;
119         invalidate();
120     }
121 
122     @Override
hasOverlappingRendering()123     public boolean hasOverlappingRendering() {
124 
125         // Prevents this view from creating a layer when alpha is animating.
126         return false;
127     }
128 
setState(int[] drawableState)129     public void setState(int[] drawableState) {
130         mBackground.setState(drawableState);
131     }
132 
setRippleColor(int color)133     public void setRippleColor(int color) {
134         if (mBackground instanceof RippleDrawable) {
135             RippleDrawable ripple = (RippleDrawable) mBackground;
136             ripple.setColor(ColorStateList.valueOf(color));
137         }
138     }
139 }
140