From 565e3ecec19e4ea011870cc02ed522d45de0b8a7 Mon Sep 17 00:00:00 2001 From: Hari Date: Tue, 22 Aug 2017 23:47:29 +0530 Subject: [PATCH] Rewrite purgeToVisibleLimit --- .../com/fsck/k9/mailstore/LocalFolder.java | 53 +++++++++++++------ 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java index be22b37dc..3daf729f2 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java @@ -398,23 +398,46 @@ public class LocalFolder extends Folder { return visibleLimit; } - public void purgeToVisibleLimit(MessageRemovalListener listener) throws MessagingException { - //don't purge messages while a Search is active since it might throw away search results - if (!Search.isActive()) { - if (visibleLimit == 0) { - return ; - } - open(OPEN_MODE_RW); - List messages = getMessages(null, false); - for (int i = visibleLimit; i < messages.size(); i++) { - if (listener != null) { - listener.messageRemoved(messages.get(i)); - } - messages.get(i).destroy(); - } + public void purgeToVisibleLimit(final MessageRemovalListener listener) throws MessagingException { + // don't purge messages while a Search is active since it might throw away search results + if (Search.isActive()) { + return; } - } + if (visibleLimit == 0) { + return; + } + + open(OPEN_MODE_RW); + + localStore.getDatabase().execute(false, new DbCallback() { + @Override + public Void doDbWork(final SQLiteDatabase db) { + Cursor cursor = db.rawQuery("SELECT uid " + + "FROM messages " + + "WHERE empty = 0 AND deleted = 0 AND folder_id = ? ORDER BY date DESC " + + " LIMIT -1 OFFSET ?", + new String[] { Long.toString(getDatabaseId()), Integer.toString(visibleLimit) }); + + try { + while (cursor.moveToNext()) { + String uid = cursor.getString(0); + LocalMessage localMessage = getMessage(uid); + + if (listener != null) { + listener.messageRemoved(localMessage); + } + destroyMessage(localMessage); + } + } catch (Exception e) { + Timber.d(e, "Got an exception"); + } finally { + Utility.closeQuietly(cursor); + } + return null; + } + }); + } public void setVisibleLimit(final int visibleLimit) throws MessagingException { updateMoreMessagesOnVisibleLimitChange(visibleLimit, this.visibleLimit);