Home | History | Annotate | Download | only in media
      1 /*
      2  * Copyright (C) 2007 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 android.media;
     18 
     19 import android.content.ContentValues;
     20 import android.provider.MediaStore.Audio;
     21 import android.provider.MediaStore.Images;
     22 import android.provider.MediaStore.Video;
     23 import android.media.DecoderCapabilities;
     24 import android.media.DecoderCapabilities.VideoDecoder;
     25 import android.media.DecoderCapabilities.AudioDecoder;
     26 import android.mtp.MtpConstants;
     27 
     28 import java.util.HashMap;
     29 import java.util.Iterator;
     30 import java.util.List;
     31 import java.util.Locale;
     32 
     33 /**
     34  * MediaScanner helper class.
     35  *
     36  * {@hide}
     37  */
     38 public class MediaFile {
     39 
     40     // Audio file types
     41     public static final int FILE_TYPE_MP3     = 1;
     42     public static final int FILE_TYPE_M4A     = 2;
     43     public static final int FILE_TYPE_WAV     = 3;
     44     public static final int FILE_TYPE_AMR     = 4;
     45     public static final int FILE_TYPE_AWB     = 5;
     46     public static final int FILE_TYPE_WMA     = 6;
     47     public static final int FILE_TYPE_OGG     = 7;
     48     public static final int FILE_TYPE_AAC     = 8;
     49     public static final int FILE_TYPE_MKA     = 9;
     50     public static final int FILE_TYPE_FLAC    = 10;
     51     private static final int FIRST_AUDIO_FILE_TYPE = FILE_TYPE_MP3;
     52     private static final int LAST_AUDIO_FILE_TYPE = FILE_TYPE_FLAC;
     53 
     54     // MIDI file types
     55     public static final int FILE_TYPE_MID     = 11;
     56     public static final int FILE_TYPE_SMF     = 12;
     57     public static final int FILE_TYPE_IMY     = 13;
     58     private static final int FIRST_MIDI_FILE_TYPE = FILE_TYPE_MID;
     59     private static final int LAST_MIDI_FILE_TYPE = FILE_TYPE_IMY;
     60 
     61     // Video file types
     62     public static final int FILE_TYPE_MP4     = 21;
     63     public static final int FILE_TYPE_M4V     = 22;
     64     public static final int FILE_TYPE_3GPP    = 23;
     65     public static final int FILE_TYPE_3GPP2   = 24;
     66     public static final int FILE_TYPE_WMV     = 25;
     67     public static final int FILE_TYPE_ASF     = 26;
     68     public static final int FILE_TYPE_MKV     = 27;
     69     public static final int FILE_TYPE_MP2TS   = 28;
     70     public static final int FILE_TYPE_AVI     = 29;
     71     public static final int FILE_TYPE_WEBM    = 30;
     72     private static final int FIRST_VIDEO_FILE_TYPE = FILE_TYPE_MP4;
     73     private static final int LAST_VIDEO_FILE_TYPE = FILE_TYPE_WEBM;
     74 
     75     // More video file types
     76     public static final int FILE_TYPE_MP2PS   = 200;
     77     private static final int FIRST_VIDEO_FILE_TYPE2 = FILE_TYPE_MP2PS;
     78     private static final int LAST_VIDEO_FILE_TYPE2 = FILE_TYPE_MP2PS;
     79 
     80     // Image file types
     81     public static final int FILE_TYPE_JPEG    = 31;
     82     public static final int FILE_TYPE_GIF     = 32;
     83     public static final int FILE_TYPE_PNG     = 33;
     84     public static final int FILE_TYPE_BMP     = 34;
     85     public static final int FILE_TYPE_WBMP    = 35;
     86     public static final int FILE_TYPE_WEBP    = 36;
     87     private static final int FIRST_IMAGE_FILE_TYPE = FILE_TYPE_JPEG;
     88     private static final int LAST_IMAGE_FILE_TYPE = FILE_TYPE_WEBP;
     89 
     90     // Playlist file types
     91     public static final int FILE_TYPE_M3U      = 41;
     92     public static final int FILE_TYPE_PLS      = 42;
     93     public static final int FILE_TYPE_WPL      = 43;
     94     public static final int FILE_TYPE_HTTPLIVE = 44;
     95 
     96     private static final int FIRST_PLAYLIST_FILE_TYPE = FILE_TYPE_M3U;
     97     private static final int LAST_PLAYLIST_FILE_TYPE = FILE_TYPE_HTTPLIVE;
     98 
     99     // Drm file types
    100     public static final int FILE_TYPE_FL      = 51;
    101     private static final int FIRST_DRM_FILE_TYPE = FILE_TYPE_FL;
    102     private static final int LAST_DRM_FILE_TYPE = FILE_TYPE_FL;
    103 
    104     // Other popular file types
    105     public static final int FILE_TYPE_TEXT          = 100;
    106     public static final int FILE_TYPE_HTML          = 101;
    107     public static final int FILE_TYPE_PDF           = 102;
    108     public static final int FILE_TYPE_XML           = 103;
    109     public static final int FILE_TYPE_MS_WORD       = 104;
    110     public static final int FILE_TYPE_MS_EXCEL      = 105;
    111     public static final int FILE_TYPE_MS_POWERPOINT = 106;
    112     public static final int FILE_TYPE_ZIP           = 107;
    113 
    114     public static class MediaFileType {
    115         public final int fileType;
    116         public final String mimeType;
    117 
    118         MediaFileType(int fileType, String mimeType) {
    119             this.fileType = fileType;
    120             this.mimeType = mimeType;
    121         }
    122     }
    123 
    124     private static final HashMap<String, MediaFileType> sFileTypeMap
    125             = new HashMap<String, MediaFileType>();
    126     private static final HashMap<String, Integer> sMimeTypeMap
    127             = new HashMap<String, Integer>();
    128     // maps file extension to MTP format code
    129     private static final HashMap<String, Integer> sFileTypeToFormatMap
    130             = new HashMap<String, Integer>();
    131     // maps mime type to MTP format code
    132     private static final HashMap<String, Integer> sMimeTypeToFormatMap
    133             = new HashMap<String, Integer>();
    134     // maps MTP format code to mime type
    135     private static final HashMap<Integer, String> sFormatToMimeTypeMap
    136             = new HashMap<Integer, String>();
    137 
    138     static void addFileType(String extension, int fileType, String mimeType) {
    139         sFileTypeMap.put(extension, new MediaFileType(fileType, mimeType));
    140         sMimeTypeMap.put(mimeType, Integer.valueOf(fileType));
    141     }
    142 
    143     static void addFileType(String extension, int fileType, String mimeType, int mtpFormatCode) {
    144         addFileType(extension, fileType, mimeType);
    145         sFileTypeToFormatMap.put(extension, Integer.valueOf(mtpFormatCode));
    146         sMimeTypeToFormatMap.put(mimeType, Integer.valueOf(mtpFormatCode));
    147         sFormatToMimeTypeMap.put(mtpFormatCode, mimeType);
    148     }
    149 
    150     private static boolean isWMAEnabled() {
    151         List<AudioDecoder> decoders = DecoderCapabilities.getAudioDecoders();
    152         int count = decoders.size();
    153         for (int i = 0; i < count; i++) {
    154             AudioDecoder decoder = decoders.get(i);
    155             if (decoder == AudioDecoder.AUDIO_DECODER_WMA) {
    156                 return true;
    157             }
    158         }
    159         return false;
    160     }
    161 
    162     private static boolean isWMVEnabled() {
    163         List<VideoDecoder> decoders = DecoderCapabilities.getVideoDecoders();
    164         int count = decoders.size();
    165         for (int i = 0; i < count; i++) {
    166             VideoDecoder decoder = decoders.get(i);
    167             if (decoder == VideoDecoder.VIDEO_DECODER_WMV) {
    168                 return true;
    169             }
    170         }
    171         return false;
    172     }
    173 
    174     static {
    175         addFileType("MP3", FILE_TYPE_MP3, "audio/mpeg", MtpConstants.FORMAT_MP3);
    176         addFileType("MPGA", FILE_TYPE_MP3, "audio/mpeg", MtpConstants.FORMAT_MP3);
    177         addFileType("M4A", FILE_TYPE_M4A, "audio/mp4", MtpConstants.FORMAT_MPEG);
    178         addFileType("WAV", FILE_TYPE_WAV, "audio/x-wav", MtpConstants.FORMAT_WAV);
    179         addFileType("AMR", FILE_TYPE_AMR, "audio/amr");
    180         addFileType("AWB", FILE_TYPE_AWB, "audio/amr-wb");
    181         if (isWMAEnabled()) {
    182             addFileType("WMA", FILE_TYPE_WMA, "audio/x-ms-wma", MtpConstants.FORMAT_WMA);
    183         }
    184         addFileType("OGG", FILE_TYPE_OGG, "audio/ogg", MtpConstants.FORMAT_OGG);
    185         addFileType("OGG", FILE_TYPE_OGG, "application/ogg", MtpConstants.FORMAT_OGG);
    186         addFileType("OGA", FILE_TYPE_OGG, "application/ogg", MtpConstants.FORMAT_OGG);
    187         addFileType("AAC", FILE_TYPE_AAC, "audio/aac", MtpConstants.FORMAT_AAC);
    188         addFileType("AAC", FILE_TYPE_AAC, "audio/aac-adts", MtpConstants.FORMAT_AAC);
    189         addFileType("MKA", FILE_TYPE_MKA, "audio/x-matroska");
    190 
    191         addFileType("MID", FILE_TYPE_MID, "audio/midi");
    192         addFileType("MIDI", FILE_TYPE_MID, "audio/midi");
    193         addFileType("XMF", FILE_TYPE_MID, "audio/midi");
    194         addFileType("RTTTL", FILE_TYPE_MID, "audio/midi");
    195         addFileType("SMF", FILE_TYPE_SMF, "audio/sp-midi");
    196         addFileType("IMY", FILE_TYPE_IMY, "audio/imelody");
    197         addFileType("RTX", FILE_TYPE_MID, "audio/midi");
    198         addFileType("OTA", FILE_TYPE_MID, "audio/midi");
    199         addFileType("MXMF", FILE_TYPE_MID, "audio/midi");
    200 
    201         addFileType("MPEG", FILE_TYPE_MP4, "video/mpeg", MtpConstants.FORMAT_MPEG);
    202         addFileType("MPG", FILE_TYPE_MP4, "video/mpeg", MtpConstants.FORMAT_MPEG);
    203         addFileType("MP4", FILE_TYPE_MP4, "video/mp4", MtpConstants.FORMAT_MPEG);
    204         addFileType("M4V", FILE_TYPE_M4V, "video/mp4", MtpConstants.FORMAT_MPEG);
    205         addFileType("3GP", FILE_TYPE_3GPP, "video/3gpp",  MtpConstants.FORMAT_3GP_CONTAINER);
    206         addFileType("3GPP", FILE_TYPE_3GPP, "video/3gpp", MtpConstants.FORMAT_3GP_CONTAINER);
    207         addFileType("3G2", FILE_TYPE_3GPP2, "video/3gpp2", MtpConstants.FORMAT_3GP_CONTAINER);
    208         addFileType("3GPP2", FILE_TYPE_3GPP2, "video/3gpp2", MtpConstants.FORMAT_3GP_CONTAINER);
    209         addFileType("MKV", FILE_TYPE_MKV, "video/x-matroska");
    210         addFileType("WEBM", FILE_TYPE_WEBM, "video/webm");
    211         addFileType("TS", FILE_TYPE_MP2TS, "video/mp2ts");
    212         addFileType("AVI", FILE_TYPE_AVI, "video/avi");
    213 
    214         if (isWMVEnabled()) {
    215             addFileType("WMV", FILE_TYPE_WMV, "video/x-ms-wmv", MtpConstants.FORMAT_WMV);
    216             addFileType("ASF", FILE_TYPE_ASF, "video/x-ms-asf");
    217         }
    218 
    219         addFileType("JPG", FILE_TYPE_JPEG, "image/jpeg", MtpConstants.FORMAT_EXIF_JPEG);
    220         addFileType("JPEG", FILE_TYPE_JPEG, "image/jpeg", MtpConstants.FORMAT_EXIF_JPEG);
    221         addFileType("GIF", FILE_TYPE_GIF, "image/gif", MtpConstants.FORMAT_GIF);
    222         addFileType("PNG", FILE_TYPE_PNG, "image/png", MtpConstants.FORMAT_PNG);
    223         addFileType("BMP", FILE_TYPE_BMP, "image/x-ms-bmp", MtpConstants.FORMAT_BMP);
    224         addFileType("WBMP", FILE_TYPE_WBMP, "image/vnd.wap.wbmp");
    225         addFileType("WEBP", FILE_TYPE_WEBP, "image/webp");
    226 
    227         addFileType("M3U", FILE_TYPE_M3U, "audio/x-mpegurl", MtpConstants.FORMAT_M3U_PLAYLIST);
    228         addFileType("M3U", FILE_TYPE_M3U, "application/x-mpegurl", MtpConstants.FORMAT_M3U_PLAYLIST);
    229         addFileType("PLS", FILE_TYPE_PLS, "audio/x-scpls", MtpConstants.FORMAT_PLS_PLAYLIST);
    230         addFileType("WPL", FILE_TYPE_WPL, "application/vnd.ms-wpl", MtpConstants.FORMAT_WPL_PLAYLIST);
    231         addFileType("M3U8", FILE_TYPE_HTTPLIVE, "application/vnd.apple.mpegurl");
    232         addFileType("M3U8", FILE_TYPE_HTTPLIVE, "audio/mpegurl");
    233         addFileType("M3U8", FILE_TYPE_HTTPLIVE, "audio/x-mpegurl");
    234 
    235         addFileType("FL", FILE_TYPE_FL, "application/x-android-drm-fl");
    236 
    237         addFileType("TXT", FILE_TYPE_TEXT, "text/plain", MtpConstants.FORMAT_TEXT);
    238         addFileType("HTM", FILE_TYPE_HTML, "text/html", MtpConstants.FORMAT_HTML);
    239         addFileType("HTML", FILE_TYPE_HTML, "text/html", MtpConstants.FORMAT_HTML);
    240         addFileType("PDF", FILE_TYPE_PDF, "application/pdf");
    241         addFileType("DOC", FILE_TYPE_MS_WORD, "application/msword", MtpConstants.FORMAT_MS_WORD_DOCUMENT);
    242         addFileType("XLS", FILE_TYPE_MS_EXCEL, "application/vnd.ms-excel", MtpConstants.FORMAT_MS_EXCEL_SPREADSHEET);
    243         addFileType("PPT", FILE_TYPE_MS_POWERPOINT, "application/mspowerpoint", MtpConstants.FORMAT_MS_POWERPOINT_PRESENTATION);
    244         addFileType("FLAC", FILE_TYPE_FLAC, "audio/flac", MtpConstants.FORMAT_FLAC);
    245         addFileType("ZIP", FILE_TYPE_ZIP, "application/zip");
    246         addFileType("MPG", FILE_TYPE_MP2PS, "video/mp2p");
    247         addFileType("MPEG", FILE_TYPE_MP2PS, "video/mp2p");
    248     }
    249 
    250     public static boolean isAudioFileType(int fileType) {
    251         return ((fileType >= FIRST_AUDIO_FILE_TYPE &&
    252                 fileType <= LAST_AUDIO_FILE_TYPE) ||
    253                 (fileType >= FIRST_MIDI_FILE_TYPE &&
    254                 fileType <= LAST_MIDI_FILE_TYPE));
    255     }
    256 
    257     public static boolean isVideoFileType(int fileType) {
    258         return (fileType >= FIRST_VIDEO_FILE_TYPE &&
    259                 fileType <= LAST_VIDEO_FILE_TYPE)
    260             || (fileType >= FIRST_VIDEO_FILE_TYPE2 &&
    261                 fileType <= LAST_VIDEO_FILE_TYPE2);
    262     }
    263 
    264     public static boolean isImageFileType(int fileType) {
    265         return (fileType >= FIRST_IMAGE_FILE_TYPE &&
    266                 fileType <= LAST_IMAGE_FILE_TYPE);
    267     }
    268 
    269     public static boolean isPlayListFileType(int fileType) {
    270         return (fileType >= FIRST_PLAYLIST_FILE_TYPE &&
    271                 fileType <= LAST_PLAYLIST_FILE_TYPE);
    272     }
    273 
    274     public static boolean isDrmFileType(int fileType) {
    275         return (fileType >= FIRST_DRM_FILE_TYPE &&
    276                 fileType <= LAST_DRM_FILE_TYPE);
    277     }
    278 
    279     public static MediaFileType getFileType(String path) {
    280         int lastDot = path.lastIndexOf('.');
    281         if (lastDot < 0)
    282             return null;
    283         return sFileTypeMap.get(path.substring(lastDot + 1).toUpperCase(Locale.ROOT));
    284     }
    285 
    286     public static boolean isMimeTypeMedia(String mimeType) {
    287         int fileType = getFileTypeForMimeType(mimeType);
    288         return isAudioFileType(fileType) || isVideoFileType(fileType)
    289                 || isImageFileType(fileType) || isPlayListFileType(fileType);
    290     }
    291 
    292     // generates a title based on file name
    293     public static String getFileTitle(String path) {
    294         // extract file name after last slash
    295         int lastSlash = path.lastIndexOf('/');
    296         if (lastSlash >= 0) {
    297             lastSlash++;
    298             if (lastSlash < path.length()) {
    299                 path = path.substring(lastSlash);
    300             }
    301         }
    302         // truncate the file extension (if any)
    303         int lastDot = path.lastIndexOf('.');
    304         if (lastDot > 0) {
    305             path = path.substring(0, lastDot);
    306         }
    307         return path;
    308     }
    309 
    310     public static int getFileTypeForMimeType(String mimeType) {
    311         Integer value = sMimeTypeMap.get(mimeType);
    312         return (value == null ? 0 : value.intValue());
    313     }
    314 
    315     public static String getMimeTypeForFile(String path) {
    316         MediaFileType mediaFileType = getFileType(path);
    317         return (mediaFileType == null ? null : mediaFileType.mimeType);
    318     }
    319 
    320     public static int getFormatCode(String fileName, String mimeType) {
    321         if (mimeType != null) {
    322             Integer value = sMimeTypeToFormatMap.get(mimeType);
    323             if (value != null) {
    324                 return value.intValue();
    325             }
    326         }
    327         int lastDot = fileName.lastIndexOf('.');
    328         if (lastDot > 0) {
    329             String extension = fileName.substring(lastDot + 1).toUpperCase(Locale.ROOT);
    330             Integer value = sFileTypeToFormatMap.get(extension);
    331             if (value != null) {
    332                 return value.intValue();
    333             }
    334         }
    335         return MtpConstants.FORMAT_UNDEFINED;
    336     }
    337 
    338     public static String getMimeTypeForFormatCode(int formatCode) {
    339         return sFormatToMimeTypeMap.get(formatCode);
    340     }
    341 }
    342