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