MimePredicate.java revision a5599ef636e37cb0b6474349936999be1afe6987
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 17package com.android.documentsui; 18 19import com.android.documentsui.model.Document; 20import com.android.internal.util.Predicate; 21 22public class MimePredicate implements Predicate<Document> { 23 private final String[] mFilters; 24 25 public MimePredicate(String[] filters) { 26 mFilters = filters; 27 } 28 29 @Override 30 public boolean apply(Document doc) { 31 if (doc.isDirectory()) { 32 return true; 33 } 34 for (String filter : mFilters) { 35 if (mimeMatches(filter, doc.mimeType)) { 36 return true; 37 } 38 } 39 return false; 40 } 41 42 public static boolean mimeMatches(String filter, String[] tests) { 43 for (String test : tests) { 44 if (mimeMatches(filter, test)) { 45 return true; 46 } 47 } 48 return false; 49 } 50 51 public static boolean mimeMatches(String filter, String test) { 52 if (test == null) { 53 return false; 54 } else if (filter.equals(test)) { 55 return true; 56 } else if ("*/*".equals(filter)) { 57 return true; 58 } else if (filter.endsWith("/*")) { 59 return filter.regionMatches(0, test, 0, filter.indexOf('/')); 60 } else { 61 return false; 62 } 63 } 64} 65