1 /*
2  * Copyright (C) 2016 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.internal.util;
18 
19 import android.content.Context;
20 import android.graphics.drawable.Drawable;
21 import android.provider.DocumentsContract;
22 
23 import com.android.internal.R;
24 
25 import java.util.HashMap;
26 
27 public class MimeIconUtils {
28 
29     private static HashMap<String, Integer> sMimeIcons = new HashMap<>();
30 
add(String mimeType, int resId)31     private static void add(String mimeType, int resId) {
32         if (sMimeIcons.put(mimeType, resId) != null) {
33             throw new RuntimeException(mimeType + " already registered!");
34         }
35     }
36 
37     static {
38         int icon;
39 
40         // Package
41         icon = R.drawable.ic_doc_apk;
42         add("application/vnd.android.package-archive", icon);
43 
44         // Audio
45         icon = R.drawable.ic_doc_audio;
46         add("application/ogg", icon);
47         add("application/x-flac", icon);
48 
49         // Certificate
50         icon = R.drawable.ic_doc_certificate;
51         add("application/pgp-keys", icon);
52         add("application/pgp-signature", icon);
53         add("application/x-pkcs12", icon);
54         add("application/x-pkcs7-certreqresp", icon);
55         add("application/x-pkcs7-crl", icon);
56         add("application/x-x509-ca-cert", icon);
57         add("application/x-x509-user-cert", icon);
58         add("application/x-pkcs7-certificates", icon);
59         add("application/x-pkcs7-mime", icon);
60         add("application/x-pkcs7-signature", icon);
61 
62         // Source code
63         icon = R.drawable.ic_doc_codes;
64         add("application/rdf+xml", icon);
65         add("application/rss+xml", icon);
66         add("application/x-object", icon);
67         add("application/xhtml+xml", icon);
68         add("text/css", icon);
69         add("text/html", icon);
70         add("text/xml", icon);
71         add("text/x-c++hdr", icon);
72         add("text/x-c++src", icon);
73         add("text/x-chdr", icon);
74         add("text/x-csrc", icon);
75         add("text/x-dsrc", icon);
76         add("text/x-csh", icon);
77         add("text/x-haskell", icon);
78         add("text/x-java", icon);
79         add("text/x-literate-haskell", icon);
80         add("text/x-pascal", icon);
81         add("text/x-tcl", icon);
82         add("text/x-tex", icon);
83         add("application/x-latex", icon);
84         add("application/x-texinfo", icon);
85         add("application/atom+xml", icon);
86         add("application/ecmascript", icon);
87         add("application/json", icon);
88         add("application/javascript", icon);
89         add("application/xml", icon);
90         add("text/javascript", icon);
91         add("application/x-javascript", icon);
92 
93         // Compressed
94         icon = R.drawable.ic_doc_compressed;
95         add("application/mac-binhex40", icon);
96         add("application/rar", icon);
97         add("application/zip", icon);
98         add("application/x-apple-diskimage", icon);
99         add("application/x-debian-package", icon);
100         add("application/x-gtar", icon);
101         add("application/x-iso9660-image", icon);
102         add("application/x-lha", icon);
103         add("application/x-lzh", icon);
104         add("application/x-lzx", icon);
105         add("application/x-stuffit", icon);
106         add("application/x-tar", icon);
107         add("application/x-webarchive", icon);
108         add("application/x-webarchive-xml", icon);
109         add("application/gzip", icon);
110         add("application/x-7z-compressed", icon);
111         add("application/x-deb", icon);
112         add("application/x-rar-compressed", icon);
113 
114         // Contact
115         icon = R.drawable.ic_doc_contact;
116         add("text/x-vcard", icon);
117         add("text/vcard", icon);
118 
119         // Event
120         icon = R.drawable.ic_doc_event;
121         add("text/calendar", icon);
122         add("text/x-vcalendar", icon);
123 
124         // Font
125         icon = R.drawable.ic_doc_font;
126         add("application/x-font", icon);
127         add("application/font-woff", icon);
128         add("application/x-font-woff", icon);
129         add("application/x-font-ttf", icon);
130 
131         // Image
132         icon = R.drawable.ic_doc_image;
133         add("application/vnd.oasis.opendocument.graphics", icon);
134         add("application/vnd.oasis.opendocument.graphics-template", icon);
135         add("application/vnd.oasis.opendocument.image", icon);
136         add("application/vnd.stardivision.draw", icon);
137         add("application/vnd.sun.xml.draw", icon);
138         add("application/vnd.sun.xml.draw.template", icon);
139 
140         // PDF
141         icon = R.drawable.ic_doc_pdf;
142         add("application/pdf", icon);
143 
144         // Presentation
145         icon = R.drawable.ic_doc_presentation;
146         add("application/vnd.stardivision.impress", icon);
147         add("application/vnd.sun.xml.impress", icon);
148         add("application/vnd.sun.xml.impress.template", icon);
149         add("application/x-kpresenter", icon);
150         add("application/vnd.oasis.opendocument.presentation", icon);
151 
152         // Spreadsheet
153         icon = R.drawable.ic_doc_spreadsheet;
154         add("application/vnd.oasis.opendocument.spreadsheet", icon);
155         add("application/vnd.oasis.opendocument.spreadsheet-template", icon);
156         add("application/vnd.stardivision.calc", icon);
157         add("application/vnd.sun.xml.calc", icon);
158         add("application/vnd.sun.xml.calc.template", icon);
159         add("application/x-kspread", icon);
160 
161         // Document
162         icon = R.drawable.ic_doc_document;
163         add("application/vnd.oasis.opendocument.text", icon);
164         add("application/vnd.oasis.opendocument.text-master", icon);
165         add("application/vnd.oasis.opendocument.text-template", icon);
166         add("application/vnd.oasis.opendocument.text-web", icon);
167         add("application/vnd.stardivision.writer", icon);
168         add("application/vnd.stardivision.writer-global", icon);
169         add("application/vnd.sun.xml.writer", icon);
170         add("application/vnd.sun.xml.writer.global", icon);
171         add("application/vnd.sun.xml.writer.template", icon);
172         add("application/x-abiword", icon);
173         add("application/x-kword", icon);
174 
175         // Video
176         icon = R.drawable.ic_doc_video;
177         add("application/x-quicktimeplayer", icon);
178         add("application/x-shockwave-flash", icon);
179 
180         // Word
181         icon = R.drawable.ic_doc_word;
182         add("application/msword", icon);
183         add("application/vnd.openxmlformats-officedocument.wordprocessingml.document", icon);
184         add("application/vnd.openxmlformats-officedocument.wordprocessingml.template", icon);
185 
186         // Excel
187         icon = R.drawable.ic_doc_excel;
188         add("application/vnd.ms-excel", icon);
189         add("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", icon);
190         add("application/vnd.openxmlformats-officedocument.spreadsheetml.template", icon);
191 
192         // Powerpoint
193         icon = R.drawable.ic_doc_powerpoint;
194         add("application/vnd.ms-powerpoint", icon);
195         add("application/vnd.openxmlformats-officedocument.presentationml.presentation", icon);
196         add("application/vnd.openxmlformats-officedocument.presentationml.template", icon);
197         add("application/vnd.openxmlformats-officedocument.presentationml.slideshow", icon);
198     }
199 
loadMimeIcon(Context context, String mimeType)200     public static Drawable loadMimeIcon(Context context, String mimeType) {
201         if (DocumentsContract.Document.MIME_TYPE_DIR.equals(mimeType)) {
202             return context.getDrawable(R.drawable.ic_doc_folder);
203         }
204 
205         // Look for exact match first
206         Integer resId = sMimeIcons.get(mimeType);
207         if (resId != null) {
208             return context.getDrawable(resId);
209         }
210 
211         if (mimeType == null) {
212             // TODO: generic icon?
213             return null;
214         }
215 
216         // Otherwise look for partial match
217         final String typeOnly = mimeType.split("/")[0];
218         if ("audio".equals(typeOnly)) {
219             return context.getDrawable(R.drawable.ic_doc_audio);
220         } else if ("image".equals(typeOnly)) {
221             return context.getDrawable(R.drawable.ic_doc_image);
222         } else if ("text".equals(typeOnly)) {
223             return context.getDrawable(R.drawable.ic_doc_text);
224         } else if ("video".equals(typeOnly)) {
225             return context.getDrawable(R.drawable.ic_doc_video);
226         } else {
227             return context.getDrawable(R.drawable.ic_doc_generic);
228         }
229     }
230 }
231