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