Home | History | Annotate | Download | only in documentsui
      1 /*
      2  * Copyright (C) 2017 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.StringRes;
     20 import android.content.Context;
     21 import android.content.res.Resources;
     22 import android.text.TextUtils;
     23 import android.util.Log;
     24 import android.util.SparseArray;
     25 
     26 import com.android.documentsui.base.Lookup;
     27 import com.android.documentsui.base.MimeTypes;
     28 
     29 import libcore.net.MimeUtils;
     30 
     31 import java.util.HashMap;
     32 
     33 /**
     34  * A map from mime type to user friendly type string.
     35  */
     36 public class FileTypeMap implements Lookup<String, String> {
     37 
     38     private static final String TAG = "FileTypeMap";
     39 
     40     private final Resources mRes;
     41 
     42     private final SparseArray<Integer> mMediaTypeStringMap = new SparseArray<>();
     43 
     44     private final HashMap<String, Integer> mFileTypeMap = new HashMap<>();
     45     private final HashMap<String, String> mArchiveTypeMap = new HashMap<>();
     46     private final HashMap<String, Integer> mSpecialMediaMimeType = new HashMap<>();
     47 
     48     FileTypeMap(Context context) {
     49         mRes = context.getResources();
     50 
     51         // Mapping from generic media type string to extension media type string
     52         mMediaTypeStringMap.put(R.string.video_file_type, R.string.video_extension_file_type);
     53         mMediaTypeStringMap.put(R.string.audio_file_type, R.string.audio_extension_file_type);
     54         mMediaTypeStringMap.put(R.string.image_file_type, R.string.image_extension_file_type);
     55 
     56         // Common file types
     57         mFileTypeMap.put(MimeTypes.APK_TYPE, R.string.apk_file_type);
     58         mFileTypeMap.put("text/plain", R.string.txt_file_type);
     59         mFileTypeMap.put("text/html", R.string.html_file_type);
     60         mFileTypeMap.put("application/xhtml+xml", R.string.html_file_type);
     61         mFileTypeMap.put("application/pdf", R.string.pdf_file_type);
     62 
     63         // MS file types
     64         mFileTypeMap.put("application/msword", R.string.word_file_type);
     65         mFileTypeMap.put(
     66                 "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
     67                 R.string.word_file_type);
     68         mFileTypeMap.put("application/vnd.ms-powerpoint", R.string.ppt_file_type);
     69         mFileTypeMap.put(
     70                 "application/vnd.openxmlformats-officedocument.presentationml.presentation",
     71                 R.string.ppt_file_type);
     72         mFileTypeMap.put("application/vnd.ms-excel", R.string.excel_file_type);
     73         mFileTypeMap.put(
     74                 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
     75                 R.string.excel_file_type);
     76 
     77         // Google doc types
     78         mFileTypeMap.put("application/vnd.google-apps.document", R.string.gdoc_file_type);
     79         mFileTypeMap.put("application/vnd.google-apps.spreadsheet", R.string.gsheet_file_type);
     80         mFileTypeMap.put("application/vnd.google-apps.presentation", R.string.gslides_file_type);
     81         mFileTypeMap.put("application/vnd.google-apps.drawing", R.string.gdraw_file_type);
     82         mFileTypeMap.put("application/vnd.google-apps.fusiontable", R.string.gtable_file_type);
     83         mFileTypeMap.put("application/vnd.google-apps.form", R.string.gform_file_type);
     84         mFileTypeMap.put("application/vnd.google-apps.map", R.string.gmap_file_type);
     85         mFileTypeMap.put("application/vnd.google-apps.sites", R.string.gsite_file_type);
     86 
     87         // Directory type
     88         mFileTypeMap.put("vnd.android.document/directory", R.string.directory_type);
     89 
     90         // Archive types
     91         mArchiveTypeMap.put("application/rar", "RAR");
     92         mArchiveTypeMap.put("application/zip", "Zip");
     93         mArchiveTypeMap.put("application/x-tar", "Tar");
     94         mArchiveTypeMap.put("application/gzip", "Gzip");
     95         mArchiveTypeMap.put("application/x-7z-compressed", "7z");
     96         mArchiveTypeMap.put("application/x-rar-compressed", "RAR");
     97 
     98         // Special media mime types
     99         mSpecialMediaMimeType.put("application/ogg", R.string.audio_file_type);
    100         mSpecialMediaMimeType.put("application/x-flac", R.string.audio_file_type);
    101     }
    102 
    103     @Override
    104     public String lookup(String mimeType) {
    105         if (mFileTypeMap.containsKey(mimeType)) {
    106             return getPredefinedFileTypeString(mimeType);
    107         }
    108 
    109         if (mArchiveTypeMap.containsKey(mimeType)) {
    110             return buildArchiveTypeString(mimeType);
    111         }
    112 
    113         if (mSpecialMediaMimeType.containsKey(mimeType)) {
    114             int genericType = mSpecialMediaMimeType.get(mimeType);
    115             return getFileTypeString(mimeType, mMediaTypeStringMap.get(genericType), genericType);
    116         }
    117 
    118         final String[] type = MimeTypes.splitMimeType(mimeType);
    119         if (type == null) {
    120             Log.w(TAG, "Unexpected mime type " + mimeType);
    121             return getGenericFileTypeString();
    122         }
    123 
    124         switch (type[0]) {
    125             case MimeTypes.IMAGE_PREFIX:
    126                 return getFileTypeString(
    127                         mimeType, R.string.image_extension_file_type, R.string.image_file_type);
    128             case MimeTypes.AUDIO_PREFIX:
    129                 return getFileTypeString(
    130                         mimeType, R.string.audio_extension_file_type, R.string.audio_file_type);
    131             case MimeTypes.VIDEO_PREFIX:
    132                 return getFileTypeString(
    133                         mimeType, R.string.video_extension_file_type, R.string.video_file_type);
    134             default:
    135                 return getFileTypeString(
    136                         mimeType, R.string.generic_extention_file_type, R.string.generic_file_type);
    137         }
    138     }
    139 
    140     private String buildArchiveTypeString(String mimeType) {
    141         final String archiveType = mArchiveTypeMap.get(mimeType);
    142 
    143         assert(!TextUtils.isEmpty(archiveType));
    144 
    145         final String format = mRes.getString(R.string.archive_file_type);
    146         return String.format(format, archiveType);
    147     }
    148 
    149     private String getPredefinedFileTypeString(String mimeType) {
    150         return mRes.getString(mFileTypeMap.get(mimeType));
    151     }
    152 
    153     private String getFileTypeString(
    154             String mimeType, @StringRes int formatStringId, @StringRes int defaultStringId) {
    155         final String extension = MimeUtils.guessExtensionFromMimeType(mimeType);
    156 
    157         return TextUtils.isEmpty(extension)
    158                 ? mRes.getString(defaultStringId)
    159                 : String.format(mRes.getString(formatStringId), extension.toUpperCase());
    160     }
    161 
    162     private String getGenericFileTypeString() {
    163         return mRes.getString(R.string.generic_file_type);
    164     }
    165 }
    166