2018-02-26 09:58:18 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
|
2020-08-19 10:06:26 +10:00
|
|
|
import androidx.annotation.NonNull;
|
2018-08-09 10:15:43 -04:00
|
|
|
|
2018-02-26 09:58:18 -08:00
|
|
|
import org.thoughtcrime.securesms.database.NoExternalStorageException;
|
2020-09-17 13:26:03 +10:00
|
|
|
import org.thoughtcrime.securesms.loki.database.BackupFileRecord;
|
2020-05-29 14:08:13 +10:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.Data;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.Job;
|
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2018-08-06 12:20:24 -04:00
|
|
|
import org.thoughtcrime.securesms.notifications.NotificationChannels;
|
2018-02-26 09:58:18 -08:00
|
|
|
import org.thoughtcrime.securesms.service.GenericForegroundService;
|
2020-09-14 23:33:44 +10:00
|
|
|
import org.thoughtcrime.securesms.util.BackupUtil;
|
2018-02-26 09:58:18 -08:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2020-09-14 23:33:44 +10:00
|
|
|
import java.util.Collections;
|
2018-08-09 10:15:43 -04:00
|
|
|
|
2020-05-29 14:08:13 +10:00
|
|
|
import network.loki.messenger.R;
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public class LocalBackupJob extends BaseJob {
|
2018-02-26 09:58:18 -08:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public static final String KEY = "LocalBackupJob";
|
2018-02-26 09:58:18 -08:00
|
|
|
|
|
|
|
private static final String TAG = LocalBackupJob.class.getSimpleName();
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public LocalBackupJob() {
|
|
|
|
this(new Job.Parameters.Builder()
|
|
|
|
.setQueue("__LOCAL_BACKUP__")
|
|
|
|
.setMaxInstances(1)
|
|
|
|
.setMaxAttempts(3)
|
|
|
|
.build());
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
private LocalBackupJob(@NonNull Job.Parameters parameters) {
|
|
|
|
super(parameters);
|
2018-02-26 09:58:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 08:56:35 -07:00
|
|
|
public @NonNull Data serialize() {
|
|
|
|
return Data.EMPTY;
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 08:56:35 -07:00
|
|
|
public @NonNull String getFactoryKey() {
|
|
|
|
return KEY;
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
2018-02-26 09:58:18 -08:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRun() throws NoExternalStorageException, IOException {
|
2018-08-02 09:25:33 -04:00
|
|
|
Log.i(TAG, "Executing backup job...");
|
2018-02-26 09:58:18 -08:00
|
|
|
|
2018-04-06 13:13:53 -04:00
|
|
|
GenericForegroundService.startForegroundTask(context,
|
2018-08-06 12:20:24 -04:00
|
|
|
context.getString(R.string.LocalBackupJob_creating_backup),
|
2018-10-27 23:34:18 -07:00
|
|
|
NotificationChannels.BACKUPS,
|
2020-05-28 14:55:17 +10:00
|
|
|
R.drawable.ic_launcher_foreground);
|
2020-05-29 14:08:13 +10:00
|
|
|
|
|
|
|
// TODO: Maybe create a new backup icon like ic_signal_backup?
|
|
|
|
|
2018-02-26 09:58:18 -08:00
|
|
|
try {
|
2020-09-14 23:33:44 +10:00
|
|
|
BackupFileRecord record = BackupUtil.createBackupFile(context);
|
|
|
|
BackupUtil.deleteAllBackupFiles(context, Collections.singletonList(record));
|
|
|
|
|
2018-02-26 09:58:18 -08:00
|
|
|
} finally {
|
|
|
|
GenericForegroundService.stopForegroundTask(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 13:51:56 -03:00
|
|
|
public boolean onShouldRetry(@NonNull Exception e) {
|
2018-02-26 09:58:18 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
2019-03-28 08:56:35 -07:00
|
|
|
}
|
2018-02-26 09:58:18 -08:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public static class Factory implements Job.Factory<LocalBackupJob> {
|
|
|
|
@Override
|
|
|
|
public @NonNull LocalBackupJob create(@NonNull Parameters parameters, @NonNull Data data) {
|
|
|
|
return new LocalBackupJob(parameters);
|
|
|
|
}
|
2018-02-26 09:58:18 -08:00
|
|
|
}
|
|
|
|
}
|