10b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey/* 20b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey * Copyright (C) 2013 The Android Open Source Project 30b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey * 40b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License"); 50b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey * you may not use this file except in compliance with the License. 60b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey * You may obtain a copy of the License at 70b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey * 80b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey * http://www.apache.org/licenses/LICENSE-2.0 90b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey * 100b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey * Unless required by applicable law or agreed to in writing, software 110b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS, 120b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 130b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey * See the License for the specific language governing permissions and 140b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey * limitations under the License. 150b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey */ 160b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey 170b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkeypackage com.android.documentsui; 180b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey 190b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkeyimport android.content.Context; 200b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkeyimport android.content.pm.PackageManager; 210b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkeyimport android.content.pm.ProviderInfo; 220b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkeyimport android.graphics.drawable.Drawable; 230b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkeyimport android.provider.DocumentsContract.Document; 24c29dd61cbfc9f073239bdce3f4fe397deae2c623Jeff Sharkeyimport android.util.TypedValue; 250b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey 260b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkeypublic class IconUtils { 270b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey public static Drawable loadPackageIcon(Context context, String authority, int icon) { 280b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey if (icon != 0) { 290b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey if (authority != null) { 300b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey final PackageManager pm = context.getPackageManager(); 310b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey final ProviderInfo info = pm.resolveContentProvider(authority, 0); 320b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey if (info != null) { 330b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey return pm.getDrawable(info.packageName, icon, info.applicationInfo); 340b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey } 350b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey } else { 3603d30a573b8bc8e169e153a0fffa053ffedcd5eeAlan Viverette return context.getDrawable(icon); 370b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey } 380b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey } 390b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey return null; 400b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey } 410b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey 42563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey public static Drawable loadMimeIcon( 43563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey Context context, String mimeType, String authority, String docId, int mode) { 44563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey if (Document.MIME_TYPE_DIR.equals(mimeType)) { 457a3b88cf56ded3afb9bd6d1ad150b2d6b1f2f4d7Steve McKay if (mode == State.MODE_GRID) { 4603d30a573b8bc8e169e153a0fffa053ffedcd5eeAlan Viverette return context.getDrawable(R.drawable.ic_grid_folder); 47563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey } else { 48c29d2b04927b9d28089d79cbaa63a6a91db1bb5aDaniel Nishi return context.getDrawable(com.android.internal.R.drawable.ic_doc_folder); 49563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey } 50563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey } 51563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey 52563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey return loadMimeIcon(context, mimeType); 53563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey } 54563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey 550b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey public static Drawable loadMimeIcon(Context context, String mimeType) { 56c29d2b04927b9d28089d79cbaa63a6a91db1bb5aDaniel Nishi return context.getContentResolver().getTypeDrawable(mimeType); 570b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey } 58c29dd61cbfc9f073239bdce3f4fe397deae2c623Jeff Sharkey 59cbce47001e15be85b084c36a64c20039a0c4a667Jeff Sharkey public static Drawable applyTintColor(Context context, int drawableId, int tintColorId) { 6003d30a573b8bc8e169e153a0fffa053ffedcd5eeAlan Viverette final Drawable icon = context.getDrawable(drawableId); 61c29dd61cbfc9f073239bdce3f4fe397deae2c623Jeff Sharkey icon.mutate(); 624a357cd2e55293402d7172766f7f9419815fc1e8Alan Viverette icon.setTintList(context.getColorStateList(tintColorId)); 63c29dd61cbfc9f073239bdce3f4fe397deae2c623Jeff Sharkey return icon; 64c29dd61cbfc9f073239bdce3f4fe397deae2c623Jeff Sharkey } 65cbce47001e15be85b084c36a64c20039a0c4a667Jeff Sharkey 66cbce47001e15be85b084c36a64c20039a0c4a667Jeff Sharkey public static Drawable applyTintAttr(Context context, int drawableId, int tintAttrId) { 67cbce47001e15be85b084c36a64c20039a0c4a667Jeff Sharkey final TypedValue outValue = new TypedValue(); 68cbce47001e15be85b084c36a64c20039a0c4a667Jeff Sharkey context.getTheme().resolveAttribute(tintAttrId, outValue, true); 69cbce47001e15be85b084c36a64c20039a0c4a667Jeff Sharkey return applyTintColor(context, drawableId, outValue.resourceId); 70cbce47001e15be85b084c36a64c20039a0c4a667Jeff Sharkey } 710b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey} 72