Use cache directory on removable storage for backups if present

Fixes #7692
This commit is contained in:
Moxie Marlinspike 2018-04-23 18:39:43 -07:00 committed by Greyson Parrelli
parent 35d158cfee
commit 6c1a1fb9ad
2 changed files with 24 additions and 7 deletions

View File

@ -52,7 +52,7 @@ public class LocalBackupJob extends ContextJob {
try { try {
String backupPassword = TextSecurePreferences.getBackupPassphrase(context); String backupPassword = TextSecurePreferences.getBackupPassphrase(context);
File backupDirectory = StorageUtil.getBackupDirectory(context); File backupDirectory = StorageUtil.getBackupCacheDirectory(context);
String timestamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US).format(new Date()); String timestamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US).format(new Date());
String fileName = String.format("signal-%s.backup", timestamp); String fileName = String.format("signal-%s.backup", timestamp);
File backupFile = new File(backupDirectory, fileName); File backupFile = new File(backupDirectory, fileName);

View File

@ -22,12 +22,7 @@ public class StorageUtil
File[] directories = context.getExternalFilesDirs(null); File[] directories = context.getExternalFilesDirs(null);
if (directories != null) { if (directories != null) {
storage = Stream.of(directories) storage = getNonEmulated(directories);
.withoutNulls()
.filterNot(f -> f.getAbsolutePath().contains("emulated"))
.limit(1)
.findSingle()
.orElse(null);
} }
} }
@ -52,6 +47,28 @@ public class StorageUtil
return backups; return backups;
} }
public static File getBackupCacheDirectory(Context context) {
if (Build.VERSION.SDK_INT >= 19) {
File[] directories = context.getExternalCacheDirs();
if (directories != null) {
File result = getNonEmulated(directories);
if (result != null) return result;
}
}
return context.getExternalCacheDir();
}
private static @Nullable File getNonEmulated(File[] directories) {
return Stream.of(directories)
.withoutNulls()
.filterNot(f -> f.getAbsolutePath().contains("emulated"))
.limit(1)
.findSingle()
.orElse(null);
}
private static File getSignalStorageDir() throws NoExternalStorageException { private static File getSignalStorageDir() throws NoExternalStorageException {
final File storage = Environment.getExternalStorageDirectory(); final File storage = Environment.getExternalStorageDirectory();