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