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 import com.google.android.collect.Maps; 27 28 import java.util.HashMap; 29 30 public class IconUtils { 31 32 private static HashMap<String, Integer> sMimeIcons = Maps.newHashMap(); 33 add(String mimeType, int resId)34 private static void add(String mimeType, int resId) { 35 if (sMimeIcons.put(mimeType, resId) != null) { 36 throw new RuntimeException(mimeType + " already registered!"); 37 } 38 } 39 40 static { 41 int icon; 42 43 // Package 44 icon = R.drawable.ic_doc_apk; 45 add("application/vnd.android.package-archive", icon); 46 47 // Audio 48 icon = R.drawable.ic_doc_audio; 49 add("application/ogg", icon); 50 add("application/x-flac", icon); 51 52 // Certificate 53 icon = R.drawable.ic_doc_certificate; 54 add("application/pgp-keys", icon); 55 add("application/pgp-signature", icon); 56 add("application/x-pkcs12", icon); 57 add("application/x-pkcs7-certreqresp", icon); 58 add("application/x-pkcs7-crl", icon); 59 add("application/x-x509-ca-cert", icon); 60 add("application/x-x509-user-cert", icon); 61 add("application/x-pkcs7-certificates", icon); 62 add("application/x-pkcs7-mime", icon); 63 add("application/x-pkcs7-signature", icon); 64 65 // Source code 66 icon = R.drawable.ic_doc_codes; 67 add("application/rdf+xml", icon); 68 add("application/rss+xml", icon); 69 add("application/x-object", icon); 70 add("application/xhtml+xml", icon); 71 add("text/css", icon); 72 add("text/html", icon); 73 add("text/xml", icon); 74 add("text/x-c++hdr", icon); 75 add("text/x-c++src", icon); 76 add("text/x-chdr", icon); 77 add("text/x-csrc", icon); 78 add("text/x-dsrc", icon); 79 add("text/x-csh", icon); 80 add("text/x-haskell", icon); 81 add("text/x-java", icon); 82 add("text/x-literate-haskell", icon); 83 add("text/x-pascal", icon); 84 add("text/x-tcl", icon); 85 add("text/x-tex", icon); 86 add("application/x-latex", icon); 87 add("application/x-texinfo", icon); 88 add("application/atom+xml", icon); 89 add("application/ecmascript", icon); 90 add("application/json", icon); 91 add("application/javascript", icon); 92 add("application/xml", icon); 93 add("text/javascript", icon); 94 add("application/x-javascript", icon); 95 96 // Compressed 97 icon = R.drawable.ic_doc_compressed; 98 add("application/mac-binhex40", icon); 99 add("application/rar", icon); 100 add("application/zip", icon); 101 add("application/x-apple-diskimage", icon); 102 add("application/x-debian-package", icon); 103 add("application/x-gtar", icon); 104 add("application/x-iso9660-image", icon); 105 add("application/x-lha", icon); 106 add("application/x-lzh", icon); 107 add("application/x-lzx", icon); 108 add("application/x-stuffit", icon); 109 add("application/x-tar", icon); 110 add("application/x-webarchive", icon); 111 add("application/x-webarchive-xml", icon); 112 add("application/gzip", icon); 113 add("application/x-7z-compressed", icon); 114 add("application/x-deb", icon); 115 add("application/x-rar-compressed", icon); 116 117 // Contact 118 icon = R.drawable.ic_doc_contact; 119 add("text/x-vcard", icon); 120 add("text/vcard", icon); 121 122 // Event 123 icon = R.drawable.ic_doc_event; 124 add("text/calendar", icon); 125 add("text/x-vcalendar", icon); 126 127 // Font 128 icon = R.drawable.ic_doc_font; 129 add("application/x-font", icon); 130 add("application/font-woff", icon); 131 add("application/x-font-woff", icon); 132 add("application/x-font-ttf", icon); 133 134 // Image 135 icon = R.drawable.ic_doc_image; 136 add("application/vnd.oasis.opendocument.graphics", icon); 137 add("application/vnd.oasis.opendocument.graphics-template", icon); 138 add("application/vnd.oasis.opendocument.image", icon); 139 add("application/vnd.stardivision.draw", icon); 140 add("application/vnd.sun.xml.draw", icon); 141 add("application/vnd.sun.xml.draw.template", icon); 142 143 // PDF 144 icon = R.drawable.ic_doc_pdf; 145 add("application/pdf", icon); 146 147 // Presentation 148 icon = R.drawable.ic_doc_presentation; 149 add("application/vnd.stardivision.impress", icon); 150 add("application/vnd.sun.xml.impress", icon); 151 add("application/vnd.sun.xml.impress.template", icon); 152 add("application/x-kpresenter", icon); 153 add("application/vnd.oasis.opendocument.presentation", icon); 154 155 // Spreadsheet 156 icon = R.drawable.ic_doc_spreadsheet; 157 add("application/vnd.oasis.opendocument.spreadsheet", icon); 158 add("application/vnd.oasis.opendocument.spreadsheet-template", icon); 159 add("application/vnd.stardivision.calc", icon); 160 add("application/vnd.sun.xml.calc", icon); 161 add("application/vnd.sun.xml.calc.template", icon); 162 add("application/x-kspread", icon); 163 164 // Text 165 icon = R.drawable.ic_doc_text; 166 add("application/vnd.oasis.opendocument.text", icon); 167 add("application/vnd.oasis.opendocument.text-master", icon); 168 add("application/vnd.oasis.opendocument.text-template", icon); 169 add("application/vnd.oasis.opendocument.text-web", icon); 170 add("application/vnd.stardivision.writer", icon); 171 add("application/vnd.stardivision.writer-global", icon); 172 add("application/vnd.sun.xml.writer", icon); 173 add("application/vnd.sun.xml.writer.global", icon); 174 add("application/vnd.sun.xml.writer.template", icon); 175 add("application/x-abiword", icon); 176 add("application/x-kword", icon); 177 178 // Video 179 icon = R.drawable.ic_doc_video; 180 add("application/x-quicktimeplayer", icon); 181 add("application/x-shockwave-flash", icon); 182 183 // Word 184 icon = R.drawable.ic_doc_word; 185 add("application/msword", icon); 186 add("application/vnd.openxmlformats-officedocument.wordprocessingml.document", icon); 187 add("application/vnd.openxmlformats-officedocument.wordprocessingml.template", icon); 188 189 // Excel 190 icon = R.drawable.ic_doc_excel; 191 add("application/vnd.ms-excel", icon); 192 add("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", icon); 193 add("application/vnd.openxmlformats-officedocument.spreadsheetml.template", icon); 194 195 // Powerpoint 196 icon = R.drawable.ic_doc_powerpoint; 197 add("application/vnd.ms-powerpoint", icon); 198 add("application/vnd.openxmlformats-officedocument.presentationml.presentation", icon); 199 add("application/vnd.openxmlformats-officedocument.presentationml.template", icon); 200 add("application/vnd.openxmlformats-officedocument.presentationml.slideshow", icon); 201 } 202 loadPackageIcon(Context context, String authority, int icon)203 public static Drawable loadPackageIcon(Context context, String authority, int icon) { 204 if (icon != 0) { 205 if (authority != null) { 206 final PackageManager pm = context.getPackageManager(); 207 final ProviderInfo info = pm.resolveContentProvider(authority, 0); 208 if (info != null) { 209 return pm.getDrawable(info.packageName, icon, info.applicationInfo); 210 } 211 } else { 212 return context.getDrawable(icon); 213 } 214 } 215 return null; 216 } 217 loadMimeIcon( Context context, String mimeType, String authority, String docId, int mode)218 public static Drawable loadMimeIcon( 219 Context context, String mimeType, String authority, String docId, int mode) { 220 if (Document.MIME_TYPE_DIR.equals(mimeType)) { 221 // TODO: eventually move these hacky assets into that package 222 if ("com.android.providers.media.documents".equals(authority) 223 && docId.startsWith("album")) { 224 return context.getDrawable(R.drawable.ic_doc_album); 225 } 226 227 if (mode == DocumentsActivity.State.MODE_GRID) { 228 return context.getDrawable(R.drawable.ic_grid_folder); 229 } else { 230 return context.getDrawable(R.drawable.ic_doc_folder); 231 } 232 } 233 234 return loadMimeIcon(context, mimeType); 235 } 236 loadMimeIcon(Context context, String mimeType)237 public static Drawable loadMimeIcon(Context context, String mimeType) { 238 if (Document.MIME_TYPE_DIR.equals(mimeType)) { 239 return context.getDrawable(R.drawable.ic_doc_folder); 240 } 241 242 // Look for exact match first 243 Integer resId = sMimeIcons.get(mimeType); 244 if (resId != null) { 245 return context.getDrawable(resId); 246 } 247 248 if (mimeType == null) { 249 // TODO: generic icon? 250 return null; 251 } 252 253 // Otherwise look for partial match 254 final String typeOnly = mimeType.split("/")[0]; 255 if ("audio".equals(typeOnly)) { 256 return context.getDrawable(R.drawable.ic_doc_audio); 257 } else if ("image".equals(typeOnly)) { 258 return context.getDrawable(R.drawable.ic_doc_image); 259 } else if ("text".equals(typeOnly)) { 260 return context.getDrawable(R.drawable.ic_doc_text); 261 } else if ("video".equals(typeOnly)) { 262 return context.getDrawable(R.drawable.ic_doc_video); 263 } else { 264 return context.getDrawable(R.drawable.ic_doc_generic); 265 } 266 } 267 applyTintColor(Context context, int drawableId, int tintColorId)268 public static Drawable applyTintColor(Context context, int drawableId, int tintColorId) { 269 final Drawable icon = context.getDrawable(drawableId); 270 icon.mutate(); 271 icon.setTintList(context.getColorStateList(tintColorId)); 272 return icon; 273 } 274 applyTintAttr(Context context, int drawableId, int tintAttrId)275 public static Drawable applyTintAttr(Context context, int drawableId, int tintAttrId) { 276 final TypedValue outValue = new TypedValue(); 277 context.getTheme().resolveAttribute(tintAttrId, outValue, true); 278 return applyTintColor(context, drawableId, outValue.resourceId); 279 } 280 } 281