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