blob: 783780f8819c676862e9873c7eaf201a3409ea38 [file] [log] [blame]
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001/*
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 */
16package com.android.systemui.bubbles;
17
18import android.content.Context;
Joshua Tsuji580c0bf2019-01-28 13:28:21 -050019import android.content.res.TypedArray;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080020import android.graphics.Canvas;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080021import android.graphics.Point;
22import android.graphics.Rect;
23import android.util.AttributeSet;
24import android.widget.ImageView;
25
Lyn Han3fd6e6f2019-05-03 17:46:23 -070026import com.android.internal.graphics.ColorUtils;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080027import com.android.systemui.R;
28
29/**
30 * View that circle crops its contents and supports displaying a coloured dot on a top corner.
31 */
32public 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 Mellor3f2efdb2018-11-21 11:30:45 -080038
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 Tsujib1a796b2019-01-16 15:43:12 -080059 mIconSize = getResources().getDimensionPixelSize(R.dimen.individual_bubble_size);
Joshua Tsuji6549e702019-05-02 13:13:16 -040060 mDotRenderer = new BadgeRenderer(getContext());
Joshua Tsuji580c0bf2019-01-28 13:28:21 -050061
62 TypedArray ta = context.obtainStyledAttributes(
63 new int[] {android.R.attr.colorBackgroundFloating});
Joshua Tsuji580c0bf2019-01-28 13:28:21 -050064 ta.recycle();
Mady Mellor3f2efdb2018-11-21 11:30:45 -080065 }
66
Mady Mellor3f2efdb2018-11-21 11:30:45 -080067 @Override
68 public void onDraw(Canvas canvas) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -080069 super.onDraw(canvas);
Mady Mellor3f2efdb2018-11-21 11:30:45 -080070 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 Tsuji6549e702019-05-02 13:13:16 -040086 public boolean getDotPosition() {
87 return mOnLeft;
88 }
89
Mady Mellor3f2efdb2018-11-21 11:30:45 -080090 /**
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 Han3fd6e6f2019-05-03 17:46:23 -0700109 mUpdateDotColor = ColorUtils.setAlphaComponent(color, 255 /* alpha */);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800110 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}