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