|
| 1 | +/* |
| 2 | + * Copyright (C) 2016 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.graphics; |
| 18 | + |
| 19 | +//import static com.android.launcher3.graphics.IconShape.getShapePath; |
| 20 | +import static com.android.launcher3.util.MainThreadInitializedObject.forOverride; |
| 21 | + |
| 22 | +import android.content.Context; |
| 23 | +import android.content.pm.ActivityInfo; |
| 24 | +import android.content.res.Resources; |
| 25 | +import android.graphics.Bitmap; |
| 26 | +import android.graphics.Canvas; |
| 27 | +import android.graphics.Color; |
| 28 | +import android.graphics.Rect; |
| 29 | +import android.graphics.drawable.BitmapDrawable; |
| 30 | +import android.graphics.drawable.Drawable; |
| 31 | +import android.os.Process; |
| 32 | +import android.os.UserHandle; |
| 33 | +import android.util.ArrayMap; |
| 34 | + |
| 35 | +import androidx.annotation.UiThread; |
| 36 | + |
| 37 | +import com.android.launcher3.icons.FastBitmapDrawable; |
| 38 | +import com.android.launcher3.model.data.ItemInfoWithIcon; |
| 39 | +import com.android.launcher3.R; |
| 40 | +import com.android.launcher3.icons.BitmapInfo; |
| 41 | +import com.android.launcher3.icons.PlaceHolderIconDrawable; |
| 42 | +import com.android.launcher3.util.MainThreadInitializedObject; |
| 43 | +import com.android.launcher3.util.ResourceBasedOverride; |
| 44 | + |
| 45 | +/** |
| 46 | + * Factory for creating new drawables. |
| 47 | + */ |
| 48 | +public class DrawableFactory implements ResourceBasedOverride { |
| 49 | + |
| 50 | + public static final MainThreadInitializedObject<DrawableFactory> INSTANCE = |
| 51 | + forOverride(DrawableFactory.class, R.string.drawable_factory_class); |
| 52 | + |
| 53 | + protected final UserHandle mMyUser = Process.myUserHandle(); |
| 54 | + protected final ArrayMap<UserHandle, Bitmap> mUserBadges = new ArrayMap<>(); |
| 55 | + |
| 56 | + /** |
| 57 | + * Returns a FastBitmapDrawable with the icon. |
| 58 | + */ |
| 59 | + public FastBitmapDrawable newIcon(Context context, ItemInfoWithIcon info) { |
| 60 | + FastBitmapDrawable drawable = info.usingLowResIcon() |
| 61 | + ? new PlaceHolderIconDrawable(info.bitmap, context) |
| 62 | + : new FastBitmapDrawable(info.bitmap); |
| 63 | + drawable.setIsDisabled(info.isDisabled()); |
| 64 | + return drawable; |
| 65 | + } |
| 66 | + |
| 67 | + public FastBitmapDrawable newIcon(Context context, BitmapInfo info, ActivityInfo target) { |
| 68 | + return info.isLowRes() |
| 69 | + ? new PlaceHolderIconDrawable(info, context) |
| 70 | + : new FastBitmapDrawable(info); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Returns a FastBitmapDrawable with the icon. |
| 75 | + */ |
| 76 | + public PreloadIconDrawable newPendingIcon(Context context, ItemInfoWithIcon info) { |
| 77 | + return new PreloadIconDrawable(info, context); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns a drawable that can be used as a badge for the user or null. |
| 82 | + */ |
| 83 | + @UiThread |
| 84 | + public Drawable getBadgeForUser(UserHandle user, Context context, int badgeSize) { |
| 85 | + if (mMyUser.equals(user)) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + Bitmap badgeBitmap = getUserBadge(user, context, badgeSize); |
| 90 | + FastBitmapDrawable d = new FastBitmapDrawable(badgeBitmap); |
| 91 | + d.setFilterBitmap(true); |
| 92 | + d.setBounds(0, 0, badgeBitmap.getWidth(), badgeBitmap.getHeight()); |
| 93 | + return d; |
| 94 | + } |
| 95 | + |
| 96 | + protected synchronized Bitmap getUserBadge(UserHandle user, Context context, int badgeSize) { |
| 97 | + Bitmap badgeBitmap = mUserBadges.get(user); |
| 98 | + if (badgeBitmap != null) { |
| 99 | + return badgeBitmap; |
| 100 | + } |
| 101 | + |
| 102 | + final Resources res = context.getApplicationContext().getResources(); |
| 103 | + badgeBitmap = Bitmap.createBitmap(badgeSize, badgeSize, Bitmap.Config.ARGB_8888); |
| 104 | + |
| 105 | + Drawable drawable = context.getPackageManager().getUserBadgedDrawableForDensity( |
| 106 | + new BitmapDrawable(res, badgeBitmap), user, new Rect(0, 0, badgeSize, badgeSize), |
| 107 | + 0); |
| 108 | + if (drawable instanceof BitmapDrawable) { |
| 109 | + badgeBitmap = ((BitmapDrawable) drawable).getBitmap(); |
| 110 | + } else { |
| 111 | + badgeBitmap.eraseColor(Color.TRANSPARENT); |
| 112 | + Canvas c = new Canvas(badgeBitmap); |
| 113 | + drawable.setBounds(0, 0, badgeSize, badgeSize); |
| 114 | + drawable.draw(c); |
| 115 | + c.setBitmap(null); |
| 116 | + } |
| 117 | + |
| 118 | + mUserBadges.put(user, badgeBitmap); |
| 119 | + return badgeBitmap; |
| 120 | + } |
| 121 | +} |
0 commit comments