1 /* 2 * Copyright (C) 2022 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 17 package com.android.systemui.dreams; 18 19 import android.annotation.ColorInt; 20 import android.content.Context; 21 import android.content.res.TypedArray; 22 import android.graphics.Bitmap; 23 import android.graphics.Canvas; 24 import android.graphics.Color; 25 import android.graphics.ColorFilter; 26 import android.graphics.Paint; 27 import android.graphics.Rect; 28 import android.graphics.drawable.Drawable; 29 import android.util.AttributeSet; 30 31 import androidx.annotation.NonNull; 32 import androidx.annotation.Nullable; 33 34 import com.android.systemui.R; 35 import com.android.systemui.statusbar.AlphaOptimizedImageView; 36 37 /** 38 * An {@link AlphaOptimizedImageView} that is responsible for rendering a dot. Used by 39 * {@link DreamOverlayStatusBarView}. 40 */ 41 public class DreamOverlayDotImageView extends AlphaOptimizedImageView { 42 private final @ColorInt int mDotColor; 43 DreamOverlayDotImageView(Context context)44 public DreamOverlayDotImageView(Context context) { 45 this(context, null); 46 } 47 DreamOverlayDotImageView(Context context, AttributeSet attrs)48 public DreamOverlayDotImageView(Context context, AttributeSet attrs) { 49 this(context, attrs, 0); 50 } 51 DreamOverlayDotImageView(Context context, AttributeSet attrs, int defStyleAttr)52 public DreamOverlayDotImageView(Context context, AttributeSet attrs, int defStyleAttr) { 53 this(context, attrs, defStyleAttr, 0); 54 } 55 DreamOverlayDotImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)56 public DreamOverlayDotImageView(Context context, AttributeSet attrs, int defStyleAttr, 57 int defStyleRes) { 58 super(context, attrs, defStyleAttr, defStyleRes); 59 TypedArray a = context.getTheme().obtainStyledAttributes(attrs, 60 R.styleable.DreamOverlayDotImageView, 0, 0); 61 62 try { 63 mDotColor = a.getColor(R.styleable.DreamOverlayDotImageView_dotColor, Color.WHITE); 64 } finally { 65 a.recycle(); 66 } 67 } 68 69 @Override onFinishInflate()70 protected void onFinishInflate() { 71 super.onFinishInflate(); 72 setImageDrawable(new DotDrawable(mDotColor)); 73 } 74 75 private static class DotDrawable extends Drawable { 76 private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 77 private Bitmap mDotBitmap; 78 private final Rect mBounds = new Rect(); 79 private final @ColorInt int mDotColor; 80 DotDrawable(@olorInt int color)81 DotDrawable(@ColorInt int color) { 82 mDotColor = color; 83 } 84 85 @Override draw(@onNull Canvas canvas)86 public void draw(@NonNull Canvas canvas) { 87 if (mBounds.isEmpty()) { 88 return; 89 } 90 91 if (mDotBitmap == null) { 92 mDotBitmap = createBitmap(mBounds.width(), mBounds.height()); 93 } 94 95 canvas.drawBitmap(mDotBitmap, null, mBounds, mPaint); 96 } 97 98 @Override onBoundsChange(Rect bounds)99 protected void onBoundsChange(Rect bounds) { 100 super.onBoundsChange(bounds); 101 mBounds.set(bounds.left, bounds.top, bounds.right, bounds.bottom); 102 // Make sure to regenerate the dot bitmap when the bounds change. 103 mDotBitmap = null; 104 } 105 106 @Override setAlpha(int alpha)107 public void setAlpha(int alpha) { 108 } 109 110 @Override setColorFilter(@ullable ColorFilter colorFilter)111 public void setColorFilter(@Nullable ColorFilter colorFilter) { 112 } 113 114 @Override getOpacity()115 public int getOpacity() { 116 return 0; 117 } 118 createBitmap(int width, int height)119 private Bitmap createBitmap(int width, int height) { 120 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 121 Canvas canvas = new Canvas(bitmap); 122 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 123 paint.setColor(mDotColor); 124 canvas.drawCircle(width / 2.f, height / 2.f, Math.min(width, height) / 2.f, paint); 125 return bitmap; 126 } 127 } 128 } 129