summaryrefslogtreecommitdiff
path: root/media/java/android/media/MediaFile.java
blob: c4eb0310f292736059ad24594d08cf8b1fd95663 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.media;

import static android.content.ContentResolver.MIME_TYPE_DEFAULT;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UnsupportedAppUsage;
import android.mtp.MtpConstants;

import libcore.content.type.MimeMap;

import java.util.HashMap;

/**
 * MediaScanner helper class.
 * <p>
 * This heavily relies upon extension to MIME type mappings which are maintained
 * in {@link MimeMap}, to ensure consistency across the OS.
 * <p>
 * When adding a new file type, first add the MIME type mapping to
 * {@link MimeMap}, and then add the MTP format mapping here.
 *
 * @hide
 */
public class MediaFile {

    /** @deprecated file types no longer exist */
    @Deprecated
    @UnsupportedAppUsage
    private static final int FIRST_AUDIO_FILE_TYPE = 1;
    /** @deprecated file types no longer exist */
    @Deprecated
    @UnsupportedAppUsage
    private static final int LAST_AUDIO_FILE_TYPE = 10;

    /** @deprecated file types no longer exist */
    @Deprecated
    public static class MediaFileType {
        @UnsupportedAppUsage
        public final int fileType;
        @UnsupportedAppUsage
        public final String mimeType;

        MediaFileType(int fileType, String mimeType) {
            this.fileType = fileType;
            this.mimeType = mimeType;
        }
    }

    /** @deprecated file types no longer exist */
    @Deprecated
    @UnsupportedAppUsage
    private static final HashMap<String, MediaFileType> sFileTypeMap = new HashMap<>();
    /** @deprecated file types no longer exist */
    @Deprecated
    @UnsupportedAppUsage
    private static final HashMap<String, Integer> sFileTypeToFormatMap = new HashMap<>();

    // maps mime type to MTP format code
    @UnsupportedAppUsage
    private static final HashMap<String, Integer> sMimeTypeToFormatMap = new HashMap<>();
    // maps MTP format code to mime type
    @UnsupportedAppUsage
    private static final HashMap<Integer, String> sFormatToMimeTypeMap = new HashMap<>();

    @UnsupportedAppUsage
    public MediaFile() {
    }

    /** @deprecated file types no longer exist */
    @Deprecated
    @UnsupportedAppUsage
    static void addFileType(String extension, int fileType, String mimeType) {
    }

    private static void addFileType(int mtpFormatCode, @NonNull String mimeType) {
        if (!sMimeTypeToFormatMap.containsKey(mimeType)) {
            sMimeTypeToFormatMap.put(mimeType, Integer.valueOf(mtpFormatCode));
        }
        if (!sFormatToMimeTypeMap.containsKey(mtpFormatCode)) {
            sFormatToMimeTypeMap.put(mtpFormatCode, mimeType);
        }
    }

    static {
        addFileType(MtpConstants.FORMAT_MP3, "audio/mpeg");
        addFileType(MtpConstants.FORMAT_WAV, "audio/x-wav");
        addFileType(MtpConstants.FORMAT_WMA, "audio/x-ms-wma");
        addFileType(MtpConstants.FORMAT_OGG, "audio/ogg");
        addFileType(MtpConstants.FORMAT_AAC, "audio/aac");
        addFileType(MtpConstants.FORMAT_FLAC, "audio/flac");
        addFileType(MtpConstants.FORMAT_AIFF, "audio/x-aiff");
        addFileType(MtpConstants.FORMAT_MP2, "audio/mpeg");

        addFileType(MtpConstants.FORMAT_MPEG, "video/mpeg");
        addFileType(MtpConstants.FORMAT_MP4_CONTAINER, "video/mp4");
        addFileType(MtpConstants.FORMAT_3GP_CONTAINER, "video/3gpp");
        addFileType(MtpConstants.FORMAT_3GP_CONTAINER, "video/3gpp2");
        addFileType(MtpConstants.FORMAT_AVI, "video/avi");
        addFileType(MtpConstants.FORMAT_WMV, "video/x-ms-wmv");
        addFileType(MtpConstants.FORMAT_ASF, "video/x-ms-asf");

        addFileType(MtpConstants.FORMAT_EXIF_JPEG, "image/jpeg");
        addFileType(MtpConstants.FORMAT_GIF, "image/gif");
        addFileType(MtpConstants.FORMAT_PNG, "image/png");
        addFileType(MtpConstants.FORMAT_BMP, "image/x-ms-bmp");
        addFileType(MtpConstants.FORMAT_HEIF, "image/heif");
        addFileType(MtpConstants.FORMAT_DNG, "image/x-adobe-dng");
        addFileType(MtpConstants.FORMAT_TIFF, "image/tiff");
        addFileType(MtpConstants.FORMAT_TIFF, "image/x-canon-cr2");
        addFileType(MtpConstants.FORMAT_TIFF, "image/x-nikon-nrw");
        addFileType(MtpConstants.FORMAT_TIFF, "image/x-sony-arw");
        addFileType(MtpConstants.FORMAT_TIFF, "image/x-panasonic-rw2");
        addFileType(MtpConstants.FORMAT_TIFF, "image/x-olympus-orf");
        addFileType(MtpConstants.FORMAT_TIFF, "image/x-pentax-pef");
        addFileType(MtpConstants.FORMAT_TIFF, "image/x-samsung-srw");
        addFileType(MtpConstants.FORMAT_TIFF_EP, "image/tiff");
        addFileType(MtpConstants.FORMAT_TIFF_EP, "image/x-nikon-nef");
        addFileType(MtpConstants.FORMAT_JP2, "image/jp2");
        addFileType(MtpConstants.FORMAT_JPX, "image/jpx");

        addFileType(MtpConstants.FORMAT_M3U_PLAYLIST, "audio/x-mpegurl");
        addFileType(MtpConstants.FORMAT_PLS_PLAYLIST, "audio/x-scpls");
        addFileType(MtpConstants.FORMAT_WPL_PLAYLIST, "application/vnd.ms-wpl");
        addFileType(MtpConstants.FORMAT_ASX_PLAYLIST, "video/x-ms-asf");

        addFileType(MtpConstants.FORMAT_TEXT, "text/plain");
        addFileType(MtpConstants.FORMAT_HTML, "text/html");
        addFileType(MtpConstants.FORMAT_XML_DOCUMENT, "text/xml");

        addFileType(MtpConstants.FORMAT_MS_WORD_DOCUMENT,
                "application/msword");
        addFileType(MtpConstants.FORMAT_MS_WORD_DOCUMENT,
                "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        addFileType(MtpConstants.FORMAT_MS_EXCEL_SPREADSHEET,
                "application/vnd.ms-excel");
        addFileType(MtpConstants.FORMAT_MS_EXCEL_SPREADSHEET,
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        addFileType(MtpConstants.FORMAT_MS_POWERPOINT_PRESENTATION,
                "application/vnd.ms-powerpoint");
        addFileType(MtpConstants.FORMAT_MS_POWERPOINT_PRESENTATION,
                "application/vnd.openxmlformats-officedocument.presentationml.presentation");
    }

    /** @deprecated file types no longer exist */
    @Deprecated
    @UnsupportedAppUsage
    public static boolean isAudioFileType(int fileType) {
        return false;
    }

    /** @deprecated file types no longer exist */
    @Deprecated
    @UnsupportedAppUsage
    public static boolean isVideoFileType(int fileType) {
        return false;
    }

    /** @deprecated file types no longer exist */
    @Deprecated
    @UnsupportedAppUsage
    public static boolean isImageFileType(int fileType) {
        return false;
    }

    /** @deprecated file types no longer exist */
    @Deprecated
    @UnsupportedAppUsage
    public static boolean isPlayListFileType(int fileType) {
        return false;
    }

    /** @deprecated file types no longer exist */
    @Deprecated
    @UnsupportedAppUsage
    public static boolean isDrmFileType(int fileType) {
        return false;
    }

    /** @deprecated file types no longer exist */
    @Deprecated
    @UnsupportedAppUsage
    public static MediaFileType getFileType(String path) {
        return null;
    }

    public static boolean isExifMimeType(@Nullable String mimeType) {
        // For simplicity, assume that all image files might have EXIF data
        return isImageMimeType(mimeType);
    }

    public static boolean isAudioMimeType(@Nullable String mimeType) {
        return normalizeMimeType(mimeType).startsWith("audio/");
    }

    public static boolean isVideoMimeType(@Nullable String mimeType) {
        return normalizeMimeType(mimeType).startsWith("video/");
    }

    public static boolean isImageMimeType(@Nullable String mimeType) {
        return normalizeMimeType(mimeType).startsWith("image/");
    }

    public static boolean isPlayListMimeType(@Nullable String mimeType) {
        switch (normalizeMimeType(mimeType)) {
            case "application/vnd.ms-wpl":
            case "audio/x-mpegurl":
            case "audio/mpegurl":
            case "application/x-mpegurl":
            case "application/vnd.apple.mpegurl":
            case "audio/x-scpls":
                return true;
            default:
                return false;
        }
    }

    public static boolean isDrmMimeType(@Nullable String mimeType) {
        return normalizeMimeType(mimeType).equals("application/x-android-drm-fl");
    }

    // generates a title based on file name
    @UnsupportedAppUsage
    public static @NonNull String getFileTitle(@NonNull String path) {
        // extract file name after last slash
        int lastSlash = path.lastIndexOf('/');
        if (lastSlash >= 0) {
            lastSlash++;
            if (lastSlash < path.length()) {
                path = path.substring(lastSlash);
            }
        }
        // truncate the file extension (if any)
        int lastDot = path.lastIndexOf('.');
        if (lastDot > 0) {
            path = path.substring(0, lastDot);
        }
        return path;
    }

    public static @Nullable String getFileExtension(@Nullable String path) {
        if (path == null) {
            return null;
        }
        int lastDot = path.lastIndexOf('.');
        if (lastDot >= 0) {
            return path.substring(lastDot + 1);
        } else {
            return null;
        }
    }

    /** @deprecated file types no longer exist */
    @Deprecated
    @UnsupportedAppUsage
    public static int getFileTypeForMimeType(String mimeType) {
        return 0;
    }

    /**
     * Find the best MIME type for the given item. Prefers mappings from file
     * extensions, since they're more accurate than format codes.
     */
    public static @NonNull String getMimeType(@Nullable String path, int formatCode) {
        // First look for extension mapping
        String mimeType = getMimeTypeForFile(path);
        if (!MIME_TYPE_DEFAULT.equals(mimeType)) {
            return mimeType;
        }

        // Otherwise look for format mapping
        return getMimeTypeForFormatCode(formatCode);
    }

    @UnsupportedAppUsage
    public static @NonNull String getMimeTypeForFile(@Nullable String path) {
        String ext = getFileExtension(path);
        final String mimeType = MimeMap.getDefault().guessMimeTypeFromExtension(ext);
        return (mimeType != null) ? mimeType : MIME_TYPE_DEFAULT;
    }

    public static @NonNull String getMimeTypeForFormatCode(int formatCode) {
        final String mimeType = sFormatToMimeTypeMap.get(formatCode);
        return (mimeType != null) ? mimeType : MIME_TYPE_DEFAULT;
    }

    /**
     * Find the best MTP format code mapping for the given item. Prefers
     * mappings from MIME types, since they're more accurate than file
     * extensions.
     */
    public static int getFormatCode(@Nullable String path, @Nullable String mimeType) {
        // First look for MIME type mapping
        int formatCode = getFormatCodeForMimeType(mimeType);
        if (formatCode != MtpConstants.FORMAT_UNDEFINED) {
            return formatCode;
        }

        // Otherwise look for extension mapping
        return getFormatCodeForFile(path);
    }

    public static int getFormatCodeForFile(@Nullable String path) {
        return getFormatCodeForMimeType(getMimeTypeForFile(path));
    }

    public static int getFormatCodeForMimeType(@Nullable String mimeType) {
        if (mimeType == null) {
            return MtpConstants.FORMAT_UNDEFINED;
        }

        // First look for direct mapping
        Integer value = sMimeTypeToFormatMap.get(mimeType);
        if (value != null) {
            return value.intValue();
        }

        // Otherwise look for indirect mapping
        mimeType = normalizeMimeType(mimeType);
        value = sMimeTypeToFormatMap.get(mimeType);
        if (value != null) {
            return value.intValue();
        } else if (mimeType.startsWith("audio/")) {
            return MtpConstants.FORMAT_UNDEFINED_AUDIO;
        } else if (mimeType.startsWith("video/")) {
            return MtpConstants.FORMAT_UNDEFINED_VIDEO;
        } else if (mimeType.startsWith("image/")) {
            return MtpConstants.FORMAT_DEFINED;
        } else {
            return MtpConstants.FORMAT_UNDEFINED;
        }
    }

    /**
     * Normalize the given MIME type by bouncing through a default file
     * extension, if defined. This handles cases like "application/x-flac" to
     * ".flac" to "audio/flac".
     */
    private static @NonNull String normalizeMimeType(@Nullable String mimeType) {
        MimeMap mimeMap = MimeMap.getDefault();
        final String extension = mimeMap.guessExtensionFromMimeType(mimeType);
        if (extension != null) {
            final String extensionMimeType = mimeMap.guessMimeTypeFromExtension(extension);
            if (extensionMimeType != null) {
                return extensionMimeType;
            }
        }
        return (mimeType != null) ? mimeType : MIME_TYPE_DEFAULT;
    }
}