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; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 21 | import android.graphics.Point; |
| 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; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 27 | import com.android.systemui.R; |
| 28 | |
| 29 | /** |
| 30 | * View that circle crops its contents and supports displaying a coloured dot on a top corner. |
| 31 | */ |
| 32 | public class BadgedImageView extends ImageView { |
| 33 | |
| 34 | private BadgeRenderer mDotRenderer; |
| 35 | private int mIconSize; |
| 36 | private Rect mTempBounds = new Rect(); |
| 37 | private Point mTempPoint = new Point(); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 38 | |
| 39 | private float mDotScale = 0f; |
| 40 | private int mUpdateDotColor; |
| 41 | private boolean mShowUpdateDot; |
| 42 | private boolean mOnLeft; |
| 43 | |
| 44 | public BadgedImageView(Context context) { |
| 45 | this(context, null); |
| 46 | } |
| 47 | |
| 48 | public BadgedImageView(Context context, AttributeSet attrs) { |
| 49 | this(context, attrs, 0); |
| 50 | } |
| 51 | |
| 52 | public BadgedImageView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 53 | this(context, attrs, defStyleAttr, 0); |
| 54 | } |
| 55 | |
| 56 | public BadgedImageView(Context context, AttributeSet attrs, int defStyleAttr, |
| 57 | int defStyleRes) { |
| 58 | super(context, attrs, defStyleAttr, defStyleRes); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 59 | mIconSize = getResources().getDimensionPixelSize(R.dimen.individual_bubble_size); |
Joshua Tsuji | 6549e70 | 2019-05-02 13:13:16 -0400 | [diff] [blame] | 60 | mDotRenderer = new BadgeRenderer(getContext()); |
Joshua Tsuji | 580c0bf | 2019-01-28 13:28:21 -0500 | [diff] [blame] | 61 | |
| 62 | TypedArray ta = context.obtainStyledAttributes( |
| 63 | new int[] {android.R.attr.colorBackgroundFloating}); |
Joshua Tsuji | 580c0bf | 2019-01-28 13:28:21 -0500 | [diff] [blame] | 64 | ta.recycle(); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 67 | @Override |
| 68 | public void onDraw(Canvas canvas) { |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 69 | super.onDraw(canvas); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 70 | if (mShowUpdateDot) { |
| 71 | getDrawingRect(mTempBounds); |
| 72 | mTempPoint.set((getWidth() - mIconSize) / 2, getPaddingTop()); |
| 73 | mDotRenderer.draw(canvas, mUpdateDotColor, mTempBounds, mDotScale, mTempPoint, |
| 74 | mOnLeft); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Set whether the dot should appear on left or right side of the view. |
| 80 | */ |
| 81 | public void setDotPosition(boolean onLeft) { |
| 82 | mOnLeft = onLeft; |
| 83 | invalidate(); |
| 84 | } |
| 85 | |
Joshua Tsuji | 6549e70 | 2019-05-02 13:13:16 -0400 | [diff] [blame] | 86 | public boolean getDotPosition() { |
| 87 | return mOnLeft; |
| 88 | } |
| 89 | |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 90 | /** |
| 91 | * Set whether the dot should show or not. |
| 92 | */ |
| 93 | public void setShowDot(boolean showBadge) { |
| 94 | mShowUpdateDot = showBadge; |
| 95 | invalidate(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @return whether the dot is being displayed. |
| 100 | */ |
| 101 | public boolean isShowingDot() { |
| 102 | return mShowUpdateDot; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * The colour to use for the dot. |
| 107 | */ |
| 108 | public void setDotColor(int color) { |
Lyn Han | 3fd6e6f | 2019-05-03 17:46:23 -0700 | [diff] [blame] | 109 | mUpdateDotColor = ColorUtils.setAlphaComponent(color, 255 /* alpha */); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 110 | invalidate(); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * How big the dot should be, fraction from 0 to 1. |
| 115 | */ |
| 116 | public void setDotScale(float fraction) { |
| 117 | mDotScale = fraction; |
| 118 | invalidate(); |
| 119 | } |
| 120 | |
| 121 | public float getDotScale() { |
| 122 | return mDotScale; |
| 123 | } |
| 124 | } |