|
|
|
@ -15,6 +15,9 @@ import org.tasks.injection.JobComponent;
|
|
|
|
|
import org.tasks.preferences.Preferences;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.Comparator;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
|
@ -22,18 +25,27 @@ import androidx.annotation.NonNull;
|
|
|
|
|
import androidx.work.Data;
|
|
|
|
|
import androidx.work.WorkerParameters;
|
|
|
|
|
|
|
|
|
|
import static com.google.common.collect.Iterables.skip;
|
|
|
|
|
import static com.google.common.collect.Lists.newArrayList;
|
|
|
|
|
|
|
|
|
|
public class DriveUploader extends InjectingWorker {
|
|
|
|
|
|
|
|
|
|
private static final String FOLDER_NAME = "Tasks Backups";
|
|
|
|
|
private static final String EXTRA_URI = "extra_uri";
|
|
|
|
|
private static final String EXTRA_PURGE = "extra_purge";
|
|
|
|
|
private static final Comparator<File> DRIVE_FILE_COMPARATOR =
|
|
|
|
|
(f1, f2) -> Long.compare(f2.getModifiedTime().getValue(), f1.getModifiedTime().getValue());
|
|
|
|
|
|
|
|
|
|
@Inject @ForApplication Context context;
|
|
|
|
|
@Inject DriveInvoker drive;
|
|
|
|
|
@Inject Preferences preferences;
|
|
|
|
|
@Inject Tracker tracker;
|
|
|
|
|
|
|
|
|
|
static Data getInputData(Uri uri) {
|
|
|
|
|
return new Data.Builder().putString(EXTRA_URI, uri.toString()).build();
|
|
|
|
|
static Data getInputData(Uri uri, boolean purge) {
|
|
|
|
|
return new Data.Builder()
|
|
|
|
|
.putString(EXTRA_URI, uri.toString())
|
|
|
|
|
.putBoolean(EXTRA_PURGE, purge)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DriveUploader(@NonNull Context context, @NonNull WorkerParameters workerParams) {
|
|
|
|
@ -48,6 +60,14 @@ public class DriveUploader extends InjectingWorker {
|
|
|
|
|
File folder = getFolder();
|
|
|
|
|
preferences.setString(R.string.p_google_drive_backup_folder, folder.getId());
|
|
|
|
|
drive.createFile(folder.getId(), uri);
|
|
|
|
|
|
|
|
|
|
if (inputData.getBoolean(EXTRA_PURGE, false)) {
|
|
|
|
|
List<File> files = drive.getFilesByPrefix(folder.getId(), "auto.");
|
|
|
|
|
for (File file : getDeleteList(files)) {
|
|
|
|
|
drive.delete(file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Result.SUCCESS;
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
tracker.reportException(e);
|
|
|
|
@ -61,6 +81,11 @@ public class DriveUploader extends InjectingWorker {
|
|
|
|
|
return file == null || file.getTrashed() ? drive.createFolder(FOLDER_NAME) : file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static List<File> getDeleteList(List<File> files) {
|
|
|
|
|
Collections.sort(files, DRIVE_FILE_COMPARATOR);
|
|
|
|
|
return newArrayList(skip(files, BackupWork.DAYS_TO_KEEP_BACKUP));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void inject(JobComponent component) {
|
|
|
|
|
component.inject(this);
|
|
|
|
|