mirror of
https://github.com/oxen-io/session-android.git
synced 2025-04-02 16:32:13 +00:00
Make an attempt to store/retrieve backups from removable storage
Fixes #7521
This commit is contained in:
parent
7dd8baba5a
commit
3f3d7f549b
@ -314,7 +314,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();
|
return BackupUtil.getLatestBackup(RegistrationActivity.this);
|
||||||
} catch (NoExternalStorageException e) {
|
} catch (NoExternalStorageException e) {
|
||||||
Log.w(TAG, e);
|
Log.w(TAG, e);
|
||||||
return null;
|
return null;
|
||||||
|
@ -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();
|
BackupUtil.deleteAllBackups(context);
|
||||||
preference.setChecked(false);
|
preference.setChecked(false);
|
||||||
})
|
})
|
||||||
.create()
|
.create()
|
||||||
|
@ -50,7 +50,7 @@ public class LocalBackupJob extends ContextJob {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
String backupPassword = TextSecurePreferences.getBackupPassphrase(context);
|
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 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);
|
||||||
@ -76,7 +76,7 @@ public class LocalBackupJob extends ContextJob {
|
|||||||
throw new IOException("Renaming temporary backup file failed!");
|
throw new IOException("Renaming temporary backup file failed!");
|
||||||
}
|
}
|
||||||
|
|
||||||
BackupUtil.deleteOldBackups();
|
BackupUtil.deleteOldBackups(context);
|
||||||
} finally {
|
} finally {
|
||||||
GenericForegroundService.stopForegroundTask(context);
|
GenericForegroundService.stopForegroundTask(context);
|
||||||
}
|
}
|
||||||
|
@ -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();
|
BackupInfo backup = getLatestBackup(context);
|
||||||
|
|
||||||
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() throws NoExternalStorageException {
|
public static @Nullable BackupInfo getLatestBackup(@NonNull Context context) throws NoExternalStorageException {
|
||||||
File backupDirectory = StorageUtil.getBackupDirectory();
|
File backupDirectory = StorageUtil.getBackupDirectory(context);
|
||||||
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() {
|
public static void deleteAllBackups(@NonNull Context context) {
|
||||||
try {
|
try {
|
||||||
File backupDirectory = StorageUtil.getBackupDirectory();
|
File backupDirectory = StorageUtil.getBackupDirectory(context);
|
||||||
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() {
|
public static void deleteOldBackups(@NonNull Context context) {
|
||||||
try {
|
try {
|
||||||
File backupDirectory = StorageUtil.getBackupDirectory();
|
File backupDirectory = StorageUtil.getBackupDirectory(context);
|
||||||
File[] backups = backupDirectory.listFiles();
|
File[] backups = backupDirectory.listFiles();
|
||||||
|
|
||||||
if (backups != null && backups.length > 5) {
|
if (backups != null && backups.length > 5) {
|
||||||
|
@ -1,8 +1,13 @@
|
|||||||
package org.thoughtcrime.securesms.util;
|
package org.thoughtcrime.securesms.util;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
|
import com.annimon.stream.Objects;
|
||||||
|
import com.annimon.stream.Stream;
|
||||||
|
|
||||||
import org.thoughtcrime.securesms.database.NoExternalStorageException;
|
import org.thoughtcrime.securesms.database.NoExternalStorageException;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -10,8 +15,25 @@ import java.io.File;
|
|||||||
public class StorageUtil
|
public class StorageUtil
|
||||||
{
|
{
|
||||||
|
|
||||||
public static File getBackupDirectory() throws NoExternalStorageException {
|
public static File getBackupDirectory(Context context) throws NoExternalStorageException {
|
||||||
File storage = Environment.getExternalStorageDirectory();
|
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()) {
|
if (!storage.canWrite()) {
|
||||||
throw new NoExternalStorageException();
|
throw new NoExternalStorageException();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user