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