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