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