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.drawable.Drawable; 26 import android.graphics.drawable.Icon; 27 28 import androidx.annotation.VisibleForTesting; 29 30 import com.android.launcher3.icons.BaseIconFactory; 31 import com.android.wm.shell.R; 32 33 /** 34 * Factory for creating normalized bubble icons. 35 * We are not using Launcher's IconFactory because bubbles only runs on the UI thread, 36 * so there is no need to manage a pool across multiple threads. 37 */ 38 @VisibleForTesting 39 public class BubbleIconFactory extends BaseIconFactory { 40 BubbleIconFactory(Context context)41 public BubbleIconFactory(Context context) { 42 super(context, context.getResources().getConfiguration().densityDpi, 43 context.getResources().getDimensionPixelSize(R.dimen.bubble_size)); 44 } 45 46 /** 47 * Returns the drawable that the developer has provided to display in the bubble. 48 */ getBubbleDrawable(@onNull final Context context, @Nullable final ShortcutInfo shortcutInfo, @Nullable final Icon ic)49 Drawable getBubbleDrawable(@NonNull final Context context, 50 @Nullable final ShortcutInfo shortcutInfo, @Nullable final Icon ic) { 51 if (shortcutInfo != null) { 52 LauncherApps launcherApps = 53 (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE); 54 int density = context.getResources().getConfiguration().densityDpi; 55 return launcherApps.getShortcutIconDrawable(shortcutInfo, density); 56 } else { 57 if (ic != null) { 58 if (ic.getType() == Icon.TYPE_URI 59 || ic.getType() == Icon.TYPE_URI_ADAPTIVE_BITMAP) { 60 context.grantUriPermission(context.getPackageName(), 61 ic.getUri(), 62 Intent.FLAG_GRANT_READ_URI_PERMISSION); 63 } 64 return ic.loadDrawable(context); 65 } 66 return null; 67 } 68 } 69 70 /** 71 * Creates the bitmap for the provided drawable and returns the scale used for 72 * drawing the actual drawable. 73 */ createIconBitmap(@onNull Drawable icon, float[] outScale)74 public Bitmap createIconBitmap(@NonNull Drawable icon, float[] outScale) { 75 if (outScale == null) { 76 outScale = new float[1]; 77 } 78 icon = normalizeAndWrapToAdaptiveIcon(icon, 79 true /* shrinkNonAdaptiveIcons */, 80 null /* outscale */, 81 outScale); 82 return createIconBitmap(icon, outScale[0], MODE_WITH_SHADOW); 83 } 84 } 85