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 static android.graphics.Paint.ANTI_ALIAS_FLAG; 19 import static android.graphics.Paint.FILTER_BITMAP_FLAG; 20 21 import android.content.Context; 22 import android.graphics.Canvas; 23 import android.graphics.Paint; 24 import android.graphics.Point; 25 import android.graphics.Rect; 26 import android.util.Log; 27 28 import com.android.systemui.R; 29 30 // 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 */ 34 public class BadgeRenderer { 35 36 private static final String TAG = "BadgeRenderer"; 37 38 /** The badge sizes are defined as percentages of the app icon size. */ 39 private static final float SIZE_PERCENTAGE = 0.38f; 40 41 /** Extra scale down of the dot. */ 42 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 BadgeRenderer(Context context)48 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. */ getDotCenterOffset(Context context)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 getDotRadius(float dotCenterOffset)60 static float getDotRadius(float dotCenterOffset) { 61 int size = (int) (DOT_SCALE * dotCenterOffset); 62 return size / 2f; 63 } 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 */ draw(Canvas canvas, int color, Rect iconBounds, float badgeScale, Point spaceForOffset, boolean onLeft)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 } 95