1/**
2 * Copyright (c) 2010, Google Inc.
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 */
16package com.android.mail.utils;
17
18import android.content.Context;
19import android.content.Intent;
20import android.content.pm.PackageManager;
21import android.content.pm.ResolveInfo;
22import android.net.Uri;
23import android.text.TextUtils;
24import android.webkit.MimeTypeMap;
25
26import com.google.common.annotations.VisibleForTesting;
27import com.google.common.collect.ImmutableSet;
28
29import java.util.List;
30import java.util.Set;
31
32/**
33 * Utilities for working with different content types within Mail.
34 */
35public class MimeType {
36    private static final String LOG_TAG = LogTag.getLogTag();
37
38    public static final String ANDROID_ARCHIVE = "application/vnd.android.package-archive";
39    private static final String TEXT_PLAIN = "text/plain";
40    @VisibleForTesting
41    static final String GENERIC_MIMETYPE = "application/octet-stream";
42
43    @VisibleForTesting
44    private static final Set<String> EML_ATTACHMENT_CONTENT_TYPES = ImmutableSet.of(
45            "message/rfc822", "application/eml");
46    public static final String EML_ATTACHMENT_CONTENT_TYPE = "message/rfc822";
47    private static final String NULL_ATTACHMENT_CONTENT_TYPE = "null";
48
49    /**
50     * Returns whether or not an attachment of the specified type is installable (e.g. an apk).
51     */
52    public static boolean isInstallable(String type) {
53        return ANDROID_ARCHIVE.equals(type);
54    }
55
56    /**
57     * Returns whether or not an attachment of the specified type is viewable.
58     */
59    public static boolean isViewable(Context context, Uri contentUri, String contentType) {
60        // The provider returns a contentType of "null" instead of null, when the
61        // content type is not known.  Changing the provider to return null,
62        // breaks other areas that will need to be fixed in a later CL.
63        // Bug 2922948 has been written up to track this
64        if (contentType == null || contentType.length() == 0 ||
65                NULL_ATTACHMENT_CONTENT_TYPE.equals(contentType)) {
66            LogUtils.d(LOG_TAG, "Attachment with null content type. '%s", contentUri);
67            return false;
68        }
69
70        final Intent mimetypeIntent = new Intent(Intent.ACTION_VIEW);
71        mimetypeIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
72                | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
73
74        if (contentUri != null) {
75            Utils.setIntentDataAndTypeAndNormalize(mimetypeIntent, contentUri, contentType);
76        } else {
77            // Fake a reasonable looking URI so that intent filters that specify a scheme will match
78            final Uri dummyUri = Uri.parse("content://" + context.getPackageName());
79            Utils.setIntentDataAndTypeAndNormalize(mimetypeIntent, dummyUri, contentType);
80        }
81
82        PackageManager manager;
83        // We need to catch the exception to make CanvasConversationHeaderView
84        // test pass.  Bug: http://b/issue?id=3470653.
85        try {
86            manager = context.getPackageManager();
87        } catch (UnsupportedOperationException e) {
88            return false;
89        }
90        final List<ResolveInfo> list = manager.queryIntentActivities(mimetypeIntent,
91                PackageManager.MATCH_DEFAULT_ONLY);
92        if (list.size() == 0) {
93            // This logging will help track down bug 7092215.  Once that bug is resolved, remove
94            // this.
95            LogUtils.w(LOG_TAG, "Unable to find supporting activity. " +
96                    "mime-type: %s, uri: %s, normalized mime-type: %s normalized uri: %s",
97                    contentType, contentUri, mimetypeIntent.getType(), mimetypeIntent.getData());
98        }
99        return list.size() > 0;
100    }
101
102    /**
103     * Extract and return filename's extension, converted to lower case, and not including the "."
104     *
105     * @return extension, or null if not found (or null/empty filename)
106     */
107    private static String getFilenameExtension(String fileName) {
108        String extension = null;
109        if (!TextUtils.isEmpty(fileName)) {
110            int lastDot = fileName.lastIndexOf('.');
111            if ((lastDot > 0) && (lastDot < fileName.length() - 1)) {
112                extension = fileName.substring(lastDot + 1).toLowerCase();
113            }
114        }
115        return extension;
116    }
117
118
119    /**
120     * Returns the mime type of the attachment based on its name and
121     * original mime type. This is an workaround for bugs where Gmail
122     * server doesn't set content-type for certain types correctly.
123     * 1) EML files -> "message/rfc822".
124     * @param name name of the attachment.
125     * @param mimeType original mime type of the attachment.
126     * @return the inferred mime type of the attachment.
127     */
128    public static String inferMimeType(String name, String mimeType) {
129        final String extension = getFilenameExtension(name);
130        if (TextUtils.isEmpty(extension)) {
131            // Attachment doesn't have extension, just return original mime
132            // type.
133            return mimeType;
134        } else {
135            final boolean isTextPlain = TEXT_PLAIN.equalsIgnoreCase(mimeType);
136            final boolean isGenericType =
137                    isTextPlain || GENERIC_MIMETYPE.equalsIgnoreCase(mimeType);
138
139            String type = null;
140            if (isGenericType || TextUtils.isEmpty(mimeType)) {
141                type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
142            }
143            if (!TextUtils.isEmpty(type)) {
144                return type;
145            } if (extension.equals("eml")) {
146                // Extension is ".eml", return mime type "message/rfc822"
147                return EML_ATTACHMENT_CONTENT_TYPE;
148            } else {
149                // Extension is not ".eml", just return original mime type.
150                return !TextUtils.isEmpty(mimeType) ? mimeType : GENERIC_MIMETYPE;
151            }
152        }
153    }
154
155    /**
156     * Checks the supplied mime type to determine if it is a valid eml file.
157     * Valid mime types are "message/rfc822" and "application/eml".
158     * @param mimeType the mime type to check
159     * @return {@code true} if the mime type is one of the valid mime types.
160     */
161    public static boolean isEmlMimeType(String mimeType) {
162        return EML_ATTACHMENT_CONTENT_TYPES.contains(mimeType);
163    }
164}
165