1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.systemui;
16 
17 import android.content.Context;
18 import android.graphics.Canvas;
19 import android.graphics.ColorFilter;
20 import android.graphics.Paint;
21 import android.graphics.PixelFormat;
22 import android.graphics.Rect;
23 import android.graphics.drawable.Drawable;
24 import android.graphics.drawable.LayerDrawable;
25 
26 import com.android.settingslib.Utils;
27 import com.android.systemui.res.R;
28 
29 public class HardwareBgDrawable extends LayerDrawable {
30 
31     private final Paint mPaint = new Paint();
32     private final Drawable[] mLayers;
33     private final boolean mRoundTop;
34     private int mPoint;
35     private boolean mRotatedBackground;
36 
HardwareBgDrawable(boolean roundTop, boolean roundEnd, Context context)37     public HardwareBgDrawable(boolean roundTop, boolean roundEnd, Context context) {
38         this(roundTop, getLayers(context, roundTop, roundEnd));
39     }
40 
HardwareBgDrawable(boolean roundTop, Drawable[] layers)41     public HardwareBgDrawable(boolean roundTop, Drawable[] layers) {
42         super(layers);
43         if (layers.length != 2) {
44             throw new IllegalArgumentException("Need 2 layers");
45         }
46         mRoundTop = roundTop;
47         mLayers = layers;
48     }
49 
getLayers(Context context, boolean roundTop, boolean roundEnd)50     private static Drawable[] getLayers(Context context, boolean roundTop, boolean roundEnd) {
51         int drawable = roundEnd ? R.drawable.rounded_bg_full : R.drawable.rounded_bg;
52         final Drawable[] layers;
53         if (roundTop) {
54             layers = new Drawable[]{
55                     context.getDrawable(drawable).mutate(),
56                     context.getDrawable(drawable).mutate(),
57             };
58         } else {
59             layers = new Drawable[]{
60                     context.getDrawable(drawable).mutate(),
61                     context.getDrawable(roundEnd ? R.drawable.rounded_full_bg_bottom
62                             : R.drawable.rounded_bg_bottom).mutate(),
63             };
64         }
65         layers[1].setTintList(Utils.getColorAttr(context, android.R.attr.colorPrimary));
66         return layers;
67     }
68 
setCutPoint(int point)69     public void setCutPoint(int point) {
70         mPoint = point;
71         invalidateSelf();
72     }
73 
getCutPoint()74     public int getCutPoint() {
75         return mPoint;
76     }
77 
78     @Override
draw(Canvas canvas)79     public void draw(Canvas canvas) {
80         if (mPoint >= 0 && !mRotatedBackground) {
81             Rect bounds = getBounds();
82             int top = bounds.top + mPoint;
83             if (top > bounds.bottom) top = bounds.bottom;
84             if (mRoundTop) {
85                 mLayers[0].setBounds(bounds.left, bounds.top, bounds.right, top);
86             } else {
87                 mLayers[1].setBounds(bounds.left, top, bounds.right, bounds.bottom);
88             }
89             if (mRoundTop) {
90                 mLayers[1].draw(canvas);
91                 mLayers[0].draw(canvas);
92             } else {
93                 mLayers[0].draw(canvas);
94                 mLayers[1].draw(canvas);
95             }
96         } else {
97             mLayers[0].draw(canvas);
98         }
99     }
100 
101     @Override
setAlpha(int alpha)102     public void setAlpha(int alpha) {
103         mPaint.setAlpha(alpha);
104     }
105 
106     @Override
setColorFilter(ColorFilter colorFilter)107     public void setColorFilter(ColorFilter colorFilter) {
108         mPaint.setColorFilter(colorFilter);
109     }
110 
111     @Override
getOpacity()112     public int getOpacity() {
113         return PixelFormat.OPAQUE;
114     }
115 
setRotatedBackground(boolean rotatedBackground)116     public void setRotatedBackground(boolean rotatedBackground) {
117         mRotatedBackground = rotatedBackground;
118     }
119 }