Make an attempt to store/retrieve backups from removable storage

Fixes #7521
This commit is contained in:
Moxie Marlinspike 2018-04-04 03:59:12 -07:00
parent 7dd8baba5a
commit 3f3d7f549b
5 changed files with 35 additions and 13 deletions

View File

@ -314,7 +314,7 @@ public class RegistrationActivity extends BaseActionBarActivity implements Verif
@Override
protected @Nullable BackupUtil.BackupInfo doInBackground(Void... voids) {
try {
return BackupUtil.getLatestBackup();
return BackupUtil.getLatestBackup(RegistrationActivity.this);
} catch (NoExternalStorageException e) {
Log.w(TAG, e);
return null;

View File

@ -77,7 +77,7 @@ public class BackupDialog {
.setPositiveButton(R.string.BackupDialog_delete_backups_statement, (dialog, which) -> {
TextSecurePreferences.setBackupPassphrase(context, null);
TextSecurePreferences.setBackupEnabled(context, false);
BackupUtil.deleteAllBackups();
BackupUtil.deleteAllBackups(context);
preference.setChecked(false);
})
.create()

View File

@ -50,7 +50,7 @@ public class LocalBackupJob extends ContextJob {
try {
String backupPassword = TextSecurePreferences.getBackupPassphrase(context);
File backupDirectory = StorageUtil.getBackupDirectory();
File backupDirectory = StorageUtil.getBackupDirectory(context);
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);
@ -76,7 +76,7 @@ public class LocalBackupJob extends ContextJob {
throw new IOException("Renaming temporary backup file failed!");
}
BackupUtil.deleteOldBackups();
BackupUtil.deleteOldBackups(context);
} finally {
GenericForegroundService.stopForegroundTask(context);
}

View File

@ -22,7 +22,7 @@ public class BackupUtil {
public static @NonNull String getLastBackupTime(@NonNull Context context, @NonNull Locale locale) {
try {
BackupInfo backup = getLatestBackup();
BackupInfo backup = getLatestBackup(context);
if (backup == null) return context.getString(R.string.BackupUtil_never);
else return DateUtils.getExtendedRelativeTimeSpanString(context, locale, backup.getTimestamp());
@ -32,8 +32,8 @@ public class BackupUtil {
}
}
public static @Nullable BackupInfo getLatestBackup() throws NoExternalStorageException {
File backupDirectory = StorageUtil.getBackupDirectory();
public static @Nullable BackupInfo getLatestBackup(@NonNull Context context) throws NoExternalStorageException {
File backupDirectory = StorageUtil.getBackupDirectory(context);
File[] backups = backupDirectory.listFiles();
BackupInfo latestBackup = null;
@ -49,9 +49,9 @@ public class BackupUtil {
}
@SuppressWarnings("ResultOfMethodCallIgnored")
public static void deleteAllBackups() {
public static void deleteAllBackups(@NonNull Context context) {
try {
File backupDirectory = StorageUtil.getBackupDirectory();
File backupDirectory = StorageUtil.getBackupDirectory(context);
File[] backups = backupDirectory.listFiles();
for (File backup : backups) {
@ -62,9 +62,9 @@ public class BackupUtil {
}
}
public static void deleteOldBackups() {
public static void deleteOldBackups(@NonNull Context context) {
try {
File backupDirectory = StorageUtil.getBackupDirectory();
File backupDirectory = StorageUtil.getBackupDirectory(context);
File[] backups = backupDirectory.listFiles();
if (backups != null && backups.length > 5) {

View File

@ -1,8 +1,13 @@
package org.thoughtcrime.securesms.util;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.Nullable;
import com.annimon.stream.Objects;
import com.annimon.stream.Stream;
import org.thoughtcrime.securesms.database.NoExternalStorageException;
import java.io.File;
@ -10,8 +15,25 @@ import java.io.File;
public class StorageUtil
{
public static File getBackupDirectory() throws NoExternalStorageException {
File storage = Environment.getExternalStorageDirectory();
public static File getBackupDirectory(Context context) throws NoExternalStorageException {
File storage = null;
if (Build.VERSION.SDK_INT >= 19) {
File[] directories = context.getExternalFilesDirs(null);
if (directories != null) {
storage = Stream.of(directories)
.withoutNulls()
.filterNot(f -> f.getAbsolutePath().contains("emulated"))
.limit(1)
.findSingle()
.orElse(null);
}
}
if (storage == null) {
storage = Environment.getExternalStorageDirectory();
}
if (!storage.canWrite()) {
throw new NoExternalStorageException();