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 
     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             Utils.setIntentTypeAndNormalize(mimetypeIntent, contentType);
     78         }
     79 
     80         PackageManager manager;
     81         // We need to catch the exception to make CanvasConversationHeaderView
     82         // test pass.  Bug: http://b/issue?id=3470653.
     83         try {
     84             manager = context.getPackageManager();
     85         } catch (UnsupportedOperationException e) {
     86             return false;
     87         }
     88         final List<ResolveInfo> list = manager.queryIntentActivities(mimetypeIntent,
     89                 PackageManager.MATCH_DEFAULT_ONLY);
     90         if (list.size() == 0) {
     91             // This logging will help track down bug 7092215.  Once that bug is resolved, remove
     92             // this.
     93             LogUtils.w(LOG_TAG, "Unable to find supporting activity. " +
     94                     "mime-type: %s, uri: %s, normalized mime-type: %s normalized uri: %s",
     95                     contentType, contentUri, mimetypeIntent.getType(), mimetypeIntent.getData());
     96         }
     97         return list.size() > 0;
     98     }
     99 
    100     /**
    101      * Extract and return filename's extension, converted to lower case, and not including the "."
    102      *
    103      * @return extension, or null if not found (or null/empty filename)
    104      */
    105     private static String getFilenameExtension(String fileName) {
    106         String extension = null;
    107         if (!TextUtils.isEmpty(fileName)) {
    108             int lastDot = fileName.lastIndexOf('.');
    109             if ((lastDot > 0) && (lastDot < fileName.length() - 1)) {
    110                 extension = fileName.substring(lastDot + 1).toLowerCase();
    111             }
    112         }
    113         return extension;
    114     }
    115 
    116 
    117     /**
    118      * Returns the mime type of the attachment based on its name and
    119      * original mime type. This is an workaround for bugs where Gmail
    120      * server doesn't set content-type for certain types correctly.
    121      * 1) EML files -> "message/rfc822".
    122      * @param name name of the attachment.
    123      * @param mimeType original mime type of the attachment.
    124      * @return the inferred mime type of the attachment.
    125      */
    126     public static String inferMimeType(String name, String mimeType) {
    127         final String extension = getFilenameExtension(name);
    128         if (TextUtils.isEmpty(extension)) {
    129             // Attachment doesn't have extension, just return original mime
    130             // type.
    131             return mimeType;
    132         } else {
    133             final boolean isTextPlain = TEXT_PLAIN.equalsIgnoreCase(mimeType);
    134             final boolean isGenericType =
    135                     isTextPlain || GENERIC_MIMETYPE.equalsIgnoreCase(mimeType);
    136 
    137             String type = null;
    138             if (isGenericType || TextUtils.isEmpty(mimeType)) {
    139                 type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    140             }
    141             if (!TextUtils.isEmpty(type)) {
    142                 return type;
    143             } if (extension.equals("eml")) {
    144                 // Extension is ".eml", return mime type "message/rfc822"
    145                 return EML_ATTACHMENT_CONTENT_TYPE;
    146             } else {
    147                 // Extension is not ".eml", just return original mime type.
    148                 return !TextUtils.isEmpty(mimeType) ? mimeType : GENERIC_MIMETYPE;
    149             }
    150         }
    151     }
    152 
    153     /**
    154      * Checks the supplied mime type to determine if it is a valid eml file.
    155      * Valid mime types are "message/rfc822" and "application/eml".
    156      * @param mimeType the mime type to check
    157      * @return {@code true} if the mime type is one of the valid mime types.
    158      */
    159     public static boolean isEmlMimeType(String mimeType) {
    160         return EML_ATTACHMENT_CONTENT_TYPES.contains(mimeType);
    161     }
    162 }
    163