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.Log; 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 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.ms-powerpoint", icon); 151 add("application/vnd.openxmlformats-officedocument.presentationml.presentation", icon); 152 add("application/vnd.openxmlformats-officedocument.presentationml.template", icon); 153 add("application/vnd.openxmlformats-officedocument.presentationml.slideshow", icon); 154 add("application/vnd.stardivision.impress", icon); 155 add("application/vnd.sun.xml.impress", icon); 156 add("application/vnd.sun.xml.impress.template", icon); 157 add("application/x-kpresenter", icon); 158 add("application/vnd.oasis.opendocument.presentation", icon); 159 160 // Spreadsheet 161 icon = R.drawable.ic_doc_spreadsheet; 162 add("application/vnd.oasis.opendocument.spreadsheet", icon); 163 add("application/vnd.oasis.opendocument.spreadsheet-template", icon); 164 add("application/vnd.ms-excel", icon); 165 add("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", icon); 166 add("application/vnd.openxmlformats-officedocument.spreadsheetml.template", icon); 167 add("application/vnd.stardivision.calc", icon); 168 add("application/vnd.sun.xml.calc", icon); 169 add("application/vnd.sun.xml.calc.template", icon); 170 add("application/x-kspread", icon); 171 172 // Text 173 icon = R.drawable.ic_doc_text; 174 add("application/vnd.oasis.opendocument.text", icon); 175 add("application/vnd.oasis.opendocument.text-master", icon); 176 add("application/vnd.oasis.opendocument.text-template", icon); 177 add("application/vnd.oasis.opendocument.text-web", icon); 178 add("application/msword", icon); 179 add("application/vnd.openxmlformats-officedocument.wordprocessingml.document", icon); 180 add("application/vnd.openxmlformats-officedocument.wordprocessingml.template", icon); 181 add("application/vnd.stardivision.writer", icon); 182 add("application/vnd.stardivision.writer-global", icon); 183 add("application/vnd.sun.xml.writer", icon); 184 add("application/vnd.sun.xml.writer.global", icon); 185 add("application/vnd.sun.xml.writer.template", icon); 186 add("application/x-abiword", icon); 187 add("application/x-kword", icon); 188 189 // Video 190 icon = R.drawable.ic_doc_video; 191 add("application/x-quicktimeplayer", icon); 192 add("application/x-shockwave-flash", icon); 193 } 194 195 public static Drawable loadPackageIcon(Context context, String authority, int icon) { 196 if (icon != 0) { 197 if (authority != null) { 198 final PackageManager pm = context.getPackageManager(); 199 final ProviderInfo info = pm.resolveContentProvider(authority, 0); 200 if (info != null) { 201 return pm.getDrawable(info.packageName, icon, info.applicationInfo); 202 } 203 } else { 204 return context.getResources().getDrawable(icon); 205 } 206 } 207 return null; 208 } 209 210 public static Drawable loadMimeIcon( 211 Context context, String mimeType, String authority, String docId, int mode) { 212 final Resources res = context.getResources(); 213 214 if (Document.MIME_TYPE_DIR.equals(mimeType)) { 215 // TODO: eventually move these hacky assets into that package 216 if ("com.android.providers.media.documents".equals(authority) 217 && docId.startsWith("album")) { 218 return res.getDrawable(R.drawable.ic_doc_album); 219 } 220 221 if (mode == DocumentsActivity.State.MODE_GRID) { 222 return res.getDrawable(R.drawable.ic_grid_folder); 223 } else { 224 return res.getDrawable(R.drawable.ic_root_folder); 225 } 226 } 227 228 return loadMimeIcon(context, mimeType); 229 } 230 231 public static Drawable loadMimeIcon(Context context, String mimeType) { 232 final Resources res = context.getResources(); 233 234 if (Document.MIME_TYPE_DIR.equals(mimeType)) { 235 // TODO: return a mipmap, since this is used for grid 236 return res.getDrawable(R.drawable.ic_root_folder); 237 } 238 239 // Look for exact match first 240 Integer resId = sMimeIcons.get(mimeType); 241 if (resId != null) { 242 return res.getDrawable(resId); 243 } 244 245 if (mimeType == null) { 246 // TODO: generic icon? 247 return null; 248 } 249 250 // Otherwise look for partial match 251 final String typeOnly = mimeType.split("/")[0]; 252 if ("audio".equals(typeOnly)) { 253 return res.getDrawable(R.drawable.ic_doc_audio); 254 } else if ("image".equals(typeOnly)) { 255 return res.getDrawable(R.drawable.ic_doc_image); 256 } else if ("text".equals(typeOnly)) { 257 return res.getDrawable(R.drawable.ic_doc_text); 258 } else if ("video".equals(typeOnly)) { 259 return res.getDrawable(R.drawable.ic_doc_video); 260 } else { 261 return res.getDrawable(R.drawable.ic_doc_generic); 262 } 263 } 264 } 265