2018-02-26 17:58:18 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
|
|
|
|
import android.Manifest;
|
2018-08-09 14:15:43 +00:00
|
|
|
|
2019-06-27 16:18:52 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2018-02-26 17:58:18 +00:00
|
|
|
|
2021-12-14 19:31:44 +00:00
|
|
|
import org.greenrobot.eventbus.EventBus;
|
|
|
|
import org.greenrobot.eventbus.Subscribe;
|
|
|
|
import org.greenrobot.eventbus.ThreadMode;
|
2020-12-04 23:31:58 +00:00
|
|
|
import org.signal.core.util.logging.Log;
|
2018-04-06 17:13:53 +00:00
|
|
|
import org.thoughtcrime.securesms.R;
|
2020-10-20 18:13:05 +00:00
|
|
|
import org.thoughtcrime.securesms.backup.BackupFileIOError;
|
2019-06-27 16:18:52 +00:00
|
|
|
import org.thoughtcrime.securesms.backup.BackupPassphrase;
|
2021-12-14 19:31:44 +00:00
|
|
|
import org.thoughtcrime.securesms.backup.FullBackupBase;
|
2018-02-26 17:58:18 +00:00
|
|
|
import org.thoughtcrime.securesms.backup.FullBackupExporter;
|
|
|
|
import org.thoughtcrime.securesms.crypto.AttachmentSecretProvider;
|
|
|
|
import org.thoughtcrime.securesms.database.NoExternalStorageException;
|
2021-11-18 17:36:52 +00:00
|
|
|
import org.thoughtcrime.securesms.database.SignalDatabase;
|
2020-09-29 14:00:46 +00:00
|
|
|
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
2019-06-27 16:18:52 +00:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.Data;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.Job;
|
2020-09-29 14:00:46 +00:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.JobManager;
|
2020-09-15 17:21:14 +00:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.impl.ChargingConstraint;
|
2018-08-06 16:20:24 +00:00
|
|
|
import org.thoughtcrime.securesms.notifications.NotificationChannels;
|
2018-02-26 17:58:18 +00:00
|
|
|
import org.thoughtcrime.securesms.permissions.Permissions;
|
|
|
|
import org.thoughtcrime.securesms.service.GenericForegroundService;
|
2019-06-27 16:18:52 +00:00
|
|
|
import org.thoughtcrime.securesms.service.NotificationController;
|
2018-02-26 17:58:18 +00:00
|
|
|
import org.thoughtcrime.securesms.util.BackupUtil;
|
|
|
|
import org.thoughtcrime.securesms.util.StorageUtil;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Locale;
|
2018-08-09 14:15:43 +00:00
|
|
|
|
2020-09-11 18:54:37 +00:00
|
|
|
public final class LocalBackupJob extends BaseJob {
|
2018-02-26 17:58:18 +00:00
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
public static final String KEY = "LocalBackupJob";
|
2018-02-26 17:58:18 +00:00
|
|
|
|
2020-09-11 18:54:37 +00:00
|
|
|
private static final String TAG = Log.tag(LocalBackupJob.class);
|
|
|
|
|
2021-03-11 18:27:25 +00:00
|
|
|
public static final String QUEUE = "__LOCAL_BACKUP__";
|
2020-09-29 14:00:46 +00:00
|
|
|
|
2020-09-11 18:54:37 +00:00
|
|
|
public static final String TEMP_BACKUP_FILE_PREFIX = ".backup";
|
|
|
|
public static final String TEMP_BACKUP_FILE_SUFFIX = ".tmp";
|
2018-02-26 17:58:18 +00:00
|
|
|
|
2020-09-29 14:00:46 +00:00
|
|
|
public static void enqueue(boolean force) {
|
|
|
|
JobManager jobManager = ApplicationDependencies.getJobManager();
|
|
|
|
Parameters.Builder parameters = new Parameters.Builder()
|
|
|
|
.setQueue(QUEUE)
|
2020-12-07 22:30:05 +00:00
|
|
|
.setMaxInstancesForFactory(1)
|
2020-09-29 14:00:46 +00:00
|
|
|
.setMaxAttempts(3);
|
|
|
|
if (force) {
|
|
|
|
jobManager.cancelAllInQueue(QUEUE);
|
|
|
|
} else {
|
|
|
|
parameters.addConstraint(ChargingConstraint.KEY);
|
2020-09-15 17:21:14 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 19:12:53 +00:00
|
|
|
if (BackupUtil.isUserSelectionRequired(ApplicationDependencies.getApplication())) {
|
|
|
|
jobManager.add(new LocalBackupJobApi29(parameters.build()));
|
|
|
|
} else {
|
|
|
|
jobManager.add(new LocalBackupJob(parameters.build()));
|
|
|
|
}
|
2018-08-09 14:15:43 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
private LocalBackupJob(@NonNull Job.Parameters parameters) {
|
|
|
|
super(parameters);
|
2018-02-26 17:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 15:56:35 +00:00
|
|
|
public @NonNull Data serialize() {
|
|
|
|
return Data.EMPTY;
|
2018-08-09 14:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 15:56:35 +00:00
|
|
|
public @NonNull String getFactoryKey() {
|
|
|
|
return KEY;
|
2018-08-09 14:15:43 +00:00
|
|
|
}
|
2018-02-26 17:58:18 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRun() throws NoExternalStorageException, IOException {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "Executing backup job...");
|
2018-02-26 17:58:18 +00:00
|
|
|
|
2020-10-20 18:13:05 +00:00
|
|
|
BackupFileIOError.clearNotification(context);
|
|
|
|
|
2018-02-26 17:58:18 +00:00
|
|
|
if (!Permissions.hasAll(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
|
|
|
|
throw new IOException("No external storage permission!");
|
|
|
|
}
|
|
|
|
|
2021-12-14 19:31:44 +00:00
|
|
|
ProgressUpdater updater = new ProgressUpdater();
|
2019-06-27 16:18:52 +00:00
|
|
|
try (NotificationController notification = GenericForegroundService.startForegroundTask(context,
|
2021-12-14 19:31:44 +00:00
|
|
|
context.getString(R.string.LocalBackupJob_creating_signal_backup),
|
2019-06-27 16:18:52 +00:00
|
|
|
NotificationChannels.BACKUPS,
|
|
|
|
R.drawable.ic_signal_backup))
|
|
|
|
{
|
2021-12-14 19:31:44 +00:00
|
|
|
updater.setNotification(notification);
|
|
|
|
EventBus.getDefault().register(updater);
|
2019-06-27 16:18:52 +00:00
|
|
|
notification.setIndeterminateProgress();
|
2018-02-26 17:58:18 +00:00
|
|
|
|
2019-02-07 17:47:06 +00:00
|
|
|
String backupPassword = BackupPassphrase.get(context);
|
2020-10-29 17:32:55 +00:00
|
|
|
File backupDirectory = StorageUtil.getOrCreateBackupDirectory();
|
2018-02-26 17:58:18 +00:00
|
|
|
String timestamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US).format(new Date());
|
|
|
|
String fileName = String.format("signal-%s.backup", timestamp);
|
|
|
|
File backupFile = new File(backupDirectory, fileName);
|
|
|
|
|
2020-09-11 18:54:37 +00:00
|
|
|
deleteOldTemporaryBackups(backupDirectory);
|
|
|
|
|
2018-02-26 17:58:18 +00:00
|
|
|
if (backupFile.exists()) {
|
|
|
|
throw new IOException("Backup file already exists?");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (backupPassword == null) {
|
|
|
|
throw new IOException("Backup password is null");
|
|
|
|
}
|
|
|
|
|
2020-09-11 18:54:37 +00:00
|
|
|
File tempFile = File.createTempFile(TEMP_BACKUP_FILE_PREFIX, TEMP_BACKUP_FILE_SUFFIX, backupDirectory);
|
2018-02-26 17:58:18 +00:00
|
|
|
|
2020-01-27 14:28:17 +00:00
|
|
|
try {
|
|
|
|
FullBackupExporter.export(context,
|
|
|
|
AttachmentSecretProvider.getInstance(context).getOrCreateAttachmentSecret(),
|
2021-11-18 17:36:52 +00:00
|
|
|
SignalDatabase.getBackupDatabase(),
|
2020-01-27 14:28:17 +00:00
|
|
|
tempFile,
|
2021-03-12 16:28:24 +00:00
|
|
|
backupPassword,
|
|
|
|
this::isCanceled);
|
2020-01-27 14:28:17 +00:00
|
|
|
|
|
|
|
if (!tempFile.renameTo(backupFile)) {
|
|
|
|
Log.w(TAG, "Failed to rename temp file");
|
|
|
|
throw new IOException("Renaming temporary backup file failed!");
|
|
|
|
}
|
2021-03-12 16:28:24 +00:00
|
|
|
} catch (FullBackupExporter.BackupCanceledException e) {
|
|
|
|
Log.w(TAG, "Backup cancelled");
|
|
|
|
throw e;
|
2020-10-20 18:13:05 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
BackupFileIOError.postNotificationForException(context, e, getRunAttempt());
|
|
|
|
throw e;
|
2020-01-27 14:28:17 +00:00
|
|
|
} finally {
|
|
|
|
if (tempFile.exists()) {
|
|
|
|
if (tempFile.delete()) {
|
|
|
|
Log.w(TAG, "Backup failed. Deleted temp file");
|
|
|
|
} else {
|
|
|
|
Log.w(TAG, "Backup failed. Failed to delete temp file " + tempFile);
|
|
|
|
}
|
|
|
|
}
|
2018-02-26 17:58:18 +00:00
|
|
|
}
|
|
|
|
|
2018-06-29 00:38:59 +00:00
|
|
|
BackupUtil.deleteOldBackups();
|
2021-12-14 19:31:44 +00:00
|
|
|
} finally {
|
|
|
|
EventBus.getDefault().unregister(updater);
|
|
|
|
updater.setNotification(null);
|
2018-02-26 17:58:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 18:54:37 +00:00
|
|
|
private static void deleteOldTemporaryBackups(@NonNull File backupDirectory) {
|
|
|
|
for (File file : backupDirectory.listFiles()) {
|
|
|
|
if (file.isFile()) {
|
|
|
|
String name = file.getName();
|
|
|
|
if (name.startsWith(TEMP_BACKUP_FILE_PREFIX) && name.endsWith(TEMP_BACKUP_FILE_SUFFIX)) {
|
|
|
|
if (file.delete()) {
|
|
|
|
Log.w(TAG, "Deleted old temporary backup file");
|
|
|
|
} else {
|
|
|
|
Log.w(TAG, "Could not delete old temporary backup file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-26 17:58:18 +00:00
|
|
|
@Override
|
2019-05-22 16:51:56 +00:00
|
|
|
public boolean onShouldRetry(@NonNull Exception e) {
|
2018-02-26 17:58:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-01-03 19:10:16 +00:00
|
|
|
public void onFailure() {
|
2019-03-28 15:56:35 +00:00
|
|
|
}
|
2018-02-26 17:58:18 +00:00
|
|
|
|
2021-12-14 19:31:44 +00:00
|
|
|
private static class ProgressUpdater {
|
|
|
|
private NotificationController notification;
|
|
|
|
|
|
|
|
@Subscribe(threadMode = ThreadMode.POSTING)
|
|
|
|
public void onEvent(FullBackupBase.BackupEvent event) {
|
|
|
|
if (notification == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.getType() == FullBackupBase.BackupEvent.Type.PROGRESS) {
|
|
|
|
if (event.getEstimatedTotalCount() == 0) {
|
|
|
|
notification.setIndeterminateProgress();
|
|
|
|
} else {
|
|
|
|
notification.setProgress(100, (int) event.getCompletionPercentage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setNotification(NotificationController notification) {
|
|
|
|
this.notification = notification;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 15:56:35 +00: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 17:58:18 +00:00
|
|
|
}
|
|
|
|
}
|