Refactoring

pull/147/head
M66B 6 years ago
parent 97d694e4da
commit 0fca2da4e2

@ -27,8 +27,11 @@ import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -76,7 +79,7 @@ public class JobDaily extends JobService {
Log.i("Start daily job"); Log.i("Start daily job");
// Cleanup folders // Cleanup folders
Log.i("Cleanup folders"); Log.i("Cleanup kept messages");
for (EntityFolder folder : db.folder().getFolders()) { for (EntityFolder folder : db.folder().getFolders()) {
Calendar cal_keep = Calendar.getInstance(); Calendar cal_keep = Calendar.getInstance();
cal_keep.add(Calendar.DAY_OF_MONTH, -folder.keep_days); cal_keep.add(Calendar.DAY_OF_MONTH, -folder.keep_days);
@ -97,31 +100,28 @@ public class JobDaily extends JobService {
long now = new Date().getTime(); long now = new Date().getTime();
// Cleanup message files List<File> files = new ArrayList<>();
Log.i("Cleanup message files");
File[] messages = new File(context.getFilesDir(), "messages").listFiles(); File[] messages = new File(context.getFilesDir(), "messages").listFiles();
File[] references = new File(context.getFilesDir(), "references").listFiles();
File[] raws = new File(context.getFilesDir(), "raw").listFiles();
if (messages != null) if (messages != null)
for (File file : messages) { files.addAll(Arrays.asList(messages));
long id = Long.parseLong(file.getName()); if (references != null)
if (db.message().countMessage(id) == 0) { files.addAll(Arrays.asList(references));
Log.i("Cleanup message id=" + id); if (raws != null)
if (!file.delete()) files.addAll(Arrays.asList(raws));
Log.w("Error deleting " + file);
}
}
// Cleanup message files // Cleanup message files
Log.i("Cleanup reference files"); Log.i("Cleanup message files");
File[] references = new File(context.getFilesDir(), "references").listFiles(); for (File file : files) {
if (references != null) long id = Long.parseLong(file.getName());
for (File file : references) { if (db.message().countMessage(id) == 0) {
long id = Long.parseLong(file.getName()); Log.i("Deleting " + file);
if (db.message().countMessage(id) == 0) { if (!file.delete())
Log.i("Cleanup message id=" + id); Log.w("Error deleting " + file);
if (!file.delete())
Log.w("Error deleting " + file);
}
} }
}
// Cleanup attachment files // Cleanup attachment files
Log.i("Cleanup attachment files"); Log.i("Cleanup attachment files");
@ -130,7 +130,7 @@ public class JobDaily extends JobService {
for (File file : attachments) { for (File file : attachments) {
long id = Long.parseLong(file.getName()); long id = Long.parseLong(file.getName());
if (db.attachment().countAttachment(id) == 0) { if (db.attachment().countAttachment(id) == 0) {
Log.i("Cleanup attachment id=" + id); Log.i("Deleting " + file);
if (!file.delete()) if (!file.delete())
Log.w("Error deleting " + file); Log.w("Error deleting " + file);
} }
@ -142,25 +142,11 @@ public class JobDaily extends JobService {
if (images != null) if (images != null)
for (File file : images) for (File file : images)
if (file.isFile() && (now - file.lastModified()) > CACHE_IMAGE_DURATION) { if (file.isFile() && (now - file.lastModified()) > CACHE_IMAGE_DURATION) {
Log.i("Deleting cached image=" + file.getName()); Log.i("Deleting " + file);
if (!file.delete()) if (!file.delete())
Log.w("Error deleting " + file); Log.w("Error deleting " + file);
} }
// Cleanup attachment files
Log.i("Cleanup raw files");
File[] raw = new File(context.getFilesDir(), "raw").listFiles();
if (raw != null)
for (File file : raw)
if (file.isFile()) {
long id = Long.parseLong(file.getName());
if (db.message().countMessage(id) == 0) {
Log.i("Cleanup raw id=" + id);
if (!file.delete())
Log.w("Error deleting " + file);
}
}
Log.i("Cleanup log"); Log.i("Cleanup log");
long before = now - KEEP_LOG_DURATION; long before = now - KEEP_LOG_DURATION;
int logs = db.log().deleteLogs(before); int logs = db.log().deleteLogs(before);

Loading…
Cancel
Save