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 
18 import android.annotation.UserIdInt;
19 import android.compat.annotation.UnsupportedAppUsage;
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;
25 import android.graphics.drawable.Drawable;
26 import android.os.Build;
27 import android.os.UserHandle;
28 import android.os.UserManager;
29 
30 /**
31  * Utility class to load app drawables with appropriate badging.
32  *
33  * @hide
34  */
35 public class IconDrawableFactory {
36 
37     protected final Context mContext;
38     protected final PackageManager mPm;
39     protected final UserManager mUm;
40     protected final LauncherIcons mLauncherIcons;
41     protected final boolean mEmbedShadow;
42 
IconDrawableFactory(Context context, boolean embedShadow)43     private IconDrawableFactory(Context context, boolean embedShadow) {
44         mContext = context;
45         mPm = context.getPackageManager();
46         mUm = context.getSystemService(UserManager.class);
47         mLauncherIcons = new LauncherIcons(context);
48         mEmbedShadow = embedShadow;
49     }
50 
needsBadging(ApplicationInfo appInfo, @UserIdInt int userId)51     protected boolean needsBadging(ApplicationInfo appInfo, @UserIdInt int userId) {
52         return appInfo.isInstantApp() || mUm.hasBadge(userId);
53     }
54 
55     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
getBadgedIcon(ApplicationInfo appInfo)56     public Drawable getBadgedIcon(ApplicationInfo appInfo) {
57         return getBadgedIcon(appInfo, UserHandle.getUserId(appInfo.uid));
58     }
59 
getBadgedIcon(ApplicationInfo appInfo, @UserIdInt int userId)60     public Drawable getBadgedIcon(ApplicationInfo appInfo, @UserIdInt int userId) {
61         return getBadgedIcon(appInfo, appInfo, userId);
62     }
63 
64     @UnsupportedAppUsage
getBadgedIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo, @UserIdInt int userId)65     public Drawable getBadgedIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo,
66             @UserIdInt int userId) {
67         Drawable icon = mPm.loadUnbadgedItemIcon(itemInfo, appInfo);
68         if (!mEmbedShadow && !needsBadging(appInfo, userId)) {
69             return icon;
70         }
71 
72         icon = getShadowedIcon(icon);
73         if (appInfo.isInstantApp()) {
74             int badgeColor = Resources.getSystem().getColor(
75                     com.android.internal.R.color.instant_app_badge, null);
76             icon = mLauncherIcons.getBadgedDrawable(icon,
77                     com.android.internal.R.drawable.ic_instant_icon_badge_bolt,
78                     badgeColor);
79         }
80         if (mUm.hasBadge(userId)) {
81             icon = mLauncherIcons.getBadgedDrawable(icon,
82                     mUm.getUserIconBadgeResId(userId),
83                     mUm.getUserBadgeColor(userId));
84         }
85         return icon;
86     }
87 
88     /**
89      * Add shadow to the icon if {@link AdaptiveIconDrawable}
90      */
getShadowedIcon(Drawable icon)91     public Drawable getShadowedIcon(Drawable icon) {
92         return mLauncherIcons.wrapIconDrawableWithShadow(icon);
93     }
94 
95     @UnsupportedAppUsage
newInstance(Context context)96     public static IconDrawableFactory newInstance(Context context) {
97         return new IconDrawableFactory(context, true);
98     }
99 
newInstance(Context context, boolean embedShadow)100     public static IconDrawableFactory newInstance(Context context, boolean embedShadow) {
101         return new IconDrawableFactory(context, embedShadow);
102     }
103 }
104