blob: 74ad0faca6d3b08dd54fa093eb9c90c29156ecb9 [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 static android.graphics.Paint.ANTI_ALIAS_FLAG;
19import static android.graphics.Paint.FILTER_BITMAP_FLAG;
20
Joshua Tsuji6549e702019-05-02 13:13:16 -040021import android.content.Context;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080022import android.graphics.Canvas;
23import android.graphics.Paint;
24import android.graphics.Point;
25import android.graphics.Rect;
26import android.util.Log;
27
Joshua Tsuji6549e702019-05-02 13:13:16 -040028import com.android.systemui.R;
29
Mady Mellor3f2efdb2018-11-21 11:30:45 -080030// XXX: Mostly opied from launcher code / can we share?
31/**
32 * Contains parameters necessary to draw a badge for an icon (e.g. the size of the badge).
33 */
34public class BadgeRenderer {
35
36 private static final String TAG = "BadgeRenderer";
37
Joshua Tsuji6549e702019-05-02 13:13:16 -040038 /** The badge sizes are defined as percentages of the app icon size. */
Mady Mellor3f2efdb2018-11-21 11:30:45 -080039 private static final float SIZE_PERCENTAGE = 0.38f;
40
Joshua Tsuji6549e702019-05-02 13:13:16 -040041 /** Extra scale down of the dot. */
Mady Mellor3f2efdb2018-11-21 11:30:45 -080042 private static final float DOT_SCALE = 0.6f;
43
44 private final float mDotCenterOffset;
45 private final float mCircleRadius;
46 private final Paint mCirclePaint = new Paint(ANTI_ALIAS_FLAG | FILTER_BITMAP_FLAG);
47
Joshua Tsuji6549e702019-05-02 13:13:16 -040048 public BadgeRenderer(Context context) {
49 mDotCenterOffset = getDotCenterOffset(context);
50 mCircleRadius = getDotRadius(mDotCenterOffset);
51 }
52
53 /** Space between the center of the dot and the top or left of the bubble stack. */
54 static float getDotCenterOffset(Context context) {
55 final int iconSizePx =
56 context.getResources().getDimensionPixelSize(R.dimen.individual_bubble_size);
57 return SIZE_PERCENTAGE * iconSizePx;
58 }
59
60 static float getDotRadius(float dotCenterOffset) {
61 int size = (int) (DOT_SCALE * dotCenterOffset);
62 return size / 2f;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080063 }
64
65 /**
66 * Draw a circle in the top right corner of the given bounds.
67 *
68 * @param color The color (based on the icon) to use for the badge.
69 * @param iconBounds The bounds of the icon being badged.
70 * @param badgeScale The progress of the animation, from 0 to 1.
71 * @param spaceForOffset How much space to offset the badge up and to the left or right.
72 * @param onLeft Whether the badge should be draw on left or right side.
73 */
74 public void draw(Canvas canvas, int color, Rect iconBounds, float badgeScale,
75 Point spaceForOffset, boolean onLeft) {
76 if (iconBounds == null) {
77 Log.e(TAG, "Invalid null argument(s) passed in call to draw.");
78 return;
79 }
80 canvas.save();
81 // We draw the badge relative to its center.
82 int x = onLeft ? iconBounds.left : iconBounds.right;
83 float offset = onLeft ? (mDotCenterOffset / 2) : -(mDotCenterOffset / 2);
84 float badgeCenterX = x + offset;
85 float badgeCenterY = iconBounds.top + mDotCenterOffset / 2;
86
87 canvas.translate(badgeCenterX + spaceForOffset.x, badgeCenterY - spaceForOffset.y);
88
89 canvas.scale(badgeScale, badgeScale);
90 mCirclePaint.setColor(color);
91 canvas.drawCircle(0, 0, mCircleRadius, mCirclePaint);
92 canvas.restore();
93 }
94}