IconUtils.java revision c29d2b04927b9d28089d79cbaa63a6a91db1bb5a
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)) { 45563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey // TODO: eventually move these hacky assets into that package 46563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey if ("com.android.providers.media.documents".equals(authority) 47563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey && docId.startsWith("album")) { 4803d30a573b8bc8e169e153a0fffa053ffedcd5eeAlan Viverette return context.getDrawable(R.drawable.ic_doc_album); 49563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey } 50563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey 517a3b88cf56ded3afb9bd6d1ad150b2d6b1f2f4d7Steve McKay if (mode == State.MODE_GRID) { 5203d30a573b8bc8e169e153a0fffa053ffedcd5eeAlan Viverette return context.getDrawable(R.drawable.ic_grid_folder); 53563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey } else { 54c29d2b04927b9d28089d79cbaa63a6a91db1bb5aDaniel Nishi return context.getDrawable(com.android.internal.R.drawable.ic_doc_folder); 55563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey } 56563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey } 57563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey 58563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey return loadMimeIcon(context, mimeType); 59563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey } 60563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey 610b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey public static Drawable loadMimeIcon(Context context, String mimeType) { 62c29d2b04927b9d28089d79cbaa63a6a91db1bb5aDaniel Nishi return context.getContentResolver().getTypeDrawable(mimeType); 630b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey } 64c29dd61cbfc9f073239bdce3f4fe397deae2c623Jeff Sharkey 65cbce47001e15be85b084c36a64c20039a0c4a667Jeff Sharkey public static Drawable applyTintColor(Context context, int drawableId, int tintColorId) { 6603d30a573b8bc8e169e153a0fffa053ffedcd5eeAlan Viverette final Drawable icon = context.getDrawable(drawableId); 67c29dd61cbfc9f073239bdce3f4fe397deae2c623Jeff Sharkey icon.mutate(); 684a357cd2e55293402d7172766f7f9419815fc1e8Alan Viverette icon.setTintList(context.getColorStateList(tintColorId)); 69c29dd61cbfc9f073239bdce3f4fe397deae2c623Jeff Sharkey return icon; 70c29dd61cbfc9f073239bdce3f4fe397deae2c623Jeff Sharkey } 71cbce47001e15be85b084c36a64c20039a0c4a667Jeff Sharkey 72cbce47001e15be85b084c36a64c20039a0c4a667Jeff Sharkey public static Drawable applyTintAttr(Context context, int drawableId, int tintAttrId) { 73cbce47001e15be85b084c36a64c20039a0c4a667Jeff Sharkey final TypedValue outValue = new TypedValue(); 74cbce47001e15be85b084c36a64c20039a0c4a667Jeff Sharkey context.getTheme().resolveAttribute(tintAttrId, outValue, true); 75cbce47001e15be85b084c36a64c20039a0c4a667Jeff Sharkey return applyTintColor(context, drawableId, outValue.resourceId); 76cbce47001e15be85b084c36a64c20039a0c4a667Jeff Sharkey } 770b14db3cf5eac43736462999337c9a3efdc1ac81Jeff Sharkey} 78