diff --git a/src/com/android/email/MessagingController.java b/src/com/android/email/MessagingController.java index 349090bf6..7f5384088 100644 --- a/src/com/android/email/MessagingController.java +++ b/src/com/android/email/MessagingController.java @@ -321,7 +321,11 @@ public class MessagingController implements Runnable { case Mailbox.TYPE_TRASH: break; default: - // TODO drop all attachment files too + // Drop all attachment files related to this mailbox + AttachmentProvider.deleteAllMailboxAttachmentFiles( + mContext, accountId, localInfo.mId); + // Delete the mailbox. Triggers will take care of + // related Message, Body and Attachment records. Uri uri = ContentUris.withAppendedId( EmailContent.Mailbox.CONTENT_URI, localInfo.mId); mContext.getContentResolver().delete(uri, null, null); diff --git a/src/com/android/email/provider/AttachmentProvider.java b/src/com/android/email/provider/AttachmentProvider.java index 70b3c8427..c38110fc6 100644 --- a/src/com/android/email/provider/AttachmentProvider.java +++ b/src/com/android/email/provider/AttachmentProvider.java @@ -19,6 +19,8 @@ package com.android.email.provider; import com.android.email.mail.internet.MimeUtility; import com.android.email.provider.EmailContent.Attachment; import com.android.email.provider.EmailContent.AttachmentColumns; +import com.android.email.provider.EmailContent.Message; +import com.android.email.provider.EmailContent.MessageColumns; import android.content.ContentProvider; import android.content.ContentResolver; @@ -376,4 +378,26 @@ public class AttachmentProvider extends ContentProvider { c.close(); } } + + /** + * In support of deleting a mailbox, find all messages and delete their attachments. + * + * @param context + * @param accountId the account for the mailbox + * @param mailboxId the mailbox for the messages + */ + public static void deleteAllMailboxAttachmentFiles(Context context, long accountId, + long mailboxId) { + Cursor c = context.getContentResolver().query(Message.CONTENT_URI, + Message.ID_COLUMN_PROJECTION, MessageColumns.MAILBOX_KEY + "=?", + new String[] { Long.toString(mailboxId) }, null); + try { + while (c.moveToNext()) { + long messageId = c.getLong(Message.ID_PROJECTION_COLUMN); + deleteAllAttachmentFiles(context, accountId, messageId); + } + } finally { + c.close(); + } + } } diff --git a/tests/src/com/android/email/provider/AttachmentProviderTests.java b/tests/src/com/android/email/provider/AttachmentProviderTests.java index f38d262d7..67e8061b6 100644 --- a/tests/src/com/android/email/provider/AttachmentProviderTests.java +++ b/tests/src/com/android/email/provider/AttachmentProviderTests.java @@ -22,6 +22,8 @@ import com.android.email.mail.store.LocalStore; import com.android.email.provider.AttachmentProvider.AttachmentProviderColumns; 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.ContentResolver; import android.content.ContentValues; @@ -464,6 +466,59 @@ public class AttachmentProviderTests extends ProviderTestCase2