Clean up attachments and cache

pull/795/head
Alex Baker 6 years ago
parent 593b5bc5c5
commit 3ca0a04f1a

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

@ -16,6 +16,9 @@ public abstract class TaskAttachmentDao {
@Query("SELECT * FROM task_attachments WHERE task_id = :taskUuid")
public abstract List<TaskAttachment> getAttachments(String taskUuid);
@Query("SELECT * FROM task_attachments INNER JOIN tasks ON tasks._id = :task WHERE task_id = tasks.remoteId")
public abstract List<TaskAttachment> getAttachments(long task);
@Query("SELECT * FROM task_attachments")
public abstract List<TaskAttachment> getAttachments();

@ -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<UserActivity> getCommentsForTask(String taskUuid);
@Query("SELECT * FROM userActivity INNER JOIN tasks ON tasks._id = :task WHERE target_id = tasks.remoteId")
public abstract List<UserActivity> getComments(long task);
@Query("SELECT * FROM userActivity")
public abstract List<UserActivity> getComments();

@ -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:

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

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

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

Loading…
Cancel
Save