From 18756aedf6c5bec1e8abd3789710cc5d59d7e090 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Thu, 28 Jun 2018 17:38:59 -0700 Subject: [PATCH] Remove the ability to save backups to the external SD card. The directory we were previously saving backups to on the external SD card is actually deleted upon app uninstall and/or clearing the app's data. There's also no reliable way to write to the root of an external SD card (that isn't comically inconvenient), so for now it's safer if we just move back to getting the regular 'ol standard external storage directory (which is likely internal storage, despite its name). Fixes #7845 --- .../securesms/RegistrationActivity.java | 2 +- .../securesms/backup/BackupDialog.java | 2 +- .../securesms/jobs/LocalBackupJob.java | 4 +-- .../securesms/util/BackupUtil.java | 14 ++++---- .../securesms/util/StorageUtil.java | 34 ++----------------- 5 files changed, 13 insertions(+), 43 deletions(-) diff --git a/src/org/thoughtcrime/securesms/RegistrationActivity.java b/src/org/thoughtcrime/securesms/RegistrationActivity.java index 21538ee090..61736b0d21 100644 --- a/src/org/thoughtcrime/securesms/RegistrationActivity.java +++ b/src/org/thoughtcrime/securesms/RegistrationActivity.java @@ -318,7 +318,7 @@ public class RegistrationActivity extends BaseActionBarActivity implements Verif @Override protected @Nullable BackupUtil.BackupInfo doInBackground(Void... voids) { try { - return BackupUtil.getLatestBackup(RegistrationActivity.this); + return BackupUtil.getLatestBackup(); } catch (NoExternalStorageException e) { Log.w(TAG, e); return null; diff --git a/src/org/thoughtcrime/securesms/backup/BackupDialog.java b/src/org/thoughtcrime/securesms/backup/BackupDialog.java index 4139e74add..364329049f 100644 --- a/src/org/thoughtcrime/securesms/backup/BackupDialog.java +++ b/src/org/thoughtcrime/securesms/backup/BackupDialog.java @@ -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(context); + BackupUtil.deleteAllBackups(); preference.setChecked(false); }) .create() diff --git a/src/org/thoughtcrime/securesms/jobs/LocalBackupJob.java b/src/org/thoughtcrime/securesms/jobs/LocalBackupJob.java index 11c921f54b..b67b23fc01 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.getBackupDirectory(); 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); @@ -78,7 +78,7 @@ public class LocalBackupJob extends ContextJob { throw new IOException("Renaming temporary backup file failed!"); } - BackupUtil.deleteOldBackups(context); + BackupUtil.deleteOldBackups(); } finally { GenericForegroundService.stopForegroundTask(context); } diff --git a/src/org/thoughtcrime/securesms/util/BackupUtil.java b/src/org/thoughtcrime/securesms/util/BackupUtil.java index eb887aba92..e21ff81a21 100644 --- a/src/org/thoughtcrime/securesms/util/BackupUtil.java +++ b/src/org/thoughtcrime/securesms/util/BackupUtil.java @@ -22,7 +22,7 @@ public class BackupUtil { public static @NonNull String getLastBackupTime(@NonNull Context context, @NonNull Locale locale) { try { - BackupInfo backup = getLatestBackup(context); + BackupInfo backup = getLatestBackup(); 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(@NonNull Context context) throws NoExternalStorageException { - File backupDirectory = StorageUtil.getBackupDirectory(context); + public static @Nullable BackupInfo getLatestBackup() throws NoExternalStorageException { + File backupDirectory = StorageUtil.getBackupDirectory(); File[] backups = backupDirectory.listFiles(); BackupInfo latestBackup = null; @@ -49,9 +49,9 @@ public class BackupUtil { } @SuppressWarnings("ResultOfMethodCallIgnored") - public static void deleteAllBackups(@NonNull Context context) { + public static void deleteAllBackups() { try { - File backupDirectory = StorageUtil.getBackupDirectory(context); + File backupDirectory = StorageUtil.getBackupDirectory(); File[] backups = backupDirectory.listFiles(); for (File backup : backups) { @@ -62,9 +62,9 @@ public class BackupUtil { } } - public static void deleteOldBackups(@NonNull Context context) { + public static void deleteOldBackups() { try { - File backupDirectory = StorageUtil.getBackupDirectory(context); + File backupDirectory = StorageUtil.getBackupDirectory(); File[] backups = backupDirectory.listFiles(); if (backups != null && backups.length > 2) { diff --git a/src/org/thoughtcrime/securesms/util/StorageUtil.java b/src/org/thoughtcrime/securesms/util/StorageUtil.java index 3950c7a5aa..1b7c834f22 100644 --- a/src/org/thoughtcrime/securesms/util/StorageUtil.java +++ b/src/org/thoughtcrime/securesms/util/StorageUtil.java @@ -14,20 +14,8 @@ import java.io.File; public class StorageUtil { - 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 = getNonEmulated(directories); - } - } - - if (storage == null) { - storage = Environment.getExternalStorageDirectory(); - } + public static File getBackupDirectory() throws NoExternalStorageException { + File storage = Environment.getExternalStorageDirectory(); if (!storage.canWrite()) { throw new NoExternalStorageException(); @@ -46,27 +34,9 @@ public class StorageUtil { } 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();