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