From 71754d3f940fe82e251c274e3e56781e702cfd6f Mon Sep 17 00:00:00 2001 From: Andrew Stadler Date: Wed, 30 Sep 2009 23:32:30 -0700 Subject: [PATCH] Delete attachments when necessary, don't leak disk space * Add AttachmentProvider.deleteAllMailboxAttachmentFiles * Call it when server deletes a mailbox * Confirmed (no change) all message deletes call deleteAllAttachmentFiles * Unit tests of course :) Bug # 2069004 Change-Id: I99731e6489fdca4cc9cebdff5fcf9c09d12b7b3a --- .../android/email/MessagingController.java | 6 +- .../email/provider/AttachmentProvider.java | 24 ++++++++ .../provider/AttachmentProviderTests.java | 55 +++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) 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