From 3ca0a04f1a46e53f7add8069ce37ed05aa512ea0 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Fri, 4 Jan 2019 11:19:42 -0600 Subject: [PATCH] Clean up attachments and cache --- .../todoroo/astrid/files/FilesControlSet.java | 11 +++++--- .../org/tasks/data/TaskAttachmentDao.java | 3 ++ .../java/org/tasks/data/UserActivityDao.java | 7 +++++ .../main/java/org/tasks/files/FileHelper.java | 16 ++++++++++- .../main/java/org/tasks/jobs/CleanupWork.java | 28 +++++++++++++++++-- .../org/tasks/preferences/Preferences.java | 2 +- .../tasks/scheduling/BackgroundScheduler.java | 13 +++++++-- 7 files changed, 69 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/todoroo/astrid/files/FilesControlSet.java b/app/src/main/java/com/todoroo/astrid/files/FilesControlSet.java index b13c7223a..b6f854321 100644 --- a/app/src/main/java/com/todoroo/astrid/files/FilesControlSet.java +++ b/app/src/main/java/com/todoroo/astrid/files/FilesControlSet.java @@ -103,10 +103,13 @@ public class FilesControlSet extends TaskEditControlFragment { @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode == REQUEST_CAMERA - || requestCode == REQUEST_STORAGE - || requestCode == REQUEST_GALLERY - || requestCode == REQUEST_AUDIO) { + if (requestCode == REQUEST_CAMERA || requestCode == REQUEST_AUDIO) { + if (resultCode == RESULT_OK) { + Uri uri = data.getData(); + copyToAttachmentDirectory(uri); + FileHelper.delete(context, uri); + } + } else if (requestCode == REQUEST_STORAGE || requestCode == REQUEST_GALLERY) { if (resultCode == RESULT_OK) { copyToAttachmentDirectory(data.getData()); } diff --git a/app/src/main/java/org/tasks/data/TaskAttachmentDao.java b/app/src/main/java/org/tasks/data/TaskAttachmentDao.java index a570568f4..5cc591d67 100644 --- a/app/src/main/java/org/tasks/data/TaskAttachmentDao.java +++ b/app/src/main/java/org/tasks/data/TaskAttachmentDao.java @@ -16,6 +16,9 @@ public abstract class TaskAttachmentDao { @Query("SELECT * FROM task_attachments WHERE task_id = :taskUuid") public abstract List getAttachments(String taskUuid); + @Query("SELECT * FROM task_attachments INNER JOIN tasks ON tasks._id = :task WHERE task_id = tasks.remoteId") + public abstract List getAttachments(long task); + @Query("SELECT * FROM task_attachments") public abstract List getAttachments(); diff --git a/app/src/main/java/org/tasks/data/UserActivityDao.java b/app/src/main/java/org/tasks/data/UserActivityDao.java index 47cb3944d..65e3253ef 100644 --- a/app/src/main/java/org/tasks/data/UserActivityDao.java +++ b/app/src/main/java/org/tasks/data/UserActivityDao.java @@ -1,6 +1,7 @@ package org.tasks.data; import androidx.room.Dao; +import androidx.room.Delete; import androidx.room.Insert; import androidx.room.Query; import androidx.room.Update; @@ -19,9 +20,15 @@ public abstract class UserActivityDao { @Update public abstract void update(UserActivity userActivity); + @Delete + public abstract void delete(UserActivity userActivity); + @Query("SELECT * FROM userActivity WHERE target_id = :taskUuid ORDER BY created_at DESC ") public abstract List getCommentsForTask(String taskUuid); + @Query("SELECT * FROM userActivity INNER JOIN tasks ON tasks._id = :task WHERE target_id = tasks.remoteId") + public abstract List getComments(long task); + @Query("SELECT * FROM userActivity") public abstract List getComments(); diff --git a/app/src/main/java/org/tasks/files/FileHelper.java b/app/src/main/java/org/tasks/files/FileHelper.java index 271712a50..ba3f3a3ef 100644 --- a/app/src/main/java/org/tasks/files/FileHelper.java +++ b/app/src/main/java/org/tasks/files/FileHelper.java @@ -97,11 +97,25 @@ public class FileHelper { documentFile.delete(); break; case "file": - new File(uri.getPath()).delete(); + delete(new File(uri.getPath())); break; } } + private static void delete(File... files) { + if (files == null) { + return; + } + + for (File file : files) { + if (file.isDirectory()) { + delete(file.listFiles()); + } else { + file.delete(); + } + } + } + public static String getFilename(Context context, Uri uri) { switch (uri.getScheme()) { case ContentResolver.SCHEME_FILE: diff --git a/app/src/main/java/org/tasks/jobs/CleanupWork.java b/app/src/main/java/org/tasks/jobs/CleanupWork.java index 57f51d071..6c3c2335a 100644 --- a/app/src/main/java/org/tasks/jobs/CleanupWork.java +++ b/app/src/main/java/org/tasks/jobs/CleanupWork.java @@ -1,16 +1,25 @@ package org.tasks.jobs; import android.content.Context; -import androidx.annotation.NonNull; -import androidx.work.WorkerParameters; + import com.todoroo.astrid.alarms.AlarmService; import com.todoroo.astrid.reminders.ReminderService; import com.todoroo.astrid.timers.TimerPlugin; -import javax.inject.Inject; + +import org.tasks.data.TaskAttachment; +import org.tasks.data.TaskAttachmentDao; +import org.tasks.data.UserActivity; +import org.tasks.data.UserActivityDao; +import org.tasks.files.FileHelper; import org.tasks.injection.InjectingWorker; import org.tasks.injection.JobComponent; import org.tasks.location.GeofenceService; import org.tasks.notifications.NotificationManager; + +import javax.inject.Inject; + +import androidx.annotation.NonNull; +import androidx.work.WorkerParameters; import timber.log.Timber; public class CleanupWork extends InjectingWorker { @@ -22,9 +31,14 @@ public class CleanupWork extends InjectingWorker { @Inject TimerPlugin timerPlugin; @Inject ReminderService reminderService; @Inject AlarmService alarmService; + @Inject TaskAttachmentDao taskAttachmentDao; + @Inject UserActivityDao userActivityDao; + + private final Context context; public CleanupWork(@NonNull Context context, @NonNull WorkerParameters workerParams) { super(context, workerParams); + this.context = context; } @NonNull @@ -40,6 +54,14 @@ public class CleanupWork extends InjectingWorker { reminderService.cancelReminder(task); notificationManager.cancel(task); geofenceService.cancelGeofences(task); + for (TaskAttachment attachment : taskAttachmentDao.getAttachments(task)) { + FileHelper.delete(context, attachment.parseUri()); + taskAttachmentDao.delete(attachment); + } + for (UserActivity comment : userActivityDao.getComments(task)) { + FileHelper.delete(context, comment.getPictureUri()); + userActivityDao.delete(comment); + } } timerPlugin.updateNotifications(); return Result.SUCCESS; diff --git a/app/src/main/java/org/tasks/preferences/Preferences.java b/app/src/main/java/org/tasks/preferences/Preferences.java index 3b027588f..6de7be91d 100644 --- a/app/src/main/java/org/tasks/preferences/Preferences.java +++ b/app/src/main/java/org/tasks/preferences/Preferences.java @@ -416,7 +416,7 @@ public class Preferences { public Uri getCacheDirectory() { if (atLeastKitKat()) { - return DocumentFile.fromFile(context.getCacheDir()).getUri(); + return DocumentFile.fromFile(context.getExternalCacheDir()).getUri(); } else { return Uri.fromFile(context.getCacheDir()); } diff --git a/app/src/main/java/org/tasks/scheduling/BackgroundScheduler.java b/app/src/main/java/org/tasks/scheduling/BackgroundScheduler.java index 9c4cb05db..8c6f8385b 100644 --- a/app/src/main/java/org/tasks/scheduling/BackgroundScheduler.java +++ b/app/src/main/java/org/tasks/scheduling/BackgroundScheduler.java @@ -2,14 +2,20 @@ package org.tasks.scheduling; import android.content.Context; import android.content.Intent; -import androidx.annotation.NonNull; + import com.todoroo.astrid.dao.TaskDao; import com.todoroo.astrid.data.Task; -import javax.inject.Inject; + +import org.tasks.files.FileHelper; import org.tasks.injection.ForApplication; import org.tasks.injection.InjectingJobIntentService; import org.tasks.injection.IntentServiceComponent; import org.tasks.jobs.WorkManager; +import org.tasks.preferences.Preferences; + +import javax.inject.Inject; + +import androidx.annotation.NonNull; import timber.log.Timber; public class BackgroundScheduler extends InjectingJobIntentService { @@ -18,6 +24,7 @@ public class BackgroundScheduler extends InjectingJobIntentService { @Inject TaskDao taskDao; @Inject WorkManager jobManager; @Inject RefreshScheduler refreshScheduler; + @Inject Preferences preferences; public static void enqueueWork(Context context) { BackgroundScheduler.enqueueWork( @@ -38,6 +45,8 @@ public class BackgroundScheduler extends InjectingJobIntentService { for (Task task : taskDao.needsRefresh()) { refreshScheduler.scheduleRefresh(task); } + + FileHelper.delete(context, preferences.getCacheDirectory()); } @Override