blob: 97d799909ed9fd0f804ca91b9911ec243adc9c68 [file] [log] [blame]
Jason Monk16fbd9d2017-04-27 14:28:49 -04001/*
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
15package com.android.systemui;
16
17import android.content.Context;
18import android.graphics.Canvas;
19import android.graphics.ColorFilter;
20import android.graphics.Paint;
21import android.graphics.PixelFormat;
22import android.graphics.Rect;
23import android.graphics.drawable.Drawable;
24import android.graphics.drawable.LayerDrawable;
Lucas Dupin448786c2017-07-24 17:44:25 -070025
26import com.android.settingslib.Utils;
Jason Monk16fbd9d2017-04-27 14:28:49 -040027
28public 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
36 public HardwareBgDrawable(boolean roundTop, boolean roundEnd, Context context) {
37 this(roundTop, getLayers(context, roundTop, roundEnd));
38 }
39
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;
Jason Monk16fbd9d2017-04-27 14:28:49 -040047 }
48
49 private static Drawable[] getLayers(Context context, boolean roundTop, boolean roundEnd) {
50 int drawable = roundEnd ? R.drawable.rounded_bg_full : R.drawable.rounded_bg;
Lucas Dupin448786c2017-07-24 17:44:25 -070051 final Drawable[] layers;
Jason Monk16fbd9d2017-04-27 14:28:49 -040052 if (roundTop) {
Lucas Dupin448786c2017-07-24 17:44:25 -070053 layers = new Drawable[]{
Jason Monk16fbd9d2017-04-27 14:28:49 -040054 context.getDrawable(drawable).mutate(),
55 context.getDrawable(drawable).mutate(),
56 };
Lucas Dupin448786c2017-07-24 17:44:25 -070057 } 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 };
Jason Monk16fbd9d2017-04-27 14:28:49 -040063 }
Jason Changb4e879d2018-04-11 11:17:58 +080064 layers[1].setTintList(Utils.getColorAttr(context, android.R.attr.colorPrimary));
Lucas Dupin448786c2017-07-24 17:44:25 -070065 return layers;
Jason Monk16fbd9d2017-04-27 14:28:49 -040066 }
67
68 public void setCutPoint(int point) {
69 mPoint = point;
70 invalidateSelf();
71 }
72
73 public int getCutPoint() {
74 return mPoint;
75 }
76
77 @Override
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
101 public void setAlpha(int alpha) {
102 mPaint.setAlpha(alpha);
103 }
104
105 @Override
106 public void setColorFilter(ColorFilter colorFilter) {
107 mPaint.setColorFilter(colorFilter);
108 }
109
110 @Override
111 public int getOpacity() {
112 return PixelFormat.OPAQUE;
113 }
114
115 public void setRotatedBackground(boolean rotatedBackground) {
116 mRotatedBackground = rotatedBackground;
117 }
118}