Sunny Goyal | bab3075 | 2017-04-12 15:36:42 -0700 | [diff] [blame] | 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 | package android.util; |
| 17 | |
Mathew Inwood | 4eb56ab | 2018-08-14 17:24:32 +0100 | [diff] [blame] | 18 | import android.annotation.UnsupportedAppUsage; |
Sunny Goyal | bab3075 | 2017-04-12 15:36:42 -0700 | [diff] [blame] | 19 | import android.annotation.UserIdInt; |
| 20 | import android.content.Context; |
| 21 | import android.content.pm.ApplicationInfo; |
| 22 | import android.content.pm.PackageItemInfo; |
| 23 | import android.content.pm.PackageManager; |
| 24 | import android.content.res.Resources; |
Sunny Goyal | bab3075 | 2017-04-12 15:36:42 -0700 | [diff] [blame] | 25 | import android.graphics.drawable.Drawable; |
| 26 | import android.os.UserHandle; |
| 27 | import android.os.UserManager; |
| 28 | |
Sunny Goyal | bab3075 | 2017-04-12 15:36:42 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Utility class to load app drawables with appropriate badging. |
| 31 | * |
| 32 | * @hide |
| 33 | */ |
| 34 | public class IconDrawableFactory { |
| 35 | |
| 36 | protected final Context mContext; |
| 37 | protected final PackageManager mPm; |
| 38 | protected final UserManager mUm; |
| 39 | protected final LauncherIcons mLauncherIcons; |
| 40 | protected final boolean mEmbedShadow; |
| 41 | |
| 42 | private IconDrawableFactory(Context context, boolean embedShadow) { |
| 43 | mContext = context; |
| 44 | mPm = context.getPackageManager(); |
| 45 | mUm = context.getSystemService(UserManager.class); |
| 46 | mLauncherIcons = new LauncherIcons(context); |
| 47 | mEmbedShadow = embedShadow; |
| 48 | } |
| 49 | |
| 50 | protected boolean needsBadging(ApplicationInfo appInfo, @UserIdInt int userId) { |
| 51 | return appInfo.isInstantApp() || mUm.isManagedProfile(userId); |
| 52 | } |
| 53 | |
Mathew Inwood | 4eb56ab | 2018-08-14 17:24:32 +0100 | [diff] [blame] | 54 | @UnsupportedAppUsage |
Sunny Goyal | bab3075 | 2017-04-12 15:36:42 -0700 | [diff] [blame] | 55 | public Drawable getBadgedIcon(ApplicationInfo appInfo) { |
| 56 | return getBadgedIcon(appInfo, UserHandle.getUserId(appInfo.uid)); |
| 57 | } |
| 58 | |
| 59 | public Drawable getBadgedIcon(ApplicationInfo appInfo, @UserIdInt int userId) { |
| 60 | return getBadgedIcon(appInfo, appInfo, userId); |
| 61 | } |
| 62 | |
Mathew Inwood | 4eb56ab | 2018-08-14 17:24:32 +0100 | [diff] [blame] | 63 | @UnsupportedAppUsage |
Sunny Goyal | bab3075 | 2017-04-12 15:36:42 -0700 | [diff] [blame] | 64 | public Drawable getBadgedIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo, |
| 65 | @UserIdInt int userId) { |
| 66 | Drawable icon = mPm.loadUnbadgedItemIcon(itemInfo, appInfo); |
| 67 | if (!mEmbedShadow && !needsBadging(appInfo, userId)) { |
| 68 | return icon; |
| 69 | } |
| 70 | |
Hyunyoung Song | dadb9e1 | 2017-08-24 18:08:35 -0700 | [diff] [blame] | 71 | icon = getShadowedIcon(icon); |
Sunny Goyal | bab3075 | 2017-04-12 15:36:42 -0700 | [diff] [blame] | 72 | if (appInfo.isInstantApp()) { |
| 73 | int badgeColor = Resources.getSystem().getColor( |
| 74 | com.android.internal.R.color.instant_app_badge, null); |
| 75 | icon = mLauncherIcons.getBadgedDrawable(icon, |
| 76 | com.android.internal.R.drawable.ic_instant_icon_badge_bolt, |
| 77 | badgeColor); |
| 78 | } |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 79 | if (mUm.hasBadge(userId)) { |
Sunny Goyal | bab3075 | 2017-04-12 15:36:42 -0700 | [diff] [blame] | 80 | icon = mLauncherIcons.getBadgedDrawable(icon, |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 81 | mUm.getUserIconBadgeResId(userId), |
| 82 | mUm.getUserBadgeColor(userId)); |
Sunny Goyal | bab3075 | 2017-04-12 15:36:42 -0700 | [diff] [blame] | 83 | } |
| 84 | return icon; |
| 85 | } |
| 86 | |
Hyunyoung Song | dadb9e1 | 2017-08-24 18:08:35 -0700 | [diff] [blame] | 87 | /** |
| 88 | * Add shadow to the icon if {@link AdaptiveIconDrawable} |
| 89 | */ |
| 90 | public Drawable getShadowedIcon(Drawable icon) { |
| 91 | return mLauncherIcons.wrapIconDrawableWithShadow(icon); |
| 92 | } |
| 93 | |
Mathew Inwood | 4eb56ab | 2018-08-14 17:24:32 +0100 | [diff] [blame] | 94 | @UnsupportedAppUsage |
Sunny Goyal | bab3075 | 2017-04-12 15:36:42 -0700 | [diff] [blame] | 95 | public static IconDrawableFactory newInstance(Context context) { |
| 96 | return new IconDrawableFactory(context, true); |
| 97 | } |
| 98 | |
| 99 | public static IconDrawableFactory newInstance(Context context, boolean embedShadow) { |
| 100 | return new IconDrawableFactory(context, embedShadow); |
| 101 | } |
| 102 | } |