Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.bubbles; |
| 17 | |
| 18 | import android.content.Context; |
Joshua Tsuji | 580c0bf | 2019-01-28 13:28:21 -0500 | [diff] [blame] | 19 | import android.content.res.TypedArray; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 20 | import android.graphics.Canvas; |
Lyn Han | 4f01acc | 2019-06-12 13:06:31 -0700 | [diff] [blame] | 21 | import android.graphics.Path; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 22 | import android.graphics.Rect; |
| 23 | import android.util.AttributeSet; |
| 24 | import android.widget.ImageView; |
| 25 | |
Lyn Han | 3fd6e6f | 2019-05-03 17:46:23 -0700 | [diff] [blame] | 26 | import com.android.internal.graphics.ColorUtils; |
Lyn Han | 4f01acc | 2019-06-12 13:06:31 -0700 | [diff] [blame] | 27 | import com.android.launcher3.icons.DotRenderer; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 28 | import com.android.systemui.R; |
| 29 | |
| 30 | /** |
| 31 | * View that circle crops its contents and supports displaying a coloured dot on a top corner. |
| 32 | */ |
| 33 | public class BadgedImageView extends ImageView { |
| 34 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 35 | private Rect mTempBounds = new Rect(); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 36 | |
Lyn Han | 0e82a3e | 2019-06-19 18:47:06 -0700 | [diff] [blame] | 37 | private DotRenderer mDotRenderer; |
| 38 | private DotRenderer.DrawParams mDrawParams; |
Lyn Han | 4f01acc | 2019-06-12 13:06:31 -0700 | [diff] [blame] | 39 | private int mIconBitmapSize; |
| 40 | private int mDotColor; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 41 | private float mDotScale = 0f; |
Lyn Han | 4f01acc | 2019-06-12 13:06:31 -0700 | [diff] [blame] | 42 | private boolean mShowDot; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 43 | private boolean mOnLeft; |
| 44 | |
Lyn Han | 4f01acc | 2019-06-12 13:06:31 -0700 | [diff] [blame] | 45 | /** Same as value in Launcher3 IconShape */ |
Lyn Han | ecdd06e | 2019-07-10 18:19:37 -0700 | [diff] [blame] | 46 | static final int DEFAULT_PATH_SIZE = 100; |
Lyn Han | 4f01acc | 2019-06-12 13:06:31 -0700 | [diff] [blame] | 47 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 48 | public BadgedImageView(Context context) { |
| 49 | this(context, null); |
| 50 | } |
| 51 | |
| 52 | public BadgedImageView(Context context, AttributeSet attrs) { |
| 53 | this(context, attrs, 0); |
| 54 | } |
| 55 | |
| 56 | public BadgedImageView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 57 | this(context, attrs, defStyleAttr, 0); |
| 58 | } |
| 59 | |
| 60 | public BadgedImageView(Context context, AttributeSet attrs, int defStyleAttr, |
| 61 | int defStyleRes) { |
| 62 | super(context, attrs, defStyleAttr, defStyleRes); |
Lyn Han | 1b4f25e | 2019-06-11 13:56:34 -0700 | [diff] [blame] | 63 | mIconBitmapSize = getResources().getDimensionPixelSize(R.dimen.bubble_icon_bitmap_size); |
Lyn Han | 0e82a3e | 2019-06-19 18:47:06 -0700 | [diff] [blame] | 64 | mDrawParams = new DotRenderer.DrawParams(); |
Joshua Tsuji | 580c0bf | 2019-01-28 13:28:21 -0500 | [diff] [blame] | 65 | |
| 66 | TypedArray ta = context.obtainStyledAttributes( |
Lyn Han | 0e82a3e | 2019-06-19 18:47:06 -0700 | [diff] [blame] | 67 | new int[]{android.R.attr.colorBackgroundFloating}); |
Joshua Tsuji | 580c0bf | 2019-01-28 13:28:21 -0500 | [diff] [blame] | 68 | ta.recycle(); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 71 | @Override |
| 72 | public void onDraw(Canvas canvas) { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 73 | super.onDraw(canvas); |
Lyn Han | 0e82a3e | 2019-06-19 18:47:06 -0700 | [diff] [blame] | 74 | if (!mShowDot) { |
| 75 | return; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 76 | } |
Lyn Han | 0e82a3e | 2019-06-19 18:47:06 -0700 | [diff] [blame] | 77 | getDrawingRect(mTempBounds); |
| 78 | |
| 79 | mDrawParams.color = mDotColor; |
| 80 | mDrawParams.iconBounds = mTempBounds; |
| 81 | mDrawParams.leftAlign = mOnLeft; |
| 82 | mDrawParams.scale = mDotScale; |
| 83 | |
| 84 | if (mDotRenderer == null) { |
| 85 | Path circlePath = new Path(); |
| 86 | float radius = DEFAULT_PATH_SIZE * 0.5f; |
| 87 | circlePath.addCircle(radius /* x */, radius /* y */, radius, Path.Direction.CW); |
| 88 | mDotRenderer = new DotRenderer(mIconBitmapSize, circlePath, DEFAULT_PATH_SIZE); |
| 89 | } |
| 90 | mDotRenderer.draw(canvas, mDrawParams); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Set whether the dot should appear on left or right side of the view. |
| 95 | */ |
Lyn Han | 61d5d56 | 2019-07-01 17:39:38 -0700 | [diff] [blame] | 96 | void setDotOnLeft(boolean onLeft) { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 97 | mOnLeft = onLeft; |
| 98 | invalidate(); |
| 99 | } |
| 100 | |
Lyn Han | 61d5d56 | 2019-07-01 17:39:38 -0700 | [diff] [blame] | 101 | boolean getDotOnLeft() { |
Joshua Tsuji | 6549e70 | 2019-05-02 13:13:16 -0400 | [diff] [blame] | 102 | return mOnLeft; |
| 103 | } |
| 104 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 105 | /** |
| 106 | * Set whether the dot should show or not. |
| 107 | */ |
Lyn Han | 61d5d56 | 2019-07-01 17:39:38 -0700 | [diff] [blame] | 108 | void setShowDot(boolean showDot) { |
Lyn Han | 4f01acc | 2019-06-12 13:06:31 -0700 | [diff] [blame] | 109 | mShowDot = showDot; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 110 | invalidate(); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @return whether the dot is being displayed. |
| 115 | */ |
Lyn Han | 61d5d56 | 2019-07-01 17:39:38 -0700 | [diff] [blame] | 116 | boolean isShowingDot() { |
Lyn Han | 4f01acc | 2019-06-12 13:06:31 -0700 | [diff] [blame] | 117 | return mShowDot; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
| 121 | * The colour to use for the dot. |
| 122 | */ |
| 123 | public void setDotColor(int color) { |
Lyn Han | 4f01acc | 2019-06-12 13:06:31 -0700 | [diff] [blame] | 124 | mDotColor = ColorUtils.setAlphaComponent(color, 255 /* alpha */); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 125 | invalidate(); |
| 126 | } |
| 127 | |
| 128 | /** |
Lyn Han | 0e82a3e | 2019-06-19 18:47:06 -0700 | [diff] [blame] | 129 | * @param iconPath The new icon path to use when calculating dot position. |
| 130 | */ |
| 131 | public void drawDot(Path iconPath) { |
| 132 | mDotRenderer = new DotRenderer(mIconBitmapSize, iconPath, DEFAULT_PATH_SIZE); |
| 133 | invalidate(); |
| 134 | } |
| 135 | |
| 136 | /** |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 137 | * How big the dot should be, fraction from 0 to 1. |
| 138 | */ |
Lyn Han | 61d5d56 | 2019-07-01 17:39:38 -0700 | [diff] [blame] | 139 | void setDotScale(float fraction) { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 140 | mDotScale = fraction; |
| 141 | invalidate(); |
| 142 | } |
Lyn Han | 61d5d56 | 2019-07-01 17:39:38 -0700 | [diff] [blame] | 143 | |
| 144 | /** |
| 145 | * Return dot position relative to bubble view container bounds. |
| 146 | */ |
| 147 | float[] getDotCenter() { |
| 148 | float[] dotPosition; |
| 149 | if (mOnLeft) { |
| 150 | dotPosition = mDotRenderer.getLeftDotPosition(); |
| 151 | } else { |
| 152 | dotPosition = mDotRenderer.getRightDotPosition(); |
| 153 | } |
| 154 | getDrawingRect(mTempBounds); |
| 155 | float dotCenterX = mTempBounds.width() * dotPosition[0]; |
| 156 | float dotCenterY = mTempBounds.height() * dotPosition[1]; |
| 157 | return new float[]{dotCenterX, dotCenterY}; |
| 158 | } |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 159 | } |