1 /*
2  * Copyright (C) 2013 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.documentsui;
18 
19 import android.content.Context;
20 import android.content.pm.PackageManager;
21 import android.content.pm.ProviderInfo;
22 import android.graphics.drawable.Drawable;
23 import android.util.TypedValue;
24 
25 import com.android.documentsui.base.UserId;
26 
27 public class IconUtils {
loadPackageIcon(Context context, UserId userId, String authority, int icon, boolean maybeShowBadge)28     public static Drawable loadPackageIcon(Context context, UserId userId, String authority,
29             int icon, boolean maybeShowBadge) {
30         if (icon != 0) {
31             final PackageManager pm = userId.getPackageManager(context);
32             Drawable packageIcon = null;
33             if (authority != null) {
34                 final ProviderInfo info = pm.resolveContentProvider(authority, 0);
35                 if (info != null) {
36                     packageIcon = pm.getDrawable(info.packageName, icon, info.applicationInfo);
37                 }
38             } else {
39                 packageIcon = userId.getDrawable(context, icon);
40             }
41             if (packageIcon != null && maybeShowBadge) {
42                 return userId.getUserBadgedIcon(context, packageIcon);
43             } else {
44                 return packageIcon;
45             }
46         }
47 
48         return null;
49     }
50 
loadMimeIcon( Context context, String mimeType, String authority, String docId, int mode)51     public static Drawable loadMimeIcon(
52             Context context, String mimeType, String authority, String docId, int mode) {
53         return loadMimeIcon(context, mimeType);
54     }
55 
56     /**
57      * Load mime type drawable from system MimeIconUtils.
58      * @param context activity context to obtain resource
59      * @param mimeType specific mime type string of file
60      * @return drawable of mime type files from system default
61      */
loadMimeIcon(Context context, String mimeType)62     public static Drawable loadMimeIcon(Context context, String mimeType) {
63         if (mimeType == null) return null;
64         return context.getContentResolver().getTypeInfo(mimeType).getIcon().loadDrawable(context);
65     }
66 
applyTintColor(Context context, int drawableId, int tintColorId)67     public static Drawable applyTintColor(Context context, int drawableId, int tintColorId) {
68         final Drawable icon = context.getDrawable(drawableId);
69         return applyTintColor(context, icon, tintColorId);
70     }
71 
applyTintColor(Context context, Drawable icon, int tintColorId)72     public static Drawable applyTintColor(Context context, Drawable icon, int tintColorId) {
73         icon.mutate();
74         icon.setTintList(context.getColorStateList(tintColorId));
75         return icon;
76     }
77 
applyTintAttr(Context context, int drawableId, int tintAttrId)78     public static Drawable applyTintAttr(Context context, int drawableId, int tintAttrId) {
79         final TypedValue outValue = new TypedValue();
80         context.getTheme().resolveAttribute(tintAttrId, outValue, true);
81         return applyTintColor(context, drawableId, outValue.resourceId);
82     }
83 }
84