Added setting to delete old attachments

This commit is contained in:
M66B
2020-04-14 19:45:56 +02:00
parent 0656590fd0
commit 358f7214e1
5 changed files with 58 additions and 4 deletions

View File

@@ -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);

View File

@@ -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()));

View File

@@ -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<Long> 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;