From 6c1a1fb9ad815062d4175873c2037d0fa608b555 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Mon, 23 Apr 2018 18:39:43 -0700 Subject: [PATCH] Use cache directory on removable storage for backups if present Fixes #7692 --- .../securesms/jobs/LocalBackupJob.java | 2 +- .../securesms/util/StorageUtil.java | 29 +++++++++++++++---- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/org/thoughtcrime/securesms/jobs/LocalBackupJob.java b/src/org/thoughtcrime/securesms/jobs/LocalBackupJob.java index 9d556a2a2..f436cdde8 100644 --- a/src/org/thoughtcrime/securesms/jobs/LocalBackupJob.java +++ b/src/org/thoughtcrime/securesms/jobs/LocalBackupJob.java @@ -52,7 +52,7 @@ public class LocalBackupJob extends ContextJob { try { 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 fileName = String.format("signal-%s.backup", timestamp); File backupFile = new File(backupDirectory, fileName); diff --git a/src/org/thoughtcrime/securesms/util/StorageUtil.java b/src/org/thoughtcrime/securesms/util/StorageUtil.java index 6a7b160f9..2bcd34f0f 100644 --- a/src/org/thoughtcrime/securesms/util/StorageUtil.java +++ b/src/org/thoughtcrime/securesms/util/StorageUtil.java @@ -22,12 +22,7 @@ public class StorageUtil File[] directories = context.getExternalFilesDirs(null); if (directories != null) { - storage = Stream.of(directories) - .withoutNulls() - .filterNot(f -> f.getAbsolutePath().contains("emulated")) - .limit(1) - .findSingle() - .orElse(null); + storage = getNonEmulated(directories); } } @@ -52,6 +47,28 @@ public class StorageUtil 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 { final File storage = Environment.getExternalStorageDirectory();