Home | History | Annotate | Download | only in badge
      1 /*
      2  * Copyright (C) 2017 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 
     17 package com.android.launcher3.badge;
     18 
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.graphics.Bitmap;
     22 import android.graphics.Canvas;
     23 import android.graphics.Color;
     24 import android.graphics.Paint;
     25 import android.graphics.Point;
     26 import android.graphics.Rect;
     27 import android.graphics.Shader;
     28 import android.support.annotation.Nullable;
     29 import android.util.SparseArray;
     30 
     31 import com.android.launcher3.R;
     32 import com.android.launcher3.graphics.IconPalette;
     33 import com.android.launcher3.graphics.ShadowGenerator;
     34 
     35 /**
     36  * Contains parameters necessary to draw a badge for an icon (e.g. the size of the badge).
     37  * @see BadgeInfo for the data to draw
     38  */
     39 public class BadgeRenderer {
     40 
     41     private static final boolean DOTS_ONLY = true;
     42 
     43     // The badge sizes are defined as percentages of the app icon size.
     44     private static final float SIZE_PERCENTAGE = 0.38f;
     45     // Used to expand the width of the badge for each additional digit.
     46     private static final float CHAR_SIZE_PERCENTAGE = 0.12f;
     47     private static final float TEXT_SIZE_PERCENTAGE = 0.26f;
     48     private static final float OFFSET_PERCENTAGE = 0.02f;
     49     private static final float STACK_OFFSET_PERCENTAGE_X = 0.05f;
     50     private static final float STACK_OFFSET_PERCENTAGE_Y = 0.06f;
     51     private static final float DOT_SCALE = 0.6f;
     52 
     53     private final Context mContext;
     54     private final int mSize;
     55     private final int mCharSize;
     56     private final int mTextHeight;
     57     private final int mOffset;
     58     private final int mStackOffsetX;
     59     private final int mStackOffsetY;
     60     private final IconDrawer mLargeIconDrawer;
     61     private final IconDrawer mSmallIconDrawer;
     62     private final Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
     63     private final Paint mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG
     64             | Paint.FILTER_BITMAP_FLAG);
     65     private final SparseArray<Bitmap> mBackgroundsWithShadow;
     66 
     67     public BadgeRenderer(Context context, int iconSizePx) {
     68         mContext = context;
     69         Resources res = context.getResources();
     70         mSize = (int) (SIZE_PERCENTAGE * iconSizePx);
     71         mCharSize = (int) (CHAR_SIZE_PERCENTAGE * iconSizePx);
     72         mOffset = (int) (OFFSET_PERCENTAGE * iconSizePx);
     73         mStackOffsetX = (int) (STACK_OFFSET_PERCENTAGE_X * iconSizePx);
     74         mStackOffsetY = (int) (STACK_OFFSET_PERCENTAGE_Y * iconSizePx);
     75         mTextPaint.setTextSize(iconSizePx * TEXT_SIZE_PERCENTAGE);
     76         mTextPaint.setTextAlign(Paint.Align.CENTER);
     77         mLargeIconDrawer = new IconDrawer(res.getDimensionPixelSize(R.dimen.badge_small_padding));
     78         mSmallIconDrawer = new IconDrawer(res.getDimensionPixelSize(R.dimen.badge_large_padding));
     79         // Measure the text height.
     80         Rect tempTextHeight = new Rect();
     81         mTextPaint.getTextBounds("0", 0, 1, tempTextHeight);
     82         mTextHeight = tempTextHeight.height();
     83 
     84         mBackgroundsWithShadow = new SparseArray<>(3);
     85     }
     86 
     87     /**
     88      * Draw a circle in the top right corner of the given bounds, and draw
     89      * {@link BadgeInfo#getNotificationCount()} on top of the circle.
     90      * @param palette The colors (based on the icon) to use for the badge.
     91      * @param badgeInfo Contains data to draw on the badge. Could be null if we are animating out.
     92      * @param iconBounds The bounds of the icon being badged.
     93      * @param badgeScale The progress of the animation, from 0 to 1.
     94      * @param spaceForOffset How much space is available to offset the badge up and to the right.
     95      */
     96     public void draw(Canvas canvas, IconPalette palette, @Nullable BadgeInfo badgeInfo,
     97             Rect iconBounds, float badgeScale, Point spaceForOffset) {
     98         mTextPaint.setColor(palette.textColor);
     99         IconDrawer iconDrawer = badgeInfo != null && badgeInfo.isIconLarge()
    100                 ? mLargeIconDrawer : mSmallIconDrawer;
    101         Shader icon = badgeInfo == null ? null : badgeInfo.getNotificationIconForBadge(
    102                 mContext, palette.backgroundColor, mSize, iconDrawer.mPadding);
    103         String notificationCount = badgeInfo == null ? "0"
    104                 : String.valueOf(badgeInfo.getNotificationCount());
    105         int numChars = notificationCount.length();
    106         int width = DOTS_ONLY ? mSize : mSize + mCharSize * (numChars - 1);
    107         // Lazily load the background with shadow.
    108         Bitmap backgroundWithShadow = mBackgroundsWithShadow.get(numChars);
    109         if (backgroundWithShadow == null) {
    110             backgroundWithShadow = new ShadowGenerator.Builder(Color.WHITE)
    111                     .setupBlurForSize(mSize).createPill(width, mSize);
    112             mBackgroundsWithShadow.put(numChars, backgroundWithShadow);
    113         }
    114         canvas.save(Canvas.MATRIX_SAVE_FLAG);
    115         // We draw the badge relative to its center.
    116         int badgeCenterX = iconBounds.right - width / 2;
    117         int badgeCenterY = iconBounds.top + mSize / 2;
    118         boolean isText = !DOTS_ONLY && badgeInfo != null && badgeInfo.getNotificationCount() != 0;
    119         boolean isIcon = !DOTS_ONLY && icon != null;
    120         boolean isDot = !(isText || isIcon);
    121         if (isDot) {
    122             badgeScale *= DOT_SCALE;
    123         }
    124         int offsetX = Math.min(mOffset, spaceForOffset.x);
    125         int offsetY = Math.min(mOffset, spaceForOffset.y);
    126         canvas.translate(badgeCenterX + offsetX, badgeCenterY - offsetY);
    127         canvas.scale(badgeScale, badgeScale);
    128         // Prepare the background and shadow and possible stacking effect.
    129         mBackgroundPaint.setColorFilter(palette.backgroundColorMatrixFilter);
    130         int backgroundWithShadowSize = backgroundWithShadow.getHeight(); // Same as width.
    131         boolean shouldStack = !isDot && badgeInfo != null
    132                 && badgeInfo.getNotificationKeys().size() > 1;
    133         if (shouldStack) {
    134             int offsetDiffX = mStackOffsetX - mOffset;
    135             int offsetDiffY = mStackOffsetY - mOffset;
    136             canvas.translate(offsetDiffX, offsetDiffY);
    137             canvas.drawBitmap(backgroundWithShadow, -backgroundWithShadowSize / 2,
    138                     -backgroundWithShadowSize / 2, mBackgroundPaint);
    139             canvas.translate(-offsetDiffX, -offsetDiffY);
    140         }
    141 
    142         if (isText) {
    143             canvas.drawBitmap(backgroundWithShadow, -backgroundWithShadowSize / 2,
    144                     -backgroundWithShadowSize / 2, mBackgroundPaint);
    145             canvas.drawText(notificationCount, 0, mTextHeight / 2, mTextPaint);
    146         } else if (isIcon) {
    147             canvas.drawBitmap(backgroundWithShadow, -backgroundWithShadowSize / 2,
    148                     -backgroundWithShadowSize / 2, mBackgroundPaint);
    149             iconDrawer.drawIcon(icon, canvas);
    150         } else if (isDot) {
    151             mBackgroundPaint.setColorFilter(palette.saturatedBackgroundColorMatrixFilter);
    152             canvas.drawBitmap(backgroundWithShadow, -backgroundWithShadowSize / 2,
    153                     -backgroundWithShadowSize / 2, mBackgroundPaint);
    154         }
    155         canvas.restore();
    156     }
    157 
    158     /** Draws the notification icon with padding of a given size. */
    159     private class IconDrawer {
    160 
    161         private final int mPadding;
    162         private final Bitmap mCircleClipBitmap;
    163         private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG |
    164                 Paint.FILTER_BITMAP_FLAG);
    165 
    166         public IconDrawer(int padding) {
    167             mPadding = padding;
    168             mCircleClipBitmap = Bitmap.createBitmap(mSize, mSize, Bitmap.Config.ALPHA_8);
    169             Canvas canvas = new Canvas();
    170             canvas.setBitmap(mCircleClipBitmap);
    171             canvas.drawCircle(mSize / 2, mSize / 2, mSize / 2 - padding, mPaint);
    172         }
    173 
    174         public void drawIcon(Shader icon, Canvas canvas) {
    175             mPaint.setShader(icon);
    176             canvas.drawBitmap(mCircleClipBitmap, -mSize / 2, -mSize / 2, mPaint);
    177             mPaint.setShader(null);
    178         }
    179     }
    180 }
    181