1 /* 2 * Copyright (C) 2020 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.wm.shell.bubbles; 17 18 import android.annotation.NonNull; 19 import android.annotation.Nullable; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.pm.LauncherApps; 23 import android.content.pm.ShortcutInfo; 24 import android.graphics.Bitmap; 25 import android.graphics.Canvas; 26 import android.graphics.Paint; 27 import android.graphics.Path; 28 import android.graphics.drawable.AdaptiveIconDrawable; 29 import android.graphics.drawable.Drawable; 30 import android.graphics.drawable.Icon; 31 32 import androidx.annotation.VisibleForTesting; 33 34 import com.android.launcher3.icons.BaseIconFactory; 35 import com.android.launcher3.icons.BitmapInfo; 36 import com.android.launcher3.icons.ShadowGenerator; 37 import com.android.wm.shell.R; 38 39 /** 40 * Factory for creating normalized bubble icons. 41 * We are not using Launcher's IconFactory because bubbles only runs on the UI thread, 42 * so there is no need to manage a pool across multiple threads. 43 */ 44 @VisibleForTesting 45 public class BubbleIconFactory extends BaseIconFactory { 46 47 private int mBadgeSize; 48 BubbleIconFactory(Context context)49 public BubbleIconFactory(Context context) { 50 super(context, context.getResources().getConfiguration().densityDpi, 51 context.getResources().getDimensionPixelSize(R.dimen.bubble_size)); 52 mBadgeSize = mContext.getResources().getDimensionPixelSize(R.dimen.bubble_badge_size); 53 } 54 55 /** 56 * Returns the drawable that the developer has provided to display in the bubble. 57 */ getBubbleDrawable(@onNull final Context context, @Nullable final ShortcutInfo shortcutInfo, @Nullable final Icon ic)58 Drawable getBubbleDrawable(@NonNull final Context context, 59 @Nullable final ShortcutInfo shortcutInfo, @Nullable final Icon ic) { 60 if (shortcutInfo != null) { 61 LauncherApps launcherApps = 62 (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE); 63 int density = context.getResources().getConfiguration().densityDpi; 64 return launcherApps.getShortcutIconDrawable(shortcutInfo, density); 65 } else { 66 if (ic != null) { 67 if (ic.getType() == Icon.TYPE_URI 68 || ic.getType() == Icon.TYPE_URI_ADAPTIVE_BITMAP) { 69 context.grantUriPermission(context.getPackageName(), 70 ic.getUri(), 71 Intent.FLAG_GRANT_READ_URI_PERMISSION); 72 } 73 return ic.loadDrawable(context); 74 } 75 return null; 76 } 77 } 78 79 /** 80 * Returns a {@link BitmapInfo} for the app-badge that is shown on top of each bubble. This 81 * will include the workprofile indicator on the badge if appropriate. 82 */ getBadgeBitmap(Drawable userBadgedAppIcon, boolean isImportantConversation)83 BitmapInfo getBadgeBitmap(Drawable userBadgedAppIcon, boolean isImportantConversation) { 84 ShadowGenerator shadowGenerator = new ShadowGenerator(mBadgeSize); 85 Bitmap userBadgedBitmap = createIconBitmap(userBadgedAppIcon, 1f, mBadgeSize); 86 87 if (userBadgedAppIcon instanceof AdaptiveIconDrawable) { 88 userBadgedBitmap = Bitmap.createScaledBitmap( 89 getCircleBitmap((AdaptiveIconDrawable) userBadgedAppIcon, /* size */ 90 userBadgedAppIcon.getIntrinsicWidth()), 91 mBadgeSize, mBadgeSize, /* filter */ true); 92 } 93 94 if (isImportantConversation) { 95 final float ringStrokeWidth = mContext.getResources().getDimensionPixelSize( 96 com.android.internal.R.dimen.importance_ring_stroke_width); 97 final int importantConversationColor = mContext.getResources().getColor( 98 R.color.important_conversation, null); 99 Bitmap badgeAndRing = Bitmap.createBitmap(userBadgedBitmap.getWidth(), 100 userBadgedBitmap.getHeight(), userBadgedBitmap.getConfig()); 101 Canvas c = new Canvas(badgeAndRing); 102 103 Paint ringPaint = new Paint(); 104 ringPaint.setStyle(Paint.Style.FILL); 105 ringPaint.setColor(importantConversationColor); 106 ringPaint.setAntiAlias(true); 107 c.drawCircle(c.getWidth() / 2, c.getHeight() / 2, c.getWidth() / 2, ringPaint); 108 109 final int bitmapTop = (int) ringStrokeWidth; 110 final int bitmapLeft = (int) ringStrokeWidth; 111 final int bitmapWidth = c.getWidth() - 2 * (int) ringStrokeWidth; 112 final int bitmapHeight = c.getHeight() - 2 * (int) ringStrokeWidth; 113 114 Bitmap scaledBitmap = Bitmap.createScaledBitmap(userBadgedBitmap, bitmapWidth, 115 bitmapHeight, /* filter */ true); 116 c.drawBitmap(scaledBitmap, bitmapTop, bitmapLeft, /* paint */null); 117 118 shadowGenerator.recreateIcon(Bitmap.createBitmap(badgeAndRing), c); 119 return createIconBitmap(badgeAndRing); 120 } else { 121 Canvas c = new Canvas(); 122 c.setBitmap(userBadgedBitmap); 123 shadowGenerator.recreateIcon(Bitmap.createBitmap(userBadgedBitmap), c); 124 return createIconBitmap(userBadgedBitmap); 125 } 126 } 127 getCircleBitmap(AdaptiveIconDrawable icon, int size)128 public Bitmap getCircleBitmap(AdaptiveIconDrawable icon, int size) { 129 Drawable foreground = icon.getForeground(); 130 Drawable background = icon.getBackground(); 131 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); 132 Canvas canvas = new Canvas(); 133 canvas.setBitmap(bitmap); 134 135 // Clip canvas to circle. 136 Path circlePath = new Path(); 137 circlePath.addCircle(/* x */ size / 2f, 138 /* y */ size / 2f, 139 /* radius */ size / 2f, 140 Path.Direction.CW); 141 canvas.clipPath(circlePath); 142 143 // Draw background. 144 background.setBounds(0, 0, size, size); 145 background.draw(canvas); 146 147 // Draw foreground. The foreground and background drawables are derived from adaptive icons 148 // Some icon shapes fill more space than others, so adaptive icons are normalized to about 149 // the same size. This size is smaller than the original bounds, so we estimate 150 // the difference in this offset. 151 int offset = size / 5; 152 foreground.setBounds(-offset, -offset, size + offset, size + offset); 153 foreground.draw(canvas); 154 155 canvas.setBitmap(null); 156 return bitmap; 157 } 158 } 159