Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 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; |
Lucas Dupin | 448786c | 2017-07-24 17:44:25 -0700 | [diff] [blame] | 25 | |
| 26 | import com.android.settingslib.Utils; |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 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 | |
| 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 Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 47 | } |
| 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 Dupin | 448786c | 2017-07-24 17:44:25 -0700 | [diff] [blame] | 51 | final Drawable[] layers; |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 52 | if (roundTop) { |
Lucas Dupin | 448786c | 2017-07-24 17:44:25 -0700 | [diff] [blame] | 53 | layers = new Drawable[]{ |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 54 | context.getDrawable(drawable).mutate(), |
| 55 | context.getDrawable(drawable).mutate(), |
| 56 | }; |
Lucas Dupin | 448786c | 2017-07-24 17:44:25 -0700 | [diff] [blame] | 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 | }; |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 63 | } |
Jason Chang | b4e879d | 2018-04-11 11:17:58 +0800 | [diff] [blame] | 64 | layers[1].setTintList(Utils.getColorAttr(context, android.R.attr.colorPrimary)); |
Lucas Dupin | 448786c | 2017-07-24 17:44:25 -0700 | [diff] [blame] | 65 | return layers; |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 66 | } |
| 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 | } |