• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.annotation.Nullable;
20 import android.provider.DocumentsContract.Document;
21 
22 import com.android.documentsui.model.DocumentInfo;
23 import com.android.internal.util.Predicate;
24 
25 public class MimePredicate implements Predicate<DocumentInfo> {
26     private final String[] mFilters;
27 
28     private static final String APK_TYPE = "application/vnd.android.package-archive";
29     /**
30      * MIME types that are visual in nature. For example, they should always be
31      * shown as thumbnails in list mode.
32      */
33     public static final String[] VISUAL_MIMES = new String[] { "image/*", "video/*" };
34 
MimePredicate(String[] filters)35     public MimePredicate(String[] filters) {
36         mFilters = filters;
37     }
38 
39     @Override
apply(DocumentInfo doc)40     public boolean apply(DocumentInfo doc) {
41         if (doc.isDirectory()) {
42             return true;
43         }
44         if (mimeMatches(mFilters, doc.mimeType)) {
45             return true;
46         }
47         return false;
48     }
49 
mimeMatches(String[] filters, String[] tests)50     public static boolean mimeMatches(String[] filters, String[] tests) {
51         if (tests == null) {
52             return false;
53         }
54         for (String test : tests) {
55             if (mimeMatches(filters, test)) {
56                 return true;
57             }
58         }
59         return false;
60     }
61 
mimeMatches(String filter, String[] tests)62     public static boolean mimeMatches(String filter, String[] tests) {
63         if (tests == null) {
64             return true;
65         }
66         for (String test : tests) {
67             if (mimeMatches(filter, test)) {
68                 return true;
69             }
70         }
71         return false;
72     }
73 
mimeMatches(String[] filters, String test)74     public static boolean mimeMatches(String[] filters, String test) {
75         if (filters == null) {
76             return true;
77         }
78         for (String filter : filters) {
79             if (mimeMatches(filter, test)) {
80                 return true;
81             }
82         }
83         return false;
84     }
85 
mimeMatches(String filter, String test)86     public static boolean mimeMatches(String filter, String test) {
87         if (test == null) {
88             return false;
89         } else if (filter == null || "*/*".equals(filter)) {
90             return true;
91         } else if (filter.equals(test)) {
92             return true;
93         } else if (filter.endsWith("/*")) {
94             return filter.regionMatches(0, test, 0, filter.indexOf('/'));
95         } else {
96             return false;
97         }
98     }
99 
isApkType(@ullable String mimeType)100     public static boolean isApkType(@Nullable String mimeType) {
101         return APK_TYPE.equals(mimeType);
102     }
103 
isDirectoryType(@ullable String mimeType)104     public static boolean isDirectoryType(@Nullable String mimeType) {
105         return Document.MIME_TYPE_DIR.equals(mimeType);
106     }
107 }
108