diff --git a/src/org/thoughtcrime/securesms/RegistrationActivity.java b/src/org/thoughtcrime/securesms/RegistrationActivity.java index 797e34bb6d..3076ad182e 100644 --- a/src/org/thoughtcrime/securesms/RegistrationActivity.java +++ b/src/org/thoughtcrime/securesms/RegistrationActivity.java @@ -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; diff --git a/src/org/thoughtcrime/securesms/backup/BackupDialog.java b/src/org/thoughtcrime/securesms/backup/BackupDialog.java index 364329049f..4139e74add 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(); + BackupUtil.deleteAllBackups(context); preference.setChecked(false); }) .create() diff --git a/src/org/thoughtcrime/securesms/jobs/LocalBackupJob.java b/src/org/thoughtcrime/securesms/jobs/LocalBackupJob.java index 6fcf5a5763..f9815e1053 100644 --- a/src/org/thoughtcrime/securesms/jobs/LocalBackupJob.java +++ b/src/org/thoughtcrime/securesms/jobs/LocalBackupJob.java @@ -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); } diff --git a/src/org/thoughtcrime/securesms/util/BackupUtil.java b/src/org/thoughtcrime/securesms/util/BackupUtil.java index 9c39f5f06c..106f8a8013 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(); + 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) { diff --git a/src/org/thoughtcrime/securesms/util/StorageUtil.java b/src/org/thoughtcrime/securesms/util/StorageUtil.java index 4b1e2855ab..6a7b160f9b 100644 --- a/src/org/thoughtcrime/securesms/util/StorageUtil.java +++ b/src/org/thoughtcrime/securesms/util/StorageUtil.java @@ -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();