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