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 27 import java.util.HashMap; 28 import java.util.Iterator; 29 import java.util.List; 30 31 /** 32 * MediaScanner helper class. 33 * 34 * {@hide} 35 */ 36 public class MediaFile { 37 // comma separated list of all file extensions supported by the media scanner 38 public final static String sFileExtensions; 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 private static final int FIRST_AUDIO_FILE_TYPE = FILE_TYPE_MP3; 51 private static final int LAST_AUDIO_FILE_TYPE = FILE_TYPE_MKA; 52 53 // MIDI file types 54 public static final int FILE_TYPE_MID = 11; 55 public static final int FILE_TYPE_SMF = 12; 56 public static final int FILE_TYPE_IMY = 13; 57 private static final int FIRST_MIDI_FILE_TYPE = FILE_TYPE_MID; 58 private static final int LAST_MIDI_FILE_TYPE = FILE_TYPE_IMY; 59 60 // Video file types 61 public static final int FILE_TYPE_MP4 = 21; 62 public static final int FILE_TYPE_M4V = 22; 63 public static final int FILE_TYPE_3GPP = 23; 64 public static final int FILE_TYPE_3GPP2 = 24; 65 public static final int FILE_TYPE_WMV = 25; 66 public static final int FILE_TYPE_ASF = 26; 67 public static final int FILE_TYPE_MKV = 27; 68 public static final int FILE_TYPE_MP2TS = 28; 69 private static final int FIRST_VIDEO_FILE_TYPE = FILE_TYPE_MP4; 70 private static final int LAST_VIDEO_FILE_TYPE = FILE_TYPE_MP2TS; 71 72 // Image file types 73 public static final int FILE_TYPE_JPEG = 31; 74 public static final int FILE_TYPE_GIF = 32; 75 public static final int FILE_TYPE_PNG = 33; 76 public static final int FILE_TYPE_BMP = 34; 77 public static final int FILE_TYPE_WBMP = 35; 78 private static final int FIRST_IMAGE_FILE_TYPE = FILE_TYPE_JPEG; 79 private static final int LAST_IMAGE_FILE_TYPE = FILE_TYPE_WBMP; 80 81 // Playlist file types 82 public static final int FILE_TYPE_M3U = 41; 83 public static final int FILE_TYPE_PLS = 42; 84 public static final int FILE_TYPE_WPL = 43; 85 private static final int FIRST_PLAYLIST_FILE_TYPE = FILE_TYPE_M3U; 86 private static final int LAST_PLAYLIST_FILE_TYPE = FILE_TYPE_WPL; 87 88 static class MediaFileType { 89 90 int fileType; 91 String mimeType; 92 93 MediaFileType(int fileType, String mimeType) { 94 this.fileType = fileType; 95 this.mimeType = mimeType; 96 } 97 } 98 99 private static HashMap<String, MediaFileType> sFileTypeMap 100 = new HashMap<String, MediaFileType>(); 101 private static HashMap<String, Integer> sMimeTypeMap 102 = new HashMap<String, Integer>(); 103 static void addFileType(String extension, int fileType, String mimeType) { 104 sFileTypeMap.put(extension, new MediaFileType(fileType, mimeType)); 105 sMimeTypeMap.put(mimeType, Integer.valueOf(fileType)); 106 } 107 108 private static boolean isWMAEnabled() { 109 List<AudioDecoder> decoders = DecoderCapabilities.getAudioDecoders(); 110 for (AudioDecoder decoder: decoders) { 111 if (decoder == AudioDecoder.AUDIO_DECODER_WMA) { 112 return true; 113 } 114 } 115 return false; 116 } 117 118 private static boolean isWMVEnabled() { 119 List<VideoDecoder> decoders = DecoderCapabilities.getVideoDecoders(); 120 for (VideoDecoder decoder: decoders) { 121 if (decoder == VideoDecoder.VIDEO_DECODER_WMV) { 122 return true; 123 } 124 } 125 return false; 126 } 127 128 static { 129 addFileType("MP3", FILE_TYPE_MP3, "audio/mpeg"); 130 addFileType("M4A", FILE_TYPE_M4A, "audio/mp4"); 131 addFileType("WAV", FILE_TYPE_WAV, "audio/x-wav"); 132 addFileType("AMR", FILE_TYPE_AMR, "audio/amr"); 133 addFileType("AWB", FILE_TYPE_AWB, "audio/amr-wb"); 134 if (isWMAEnabled()) { 135 addFileType("WMA", FILE_TYPE_WMA, "audio/x-ms-wma"); 136 } 137 addFileType("OGG", FILE_TYPE_OGG, "application/ogg"); 138 addFileType("OGA", FILE_TYPE_OGG, "application/ogg"); 139 addFileType("AAC", FILE_TYPE_AAC, "audio/aac"); 140 addFileType("MKA", FILE_TYPE_MKA, "audio/x-matroska"); 141 142 addFileType("MID", FILE_TYPE_MID, "audio/midi"); 143 addFileType("MIDI", FILE_TYPE_MID, "audio/midi"); 144 addFileType("XMF", FILE_TYPE_MID, "audio/midi"); 145 addFileType("RTTTL", FILE_TYPE_MID, "audio/midi"); 146 addFileType("SMF", FILE_TYPE_SMF, "audio/sp-midi"); 147 addFileType("IMY", FILE_TYPE_IMY, "audio/imelody"); 148 addFileType("RTX", FILE_TYPE_MID, "audio/midi"); 149 addFileType("OTA", FILE_TYPE_MID, "audio/midi"); 150 151 addFileType("MPEG", FILE_TYPE_MP4, "video/mpeg"); 152 addFileType("MP4", FILE_TYPE_MP4, "video/mp4"); 153 addFileType("M4V", FILE_TYPE_M4V, "video/mp4"); 154 addFileType("3GP", FILE_TYPE_3GPP, "video/3gpp"); 155 addFileType("3GPP", FILE_TYPE_3GPP, "video/3gpp"); 156 addFileType("3G2", FILE_TYPE_3GPP2, "video/3gpp2"); 157 addFileType("3GPP2", FILE_TYPE_3GPP2, "video/3gpp2"); 158 addFileType("MKV", FILE_TYPE_MKV, "video/x-matroska"); 159 addFileType("WEBM", FILE_TYPE_MKV, "video/x-matroska"); 160 addFileType("TS", FILE_TYPE_MP2TS, "video/mp2ts"); 161 162 if (isWMVEnabled()) { 163 addFileType("WMV", FILE_TYPE_WMV, "video/x-ms-wmv"); 164 addFileType("ASF", FILE_TYPE_ASF, "video/x-ms-asf"); 165 } 166 167 addFileType("JPG", FILE_TYPE_JPEG, "image/jpeg"); 168 addFileType("JPEG", FILE_TYPE_JPEG, "image/jpeg"); 169 addFileType("GIF", FILE_TYPE_GIF, "image/gif"); 170 addFileType("PNG", FILE_TYPE_PNG, "image/png"); 171 addFileType("BMP", FILE_TYPE_BMP, "image/x-ms-bmp"); 172 addFileType("WBMP", FILE_TYPE_WBMP, "image/vnd.wap.wbmp"); 173 174 addFileType("M3U", FILE_TYPE_M3U, "audio/x-mpegurl"); 175 addFileType("PLS", FILE_TYPE_PLS, "audio/x-scpls"); 176 addFileType("WPL", FILE_TYPE_WPL, "application/vnd.ms-wpl"); 177 178 // compute file extensions list for native Media Scanner 179 StringBuilder builder = new StringBuilder(); 180 Iterator<String> iterator = sFileTypeMap.keySet().iterator(); 181 182 while (iterator.hasNext()) { 183 if (builder.length() > 0) { 184 builder.append(','); 185 } 186 builder.append(iterator.next()); 187 } 188 sFileExtensions = builder.toString(); 189 } 190 191 public static boolean isAudioFileType(int fileType) { 192 return ((fileType >= FIRST_AUDIO_FILE_TYPE && 193 fileType <= LAST_AUDIO_FILE_TYPE) || 194 (fileType >= FIRST_MIDI_FILE_TYPE && 195 fileType <= LAST_MIDI_FILE_TYPE)); 196 } 197 198 public static boolean isVideoFileType(int fileType) { 199 return (fileType >= FIRST_VIDEO_FILE_TYPE && 200 fileType <= LAST_VIDEO_FILE_TYPE); 201 } 202 203 public static boolean isImageFileType(int fileType) { 204 return (fileType >= FIRST_IMAGE_FILE_TYPE && 205 fileType <= LAST_IMAGE_FILE_TYPE); 206 } 207 208 public static boolean isPlayListFileType(int fileType) { 209 return (fileType >= FIRST_PLAYLIST_FILE_TYPE && 210 fileType <= LAST_PLAYLIST_FILE_TYPE); 211 } 212 213 public static MediaFileType getFileType(String path) { 214 int lastDot = path.lastIndexOf("."); 215 if (lastDot < 0) 216 return null; 217 return sFileTypeMap.get(path.substring(lastDot + 1).toUpperCase()); 218 } 219 220 public static int getFileTypeForMimeType(String mimeType) { 221 Integer value = sMimeTypeMap.get(mimeType); 222 return (value == null ? 0 : value.intValue()); 223 } 224 225 } 226