OSDN Git Service

3aaeb6915dcd431b702d6b55bd505f1e57ece1c7
[android-x86/packages-apps-CMFileManager.git] / src / com / cyanogenmod / filemanager / util / AmbiguousExtensionHelper.java
1 /*
2  * Copyright (C) 2015 The CyanogenMod 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 com.cyanogenmod.filemanager.util;
18
19 import android.media.MediaMetadataRetriever;
20 import android.util.Log;
21
22 import java.util.HashMap;
23
24 /**
25  * Provides the ability to determine the mimetype of a known file extension that can support
26  * multiple mimetypes.
27  */
28 public abstract class AmbiguousExtensionHelper {
29     /**
30      * All available ambiguous extension helpers.
31      */
32     public static final HashMap<String, AmbiguousExtensionHelper> AMBIGUOUS_EXTENSIONS_MAP = new
33             HashMap<String, AmbiguousExtensionHelper>();
34
35     static {
36        addAmbiguousHelperToMap(new ThreeGPExtensionHelper());
37     }
38
39     public abstract String getMimeType(String absolutePath, String extension);
40     public abstract String[] getSupportedExtensions();
41
42     private static void addAmbiguousHelperToMap(AmbiguousExtensionHelper instance) {
43         for(String extension : instance.getSupportedExtensions()) {
44             AmbiguousExtensionHelper.AMBIGUOUS_EXTENSIONS_MAP.put(extension, instance);
45         }
46     }
47
48     /**
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.
53      */
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";
61
62         @Override
63         public String getMimeType(String absolutePath, String extension) {
64             MediaMetadataRetriever retriever = new MediaMetadataRetriever();
65             try {
66                 retriever.setDataSource(absolutePath);
67                 boolean hasVideo =
68                         retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO) !=
69                         null;
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;
74                 }
75             } catch (RuntimeException e) {
76                 Log.e(TAG, "Unable to open 3GP file to determine mimetype");
77             }
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;
81         }
82
83         @Override
84         public String[] getSupportedExtensions() {
85             return sSupportedExtensions;
86         }
87
88         private boolean is3GPP(String ext) {
89             return "3gp".equals(ext) || "3gpp".equals(ext);
90         }
91
92         private boolean is3GPP2(String ext) {
93             return "3g2".equals(ext) || "3gpp2".equals(ext);
94         }
95     }
96 }