blob: 46d0321242860ef7e2a7ea081d184e86139d9e0a [file] [log] [blame]
Jeff Sharkey5629ec52013-09-04 18:03:18 -07001/*
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
17package com.android.documentsui;
18
19import android.content.Context;
20import android.content.pm.PackageManager;
21import android.content.pm.ProviderInfo;
Jeff Sharkey5629ec52013-09-04 18:03:18 -070022import android.graphics.drawable.Drawable;
Jeff Sharkey34c54092014-08-08 13:08:56 -070023import android.util.TypedValue;
Jeff Sharkey5629ec52013-09-04 18:03:18 -070024
Bill Lin49bef292018-11-30 21:51:47 +080025import com.android.documentsui.base.MimeTypes;
26
Jeff Sharkey5629ec52013-09-04 18:03:18 -070027public class IconUtils {
Jeff Sharkey5629ec52013-09-04 18:03:18 -070028 public static Drawable loadPackageIcon(Context context, String authority, int icon) {
29 if (icon != 0) {
30 if (authority != null) {
31 final PackageManager pm = context.getPackageManager();
32 final ProviderInfo info = pm.resolveContentProvider(authority, 0);
33 if (info != null) {
34 return pm.getDrawable(info.packageName, icon, info.applicationInfo);
35 }
36 } else {
Alan Viverette61791442014-08-14 12:59:10 -070037 return context.getDrawable(icon);
Jeff Sharkey5629ec52013-09-04 18:03:18 -070038 }
39 }
40 return null;
41 }
42
Jeff Sharkey2ceff512013-09-18 18:03:49 -070043 public static Drawable loadMimeIcon(
44 Context context, String mimeType, String authority, String docId, int mode) {
Jeff Sharkey2ceff512013-09-18 18:03:49 -070045 return loadMimeIcon(context, mimeType);
46 }
47
Bill Lin49bef292018-11-30 21:51:47 +080048 /**
49 * Load mime type drawable from system MimeIconUtils.
50 * @param context activity context to obtain resource
51 * @param mimeType specific mime type string of file
52 * @return drawable of mime type files from system default
53 */
Jeff Sharkey5629ec52013-09-04 18:03:18 -070054 public static Drawable loadMimeIcon(Context context, String mimeType) {
Jeff Sharkey6396c472019-02-19 14:43:04 -070055 if (mimeType == null) return null;
56 return context.getContentResolver().getTypeInfo(mimeType).getIcon().loadDrawable(context);
Jeff Sharkey5629ec52013-09-04 18:03:18 -070057 }
Jeff Sharkey34c54092014-08-08 13:08:56 -070058
Jeff Sharkey7e544612014-08-29 15:38:27 -070059 public static Drawable applyTintColor(Context context, int drawableId, int tintColorId) {
Alan Viverette61791442014-08-14 12:59:10 -070060 final Drawable icon = context.getDrawable(drawableId);
Bill Lin49bef292018-11-30 21:51:47 +080061 return applyTintColor(context, icon, tintColorId);
62 }
63
64 public static Drawable applyTintColor(Context context, Drawable icon, int tintColorId) {
Jeff Sharkey34c54092014-08-08 13:08:56 -070065 icon.mutate();
Alan Viverette09195012015-03-18 18:37:18 -070066 icon.setTintList(context.getColorStateList(tintColorId));
Jeff Sharkey34c54092014-08-08 13:08:56 -070067 return icon;
68 }
Jeff Sharkey7e544612014-08-29 15:38:27 -070069
70 public static Drawable applyTintAttr(Context context, int drawableId, int tintAttrId) {
71 final TypedValue outValue = new TypedValue();
72 context.getTheme().resolveAttribute(tintAttrId, outValue, true);
73 return applyTintColor(context, drawableId, outValue.resourceId);
74 }
Jeff Sharkey5629ec52013-09-04 18:03:18 -070075}