• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 androidx.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     public static final String APK_TYPE = "application/vnd.android.package-archive";
28     public static final String GENERIC_TYPE = "application/*";
29 
30     public static final String IMAGE_MIME = "image/*";
31     public static final String AUDIO_MIME = "audio/*";
32     public static final String VIDEO_MIME = "video/*";
33 
34     /**
35      * MIME types that are visual in nature. For example, they should always be
36      * shown as thumbnails in list mode.
37      */
38     public static final String[] VISUAL_MIMES = new String[] { IMAGE_MIME, VIDEO_MIME };
39 
splitMimeType(String mimeType)40     public static @Nullable String[] splitMimeType(String mimeType) {
41         final String[] groups = mimeType.split("/");
42 
43         if (groups.length != 2 || groups[0].isEmpty() || groups[1].isEmpty()) {
44             return null;
45         }
46 
47         return groups;
48     }
49 
findCommonMimeType(List<String> mimeTypes)50     public static String findCommonMimeType(List<String> mimeTypes) {
51         String[] commonType = splitMimeType(mimeTypes.get(0));
52         if (commonType == null) {
53             return "*/*";
54         }
55 
56         for (int i = 1; i < mimeTypes.size(); i++) {
57             String[] type = mimeTypes.get(i).split("/");
58             if (type.length != 2) continue;
59 
60             if (!commonType[1].equals(type[1])) {
61                 commonType[1] = "*";
62             }
63 
64             if (!commonType[0].equals(type[0])) {
65                 commonType[0] = "*";
66                 commonType[1] = "*";
67                 break;
68             }
69         }
70 
71         return commonType[0] + "/" + commonType[1];
72     }
73 
mimeMatches(String[] filters, String[] tests)74     public static boolean mimeMatches(String[] filters, String[] tests) {
75         if (tests == null) {
76             return false;
77         }
78         for (String test : tests) {
79             if (mimeMatches(filters, test)) {
80                 return true;
81             }
82         }
83         return false;
84     }
85 
mimeMatches(String filter, String[] tests)86     public static boolean mimeMatches(String filter, String[] tests) {
87         if (tests == null) {
88             return true;
89         }
90         for (String test : tests) {
91             if (mimeMatches(filter, test)) {
92                 return true;
93             }
94         }
95         return false;
96     }
97 
mimeMatches(String[] filters, String test)98     public static boolean mimeMatches(String[] filters, String test) {
99         if (filters == null) {
100             return true;
101         }
102         for (String filter : filters) {
103             if (mimeMatches(filter, test)) {
104                 return true;
105             }
106         }
107         return false;
108     }
109 
mimeMatches(String filter, String test)110     public static boolean mimeMatches(String filter, String test) {
111         if (test == null) {
112             return false;
113         } else if (filter == null || "*/*".equals(filter)) {
114             return true;
115         } else if (filter.equals(test)) {
116             return true;
117         } else if (filter.endsWith("/*")) {
118             return filter.regionMatches(0, test, 0, filter.indexOf('/'));
119         } else {
120             return false;
121         }
122     }
123 
isApkType(@ullable String mimeType)124     public static boolean isApkType(@Nullable String mimeType) {
125         return APK_TYPE.equals(mimeType);
126     }
127 
isDirectoryType(@ullable String mimeType)128     public static boolean isDirectoryType(@Nullable String mimeType) {
129         return Document.MIME_TYPE_DIR.equals(mimeType);
130     }
131 }
132