blob: 44ef6b448002b9eaf5b64d77ce2ea87aa147f17c [file] [log] [blame]
Beth Thibodeau8bfbf1f2020-02-13 13:55:37 -05001/*
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 */
16package com.android.systemui.statusbar;
17
18import android.annotation.NonNull;
19import android.annotation.Nullable;
20import android.content.res.Resources;
21import android.graphics.Canvas;
22import android.graphics.Color;
23import android.graphics.Paint;
24import android.graphics.Rect;
25import android.graphics.drawable.Drawable;
26import android.graphics.drawable.DrawableWrapper;
27import android.util.AttributeSet;
28
29import com.android.systemui.R;
30
31import org.xmlpull.v1.XmlPullParser;
32import org.xmlpull.v1.XmlPullParserException;
33
34import 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 */
40public 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}