diff --git a/src/com/android/email/Controller.java b/src/com/android/email/Controller.java index 88ecc713d..8987acf40 100644 --- a/src/com/android/email/Controller.java +++ b/src/com/android/email/Controller.java @@ -838,7 +838,7 @@ public class Controller { final long accountId) { Attachment attachInfo = Attachment.restoreAttachmentWithId(mProviderContext, attachmentId); - if (Utility.attachmentExists(mProviderContext, accountId, attachInfo)) { + if (Utility.attachmentExists(mProviderContext, attachInfo)) { // The attachment has already been downloaded, so we will just "pretend" to download it // This presumably is for POP3 messages synchronized (mListeners) { diff --git a/src/com/android/email/MessagingController.java b/src/com/android/email/MessagingController.java index 6826ba1a5..b37db9e28 100644 --- a/src/com/android/email/MessagingController.java +++ b/src/com/android/email/MessagingController.java @@ -1832,7 +1832,7 @@ public class MessagingController implements Runnable { new MessagingException("The attachment is null")); return; } - if (Utility.attachmentExists(mContext, accountId, attachment)) { + if (Utility.attachmentExists(mContext, attachment)) { mListeners.loadAttachmentFinished(accountId, messageId, attachmentId); return; } diff --git a/src/com/android/email/Utility.java b/src/com/android/email/Utility.java index 72859135b..5ba0f35c7 100644 --- a/src/com/android/email/Utility.java +++ b/src/com/android/email/Utility.java @@ -877,8 +877,12 @@ public class Utility { }; } - public static boolean attachmentExists(Context context, long accountId, Attachment attachment) { - if ((attachment == null) || (TextUtils.isEmpty(attachment.mContentUri))) { + public static boolean attachmentExists(Context context, Attachment attachment) { + if (attachment == null) { + return false; + } else if (attachment.mContentBytes != null) { + return true; + } else if (TextUtils.isEmpty(attachment.mContentUri)) { Log.w(Email.LOG_TAG, "attachmentExists ContentUri null."); return false; } @@ -917,7 +921,7 @@ public class Utility { if (msg == null) return false; Attachment[] atts = Attachment.restoreAttachmentsWithMessageId(context, messageId); for (Attachment att: atts) { - if (!attachmentExists(context, msg.mAccountKey, att)) { + if (!attachmentExists(context, att)) { // If the attachment doesn't exist and isn't marked for download, we're in trouble // since the outbound message will be stuck indefinitely in the Outbox. Instead, // we'll just delete the attachment and continue; this is far better than the diff --git a/src/com/android/email/activity/MessageViewFragmentBase.java b/src/com/android/email/activity/MessageViewFragmentBase.java index 97350835c..6a24be73c 100644 --- a/src/com/android/email/activity/MessageViewFragmentBase.java +++ b/src/com/android/email/activity/MessageViewFragmentBase.java @@ -791,7 +791,7 @@ public abstract class MessageViewFragmentBase extends Fragment implements View.O // If the attachment is loaded, show 100% progress // Note that for POP3 messages, the user will only see "Open" and "Save" since the entire // message is loaded before being shown. - if (Utility.attachmentExists(mContext, mAccountId, attachment)) { + if (Utility.attachmentExists(mContext, attachment)) { // Hide "Load", show "View" and "Save" attachmentProgress.setVisibility(View.VISIBLE); attachmentProgress.setProgress(100); diff --git a/tests/src/com/android/email/UtilityMediumTests.java b/tests/src/com/android/email/UtilityMediumTests.java index 3293da653..14b62e75f 100644 --- a/tests/src/com/android/email/UtilityMediumTests.java +++ b/tests/src/com/android/email/UtilityMediumTests.java @@ -19,11 +19,18 @@ package com.android.email; import com.android.email.provider.EmailProvider; import com.android.email.provider.ProviderTestUtils; import com.android.email.provider.EmailContent.Account; +import com.android.email.provider.EmailContent.Attachment; +import com.android.email.provider.EmailContent.Mailbox; +import com.android.email.provider.EmailContent.Message; import android.content.Context; import android.test.ProviderTestCase2; import android.test.suitebuilder.annotation.MediumTest; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + /** * This is a series of medium tests for the Utility class. These tests must be locally * complete - no server(s) required. @@ -71,4 +78,44 @@ public class UtilityMediumTests extends ProviderTestCase2 { acct = Utility.findExistingAccount(mMockContext, account1.mId, "address-ha1", "login-ha1"); assertNull(acct); } + + public void testAttachmentExists() throws IOException { + Account account = ProviderTestUtils.setupAccount("account", true, mMockContext); + // We return false with null attachment + assertFalse(Utility.attachmentExists(mMockContext, null)); + + Mailbox mailbox = + ProviderTestUtils.setupMailbox("mailbox", account.mId, true, mMockContext); + Message message = ProviderTestUtils.setupMessage("foo", account.mId, mailbox.mId, false, + true, mMockContext); + Attachment attachment = ProviderTestUtils.setupAttachment(message.mId, "filename.ext", + 69105, true, mMockContext); + attachment.mContentBytes = null; + // With no contentUri, we should return false + assertFalse(Utility.attachmentExists(mMockContext, attachment)); + + attachment.mContentBytes = new byte[0]; + // With contentBytes set, we should return true + assertTrue(Utility.attachmentExists(mMockContext, attachment)); + + attachment.mContentBytes = null; + // Generate a file name in our data directory, and use that for contentUri + File file = mMockContext.getFileStreamPath("test.att"); + // Delete the file if it already exists + if (file.exists()) { + assertTrue(file.delete()); + } + // Should return false, because the file doesn't exist + assertFalse(Utility.attachmentExists(mMockContext, attachment)); + + assertTrue(file.createNewFile()); + // Put something in the file + FileWriter writer = new FileWriter(file); + writer.write("Foo"); + writer.flush(); + writer.close(); + attachment.mContentUri = "file://" + file.getAbsolutePath(); + // Now, this should return true + assertTrue(Utility.attachmentExists(mMockContext, attachment)); + } }