Rewrite purgeToVisibleLimit

This commit is contained in:
Hari 2017-08-22 23:47:29 +05:30 committed by Vincent Breitmoser
parent 65dc6d12fd
commit 565e3ecec1

View file

@ -398,23 +398,46 @@ public class LocalFolder extends Folder<LocalMessage> {
return visibleLimit; 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 // don't purge messages while a Search is active since it might throw away search results
if (!Search.isActive()) { if (Search.isActive()) {
if (visibleLimit == 0) { return;
return ;
}
open(OPEN_MODE_RW);
List<? extends Message> messages = getMessages(null, false);
for (int i = visibleLimit; i < messages.size(); i++) {
if (listener != null) {
listener.messageRemoved(messages.get(i));
}
messages.get(i).destroy();
}
} }
}
if (visibleLimit == 0) {
return;
}
open(OPEN_MODE_RW);
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(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 { public void setVisibleLimit(final int visibleLimit) throws MessagingException {
updateMoreMessagesOnVisibleLimitChange(visibleLimit, this.visibleLimit); updateMoreMessagesOnVisibleLimitChange(visibleLimit, this.visibleLimit);