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.annotation.Nullable;
19 import android.provider.DocumentsContract.Document;
20 
21 import java.util.List;
22 
23 public final class MimeTypes {
24 
MimeTypes()25     private MimeTypes() {}
26 
27     private static final String APK_TYPE = "application/vnd.android.package-archive";
28     /**
29      * MIME types that are visual in nature. For example, they should always be
30      * shown as thumbnails in list mode.
31      */
32     public static final String[] VISUAL_MIMES = new String[] { "image/*", "video/*" };
33 
findCommonMimeType(List<String> mimeTypes)34     public static String findCommonMimeType(List<String> mimeTypes) {
35         String[] commonType = mimeTypes.get(0).split("/");
36         if (commonType.length != 2) {
37             return "*/*";
38         }
39 
40         for (int i = 1; i < mimeTypes.size(); i++) {
41             String[] type = mimeTypes.get(i).split("/");
42             if (type.length != 2) continue;
43 
44             if (!commonType[1].equals(type[1])) {
45                 commonType[1] = "*";
46             }
47 
48             if (!commonType[0].equals(type[0])) {
49                 commonType[0] = "*";
50                 commonType[1] = "*";
51                 break;
52             }
53         }
54 
55         return commonType[0] + "/" + commonType[1];
56     }
57 
mimeMatches(String[] filters, String[] tests)58     public static boolean mimeMatches(String[] filters, String[] tests) {
59         if (tests == null) {
60             return false;
61         }
62         for (String test : tests) {
63             if (mimeMatches(filters, test)) {
64                 return true;
65             }
66         }
67         return false;
68     }
69 
mimeMatches(String filter, String[] tests)70     public static boolean mimeMatches(String filter, String[] tests) {
71         if (tests == null) {
72             return true;
73         }
74         for (String test : tests) {
75             if (mimeMatches(filter, test)) {
76                 return true;
77             }
78         }
79         return false;
80     }
81 
mimeMatches(String[] filters, String test)82     public static boolean mimeMatches(String[] filters, String test) {
83         if (filters == null) {
84             return true;
85         }
86         for (String filter : filters) {
87             if (mimeMatches(filter, test)) {
88                 return true;
89             }
90         }
91         return false;
92     }
93 
mimeMatches(String filter, String test)94     public static boolean mimeMatches(String filter, String test) {
95         if (test == null) {
96             return false;
97         } else if (filter == null || "*/*".equals(filter)) {
98             return true;
99         } else if (filter.equals(test)) {
100             return true;
101         } else if (filter.endsWith("/*")) {
102             return filter.regionMatches(0, test, 0, filter.indexOf('/'));
103         } else {
104             return false;
105         }
106     }
107 
isApkType(@ullable String mimeType)108     public static boolean isApkType(@Nullable String mimeType) {
109         return APK_TYPE.equals(mimeType);
110     }
111 
isDirectoryType(@ullable String mimeType)112     public static boolean isDirectoryType(@Nullable String mimeType) {
113         return Document.MIME_TYPE_DIR.equals(mimeType);
114     }
115 }
116