2011-01-18 01:26:31 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.email;
|
|
|
|
|
2011-02-10 02:47:43 +00:00
|
|
|
import com.android.emailcommon.internet.MimeUtility;
|
2011-02-10 18:26:56 +00:00
|
|
|
import com.android.emailcommon.provider.EmailContent.Attachment;
|
2011-02-09 01:50:30 +00:00
|
|
|
import com.android.emailcommon.utility.AttachmentUtilities;
|
2011-01-18 01:26:31 +00:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.content.pm.ResolveInfo;
|
2011-01-20 01:44:55 +00:00
|
|
|
import android.database.Cursor;
|
2011-01-18 01:26:31 +00:00
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
import android.net.NetworkInfo;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.provider.Settings;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encapsulates commonly used attachment information related to suitability for viewing and saving,
|
|
|
|
* based on the attachment's filename and mime type.
|
|
|
|
*/
|
|
|
|
public class AttachmentInfo {
|
2011-01-20 01:44:55 +00:00
|
|
|
// Projection which can be used with the constructor taking a Cursor argument
|
|
|
|
public static final String[] PROJECTION = new String[] {Attachment.RECORD_ID, Attachment.SIZE,
|
|
|
|
Attachment.FILENAME, Attachment.MIME_TYPE, Attachment.ACCOUNT_KEY};
|
|
|
|
// Offsets into PROJECTION
|
|
|
|
public static final int COLUMN_ID = 0;
|
|
|
|
public static final int COLUMN_SIZE = 1;
|
|
|
|
public static final int COLUMN_FILENAME = 2;
|
|
|
|
public static final int COLUMN_MIME_TYPE = 3;
|
|
|
|
public static final int COLUMN_ACCOUNT_KEY = 4;
|
|
|
|
|
|
|
|
public final long mId;
|
|
|
|
public final long mSize;
|
2011-01-18 01:26:31 +00:00
|
|
|
public final String mName;
|
|
|
|
public final String mContentType;
|
2011-01-20 01:44:55 +00:00
|
|
|
public final long mAccountKey;
|
2011-01-18 01:26:31 +00:00
|
|
|
public final boolean mAllowView;
|
|
|
|
public final boolean mAllowSave;
|
|
|
|
|
|
|
|
public AttachmentInfo(Context context, Attachment attachment) {
|
2011-01-20 01:44:55 +00:00
|
|
|
this(context, attachment.mId, attachment.mSize, attachment.mFileName, attachment.mMimeType,
|
|
|
|
attachment.mAccountKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
public AttachmentInfo(Context context, Cursor c) {
|
|
|
|
this(context, c.getLong(COLUMN_ID), c.getLong(COLUMN_SIZE), c.getString(COLUMN_FILENAME),
|
|
|
|
c.getString(COLUMN_MIME_TYPE), c.getLong(COLUMN_ACCOUNT_KEY));
|
|
|
|
}
|
|
|
|
|
|
|
|
public AttachmentInfo(Context context, long id, long size, String fileName, String mimeType,
|
|
|
|
long accountKey) {
|
|
|
|
mSize = size;
|
2011-02-09 01:50:30 +00:00
|
|
|
mContentType = AttachmentUtilities.inferMimeType(fileName, mimeType);
|
2011-01-20 01:44:55 +00:00
|
|
|
mName = fileName;
|
|
|
|
mId = id;
|
|
|
|
mAccountKey = accountKey;
|
2011-01-18 01:26:31 +00:00
|
|
|
boolean canView = true;
|
|
|
|
boolean canSave = true;
|
|
|
|
|
|
|
|
// Check for acceptable / unacceptable attachments by MIME-type
|
|
|
|
if ((!MimeUtility.mimeTypeMatches(mContentType, Email.ACCEPTABLE_ATTACHMENT_VIEW_TYPES)) ||
|
|
|
|
(MimeUtility.mimeTypeMatches(mContentType, Email.UNACCEPTABLE_ATTACHMENT_VIEW_TYPES))) {
|
|
|
|
canView = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for unacceptable attachments by filename extension
|
2011-02-09 01:50:30 +00:00
|
|
|
String extension = AttachmentUtilities.getFilenameExtension(mName);
|
2011-01-18 01:26:31 +00:00
|
|
|
if (!TextUtils.isEmpty(extension) &&
|
|
|
|
Utility.arrayContains(Email.UNACCEPTABLE_ATTACHMENT_EXTENSIONS, extension)) {
|
|
|
|
canView = false;
|
|
|
|
canSave = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for installable attachments by filename extension
|
2011-02-09 01:50:30 +00:00
|
|
|
extension = AttachmentUtilities.getFilenameExtension(mName);
|
2011-01-18 01:26:31 +00:00
|
|
|
if (!TextUtils.isEmpty(extension) &&
|
|
|
|
Utility.arrayContains(Email.INSTALLABLE_ATTACHMENT_EXTENSIONS, extension)) {
|
|
|
|
int sideloadEnabled;
|
|
|
|
sideloadEnabled = Settings.Secure.getInt(context.getContentResolver(),
|
|
|
|
Settings.Secure.INSTALL_NON_MARKET_APPS, 0 /* sideload disabled */);
|
|
|
|
canView = false;
|
|
|
|
canSave &= (sideloadEnabled == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for file size exceeded
|
|
|
|
// The size limit is overridden when on a wifi connection - any size is OK
|
|
|
|
if (mSize > Email.MAX_ATTACHMENT_DOWNLOAD_SIZE) {
|
|
|
|
ConnectivityManager cm = (ConnectivityManager)
|
|
|
|
context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
NetworkInfo network = cm.getActiveNetworkInfo();
|
|
|
|
if (network == null || network.getType() != ConnectivityManager.TYPE_WIFI) {
|
|
|
|
canView = false;
|
|
|
|
canSave = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-22 21:55:14 +00:00
|
|
|
// Check to see if any activities can view this attachment; if none, we can't view it
|
2011-01-18 01:26:31 +00:00
|
|
|
Intent intent = getAttachmentIntent(context, 0);
|
|
|
|
PackageManager pm = context.getPackageManager();
|
2011-01-22 21:55:14 +00:00
|
|
|
List<ResolveInfo> activityList = pm.queryIntentActivities(intent, 0 /*no account*/);
|
2011-01-18 01:26:31 +00:00
|
|
|
if (activityList.isEmpty()) {
|
|
|
|
canView = false;
|
|
|
|
canSave = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mAllowView = canView;
|
|
|
|
mAllowSave = canSave;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an <code>Intent</code> to load the given attachment.
|
2011-01-22 21:55:14 +00:00
|
|
|
* @param context the caller's context
|
|
|
|
* @param accountId the account associated with the attachment (or 0 if we don't need to
|
|
|
|
* resolve from attachmentUri to contentUri)
|
|
|
|
* @return an Intent suitable for loading the attachment
|
2011-01-18 01:26:31 +00:00
|
|
|
*/
|
|
|
|
public Intent getAttachmentIntent(Context context, long accountId) {
|
2011-02-09 01:50:30 +00:00
|
|
|
Uri contentUri = AttachmentUtilities.getAttachmentUri(accountId, mId);
|
2011-01-22 21:55:14 +00:00
|
|
|
if (accountId > 0) {
|
2011-02-09 01:50:30 +00:00
|
|
|
contentUri = AttachmentUtilities.resolveAttachmentIdToContentUri(
|
2011-01-22 21:55:14 +00:00
|
|
|
context.getContentResolver(), contentUri);
|
|
|
|
}
|
2011-01-18 01:26:31 +00:00
|
|
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
|
|
intent.setData(contentUri);
|
|
|
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
|
2011-01-22 21:55:14 +00:00
|
|
|
| Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
|
2011-01-18 01:26:31 +00:00
|
|
|
return intent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An attachment is eligible for download if it can either be viewed or saved (or both)
|
|
|
|
* @return whether the attachment is eligible for download
|
|
|
|
*/
|
2011-01-20 01:44:55 +00:00
|
|
|
public boolean isEligibleForDownload() {
|
2011-01-18 01:26:31 +00:00
|
|
|
return mAllowView || mAllowSave;
|
|
|
|
}
|
2011-01-20 01:44:55 +00:00
|
|
|
|
2011-02-09 01:50:30 +00:00
|
|
|
@Override
|
2011-01-20 01:44:55 +00:00
|
|
|
public String toString() {
|
|
|
|
return "{Attachment " + mId + ":" + mName + "," + mContentType + "," + mSize + "}";
|
|
|
|
}
|
2011-01-18 01:26:31 +00:00
|
|
|
}
|