13a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank/*
23a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank * Copyright (C) 2011 The Android Open Source Project
33a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank *
43a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank * Licensed under the Apache License, Version 2.0 (the "License");
53a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank * you may not use this file except in compliance with the License.
63a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank * You may obtain a copy of the License at
73a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank *
83a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank *      http://www.apache.org/licenses/LICENSE-2.0
93a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank *
103a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank * Unless required by applicable law or agreed to in writing, software
113a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank * distributed under the License is distributed on an "AS IS" BASIS,
123a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank * See the License for the specific language governing permissions and
143a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank * limitations under the License.
153a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank */
163a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank
173a761c8038173efe42e67cd9d48a95b059427f0fMarc Blankpackage com.android.email;
183a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank
192193962ca2b3157e79f731736afa2a0c972e778aMarc Blankimport com.android.emailcommon.internet.MimeUtility;
20a7bc0319a75184ad706bb35c049af107ac3688e6Marc Blankimport com.android.emailcommon.provider.EmailContent.Attachment;
213dd85723a1af5537e23e4b05bdc361cce9cd42beTony Mantlerimport com.android.emailcommon.provider.EmailContent.AttachmentColumns;
228a574694606f0e5d781334d0d426fc379c51f3edMarc Blankimport com.android.emailcommon.utility.AttachmentUtilities;
2331d9acbf0623872f9d4a2b3210b5970854b654c7Marc Blankimport com.android.emailcommon.utility.Utility;
243a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank
253a761c8038173efe42e67cd9d48a95b059427f0fMarc Blankimport android.content.Context;
263a761c8038173efe42e67cd9d48a95b059427f0fMarc Blankimport android.content.Intent;
273a761c8038173efe42e67cd9d48a95b059427f0fMarc Blankimport android.content.pm.PackageManager;
283a761c8038173efe42e67cd9d48a95b059427f0fMarc Blankimport android.content.pm.ResolveInfo;
29475c20d3a883737853aa7301cc649736a36387c5Marc Blankimport android.database.Cursor;
303a761c8038173efe42e67cd9d48a95b059427f0fMarc Blankimport android.net.ConnectivityManager;
313a761c8038173efe42e67cd9d48a95b059427f0fMarc Blankimport android.net.Uri;
323a761c8038173efe42e67cd9d48a95b059427f0fMarc Blankimport android.provider.Settings;
333a761c8038173efe42e67cd9d48a95b059427f0fMarc Blankimport android.text.TextUtils;
343a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank
353a761c8038173efe42e67cd9d48a95b059427f0fMarc Blankimport java.util.List;
363a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank
373a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank/**
383a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank * Encapsulates commonly used attachment information related to suitability for viewing and saving,
39f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank * based on the attachment's filename and mimetype.
403a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank */
413a761c8038173efe42e67cd9d48a95b059427f0fMarc Blankpublic class AttachmentInfo {
42475c20d3a883737853aa7301cc649736a36387c5Marc Blank    // Projection which can be used with the constructor taking a Cursor argument
433dd85723a1af5537e23e4b05bdc361cce9cd42beTony Mantler    public static final String[] PROJECTION = {
443dd85723a1af5537e23e4b05bdc361cce9cd42beTony Mantler            AttachmentColumns._ID,
453dd85723a1af5537e23e4b05bdc361cce9cd42beTony Mantler            AttachmentColumns.SIZE,
463dd85723a1af5537e23e4b05bdc361cce9cd42beTony Mantler            AttachmentColumns.FILENAME,
473dd85723a1af5537e23e4b05bdc361cce9cd42beTony Mantler            AttachmentColumns.MIME_TYPE,
483dd85723a1af5537e23e4b05bdc361cce9cd42beTony Mantler            AttachmentColumns.ACCOUNT_KEY,
493dd85723a1af5537e23e4b05bdc361cce9cd42beTony Mantler            AttachmentColumns.FLAGS
503dd85723a1af5537e23e4b05bdc361cce9cd42beTony Mantler    };
51475c20d3a883737853aa7301cc649736a36387c5Marc Blank    // Offsets into PROJECTION
52475c20d3a883737853aa7301cc649736a36387c5Marc Blank    public static final int COLUMN_ID = 0;
53475c20d3a883737853aa7301cc649736a36387c5Marc Blank    public static final int COLUMN_SIZE = 1;
54475c20d3a883737853aa7301cc649736a36387c5Marc Blank    public static final int COLUMN_FILENAME = 2;
55475c20d3a883737853aa7301cc649736a36387c5Marc Blank    public static final int COLUMN_MIME_TYPE = 3;
56475c20d3a883737853aa7301cc649736a36387c5Marc Blank    public static final int COLUMN_ACCOUNT_KEY = 4;
579d9b481a85df540f8f338d28919802828a387efeMarc Blank    public static final int COLUMN_FLAGS = 5;
58475c20d3a883737853aa7301cc649736a36387c5Marc Blank
593f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy    /** Attachment not denied */
603f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy    public static final int ALLOW           = 0x00;
613f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy    /** Attachment suspected of being malware */
623f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy    public static final int DENY_MALWARE    = 0x01;
633f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy    /** Attachment too large; must download over wi-fi */
643f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy    public static final int DENY_WIFIONLY   = 0x02;
653f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy    /** No receiving intent to handle attachment type */
663f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy    public static final int DENY_NOINTENT   = 0x04;
673f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy    /** Side load of applications is disabled */
683f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy    public static final int DENY_NOSIDELOAD = 0x08;
693f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy    // TODO Remove DENY_APKINSTALL when we can install directly from the Email activity
703f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy    /** Unable to install any APK */
713f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy    public static final int DENY_APKINSTALL = 0x10;
729d9b481a85df540f8f338d28919802828a387efeMarc Blank    /** Security policy prohibits install */
739d9b481a85df540f8f338d28919802828a387efeMarc Blank    public static final int DENY_POLICY = 0x20;
743f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy
75475c20d3a883737853aa7301cc649736a36387c5Marc Blank    public final long mId;
76475c20d3a883737853aa7301cc649736a36387c5Marc Blank    public final long mSize;
773a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank    public final String mName;
783a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank    public final String mContentType;
79475c20d3a883737853aa7301cc649736a36387c5Marc Blank    public final long mAccountKey;
809d9b481a85df540f8f338d28919802828a387efeMarc Blank    public final int mFlags;
813f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy
823f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy    /** Whether or not this attachment can be viewed */
833a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank    public final boolean mAllowView;
843f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy    /** Whether or not this attachment can be saved */
853a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank    public final boolean mAllowSave;
863f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy    /** Whether or not this attachment can be installed [only true for APKs] */
873f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy    public final boolean mAllowInstall;
883f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy    /** Reason(s) why this attachment is denied from being viewed */
893f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy    public final int mDenyFlags;
903a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank
913a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank    public AttachmentInfo(Context context, Attachment attachment) {
92475c20d3a883737853aa7301cc649736a36387c5Marc Blank        this(context, attachment.mId, attachment.mSize, attachment.mFileName, attachment.mMimeType,
939d9b481a85df540f8f338d28919802828a387efeMarc Blank                attachment.mAccountKey, attachment.mFlags);
94475c20d3a883737853aa7301cc649736a36387c5Marc Blank    }
95475c20d3a883737853aa7301cc649736a36387c5Marc Blank
96475c20d3a883737853aa7301cc649736a36387c5Marc Blank    public AttachmentInfo(Context context, Cursor c) {
97475c20d3a883737853aa7301cc649736a36387c5Marc Blank        this(context, c.getLong(COLUMN_ID), c.getLong(COLUMN_SIZE), c.getString(COLUMN_FILENAME),
989d9b481a85df540f8f338d28919802828a387efeMarc Blank                c.getString(COLUMN_MIME_TYPE), c.getLong(COLUMN_ACCOUNT_KEY),
999d9b481a85df540f8f338d28919802828a387efeMarc Blank                c.getInt(COLUMN_FLAGS));
100475c20d3a883737853aa7301cc649736a36387c5Marc Blank    }
101475c20d3a883737853aa7301cc649736a36387c5Marc Blank
1023f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy    public AttachmentInfo(Context context, AttachmentInfo info) {
1039d9b481a85df540f8f338d28919802828a387efeMarc Blank        this(context, info.mId, info.mSize, info.mName, info.mContentType, info.mAccountKey,
1049d9b481a85df540f8f338d28919802828a387efeMarc Blank                info.mFlags);
1053f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy    }
1063f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy
107475c20d3a883737853aa7301cc649736a36387c5Marc Blank    public AttachmentInfo(Context context, long id, long size, String fileName, String mimeType,
1089d9b481a85df540f8f338d28919802828a387efeMarc Blank            long accountKey, int flags) {
109475c20d3a883737853aa7301cc649736a36387c5Marc Blank        mSize = size;
1108a574694606f0e5d781334d0d426fc379c51f3edMarc Blank        mContentType = AttachmentUtilities.inferMimeType(fileName, mimeType);
111475c20d3a883737853aa7301cc649736a36387c5Marc Blank        mName = fileName;
112475c20d3a883737853aa7301cc649736a36387c5Marc Blank        mId = id;
113475c20d3a883737853aa7301cc649736a36387c5Marc Blank        mAccountKey = accountKey;
1149d9b481a85df540f8f338d28919802828a387efeMarc Blank        mFlags = flags;
1153a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank        boolean canView = true;
1163a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank        boolean canSave = true;
1173f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy        boolean canInstall = false;
1183f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy        int denyFlags = ALLOW;
1193f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy
1203f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy        // Don't enable the "save" button if we've got no place to save the file
1213f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy        if (!Utility.isExternalStorageMounted()) {
1223f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy            canSave = false;
1233f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy        }
1243a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank
1253a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank        // Check for acceptable / unacceptable attachments by MIME-type
12631d9acbf0623872f9d4a2b3210b5970854b654c7Marc Blank        if ((!MimeUtility.mimeTypeMatches(mContentType,
12731d9acbf0623872f9d4a2b3210b5970854b654c7Marc Blank                AttachmentUtilities.ACCEPTABLE_ATTACHMENT_VIEW_TYPES)) ||
12831d9acbf0623872f9d4a2b3210b5970854b654c7Marc Blank            (MimeUtility.mimeTypeMatches(mContentType,
12931d9acbf0623872f9d4a2b3210b5970854b654c7Marc Blank                    AttachmentUtilities.UNACCEPTABLE_ATTACHMENT_VIEW_TYPES))) {
1303a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank            canView = false;
1313a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank        }
1323a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank
1333a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank        // Check for unacceptable attachments by filename extension
1348a574694606f0e5d781334d0d426fc379c51f3edMarc Blank        String extension = AttachmentUtilities.getFilenameExtension(mName);
1353a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank        if (!TextUtils.isEmpty(extension) &&
13631d9acbf0623872f9d4a2b3210b5970854b654c7Marc Blank                Utility.arrayContains(AttachmentUtilities.UNACCEPTABLE_ATTACHMENT_EXTENSIONS,
13731d9acbf0623872f9d4a2b3210b5970854b654c7Marc Blank                        extension)) {
1383a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank            canView = false;
1393a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank            canSave = false;
1403f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy            denyFlags |= DENY_MALWARE;
1413a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank        }
1423a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank
1439d9b481a85df540f8f338d28919802828a387efeMarc Blank        // Check for policy restrictions on download
1449d9b481a85df540f8f338d28919802828a387efeMarc Blank        if ((flags & Attachment.FLAG_POLICY_DISALLOWS_DOWNLOAD) != 0) {
1459d9b481a85df540f8f338d28919802828a387efeMarc Blank            canView = false;
1469d9b481a85df540f8f338d28919802828a387efeMarc Blank            canSave = false;
1479d9b481a85df540f8f338d28919802828a387efeMarc Blank            denyFlags |= DENY_POLICY;
1489d9b481a85df540f8f338d28919802828a387efeMarc Blank        }
1499d9b481a85df540f8f338d28919802828a387efeMarc Blank
1503a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank        // Check for installable attachments by filename extension
1518a574694606f0e5d781334d0d426fc379c51f3edMarc Blank        extension = AttachmentUtilities.getFilenameExtension(mName);
1523a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank        if (!TextUtils.isEmpty(extension) &&
15331d9acbf0623872f9d4a2b3210b5970854b654c7Marc Blank                Utility.arrayContains(AttachmentUtilities.INSTALLABLE_ATTACHMENT_EXTENSIONS,
15431d9acbf0623872f9d4a2b3210b5970854b654c7Marc Blank                        extension)) {
1553f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy            boolean sideloadEnabled;
1563a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank            sideloadEnabled = Settings.Secure.getInt(context.getContentResolver(),
1573f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy                    Settings.Secure.INSTALL_NON_MARKET_APPS, 0 /* sideload disabled */) == 1;
1583f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy            canSave &= sideloadEnabled;
159e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo            canView = canSave;
160e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo            canInstall = canSave;
1613f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy            if (!sideloadEnabled) {
1623f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy                denyFlags |= DENY_NOSIDELOAD;
1633f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy            }
1643a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank        }
1653a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank
1663a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank        // Check for file size exceeded
1673a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank        // The size limit is overridden when on a wifi connection - any size is OK
16831d9acbf0623872f9d4a2b3210b5970854b654c7Marc Blank        if (mSize > AttachmentUtilities.MAX_ATTACHMENT_DOWNLOAD_SIZE) {
169973702b30e8c2fb2f622f4ef37b42b3bdbd3ef17Marc Blank            int networkType = EmailConnectivityManager.getActiveNetworkType(context);
170973702b30e8c2fb2f622f4ef37b42b3bdbd3ef17Marc Blank            if (networkType != ConnectivityManager.TYPE_WIFI) {
1713a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank                canView = false;
1723a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank                canSave = false;
1733f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy                denyFlags |= DENY_WIFIONLY;
1743a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank            }
1753a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank        }
1763a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank
1771264651c69da9a6389de40c49f61e28873b4d07dMarc Blank        // Check to see if any activities can view this attachment; if none, we can't view it
1783a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank        Intent intent = getAttachmentIntent(context, 0);
1793a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank        PackageManager pm = context.getPackageManager();
1801264651c69da9a6389de40c49f61e28873b4d07dMarc Blank        List<ResolveInfo> activityList = pm.queryIntentActivities(intent, 0 /*no account*/);
1813a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank        if (activityList.isEmpty()) {
1823a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank            canView = false;
1833a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank            canSave = false;
1843f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy            denyFlags |= DENY_NOINTENT;
1853a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank        }
1863a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank
1873a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank        mAllowView = canView;
1883a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank        mAllowSave = canSave;
1893f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy        mAllowInstall = canInstall;
1903f60e9312b1bf1324dd0e343b07cb69c9f8d31fdTodd Kennedy        mDenyFlags = denyFlags;
1913a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank    }
1923a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank
1933a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank    /**
1943a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank     * Returns an <code>Intent</code> to load the given attachment.
1951264651c69da9a6389de40c49f61e28873b4d07dMarc Blank     * @param context the caller's context
1961264651c69da9a6389de40c49f61e28873b4d07dMarc Blank     * @param accountId the account associated with the attachment (or 0 if we don't need to
197e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo     *     resolve from attachmentUri to contentUri)
198e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo     * @return an Intent suitable for viewing the attachment
1993a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank     */
2003a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank    public Intent getAttachmentIntent(Context context, long accountId) {
201e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo        Uri contentUri = getUriForIntent(context, accountId);
202e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo        Intent intent = new Intent(Intent.ACTION_VIEW);
203e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo        intent.setDataAndType(contentUri, mContentType);
204e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
205e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo                | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
206e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo        return intent;
207e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo    }
208e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo
209e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo    protected Uri getUriForIntent(Context context, long accountId) {
2108a574694606f0e5d781334d0d426fc379c51f3edMarc Blank        Uri contentUri = AttachmentUtilities.getAttachmentUri(accountId, mId);
2111264651c69da9a6389de40c49f61e28873b4d07dMarc Blank        if (accountId > 0) {
2128a574694606f0e5d781334d0d426fc379c51f3edMarc Blank            contentUri = AttachmentUtilities.resolveAttachmentIdToContentUri(
2131264651c69da9a6389de40c49f61e28873b4d07dMarc Blank                    context.getContentResolver(), contentUri);
2141264651c69da9a6389de40c49f61e28873b4d07dMarc Blank        }
215e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo
216e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo        return contentUri;
2173a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank    }
2183a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank
2193a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank    /**
2203a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank     * An attachment is eligible for download if it can either be viewed or saved (or both)
2213a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank     * @return whether the attachment is eligible for download
2223a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank     */
223475c20d3a883737853aa7301cc649736a36387c5Marc Blank    public boolean isEligibleForDownload() {
2243a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank        return mAllowView || mAllowSave;
2253a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank    }
226475c20d3a883737853aa7301cc649736a36387c5Marc Blank
2278a574694606f0e5d781334d0d426fc379c51f3edMarc Blank    @Override
228e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo    public int hashCode() {
229e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo        return (int) (mId ^ (mId >>> 32));
230e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo    }
231e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo
232e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo    @Override
233e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo    public boolean equals(Object o) {
234e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo        if (o == this) {
235e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo            return true;
236e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo        }
237e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo
238e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo        if ((o == null) || (o.getClass() != getClass())) {
239e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo            return false;
240e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo        }
241e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo
242e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo        return ((AttachmentInfo) o).mId == mId;
243e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo    }
244e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo
245e36648697f242991a8df0fdc6b23330f7be5e331Ben Komalo    @Override
246475c20d3a883737853aa7301cc649736a36387c5Marc Blank    public String toString() {
247475c20d3a883737853aa7301cc649736a36387c5Marc Blank        return "{Attachment " + mId + ":" + mName + "," + mContentType + "," + mSize + "}";
248475c20d3a883737853aa7301cc649736a36387c5Marc Blank    }
2493a761c8038173efe42e67cd9d48a95b059427f0fMarc Blank}
250