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