Beth Thibodeau | 8bfbf1f | 2020-02-13 13:55:37 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | package com.android.systemui.statusbar; |
| 17 | |
| 18 | import android.annotation.NonNull; |
| 19 | import android.annotation.Nullable; |
| 20 | import android.content.res.Resources; |
| 21 | import android.graphics.Canvas; |
| 22 | import android.graphics.Color; |
| 23 | import android.graphics.Paint; |
| 24 | import android.graphics.Rect; |
| 25 | import android.graphics.drawable.Drawable; |
| 26 | import android.graphics.drawable.DrawableWrapper; |
| 27 | import android.util.AttributeSet; |
| 28 | |
| 29 | import com.android.systemui.R; |
| 30 | |
| 31 | import org.xmlpull.v1.XmlPullParser; |
| 32 | import org.xmlpull.v1.XmlPullParserException; |
| 33 | |
| 34 | import java.io.IOException; |
| 35 | |
| 36 | /** |
| 37 | * The screen record drawable draws a colored background and either a countdown or circle to |
| 38 | * indicate that the screen is being recorded. |
| 39 | */ |
| 40 | public class ScreenRecordDrawable extends DrawableWrapper { |
| 41 | private Drawable mFillDrawable; |
| 42 | private int mHorizontalPadding; |
| 43 | private int mLevel; |
| 44 | private float mTextSize; |
| 45 | private float mIconRadius; |
| 46 | private Paint mPaint; |
| 47 | |
| 48 | /** No-arg constructor used by drawable inflation. */ |
| 49 | public ScreenRecordDrawable() { |
| 50 | super(null); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public void inflate(@NonNull Resources r, @NonNull XmlPullParser parser, |
| 55 | @NonNull AttributeSet attrs, @Nullable Resources.Theme theme) |
| 56 | throws XmlPullParserException, IOException { |
| 57 | super.inflate(r, parser, attrs, theme); |
| 58 | setDrawable(r.getDrawable(R.drawable.ic_screen_record_background, theme).mutate()); |
| 59 | mFillDrawable = r.getDrawable(R.drawable.ic_screen_record_background, theme).mutate(); |
| 60 | mHorizontalPadding = r.getDimensionPixelSize(R.dimen.status_bar_horizontal_padding); |
| 61 | |
| 62 | mTextSize = r.getDimensionPixelSize(R.dimen.screenrecord_status_text_size); |
| 63 | mIconRadius = r.getDimensionPixelSize(R.dimen.screenrecord_status_icon_radius); |
| 64 | mLevel = attrs.getAttributeIntValue(null, "level", 0); |
| 65 | |
| 66 | mPaint = new Paint(); |
| 67 | mPaint.setTextAlign(Paint.Align.CENTER); |
| 68 | mPaint.setColor(Color.WHITE); |
| 69 | mPaint.setTextSize(mTextSize); |
| 70 | mPaint.setFakeBoldText(true); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public boolean canApplyTheme() { |
| 75 | return mFillDrawable.canApplyTheme() || super.canApplyTheme(); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void applyTheme(Resources.Theme t) { |
| 80 | super.applyTheme(t); |
| 81 | mFillDrawable.applyTheme(t); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | protected void onBoundsChange(Rect bounds) { |
| 86 | super.onBoundsChange(bounds); |
| 87 | mFillDrawable.setBounds(bounds); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public boolean onLayoutDirectionChanged(int layoutDirection) { |
| 92 | mFillDrawable.setLayoutDirection(layoutDirection); |
| 93 | return super.onLayoutDirectionChanged(layoutDirection); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void draw(Canvas canvas) { |
| 98 | super.draw(canvas); |
| 99 | mFillDrawable.draw(canvas); |
| 100 | |
| 101 | Rect b = mFillDrawable.getBounds(); |
| 102 | if (mLevel > 0) { |
| 103 | String val = String.valueOf(mLevel); |
| 104 | Rect textBounds = new Rect(); |
| 105 | mPaint.getTextBounds(val, 0, val.length(), textBounds); |
| 106 | float yOffset = textBounds.height() / 4; // half, and half again since it's centered |
| 107 | canvas.drawText(val, b.centerX(), b.centerY() + yOffset, mPaint); |
| 108 | } else { |
| 109 | canvas.drawCircle(b.centerX(), b.centerY() - mIconRadius / 2, mIconRadius, mPaint); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public boolean getPadding(Rect padding) { |
| 115 | padding.left += mHorizontalPadding; |
| 116 | padding.right += mHorizontalPadding; |
| 117 | padding.top = 0; |
| 118 | padding.bottom = 0; |
| 119 | android.util.Log.d("ScreenRecordDrawable", "set zero top/bottom pad"); |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void setAlpha(int alpha) { |
| 125 | super.setAlpha(alpha); |
| 126 | mFillDrawable.setAlpha(alpha); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public boolean setVisible(boolean visible, boolean restart) { |
| 131 | mFillDrawable.setVisible(visible, restart); |
| 132 | return super.setVisible(visible, restart); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public Drawable mutate() { |
| 137 | mFillDrawable.mutate(); |
| 138 | return super.mutate(); |
| 139 | } |
| 140 | } |