blob: 7b6678d6b447a7c6c704cda16f86f969eae495ea [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;
23import android.provider.DocumentsContract.Document;
Jeff Sharkey34c54092014-08-08 13:08:56 -070024import android.util.TypedValue;
Jeff Sharkey5629ec52013-09-04 18:03:18 -070025
Jeff Sharkey5629ec52013-09-04 18:03:18 -070026public class IconUtils {
Jeff Sharkey5629ec52013-09-04 18:03:18 -070027 public static Drawable loadPackageIcon(Context context, String authority, int icon) {
28 if (icon != 0) {
29 if (authority != null) {
30 final PackageManager pm = context.getPackageManager();
31 final ProviderInfo info = pm.resolveContentProvider(authority, 0);
32 if (info != null) {
33 return pm.getDrawable(info.packageName, icon, info.applicationInfo);
34 }
35 } else {
Alan Viverette61791442014-08-14 12:59:10 -070036 return context.getDrawable(icon);
Jeff Sharkey5629ec52013-09-04 18:03:18 -070037 }
38 }
39 return null;
40 }
41
Jeff Sharkey2ceff512013-09-18 18:03:49 -070042 public static Drawable loadMimeIcon(
43 Context context, String mimeType, String authority, String docId, int mode) {
Jeff Sharkey2ceff512013-09-18 18:03:49 -070044 if (Document.MIME_TYPE_DIR.equals(mimeType)) {
Ben Linfc84b342017-02-17 11:44:45 -080045 return context.getDrawable(R.drawable.ic_doc_folder);
Jeff Sharkey2ceff512013-09-18 18:03:49 -070046 }
47
48 return loadMimeIcon(context, mimeType);
49 }
50
Jeff Sharkey5629ec52013-09-04 18:03:18 -070051 public static Drawable loadMimeIcon(Context context, String mimeType) {
Daniel Nishif8ed15b2016-06-30 12:20:41 -070052 return context.getContentResolver().getTypeDrawable(mimeType);
Jeff Sharkey5629ec52013-09-04 18:03:18 -070053 }
Jeff Sharkey34c54092014-08-08 13:08:56 -070054
Jeff Sharkey7e544612014-08-29 15:38:27 -070055 public static Drawable applyTintColor(Context context, int drawableId, int tintColorId) {
Alan Viverette61791442014-08-14 12:59:10 -070056 final Drawable icon = context.getDrawable(drawableId);
Jeff Sharkey34c54092014-08-08 13:08:56 -070057 icon.mutate();
Alan Viverette09195012015-03-18 18:37:18 -070058 icon.setTintList(context.getColorStateList(tintColorId));
Jeff Sharkey34c54092014-08-08 13:08:56 -070059 return icon;
60 }
Jeff Sharkey7e544612014-08-29 15:38:27 -070061
62 public static Drawable applyTintAttr(Context context, int drawableId, int tintAttrId) {
63 final TypedValue outValue = new TypedValue();
64 context.getTheme().resolveAttribute(tintAttrId, outValue, true);
65 return applyTintColor(context, drawableId, outValue.resourceId);
66 }
Jeff Sharkey5629ec52013-09-04 18:03:18 -070067}