Rewrite purgeToVisibleLimit
@@ -398,24 +398,47 @@ public class LocalFolder extends Folder<LocalMessage> {
return visibleLimit;
}
- public void purgeToVisibleLimit(MessageRemovalListener listener) throws MessagingException {
+ 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()) {
+ if (Search.isActive()) {
+ return;
+ }
+
if (visibleLimit == 0) {
return;
}
+
open(OPEN_MODE_RW);
- List<? extends Message> messages = getMessages(null, false);
- for (int i = visibleLimit; i < messages.size(); i++) {
+
+ localStore.getDatabase().execute(false, new DbCallback<Void>() {
+ @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(messages.get(i));
+ listener.messageRemoved(localMessage);
}
- messages.get(i).destroy();
+ 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);
