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