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 java.util.HashMap;
27 
28 public class IconUtils {
29 
30     private static HashMap<String, Integer> sMimeIcons = new HashMap<>();
31 
add(String mimeType, int resId)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
46         icon = R.drawable.ic_doc_audio;
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
133         icon = R.drawable.ic_doc_image;
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;
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);
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         // Document
163         icon = R.drawable.ic_doc_document;
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);
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
177         icon = R.drawable.ic_doc_video;
178         add("application/x-quicktimeplayer", icon);
179         add("application/x-shockwave-flash", icon);
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);
199     }
200 
loadPackageIcon(Context context, String authority, int icon)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 {
210                 return context.getDrawable(icon);
211             }
212         }
213         return null;
214     }
215 
loadMimeIcon( Context context, String mimeType, String authority, String docId, int mode)216     public static Drawable loadMimeIcon(
217             Context context, String mimeType, String authority, String docId, int mode) {
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")) {
222                 return context.getDrawable(R.drawable.ic_doc_album);
223             }
224 
225             if (mode == State.MODE_GRID) {
226                 return context.getDrawable(R.drawable.ic_grid_folder);
227             } else {
228                 return context.getDrawable(R.drawable.ic_doc_folder);
229             }
230         }
231 
232         return loadMimeIcon(context, mimeType);
233     }
234 
loadMimeIcon(Context context, String mimeType)235     public static Drawable loadMimeIcon(Context context, String mimeType) {
236         if (Document.MIME_TYPE_DIR.equals(mimeType)) {
237             return context.getDrawable(R.drawable.ic_doc_folder);
238         }
239 
240         // Look for exact match first
241         Integer resId = sMimeIcons.get(mimeType);
242         if (resId != null) {
243             return context.getDrawable(resId);
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)) {
254             return context.getDrawable(R.drawable.ic_doc_audio);
255         } else if ("image".equals(typeOnly)) {
256             return context.getDrawable(R.drawable.ic_doc_image);
257         } else if ("text".equals(typeOnly)) {
258             return context.getDrawable(R.drawable.ic_doc_text);
259         } else if ("video".equals(typeOnly)) {
260             return context.getDrawable(R.drawable.ic_doc_video);
261         } else {
262             return context.getDrawable(R.drawable.ic_doc_generic);
263         }
264     }
265 
applyTintColor(Context context, int drawableId, int tintColorId)266     public static Drawable applyTintColor(Context context, int drawableId, int tintColorId) {
267         final Drawable icon = context.getDrawable(drawableId);
268         icon.mutate();
269         icon.setTintList(context.getColorStateList(tintColorId));
270         return icon;
271     }
272 
applyTintAttr(Context context, int drawableId, int tintAttrId)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     }
278 }
279