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
17package com.android.systemui.statusbar;
18
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.ValueAnimator;
22import android.content.Context;
23import android.graphics.Canvas;
24import android.graphics.Color;
25import android.graphics.PorterDuff;
26import android.util.AttributeSet;
27import android.view.View;
28import android.view.animation.Interpolator;
29
30/**
31 * A view which can draw a scrim
32 */
33public class ScrimView extends View
34{
35    private int mScrimColor;
36    private boolean mIsEmpty = true;
37    private boolean mDrawAsSrc;
38    private float mViewAlpha = 1.0f;
39    private ValueAnimator mAlphaAnimator;
40    private ValueAnimator.AnimatorUpdateListener mAlphaUpdateListener
41            = new ValueAnimator.AnimatorUpdateListener() {
42        @Override
43        public void onAnimationUpdate(ValueAnimator animation) {
44            mViewAlpha = (float) animation.getAnimatedValue();
45            invalidate();
46        }
47    };
48    private AnimatorListenerAdapter mClearAnimatorListener = new AnimatorListenerAdapter() {
49        @Override
50        public void onAnimationEnd(Animator animation) {
51            mAlphaAnimator = null;
52        }
53    };
54
55    public ScrimView(Context context) {
56        this(context, null);
57    }
58
59    public ScrimView(Context context, AttributeSet attrs) {
60        this(context, attrs, 0);
61    }
62
63    public ScrimView(Context context, AttributeSet attrs, int defStyleAttr) {
64        this(context, attrs, defStyleAttr, 0);
65    }
66
67    public ScrimView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
68        super(context, attrs, defStyleAttr, defStyleRes);
69    }
70
71    @Override
72    protected void onDraw(Canvas canvas) {
73        if (mDrawAsSrc || (!mIsEmpty && mViewAlpha > 0f)) {
74            PorterDuff.Mode mode = mDrawAsSrc ? PorterDuff.Mode.SRC : PorterDuff.Mode.SRC_OVER;
75            int color = mScrimColor;
76            color = Color.argb((int) (Color.alpha(color) * mViewAlpha), Color.red(color),
77                    Color.green(color), Color.blue(color));
78            canvas.drawColor(color, mode);
79        }
80    }
81
82    public void setDrawAsSrc(boolean asSrc) {
83        mDrawAsSrc = asSrc;
84        invalidate();
85    }
86
87    public void setScrimColor(int color) {
88        if (color != mScrimColor) {
89            mIsEmpty = Color.alpha(color) == 0;
90            mScrimColor = color;
91            invalidate();
92        }
93    }
94
95    public int getScrimColor() {
96        return mScrimColor;
97    }
98
99    @Override
100    public boolean hasOverlappingRendering() {
101        return false;
102    }
103
104    public void setViewAlpha(float alpha) {
105        if (mAlphaAnimator != null) {
106            mAlphaAnimator.cancel();
107        }
108        mViewAlpha = alpha;
109        invalidate();
110    }
111
112    public void animateViewAlpha(float alpha, long durationOut, Interpolator interpolator) {
113        if (mAlphaAnimator != null) {
114            mAlphaAnimator.cancel();
115        }
116        mAlphaAnimator = ValueAnimator.ofFloat(mViewAlpha, alpha);
117        mAlphaAnimator.addUpdateListener(mAlphaUpdateListener);
118        mAlphaAnimator.addListener(mClearAnimatorListener);
119        mAlphaAnimator.setInterpolator(interpolator);
120        mAlphaAnimator.setDuration(durationOut);
121        mAlphaAnimator.start();
122    }
123}
124