Home | History | Annotate | Download | only in util
      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.content.Context;
     20 import android.content.pm.ApplicationInfo;
     21 import android.content.pm.PackageItemInfo;
     22 import android.content.pm.PackageManager;
     23 import android.content.res.Resources;
     24 import android.graphics.Color;
     25 import android.graphics.drawable.Drawable;
     26 import android.os.UserHandle;
     27 import android.os.UserManager;
     28 
     29 import com.android.internal.annotations.VisibleForTesting;
     30 
     31 /**
     32  * Utility class to load app drawables with appropriate badging.
     33  *
     34  * @hide
     35  */
     36 public class IconDrawableFactory {
     37 
     38     protected final Context mContext;
     39     protected final PackageManager mPm;
     40     protected final UserManager mUm;
     41     protected final LauncherIcons mLauncherIcons;
     42     protected final boolean mEmbedShadow;
     43 
     44     private IconDrawableFactory(Context context, boolean embedShadow) {
     45         mContext = context;
     46         mPm = context.getPackageManager();
     47         mUm = context.getSystemService(UserManager.class);
     48         mLauncherIcons = new LauncherIcons(context);
     49         mEmbedShadow = embedShadow;
     50     }
     51 
     52     protected boolean needsBadging(ApplicationInfo appInfo, @UserIdInt int userId) {
     53         return appInfo.isInstantApp() || mUm.isManagedProfile(userId);
     54     }
     55 
     56     public Drawable getBadgedIcon(ApplicationInfo appInfo) {
     57         return getBadgedIcon(appInfo, UserHandle.getUserId(appInfo.uid));
     58     }
     59 
     60     public Drawable getBadgedIcon(ApplicationInfo appInfo, @UserIdInt int userId) {
     61         return getBadgedIcon(appInfo, appInfo, userId);
     62     }
     63 
     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 
     71         // Before badging, add shadow to adaptive icon if needed.
     72         icon = mLauncherIcons.wrapIconDrawableWithShadow(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.isManagedProfile(userId)) {
     81             icon = mLauncherIcons.getBadgedDrawable(icon,
     82                     com.android.internal.R.drawable.ic_corp_icon_badge_case,
     83                     getUserBadgeColor(mUm, userId));
     84         }
     85         return icon;
     86     }
     87 
     88     // Should have enough colors to cope with UserManagerService.getMaxManagedProfiles()
     89     @VisibleForTesting
     90     public static final int[] CORP_BADGE_COLORS = new int[] {
     91             com.android.internal.R.color.profile_badge_1,
     92             com.android.internal.R.color.profile_badge_2,
     93             com.android.internal.R.color.profile_badge_3
     94     };
     95 
     96     public static int getUserBadgeColor(UserManager um, @UserIdInt int userId) {
     97         int badge = um.getManagedProfileBadge(userId);
     98         if (badge < 0) {
     99             badge = 0;
    100         }
    101         int resourceId = CORP_BADGE_COLORS[badge % CORP_BADGE_COLORS.length];
    102         return Resources.getSystem().getColor(resourceId, null);
    103     }
    104 
    105     public static IconDrawableFactory newInstance(Context context) {
    106         return new IconDrawableFactory(context, true);
    107     }
    108 
    109     public static IconDrawableFactory newInstance(Context context, boolean embedShadow) {
    110         return new IconDrawableFactory(context, embedShadow);
    111     }
    112 }
    113