From 9d6a8b1c06f9069698ff52dfc44c890bd3ef6f77 Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 26 Nov 2018 17:04:32 +0100 Subject: [PATCH] Added daily folder cleanup --- .../java/eu/faircode/email/DaoFolder.java | 3 +++ .../main/java/eu/faircode/email/JobDaily.java | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/app/src/main/java/eu/faircode/email/DaoFolder.java b/app/src/main/java/eu/faircode/email/DaoFolder.java index 77f51499d5..6e01bfa2e8 100644 --- a/app/src/main/java/eu/faircode/email/DaoFolder.java +++ b/app/src/main/java/eu/faircode/email/DaoFolder.java @@ -82,6 +82,9 @@ public interface DaoFolder { " WHERE folder.id = :id") LiveData liveFolderEx(long id); + @Query("SELECT * FROM folder ORDER BY account, name") + List getFolders(); + @Query("SELECT * FROM folder WHERE id = :id") EntityFolder getFolder(Long id); diff --git a/app/src/main/java/eu/faircode/email/JobDaily.java b/app/src/main/java/eu/faircode/email/JobDaily.java index eb59d235b7..256e61c496 100644 --- a/app/src/main/java/eu/faircode/email/JobDaily.java +++ b/app/src/main/java/eu/faircode/email/JobDaily.java @@ -28,6 +28,7 @@ import android.content.Context; import android.util.Log; import java.io.File; +import java.util.Calendar; import java.util.Date; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -66,6 +67,25 @@ public class JobDaily extends JobService { Log.i(Helper.TAG, "Start daily job"); + // Cleanup folders + Log.i(Helper.TAG, "Cleanup folders"); + for (EntityFolder folder : db.folder().getFolders()) { + Calendar cal_keep = Calendar.getInstance(); + cal_keep.add(Calendar.DAY_OF_MONTH, -folder.keep_days); + cal_keep.set(Calendar.HOUR_OF_DAY, 0); + cal_keep.set(Calendar.MINUTE, 0); + cal_keep.set(Calendar.SECOND, 0); + cal_keep.set(Calendar.MILLISECOND, 0); + + long keep_time = cal_keep.getTimeInMillis(); + if (keep_time < 0) + keep_time = 0; + + int messages = db.message().deleteMessagesBefore(folder.id, keep_time, false); + Log.i(Helper.TAG, "Cleanup folder=" + folder.account + ":" + folder.name + + " deleted before=" + new Date(keep_time) + " count=" + messages); + } + // Cleanup message files Log.i(Helper.TAG, "Cleanup message files"); File[] messages = new File(getFilesDir(), "messages").listFiles();