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