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;
|
2014-04-11 21:42:28 +00:00
|
|
|
import com.android.emailcommon.provider.EmailContent.AttachmentColumns;
|
2011-02-09 01:50:30 +00:00
|
|
|
import com.android.emailcommon.utility.AttachmentUtilities;
|
2011-02-11 23:05:17 +00:00
|
|
|
import com.android.emailcommon.utility.Utility;
|
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.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,
|
2012-06-28 17:40:46 +00:00
|
|
|
* based on the attachment's filename and mimetype.
|
2011-01-18 01:26:31 +00:00
|
|
|
*/
|
|
|
|
public class AttachmentInfo {
|
2011-01-20 01:44:55 +00:00
|
|
|
// Projection which can be used with the constructor taking a Cursor argument
|
2014-04-11 21:42:28 +00:00
|
|
|
public static final String[] PROJECTION = {
|
|
|
|
AttachmentColumns._ID,
|
|
|
|
AttachmentColumns.SIZE,
|
|
|
|
AttachmentColumns.FILENAME,
|
|
|
|
AttachmentColumns.MIME_TYPE,
|
|
|
|
AttachmentColumns.ACCOUNT_KEY,
|
|
|
|
AttachmentColumns.FLAGS
|
|
|
|
};
|
2011-01-20 01:44:55 +00:00
|
|
|
// 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;
|
2011-05-11 01:10:02 +00:00
|
|
|
public static final int COLUMN_FLAGS = 5;
|
2011-01-20 01:44:55 +00:00
|
|
|
|
2011-02-19 00:41:41 +00:00
|
|
|
/** Attachment not denied */
|
|
|
|
public static final int ALLOW = 0x00;
|
|
|
|
/** Attachment suspected of being malware */
|
|
|
|
public static final int DENY_MALWARE = 0x01;
|
|
|
|
/** Attachment too large; must download over wi-fi */
|
|
|
|
public static final int DENY_WIFIONLY = 0x02;
|
|
|
|
/** No receiving intent to handle attachment type */
|
|
|
|
public static final int DENY_NOINTENT = 0x04;
|
|
|
|
/** Side load of applications is disabled */
|
|
|
|
public static final int DENY_NOSIDELOAD = 0x08;
|
|
|
|
// TODO Remove DENY_APKINSTALL when we can install directly from the Email activity
|
|
|
|
/** Unable to install any APK */
|
|
|
|
public static final int DENY_APKINSTALL = 0x10;
|
2011-05-11 01:10:02 +00:00
|
|
|
/** Security policy prohibits install */
|
|
|
|
public static final int DENY_POLICY = 0x20;
|
2011-02-19 00:41:41 +00:00
|
|
|
|
2011-01-20 01:44:55 +00:00
|
|
|
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-05-11 01:10:02 +00:00
|
|
|
public final int mFlags;
|
2011-02-19 00:41:41 +00:00
|
|
|
|
|
|
|
/** Whether or not this attachment can be viewed */
|
2011-01-18 01:26:31 +00:00
|
|
|
public final boolean mAllowView;
|
2011-02-19 00:41:41 +00:00
|
|
|
/** Whether or not this attachment can be saved */
|
2011-01-18 01:26:31 +00:00
|
|
|
public final boolean mAllowSave;
|
2011-02-19 00:41:41 +00:00
|
|
|
/** Whether or not this attachment can be installed [only true for APKs] */
|
|
|
|
public final boolean mAllowInstall;
|
|
|
|
/** Reason(s) why this attachment is denied from being viewed */
|
|
|
|
public final int mDenyFlags;
|
2011-01-18 01:26:31 +00:00
|
|
|
|
|
|
|
public AttachmentInfo(Context context, Attachment attachment) {
|
2011-01-20 01:44:55 +00:00
|
|
|
this(context, attachment.mId, attachment.mSize, attachment.mFileName, attachment.mMimeType,
|
2011-05-11 01:10:02 +00:00
|
|
|
attachment.mAccountKey, attachment.mFlags);
|
2011-01-20 01:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public AttachmentInfo(Context context, Cursor c) {
|
|
|
|
this(context, c.getLong(COLUMN_ID), c.getLong(COLUMN_SIZE), c.getString(COLUMN_FILENAME),
|
2011-05-11 01:10:02 +00:00
|
|
|
c.getString(COLUMN_MIME_TYPE), c.getLong(COLUMN_ACCOUNT_KEY),
|
|
|
|
c.getInt(COLUMN_FLAGS));
|
2011-01-20 01:44:55 +00:00
|
|
|
}
|
|
|
|
|
2011-02-19 00:41:41 +00:00
|
|
|
public AttachmentInfo(Context context, AttachmentInfo info) {
|
2011-05-11 01:10:02 +00:00
|
|
|
this(context, info.mId, info.mSize, info.mName, info.mContentType, info.mAccountKey,
|
|
|
|
info.mFlags);
|
2011-02-19 00:41:41 +00:00
|
|
|
}
|
|
|
|
|
2011-01-20 01:44:55 +00:00
|
|
|
public AttachmentInfo(Context context, long id, long size, String fileName, String mimeType,
|
2011-05-11 01:10:02 +00:00
|
|
|
long accountKey, int flags) {
|
2011-01-20 01:44:55 +00:00
|
|
|
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-05-11 01:10:02 +00:00
|
|
|
mFlags = flags;
|
2011-01-18 01:26:31 +00:00
|
|
|
boolean canView = true;
|
|
|
|
boolean canSave = true;
|
2011-02-19 00:41:41 +00:00
|
|
|
boolean canInstall = false;
|
|
|
|
int denyFlags = ALLOW;
|
|
|
|
|
|
|
|
// Don't enable the "save" button if we've got no place to save the file
|
|
|
|
if (!Utility.isExternalStorageMounted()) {
|
|
|
|
canSave = false;
|
|
|
|
}
|
2011-01-18 01:26:31 +00:00
|
|
|
|
|
|
|
// Check for acceptable / unacceptable attachments by MIME-type
|
2011-02-11 23:05:17 +00:00
|
|
|
if ((!MimeUtility.mimeTypeMatches(mContentType,
|
|
|
|
AttachmentUtilities.ACCEPTABLE_ATTACHMENT_VIEW_TYPES)) ||
|
|
|
|
(MimeUtility.mimeTypeMatches(mContentType,
|
|
|
|
AttachmentUtilities.UNACCEPTABLE_ATTACHMENT_VIEW_TYPES))) {
|
2011-01-18 01:26:31 +00:00
|
|
|
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) &&
|
2011-02-11 23:05:17 +00:00
|
|
|
Utility.arrayContains(AttachmentUtilities.UNACCEPTABLE_ATTACHMENT_EXTENSIONS,
|
|
|
|
extension)) {
|
2011-01-18 01:26:31 +00:00
|
|
|
canView = false;
|
|
|
|
canSave = false;
|
2011-02-19 00:41:41 +00:00
|
|
|
denyFlags |= DENY_MALWARE;
|
2011-01-18 01:26:31 +00:00
|
|
|
}
|
|
|
|
|
2011-05-11 01:10:02 +00:00
|
|
|
// Check for policy restrictions on download
|
|
|
|
if ((flags & Attachment.FLAG_POLICY_DISALLOWS_DOWNLOAD) != 0) {
|
|
|
|
canView = false;
|
|
|
|
canSave = false;
|
|
|
|
denyFlags |= DENY_POLICY;
|
|
|
|
}
|
|
|
|
|
2011-01-18 01:26:31 +00:00
|
|
|
// 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) &&
|
2011-02-11 23:05:17 +00:00
|
|
|
Utility.arrayContains(AttachmentUtilities.INSTALLABLE_ATTACHMENT_EXTENSIONS,
|
|
|
|
extension)) {
|
2011-02-19 00:41:41 +00:00
|
|
|
boolean sideloadEnabled;
|
2011-01-18 01:26:31 +00:00
|
|
|
sideloadEnabled = Settings.Secure.getInt(context.getContentResolver(),
|
2011-02-19 00:41:41 +00:00
|
|
|
Settings.Secure.INSTALL_NON_MARKET_APPS, 0 /* sideload disabled */) == 1;
|
|
|
|
canSave &= sideloadEnabled;
|
2011-04-20 17:47:04 +00:00
|
|
|
canView = canSave;
|
|
|
|
canInstall = canSave;
|
2011-02-19 00:41:41 +00:00
|
|
|
if (!sideloadEnabled) {
|
|
|
|
denyFlags |= DENY_NOSIDELOAD;
|
|
|
|
}
|
2011-01-18 01:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for file size exceeded
|
|
|
|
// The size limit is overridden when on a wifi connection - any size is OK
|
2011-02-11 23:05:17 +00:00
|
|
|
if (mSize > AttachmentUtilities.MAX_ATTACHMENT_DOWNLOAD_SIZE) {
|
2011-03-08 02:01:58 +00:00
|
|
|
int networkType = EmailConnectivityManager.getActiveNetworkType(context);
|
|
|
|
if (networkType != ConnectivityManager.TYPE_WIFI) {
|
2011-01-18 01:26:31 +00:00
|
|
|
canView = false;
|
|
|
|
canSave = false;
|
2011-02-19 00:41:41 +00:00
|
|
|
denyFlags |= DENY_WIFIONLY;
|
2011-01-18 01:26:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2011-02-19 00:41:41 +00:00
|
|
|
denyFlags |= DENY_NOINTENT;
|
2011-01-18 01:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mAllowView = canView;
|
|
|
|
mAllowSave = canSave;
|
2011-02-19 00:41:41 +00:00
|
|
|
mAllowInstall = canInstall;
|
|
|
|
mDenyFlags = denyFlags;
|
2011-01-18 01:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2011-04-20 17:47:04 +00:00
|
|
|
* resolve from attachmentUri to contentUri)
|
|
|
|
* @return an Intent suitable for viewing the attachment
|
2011-01-18 01:26:31 +00:00
|
|
|
*/
|
|
|
|
public Intent getAttachmentIntent(Context context, long accountId) {
|
2011-04-20 17:47:04 +00:00
|
|
|
Uri contentUri = getUriForIntent(context, accountId);
|
|
|
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
|
|
intent.setDataAndType(contentUri, mContentType);
|
|
|
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
|
|
|
|
| Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
|
|
|
|
return intent;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Uri getUriForIntent(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-04-20 17:47:04 +00:00
|
|
|
|
|
|
|
return contentUri;
|
2011-01-18 01:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-04-20 17:47:04 +00:00
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return (int) (mId ^ (mId >>> 32));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (o == this) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((o == null) || (o.getClass() != getClass())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ((AttachmentInfo) o).mId == mId;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|