Jeff Sharkey | 5629ec5 | 2013-09-04 18:03:18 -0700 | [diff] [blame] | 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; |
Jeff Sharkey | 5629ec5 | 2013-09-04 18:03:18 -0700 | [diff] [blame] | 22 | import android.graphics.drawable.Drawable; |
| 23 | import android.provider.DocumentsContract.Document; |
Jeff Sharkey | 34c5409 | 2014-08-08 13:08:56 -0700 | [diff] [blame] | 24 | import android.util.TypedValue; |
Jeff Sharkey | 5629ec5 | 2013-09-04 18:03:18 -0700 | [diff] [blame] | 25 | |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 26 | import com.android.documentsui.base.State; |
| 27 | |
Jeff Sharkey | 5629ec5 | 2013-09-04 18:03:18 -0700 | [diff] [blame] | 28 | public class IconUtils { |
Jeff Sharkey | 5629ec5 | 2013-09-04 18:03:18 -0700 | [diff] [blame] | 29 | public static Drawable loadPackageIcon(Context context, String authority, int icon) { |
| 30 | if (icon != 0) { |
| 31 | if (authority != null) { |
| 32 | final PackageManager pm = context.getPackageManager(); |
| 33 | final ProviderInfo info = pm.resolveContentProvider(authority, 0); |
| 34 | if (info != null) { |
| 35 | return pm.getDrawable(info.packageName, icon, info.applicationInfo); |
| 36 | } |
| 37 | } else { |
Alan Viverette | 6179144 | 2014-08-14 12:59:10 -0700 | [diff] [blame] | 38 | return context.getDrawable(icon); |
Jeff Sharkey | 5629ec5 | 2013-09-04 18:03:18 -0700 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | return null; |
| 42 | } |
| 43 | |
Jeff Sharkey | 2ceff51 | 2013-09-18 18:03:49 -0700 | [diff] [blame] | 44 | public static Drawable loadMimeIcon( |
| 45 | Context context, String mimeType, String authority, String docId, int mode) { |
Jeff Sharkey | 2ceff51 | 2013-09-18 18:03:49 -0700 | [diff] [blame] | 46 | if (Document.MIME_TYPE_DIR.equals(mimeType)) { |
| 47 | // TODO: eventually move these hacky assets into that package |
| 48 | if ("com.android.providers.media.documents".equals(authority) |
| 49 | && docId.startsWith("album")) { |
Alan Viverette | 6179144 | 2014-08-14 12:59:10 -0700 | [diff] [blame] | 50 | return context.getDrawable(R.drawable.ic_doc_album); |
Jeff Sharkey | 2ceff51 | 2013-09-18 18:03:49 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 53 | if (mode == State.MODE_GRID) { |
Alan Viverette | 6179144 | 2014-08-14 12:59:10 -0700 | [diff] [blame] | 54 | return context.getDrawable(R.drawable.ic_grid_folder); |
Jeff Sharkey | 2ceff51 | 2013-09-18 18:03:49 -0700 | [diff] [blame] | 55 | } else { |
Daniel Nishi | f8ed15b | 2016-06-30 12:20:41 -0700 | [diff] [blame] | 56 | return context.getDrawable(com.android.internal.R.drawable.ic_doc_folder); |
Jeff Sharkey | 2ceff51 | 2013-09-18 18:03:49 -0700 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | return loadMimeIcon(context, mimeType); |
| 61 | } |
| 62 | |
Jeff Sharkey | 5629ec5 | 2013-09-04 18:03:18 -0700 | [diff] [blame] | 63 | public static Drawable loadMimeIcon(Context context, String mimeType) { |
Daniel Nishi | f8ed15b | 2016-06-30 12:20:41 -0700 | [diff] [blame] | 64 | return context.getContentResolver().getTypeDrawable(mimeType); |
Jeff Sharkey | 5629ec5 | 2013-09-04 18:03:18 -0700 | [diff] [blame] | 65 | } |
Jeff Sharkey | 34c5409 | 2014-08-08 13:08:56 -0700 | [diff] [blame] | 66 | |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 67 | public static Drawable applyTintColor(Context context, int drawableId, int tintColorId) { |
Alan Viverette | 6179144 | 2014-08-14 12:59:10 -0700 | [diff] [blame] | 68 | final Drawable icon = context.getDrawable(drawableId); |
Jeff Sharkey | 34c5409 | 2014-08-08 13:08:56 -0700 | [diff] [blame] | 69 | icon.mutate(); |
Alan Viverette | 0919501 | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 70 | icon.setTintList(context.getColorStateList(tintColorId)); |
Jeff Sharkey | 34c5409 | 2014-08-08 13:08:56 -0700 | [diff] [blame] | 71 | return icon; |
| 72 | } |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 73 | |
| 74 | public static Drawable applyTintAttr(Context context, int drawableId, int tintAttrId) { |
| 75 | final TypedValue outValue = new TypedValue(); |
| 76 | context.getTheme().resolveAttribute(tintAttrId, outValue, true); |
| 77 | return applyTintColor(context, drawableId, outValue.resourceId); |
| 78 | } |
Jeff Sharkey | 5629ec5 | 2013-09-04 18:03:18 -0700 | [diff] [blame] | 79 | } |