• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28 public class HardwareBgDrawable extends LayerDrawable {
29 
30     private final Paint mPaint = new Paint();
31     private final Drawable[] mLayers;
32     private final boolean mRoundTop;
33     private int mPoint;
34     private boolean mRotatedBackground;
35 
HardwareBgDrawable(boolean roundTop, boolean roundEnd, Context context)36     public HardwareBgDrawable(boolean roundTop, boolean roundEnd, Context context) {
37         this(roundTop, getLayers(context, roundTop, roundEnd));
38     }
39 
HardwareBgDrawable(boolean roundTop, Drawable[] layers)40     public HardwareBgDrawable(boolean roundTop, Drawable[] layers) {
41         super(layers);
42         if (layers.length != 2) {
43             throw new IllegalArgumentException("Need 2 layers");
44         }
45         mRoundTop = roundTop;
46         mLayers = layers;
47     }
48 
getLayers(Context context, boolean roundTop, boolean roundEnd)49     private static Drawable[] getLayers(Context context, boolean roundTop, boolean roundEnd) {
50         int drawable = roundEnd ? R.drawable.rounded_bg_full : R.drawable.rounded_bg;
51         final Drawable[] layers;
52         if (roundTop) {
53             layers = new Drawable[]{
54                     context.getDrawable(drawable).mutate(),
55                     context.getDrawable(drawable).mutate(),
56             };
57         } else {
58             layers = new Drawable[]{
59                     context.getDrawable(drawable).mutate(),
60                     context.getDrawable(roundEnd ? R.drawable.rounded_full_bg_bottom
61                             : R.drawable.rounded_bg_bottom).mutate(),
62             };
63         }
64         layers[1].setTintList(Utils.getColorAttr(context, android.R.attr.colorPrimary));
65         return layers;
66     }
67 
setCutPoint(int point)68     public void setCutPoint(int point) {
69         mPoint = point;
70         invalidateSelf();
71     }
72 
getCutPoint()73     public int getCutPoint() {
74         return mPoint;
75     }
76 
77     @Override
draw(Canvas canvas)78     public void draw(Canvas canvas) {
79         if (mPoint >= 0 && !mRotatedBackground) {
80             Rect bounds = getBounds();
81             int top = bounds.top + mPoint;
82             if (top > bounds.bottom) top = bounds.bottom;
83             if (mRoundTop) {
84                 mLayers[0].setBounds(bounds.left, bounds.top, bounds.right, top);
85             } else {
86                 mLayers[1].setBounds(bounds.left, top, bounds.right, bounds.bottom);
87             }
88             if (mRoundTop) {
89                 mLayers[1].draw(canvas);
90                 mLayers[0].draw(canvas);
91             } else {
92                 mLayers[0].draw(canvas);
93                 mLayers[1].draw(canvas);
94             }
95         } else {
96             mLayers[0].draw(canvas);
97         }
98     }
99 
100     @Override
setAlpha(int alpha)101     public void setAlpha(int alpha) {
102         mPaint.setAlpha(alpha);
103     }
104 
105     @Override
setColorFilter(ColorFilter colorFilter)106     public void setColorFilter(ColorFilter colorFilter) {
107         mPaint.setColorFilter(colorFilter);
108     }
109 
110     @Override
getOpacity()111     public int getOpacity() {
112         return PixelFormat.OPAQUE;
113     }
114 
setRotatedBackground(boolean rotatedBackground)115     public void setRotatedBackground(boolean rotatedBackground) {
116         mRotatedBackground = rotatedBackground;
117     }
118 }