2 * Copyright (C) 2015 The CyanogenMod Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.cyanogenmod.filemanager.util;
19 import android.media.MediaMetadataRetriever;
20 import android.util.Log;
22 import java.util.HashMap;
25 * Provides the ability to determine the mimetype of a known file extension that can support
28 public abstract class AmbiguousExtensionHelper {
30 * All available ambiguous extension helpers.
32 public static final HashMap<String, AmbiguousExtensionHelper> AMBIGUOUS_EXTENSIONS_MAP = new
33 HashMap<String, AmbiguousExtensionHelper>();
36 addAmbiguousHelperToMap(new ThreeGPExtensionHelper());
39 public abstract String getMimeType(String absolutePath, String extension);
40 public abstract String[] getSupportedExtensions();
42 private static void addAmbiguousHelperToMap(AmbiguousExtensionHelper instance) {
43 for(String extension : instance.getSupportedExtensions()) {
44 AmbiguousExtensionHelper.AMBIGUOUS_EXTENSIONS_MAP.put(extension, instance);
49 * An AmbiguousExtensionHelper subclass that can distinguish the mimetype of a given
50 * .g3p, .g3pp, .3g2 or .3gpp2 file. The 3GP and 3G2 file formats support both audio and
51 * video, and a file with that extension has the possibility of multiple mimetypes, depending
52 * on the content of the file.
54 public static class ThreeGPExtensionHelper extends AmbiguousExtensionHelper {
55 private static final String TAG = "ThreeGPExtensionHelper";
56 private static final String[] sSupportedExtensions = {"3gp", "3gpp", "3g2", "3gpp2"};
57 public static final String VIDEO_3GPP_MIME_TYPE = "video/3gpp";
58 public static final String AUDIO_3GPP_MIME_TYPE = "audio/3gpp";
59 public static final String VIDEO_3GPP2_MIME_TYPE = "video/3gpp2";
60 public static final String AUDIO_3GPP2_MIME_TYPE = "audio/3gpp2";
63 public String getMimeType(String absolutePath, String extension) {
64 MediaMetadataRetriever retriever = new MediaMetadataRetriever();
66 retriever.setDataSource(absolutePath);
68 retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO) !=
70 if (is3GPP(extension)) {
71 return hasVideo ? VIDEO_3GPP_MIME_TYPE : AUDIO_3GPP_MIME_TYPE;
72 } else if (is3GPP2(extension)) {
73 return hasVideo ? VIDEO_3GPP2_MIME_TYPE : AUDIO_3GPP2_MIME_TYPE;
75 } catch (RuntimeException e) {
76 Log.e(TAG, "Unable to open 3GP file to determine mimetype");
78 // Default to video 3gp if the file is unreadable as this was the default before
79 // ambiguous resolution support was added.
80 return VIDEO_3GPP_MIME_TYPE;
84 public String[] getSupportedExtensions() {
85 return sSupportedExtensions;
88 private boolean is3GPP(String ext) {
89 return "3gp".equals(ext) || "3gpp".equals(ext);
92 private boolean is3GPP2(String ext) {
93 return "3g2".equals(ext) || "3gpp2".equals(ext);