1 /*
2  * Copyright (C) 2007-2008 Esmertec AG.
3  * Copyright (C) 2007-2008 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package com.android.messaging.util;
19 
20 import android.webkit.MimeTypeMap;
21 
22 public final class ContentType {
23     public static String THREE_GPP_EXTENSION = "3gp";
24     public static String VIDEO_MP4_EXTENSION = "mp4";
25     // Default extension used when we don't know one.
26     public static String DEFAULT_EXTENSION = "dat";
27 
28     public static final int TYPE_IMAGE = 0;
29     public static final int TYPE_VIDEO = 1;
30     public static final int TYPE_AUDIO = 2;
31     public static final int TYPE_VCARD = 3;
32     public static final int TYPE_OTHER = 4;
33 
34     public static final String ANY_TYPE          = "*/*";
35     public static final String MMS_MESSAGE       = "application/vnd.wap.mms-message";
36     // The phony content type for generic PDUs (e.g. ReadOrig.ind,
37     // Notification.ind, Delivery.ind).
38     public static final String MMS_GENERIC       = "application/vnd.wap.mms-generic";
39     public static final String MMS_MULTIPART_MIXED   = "application/vnd.wap.multipart.mixed";
40     public static final String MMS_MULTIPART_RELATED = "application/vnd.wap.multipart.related";
41     public static final String MMS_MULTIPART_ALTERNATIVE =
42             "application/vnd.wap.multipart.alternative";
43 
44     public static final String TEXT_PLAIN        = "text/plain";
45     public static final String TEXT_HTML         = "text/html";
46     public static final String TEXT_VCALENDAR    = "text/x-vCalendar";
47     public static final String TEXT_VCARD        = "text/x-vCard";
48 
49     public static final String IMAGE_PREFIX      = "image/";
50     public static final String IMAGE_UNSPECIFIED = "image/*";
51     public static final String IMAGE_JPEG        = "image/jpeg";
52     public static final String IMAGE_JPG         = "image/jpg";
53     public static final String IMAGE_GIF         = "image/gif";
54     public static final String IMAGE_WBMP        = "image/vnd.wap.wbmp";
55     public static final String IMAGE_PNG         = "image/png";
56     public static final String IMAGE_X_MS_BMP    = "image/x-ms-bmp";
57 
58     public static final String AUDIO_UNSPECIFIED = "audio/*";
59     public static final String AUDIO_AAC         = "audio/aac";
60     public static final String AUDIO_AMR         = "audio/amr";
61     public static final String AUDIO_IMELODY     = "audio/imelody";
62     public static final String AUDIO_MID         = "audio/mid";
63     public static final String AUDIO_MIDI        = "audio/midi";
64     public static final String AUDIO_MP3         = "audio/mp3";
65     public static final String AUDIO_MPEG3       = "audio/mpeg3";
66     public static final String AUDIO_MPEG        = "audio/mpeg";
67     public static final String AUDIO_MPG         = "audio/mpg";
68     public static final String AUDIO_MP4         = "audio/mp4";
69     public static final String AUDIO_MP4_LATM    = "audio/mp4-latm";
70     public static final String AUDIO_X_MID       = "audio/x-mid";
71     public static final String AUDIO_X_MIDI      = "audio/x-midi";
72     public static final String AUDIO_X_MP3       = "audio/x-mp3";
73     public static final String AUDIO_X_MPEG3     = "audio/x-mpeg3";
74     public static final String AUDIO_X_MPEG      = "audio/x-mpeg";
75     public static final String AUDIO_X_MPG       = "audio/x-mpg";
76     public static final String AUDIO_3GPP        = "audio/3gpp";
77     public static final String AUDIO_X_WAV       = "audio/x-wav";
78     public static final String AUDIO_OGG         = "application/ogg";
79 
80     public static final String MULTIPART_MIXED = "multipart/mixed";
81 
82     public static final String VIDEO_UNSPECIFIED = "video/*";
83     public static final String VIDEO_3GP         = "video/3gp";
84     public static final String VIDEO_3GPP        = "video/3gpp";
85     public static final String VIDEO_3G2         = "video/3gpp2";
86     public static final String VIDEO_H263        = "video/h263";
87     public static final String VIDEO_M4V         = "video/m4v";
88     public static final String VIDEO_MP4         = "video/mp4";
89     public static final String VIDEO_MPEG        = "video/mpeg";
90     public static final String VIDEO_MPEG4       = "video/mpeg4";
91     public static final String VIDEO_WEBM        = "video/webm";
92 
93     public static final String APP_SMIL          = "application/smil";
94     public static final String APP_WAP_XHTML     = "application/vnd.wap.xhtml+xml";
95     public static final String APP_XHTML         = "application/xhtml+xml";
96 
97     public static final String APP_DRM_CONTENT   = "application/vnd.oma.drm.content";
98     public static final String APP_DRM_MESSAGE   = "application/vnd.oma.drm.message";
99 
100     // This class should never be instantiated.
ContentType()101     private ContentType() {
102     }
103 
isTextType(final String contentType)104     public static boolean isTextType(final String contentType) {
105         return TEXT_PLAIN.equals(contentType)
106                 || TEXT_HTML.equals(contentType)
107                 || APP_WAP_XHTML.equals(contentType);
108     }
109 
isMediaType(final String contentType)110     public static boolean isMediaType(final String contentType) {
111         return isImageType(contentType)
112                 || isVideoType(contentType)
113                 || isAudioType(contentType)
114                 || isVCardType(contentType);
115     }
116 
isImageType(final String contentType)117     public static boolean isImageType(final String contentType) {
118         return (null != contentType) && contentType.startsWith(IMAGE_PREFIX);
119     }
120 
isAudioType(final String contentType)121     public static boolean isAudioType(final String contentType) {
122         return (null != contentType) &&
123                 (contentType.startsWith("audio/") || contentType.equalsIgnoreCase(AUDIO_OGG));
124     }
125 
isVideoType(final String contentType)126     public static boolean isVideoType(final String contentType) {
127         return (null != contentType) && contentType.startsWith("video/");
128     }
129 
isVCardType(final String contentType)130     public static boolean isVCardType(final String contentType) {
131         return (null != contentType) && contentType.equalsIgnoreCase(TEXT_VCARD);
132     }
133 
isDrmType(final String contentType)134     public static boolean isDrmType(final String contentType) {
135         return (null != contentType)
136                 && (contentType.equals(APP_DRM_CONTENT)
137                         || contentType.equals(APP_DRM_MESSAGE));
138     }
139 
isUnspecified(final String contentType)140     public static boolean isUnspecified(final String contentType) {
141         return (null != contentType) && contentType.endsWith("*");
142     }
143 
144     /**
145      * If the content type is a type which can be displayed in the conversation list as a preview.
146      */
isConversationListPreviewableType(final String contentType)147     public static boolean isConversationListPreviewableType(final String contentType) {
148         return ContentType.isAudioType(contentType) || ContentType.isVideoType(contentType) ||
149                 ContentType.isImageType(contentType) || ContentType.isVCardType(contentType);
150     }
151 
152     /**
153      * Given a filename, look at the extension and try and determine the mime type.
154      *
155      * @param fileName a filename to determine the type from, such as img1231.jpg
156      * @param contentTypeDefault type to use when the content type can't be determined from the file
157      *      extension. It can be null or a type such as ContentType.IMAGE_UNSPECIFIED
158      * @return Content type of the extension.
159      */
getContentTypeFromExtension(final String fileName, final String contentTypeDefault)160     public static String getContentTypeFromExtension(final String fileName,
161             final String contentTypeDefault) {
162         final MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
163         final String extension = MimeTypeMap.getFileExtensionFromUrl(fileName);
164         String contentType = mimeTypeMap.getMimeTypeFromExtension(extension);
165         if (contentType == null) {
166             contentType = contentTypeDefault;
167         }
168         return contentType;
169     }
170 
getExtensionFromMimeType(final String mimeType)171     public static String getExtensionFromMimeType(final String mimeType) {
172         final MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
173         final String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
174         return extension;
175     }
176 
177     /**
178      * Get the common file extension for a given content type
179      * @param contentType The content type
180      * @return The extension without the .
181      */
getExtension(final String contentType)182     public static String getExtension(final String contentType) {
183         if (VIDEO_MP4.equals(contentType)) {
184             return VIDEO_MP4_EXTENSION;
185         } else if (VIDEO_3GPP.equals(contentType)) {
186             return THREE_GPP_EXTENSION;
187         } else {
188             return DEFAULT_EXTENSION;
189         }
190     }
191 }
192