AttachmentInfo.java revision 1264651c69da9a6389de40c49f61e28873b4d07d
1/*
2 * Copyright (C) 2011 The Android Open Source 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
17package com.android.email;
18
19import com.android.email.mail.internet.MimeUtility;
20import com.android.email.provider.AttachmentProvider;
21import com.android.email.provider.EmailContent.Attachment;
22
23import android.content.Context;
24import android.content.Intent;
25import android.content.pm.PackageManager;
26import android.content.pm.ResolveInfo;
27import android.database.Cursor;
28import android.net.ConnectivityManager;
29import android.net.NetworkInfo;
30import android.net.Uri;
31import android.provider.Settings;
32import android.text.TextUtils;
33
34import java.util.List;
35
36/**
37 * Encapsulates commonly used attachment information related to suitability for viewing and saving,
38 * based on the attachment's filename and mime type.
39 */
40public class AttachmentInfo {
41    // Projection which can be used with the constructor taking a Cursor argument
42    public static final String[] PROJECTION = new String[] {Attachment.RECORD_ID, Attachment.SIZE,
43        Attachment.FILENAME, Attachment.MIME_TYPE, Attachment.ACCOUNT_KEY};
44    // Offsets into PROJECTION
45    public static final int COLUMN_ID = 0;
46    public static final int COLUMN_SIZE = 1;
47    public static final int COLUMN_FILENAME = 2;
48    public static final int COLUMN_MIME_TYPE = 3;
49    public static final int COLUMN_ACCOUNT_KEY = 4;
50
51    public final long mId;
52    public final long mSize;
53    public final String mName;
54    public final String mContentType;
55    public final long mAccountKey;
56    public final boolean mAllowView;
57    public final boolean mAllowSave;
58
59    public AttachmentInfo(Context context, Attachment attachment) {
60        this(context, attachment.mId, attachment.mSize, attachment.mFileName, attachment.mMimeType,
61                attachment.mAccountKey);
62    }
63
64    public AttachmentInfo(Context context, Cursor c) {
65        this(context, c.getLong(COLUMN_ID), c.getLong(COLUMN_SIZE), c.getString(COLUMN_FILENAME),
66                c.getString(COLUMN_MIME_TYPE), c.getLong(COLUMN_ACCOUNT_KEY));
67    }
68
69    public AttachmentInfo(Context context, long id, long size, String fileName, String mimeType,
70            long accountKey) {
71        mSize = size;
72        mContentType = AttachmentProvider.inferMimeType(fileName, mimeType);
73        mName = fileName;
74        mId = id;
75        mAccountKey = accountKey;
76        boolean canView = true;
77        boolean canSave = true;
78
79        // Check for acceptable / unacceptable attachments by MIME-type
80        if ((!MimeUtility.mimeTypeMatches(mContentType, Email.ACCEPTABLE_ATTACHMENT_VIEW_TYPES)) ||
81            (MimeUtility.mimeTypeMatches(mContentType, Email.UNACCEPTABLE_ATTACHMENT_VIEW_TYPES))) {
82            canView = false;
83        }
84
85        // Check for unacceptable attachments by filename extension
86        String extension = AttachmentProvider.getFilenameExtension(mName);
87        if (!TextUtils.isEmpty(extension) &&
88                Utility.arrayContains(Email.UNACCEPTABLE_ATTACHMENT_EXTENSIONS, extension)) {
89            canView = false;
90            canSave = false;
91        }
92
93        // Check for installable attachments by filename extension
94        extension = AttachmentProvider.getFilenameExtension(mName);
95        if (!TextUtils.isEmpty(extension) &&
96                Utility.arrayContains(Email.INSTALLABLE_ATTACHMENT_EXTENSIONS, extension)) {
97            int sideloadEnabled;
98            sideloadEnabled = Settings.Secure.getInt(context.getContentResolver(),
99                    Settings.Secure.INSTALL_NON_MARKET_APPS, 0 /* sideload disabled */);
100            canView = false;
101            canSave &= (sideloadEnabled == 1);
102        }
103
104        // Check for file size exceeded
105        // The size limit is overridden when on a wifi connection - any size is OK
106        if (mSize > Email.MAX_ATTACHMENT_DOWNLOAD_SIZE) {
107            ConnectivityManager cm = (ConnectivityManager)
108                    context.getSystemService(Context.CONNECTIVITY_SERVICE);
109            NetworkInfo network = cm.getActiveNetworkInfo();
110            if (network == null || network.getType() != ConnectivityManager.TYPE_WIFI) {
111                canView = false;
112                canSave = false;
113            }
114        }
115
116        // Check to see if any activities can view this attachment; if none, we can't view it
117        Intent intent = getAttachmentIntent(context, 0);
118        PackageManager pm = context.getPackageManager();
119        List<ResolveInfo> activityList = pm.queryIntentActivities(intent, 0 /*no account*/);
120        if (activityList.isEmpty()) {
121            canView = false;
122            canSave = false;
123        }
124
125        mAllowView = canView;
126        mAllowSave = canSave;
127    }
128
129    /**
130     * Returns an <code>Intent</code> to load the given attachment.
131     * @param context the caller's context
132     * @param accountId the account associated with the attachment (or 0 if we don't need to
133     * resolve from attachmentUri to contentUri)
134     * @return an Intent suitable for loading the attachment
135     */
136    public Intent getAttachmentIntent(Context context, long accountId) {
137        Uri contentUri = AttachmentProvider.getAttachmentUri(accountId, mId);
138        if (accountId > 0) {
139            contentUri = AttachmentProvider.resolveAttachmentIdToContentUri(
140                    context.getContentResolver(), contentUri);
141        }
142        Intent intent = new Intent(Intent.ACTION_VIEW);
143        intent.setData(contentUri);
144        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
145                | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
146        return intent;
147    }
148
149    /**
150     * An attachment is eligible for download if it can either be viewed or saved (or both)
151     * @return whether the attachment is eligible for download
152     */
153    public boolean isEligibleForDownload() {
154        return mAllowView || mAllowSave;
155    }
156
157    public String toString() {
158        return "{Attachment " + mId + ":" + mName + "," + mContentType + "," + mSize + "}";
159    }
160}
161