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 package com.android.documentsui.base;
17 
18 import android.provider.DocumentsContract.Document;
19 import android.util.ArraySet;
20 
21 import androidx.annotation.Nullable;
22 
23 import java.util.List;
24 import java.util.Set;
25 
26 public final class MimeTypes {
27 
MimeTypes()28     private MimeTypes() {
29     }
30 
31     public static final String APK_TYPE = "application/vnd.android.package-archive";
32     public static final String GENERIC_TYPE = "application/*";
33 
34     public static final String IMAGE_MIME = "image/*";
35     public static final String AUDIO_MIME = "audio/*";
36     public static final String VIDEO_MIME = "video/*";
37 
38     private static final Set<String> sDocumentsMimeTypes = new ArraySet<>();
39 
40     static {
41         // all lower case
42         sDocumentsMimeTypes.add("application/epub+zip");
43         sDocumentsMimeTypes.add("application/msword");
44         sDocumentsMimeTypes.add("application/pdf");
45         sDocumentsMimeTypes.add("application/rtf");
46         sDocumentsMimeTypes.add("application/vnd.ms-excel");
47         sDocumentsMimeTypes.add("application/vnd.ms-excel.addin.macroenabled.12");
48         sDocumentsMimeTypes.add("application/vnd.ms-excel.sheet.binary.macroenabled.12");
49         sDocumentsMimeTypes.add("application/vnd.ms-excel.sheet.macroenabled.12");
50         sDocumentsMimeTypes.add("application/vnd.ms-excel.template.macroenabled.12");
51         sDocumentsMimeTypes.add("application/vnd.ms-powerpoint");
52         sDocumentsMimeTypes.add("application/vnd.ms-powerpoint.addin.macroenabled.12");
53         sDocumentsMimeTypes.add("application/vnd.ms-powerpoint.presentation.macroenabled.12");
54         sDocumentsMimeTypes.add("application/vnd.ms-powerpoint.slideshow.macroenabled.12");
55         sDocumentsMimeTypes.add("application/vnd.ms-powerpoint.template.macroenabled.12");
56         sDocumentsMimeTypes.add("application/vnd.ms-word.document.macroenabled.12");
57         sDocumentsMimeTypes.add("application/vnd.ms-word.template.macroenabled.12");
58         sDocumentsMimeTypes.add("application/vnd.oasis.opendocument.chart");
59         sDocumentsMimeTypes.add("application/vnd.oasis.opendocument.database");
60         sDocumentsMimeTypes.add("application/vnd.oasis.opendocument.formula");
61         sDocumentsMimeTypes.add("application/vnd.oasis.opendocument.graphics");
62         sDocumentsMimeTypes.add("application/vnd.oasis.opendocument.graphics-template");
63         sDocumentsMimeTypes.add("application/vnd.oasis.opendocument.presentation");
64         sDocumentsMimeTypes.add("application/vnd.oasis.opendocument.presentation-template");
65         sDocumentsMimeTypes.add("application/vnd.oasis.opendocument.spreadsheet");
66         sDocumentsMimeTypes.add("application/vnd.oasis.opendocument.spreadsheet-template");
67         sDocumentsMimeTypes.add("application/vnd.oasis.opendocument.text");
68         sDocumentsMimeTypes.add("application/vnd.oasis.opendocument.text-master");
69         sDocumentsMimeTypes.add("application/vnd.oasis.opendocument.text-template");
70         sDocumentsMimeTypes.add("application/vnd.oasis.opendocument.text-web");
71         sDocumentsMimeTypes.add(
72                 "application/vnd.openxmlformats-officedocument.presentationml.presentation");
73         sDocumentsMimeTypes.add(
74                 "application/vnd.openxmlformats-officedocument.presentationml.slideshow");
75         sDocumentsMimeTypes.add(
76                 "application/vnd.openxmlformats-officedocument.presentationml.template");
77         sDocumentsMimeTypes.add(
78                 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
79         sDocumentsMimeTypes.add(
80                 "application/vnd.openxmlformats-officedocument.spreadsheetml.template");
81         sDocumentsMimeTypes.add(
82                 "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
83         sDocumentsMimeTypes.add(
84                 "application/vnd.openxmlformats-officedocument.wordprocessingml.template");
85         sDocumentsMimeTypes.add("application/vnd.stardivision.calc");
86         sDocumentsMimeTypes.add("application/vnd.stardivision.chart");
87         sDocumentsMimeTypes.add("application/vnd.stardivision.draw");
88         sDocumentsMimeTypes.add("application/vnd.stardivision.impress");
89         sDocumentsMimeTypes.add("application/vnd.stardivision.impress-packed");
90         sDocumentsMimeTypes.add("application/vnd.stardivision.mail");
91         sDocumentsMimeTypes.add("application/vnd.stardivision.math");
92         sDocumentsMimeTypes.add("application/vnd.stardivision.writer");
93         sDocumentsMimeTypes.add("application/vnd.stardivision.writer-global");
94         sDocumentsMimeTypes.add("application/vnd.sun.xml.calc");
95         sDocumentsMimeTypes.add("application/vnd.sun.xml.calc.template");
96         sDocumentsMimeTypes.add("application/vnd.sun.xml.draw");
97         sDocumentsMimeTypes.add("application/vnd.sun.xml.draw.template");
98         sDocumentsMimeTypes.add("application/vnd.sun.xml.impress");
99         sDocumentsMimeTypes.add("application/vnd.sun.xml.impress.template");
100         sDocumentsMimeTypes.add("application/vnd.sun.xml.math");
101         sDocumentsMimeTypes.add("application/vnd.sun.xml.writer");
102         sDocumentsMimeTypes.add("application/vnd.sun.xml.writer.global");
103         sDocumentsMimeTypes.add("application/vnd.sun.xml.writer.template");
104         sDocumentsMimeTypes.add("application/x-mspublisher");
105         sDocumentsMimeTypes.add("text/*");
106     }
107 
108     /**
109      * Get the Document mime type array
110      *
111      * @return the mime type array of document
112      */
getDocumentMimeTypeArray()113     public static String[] getDocumentMimeTypeArray() {
114         return sDocumentsMimeTypes.toArray((new String[0]));
115     }
116 
117     /**
118      * MIME types that are visual in nature. For example, they should always be shown as thumbnails
119      * in list mode.
120      */
121     public static final String[] VISUAL_MIMES = new String[]{IMAGE_MIME, VIDEO_MIME};
122 
splitMimeType(String mimeType)123     public static @Nullable String[] splitMimeType(String mimeType) {
124         final String[] groups = mimeType.split("/");
125 
126         if (groups.length != 2 || groups[0].isEmpty() || groups[1].isEmpty()) {
127             return null;
128         }
129 
130         return groups;
131     }
132 
findCommonMimeType(List<String> mimeTypes)133     public static String findCommonMimeType(List<String> mimeTypes) {
134         String[] commonType = splitMimeType(mimeTypes.get(0));
135         if (commonType == null) {
136             return "*/*";
137         }
138 
139         for (int i = 1; i < mimeTypes.size(); i++) {
140             String[] type = mimeTypes.get(i).split("/");
141             if (type.length != 2) continue;
142 
143             if (!commonType[1].equals(type[1])) {
144                 commonType[1] = "*";
145             }
146 
147             if (!commonType[0].equals(type[0])) {
148                 commonType[0] = "*";
149                 commonType[1] = "*";
150                 break;
151             }
152         }
153 
154         return commonType[0] + "/" + commonType[1];
155     }
156 
mimeMatches(String[] filters, String[] tests)157     public static boolean mimeMatches(String[] filters, String[] tests) {
158         if (tests == null) {
159             return false;
160         }
161         for (String test : tests) {
162             if (mimeMatches(filters, test)) {
163                 return true;
164             }
165         }
166         return false;
167     }
168 
mimeMatches(String filter, String[] tests)169     public static boolean mimeMatches(String filter, String[] tests) {
170         if (tests == null) {
171             return true;
172         }
173         for (String test : tests) {
174             if (mimeMatches(filter, test)) {
175                 return true;
176             }
177         }
178         return false;
179     }
180 
mimeMatches(String[] filters, String test)181     public static boolean mimeMatches(String[] filters, String test) {
182         if (filters == null) {
183             return true;
184         }
185         for (String filter : filters) {
186             if (mimeMatches(filter, test)) {
187                 return true;
188             }
189         }
190         return false;
191     }
192 
mimeMatches(String filter, String test)193     public static boolean mimeMatches(String filter, String test) {
194         if (test == null) {
195             return false;
196         } else if (filter == null || "*/*".equals(filter)) {
197             return true;
198         } else if (filter.equals(test)) {
199             return true;
200         } else if (filter.endsWith("/*")) {
201             return filter.regionMatches(0, test, 0, filter.indexOf('/'));
202         } else {
203             return false;
204         }
205     }
206 
isApkType(@ullable String mimeType)207     public static boolean isApkType(@Nullable String mimeType) {
208         return APK_TYPE.equals(mimeType);
209     }
210 
isDirectoryType(@ullable String mimeType)211     public static boolean isDirectoryType(@Nullable String mimeType) {
212         return Document.MIME_TYPE_DIR.equals(mimeType);
213     }
214 }
215