Home | History | Annotate | Download | only in documentsui
      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.provider.DocumentsContract.Document;
     24 import android.util.TypedValue;
     25 
     26 public class IconUtils {
     27     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 {
     36                 return context.getDrawable(icon);
     37             }
     38         }
     39         return null;
     40     }
     41 
     42     public static Drawable loadMimeIcon(
     43             Context context, String mimeType, String authority, String docId, int mode) {
     44         if (Document.MIME_TYPE_DIR.equals(mimeType)) {
     45             if (mode == State.MODE_GRID) {
     46                 return context.getDrawable(R.drawable.ic_grid_folder);
     47             } else {
     48                 return context.getDrawable(com.android.internal.R.drawable.ic_doc_folder);
     49             }
     50         }
     51 
     52         return loadMimeIcon(context, mimeType);
     53     }
     54 
     55     public static Drawable loadMimeIcon(Context context, String mimeType) {
     56         return context.getContentResolver().getTypeDrawable(mimeType);
     57     }
     58 
     59     public static Drawable applyTintColor(Context context, int drawableId, int tintColorId) {
     60         final Drawable icon = context.getDrawable(drawableId);
     61         icon.mutate();
     62         icon.setTintList(context.getColorStateList(tintColorId));
     63         return icon;
     64     }
     65 
     66     public static Drawable applyTintAttr(Context context, int drawableId, int tintAttrId) {
     67         final TypedValue outValue = new TypedValue();
     68         context.getTheme().resolveAttribute(tintAttrId, outValue, true);
     69         return applyTintColor(context, drawableId, outValue.resourceId);
     70     }
     71 }
     72