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