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
This commit is contained in:
Greyson Parrelli 2018-06-28 17:38:59 -07:00
parent 290b184491
commit 18756aedf6
5 changed files with 13 additions and 43 deletions

View File

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

View File

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

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.getBackupDirectory();
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);
@ -78,7 +78,7 @@ public class LocalBackupJob extends ContextJob {
throw new IOException("Renaming temporary backup file failed!"); throw new IOException("Renaming temporary backup file failed!");
} }
BackupUtil.deleteOldBackups(context); BackupUtil.deleteOldBackups();
} finally { } finally {
GenericForegroundService.stopForegroundTask(context); GenericForegroundService.stopForegroundTask(context);
} }

View File

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

View File

@ -14,20 +14,8 @@ import java.io.File;
public class StorageUtil { public class StorageUtil {
public static File getBackupDirectory(Context context) throws NoExternalStorageException { public static File getBackupDirectory() throws NoExternalStorageException {
File storage = null; File storage = Environment.getExternalStorageDirectory();
if (Build.VERSION.SDK_INT >= 19) {
File[] directories = context.getExternalFilesDirs(null);
if (directories != null) {
storage = getNonEmulated(directories);
}
}
if (storage == null) {
storage = Environment.getExternalStorageDirectory();
}
if (!storage.canWrite()) { if (!storage.canWrite()) {
throw new NoExternalStorageException(); throw new NoExternalStorageException();
@ -46,27 +34,9 @@ public class StorageUtil {
} }
public static File getBackupCacheDirectory(Context context) { 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(); 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();