2018-02-26 17:58:18 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
|
|
|
|
import android.Manifest;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.support.annotation.NonNull;
|
2018-08-09 14:15:43 +00:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.SafeData;
|
2018-08-01 15:09:24 +00:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2018-02-26 17:58:18 +00:00
|
|
|
|
2018-04-06 17:13:53 +00:00
|
|
|
import org.thoughtcrime.securesms.R;
|
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.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.NoExternalStorageException;
|
2018-06-18 19:27:04 +00:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.JobParameters;
|
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;
|
|
|
|
import org.thoughtcrime.securesms.util.BackupUtil;
|
|
|
|
import org.thoughtcrime.securesms.util.StorageUtil;
|
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
import androidx.work.Data;
|
2018-02-26 17:58:18 +00:00
|
|
|
|
|
|
|
public class LocalBackupJob extends ContextJob {
|
|
|
|
|
|
|
|
private static final String TAG = LocalBackupJob.class.getSimpleName();
|
|
|
|
|
2018-08-09 14:15:43 +00:00
|
|
|
public LocalBackupJob() {
|
|
|
|
super(null, null);
|
|
|
|
}
|
|
|
|
|
2018-02-26 17:58:18 +00:00
|
|
|
public LocalBackupJob(@NonNull Context context) {
|
|
|
|
super(context, JobParameters.newBuilder()
|
|
|
|
.withGroupId("__LOCAL_BACKUP__")
|
2018-08-09 14:15:43 +00:00
|
|
|
.withDuplicatesIgnored(true)
|
2018-02-26 17:58:18 +00:00
|
|
|
.create());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-09 14:15:43 +00:00
|
|
|
protected void initialize(@NonNull SafeData data) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected @NonNull Data serialize(@NonNull Data.Builder dataBuilder) {
|
|
|
|
return dataBuilder.build();
|
|
|
|
}
|
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
|
|
|
|
|
|
|
if (!Permissions.hasAll(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
|
|
|
|
throw new IOException("No external storage permission!");
|
|
|
|
}
|
|
|
|
|
2018-04-06 17:13:53 +00:00
|
|
|
GenericForegroundService.startForegroundTask(context,
|
2018-08-06 16:20:24 +00:00
|
|
|
context.getString(R.string.LocalBackupJob_creating_backup),
|
|
|
|
NotificationChannels.BACKUPS);
|
2018-02-26 17:58:18 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
String backupPassword = TextSecurePreferences.getBackupPassphrase(context);
|
2018-06-29 00:38:59 +00:00
|
|
|
File backupDirectory = StorageUtil.getBackupDirectory();
|
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);
|
|
|
|
|
|
|
|
if (backupFile.exists()) {
|
|
|
|
throw new IOException("Backup file already exists?");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (backupPassword == null) {
|
|
|
|
throw new IOException("Backup password is null");
|
|
|
|
}
|
|
|
|
|
2018-05-24 16:57:16 +00:00
|
|
|
File tempFile = File.createTempFile("backup", "tmp", StorageUtil.getBackupCacheDirectory(context));
|
2018-02-26 17:58:18 +00:00
|
|
|
|
|
|
|
FullBackupExporter.export(context,
|
|
|
|
AttachmentSecretProvider.getInstance(context).getOrCreateAttachmentSecret(),
|
|
|
|
DatabaseFactory.getBackupDatabase(context),
|
|
|
|
tempFile,
|
|
|
|
backupPassword);
|
|
|
|
|
|
|
|
if (!tempFile.renameTo(backupFile)) {
|
|
|
|
tempFile.delete();
|
|
|
|
throw new IOException("Renaming temporary backup file failed!");
|
|
|
|
}
|
|
|
|
|
2018-06-29 00:38:59 +00:00
|
|
|
BackupUtil.deleteOldBackups();
|
2018-02-26 17:58:18 +00:00
|
|
|
} finally {
|
|
|
|
GenericForegroundService.stopForegroundTask(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onShouldRetry(Exception e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|