blob: d86ebf3a44b341b56a1f526da0e70c205d449f73 [file] [log] [blame]
Sunny Goyalbab30752017-04-12 15:36:42 -07001/*
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 */
16package android.util;
17
Mathew Inwood4eb56ab2018-08-14 17:24:32 +010018import android.annotation.UnsupportedAppUsage;
Sunny Goyalbab30752017-04-12 15:36:42 -070019import android.annotation.UserIdInt;
20import android.content.Context;
21import android.content.pm.ApplicationInfo;
22import android.content.pm.PackageItemInfo;
23import android.content.pm.PackageManager;
24import android.content.res.Resources;
Sunny Goyalbab30752017-04-12 15:36:42 -070025import android.graphics.drawable.Drawable;
26import android.os.UserHandle;
27import android.os.UserManager;
28
Sunny Goyalbab30752017-04-12 15:36:42 -070029/**
30 * Utility class to load app drawables with appropriate badging.
31 *
32 * @hide
33 */
34public 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 Inwood4eb56ab2018-08-14 17:24:32 +010054 @UnsupportedAppUsage
Sunny Goyalbab30752017-04-12 15:36:42 -070055 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 Inwood4eb56ab2018-08-14 17:24:32 +010063 @UnsupportedAppUsage
Sunny Goyalbab30752017-04-12 15:36:42 -070064 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 Songdadb9e12017-08-24 18:08:35 -070071 icon = getShadowedIcon(icon);
Sunny Goyalbab30752017-04-12 15:36:42 -070072 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 }
Bookatz029832a2019-10-04 16:50:22 -070079 if (mUm.hasBadge(userId)) {
Sunny Goyalbab30752017-04-12 15:36:42 -070080 icon = mLauncherIcons.getBadgedDrawable(icon,
Bookatz029832a2019-10-04 16:50:22 -070081 mUm.getUserIconBadgeResId(userId),
82 mUm.getUserBadgeColor(userId));
Sunny Goyalbab30752017-04-12 15:36:42 -070083 }
84 return icon;
85 }
86
Hyunyoung Songdadb9e12017-08-24 18:08:35 -070087 /**
88 * Add shadow to the icon if {@link AdaptiveIconDrawable}
89 */
90 public Drawable getShadowedIcon(Drawable icon) {
91 return mLauncherIcons.wrapIconDrawableWithShadow(icon);
92 }
93
Mathew Inwood4eb56ab2018-08-14 17:24:32 +010094 @UnsupportedAppUsage
Sunny Goyalbab30752017-04-12 15:36:42 -070095 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}