diff --git a/app/src/main/java/eu/faircode/email/DaoAttachment.java b/app/src/main/java/eu/faircode/email/DaoAttachment.java index 1735728330..6334ddb7c8 100644 --- a/app/src/main/java/eu/faircode/email/DaoAttachment.java +++ b/app/src/main/java/eu/faircode/email/DaoAttachment.java @@ -97,6 +97,17 @@ public interface DaoAttachment { " WHERE id = :id") void setCid(long id, String cid); + @Query("UPDATE attachment" + + " SET available = 0" + + " WHERE EXISTS" + + " (SELECT * FROM attachment a" + + " JOIN message ON message.id = a.message" + + " JOIN folder ON folder.id = message.folder" + + " WHERE a.id = attachment.id" + + " AND a.available" + + " AND message.stored < :now - folder.sync_days * 24 * 3600 * 1000)") + int purge(long now); + @Insert long insertAttachment(EntityAttachment attachment); diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java b/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java index 1862ae6a3c..414c0d590d 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java @@ -71,6 +71,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc private TextView tvUuid; private SwitchCompat swDebug; private Button btnReset; + private SwitchCompat swCleanupAttachments; private Button btnCleanup; private TextView tvLastCleanup; private Button btnMore; @@ -84,7 +85,8 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc private Group grpDebug; private final static String[] RESET_OPTIONS = new String[]{ - "shortcuts", "fts", "english", "watchdog", "auto_optimize", "updates", "experiments", "crash_reports", "debug" + "shortcuts", "fts", "english", "watchdog", "auto_optimize", "updates", + "experiments", "crash_reports", "debug", "cleanup_attachments" }; private final static String[] RESET_QUESTIONS = new String[]{ @@ -122,6 +124,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc tvUuid = view.findViewById(R.id.tvUuid); swDebug = view.findViewById(R.id.swDebug); btnReset = view.findViewById(R.id.btnReset); + swCleanupAttachments = view.findViewById(R.id.swCleanupAttachments); btnCleanup = view.findViewById(R.id.btnCleanup); tvLastCleanup = view.findViewById(R.id.tvLastCleanup); btnMore = view.findViewById(R.id.btnMore); @@ -272,6 +275,13 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc } }); + swCleanupAttachments.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { + prefs.edit().putBoolean("cleanup_attachments", checked).apply(); + } + }); + btnCleanup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -447,6 +457,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc swCrashReports.setChecked(prefs.getBoolean("crash_reports", false)); tvUuid.setText(prefs.getString("uuid", null)); swDebug.setChecked(prefs.getBoolean("debug", false)); + swCleanupAttachments.setChecked(prefs.getBoolean("cleanup_attachments", false)); tvProcessors.setText(getString(R.string.title_advanced_processors, Runtime.getRuntime().availableProcessors())); diff --git a/app/src/main/java/eu/faircode/email/WorkerCleanup.java b/app/src/main/java/eu/faircode/email/WorkerCleanup.java index 5df6381015..03c1937ed6 100644 --- a/app/src/main/java/eu/faircode/email/WorkerCleanup.java +++ b/app/src/main/java/eu/faircode/email/WorkerCleanup.java @@ -68,8 +68,11 @@ public class WorkerCleanup extends Worker { } static void cleanup(Context context, boolean manual) { - DB db = DB.getInstance(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + boolean fts = prefs.getBoolean("fts", true); + boolean cleanup_attachments = prefs.getBoolean("cleanup_attachments", false); + + DB db = DB.getInstance(context); try { Log.i("Start cleanup manual=" + manual); @@ -88,6 +91,11 @@ public class WorkerCleanup extends Worker { } } + if (cleanup_attachments) { + int purged = db.attachment().purge(new Date().getTime()); + Log.i("Attachments purged=" + purged); + } + // Check attachments files Log.i("Checking attachments files"); List aids = db.attachment().getAttachmentAvailable(); @@ -195,7 +203,6 @@ public class WorkerCleanup extends Worker { } } - boolean fts = prefs.getBoolean("fts", true); Log.i("Cleanup FTS=" + fts); if (fts) { int deleted = 0; diff --git a/app/src/main/res/layout/fragment_options_misc.xml b/app/src/main/res/layout/fragment_options_misc.xml index 90584acc60..55795b419d 100644 --- a/app/src/main/res/layout/fragment_options_misc.xml +++ b/app/src/main/res/layout/fragment_options_misc.xml @@ -267,6 +267,29 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/tvDebugHint" /> + + + +