From c9e686551f1558ae1c8ac87905260958cadd2ad3 Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Thu, 3 Sep 2020 10:27:00 +1000 Subject: [PATCH] Fix file storage --- .../securesms/RegistrationActivity.java | 2 +- .../securesms/backup/BackupDialog.java | 10 +++--- .../securesms/jobs/LocalBackupJob.java | 4 +-- .../securesms/util/BackupUtil.java | 20 ++++++------ .../securesms/util/SaveAttachmentTask.java | 21 ++++++------ .../securesms/util/StorageUtil.java | 32 +++++++++---------- 6 files changed, 48 insertions(+), 41 deletions(-) diff --git a/src/org/thoughtcrime/securesms/RegistrationActivity.java b/src/org/thoughtcrime/securesms/RegistrationActivity.java index 29ca7d34d9..b43c28b852 100644 --- a/src/org/thoughtcrime/securesms/RegistrationActivity.java +++ b/src/org/thoughtcrime/securesms/RegistrationActivity.java @@ -278,7 +278,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 11ca52a688..34eeed48ca 100644 --- a/src/org/thoughtcrime/securesms/backup/BackupDialog.java +++ b/src/org/thoughtcrime/securesms/backup/BackupDialog.java @@ -4,20 +4,22 @@ package org.thoughtcrime.securesms.backup; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; -import androidx.annotation.NonNull; -import androidx.appcompat.app.AlertDialog; import android.widget.Button; import android.widget.CheckBox; import android.widget.TextView; import android.widget.Toast; -import network.loki.messenger.R; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; + import org.thoughtcrime.securesms.components.SwitchPreferenceCompat; import org.thoughtcrime.securesms.service.LocalBackupListener; import org.thoughtcrime.securesms.util.BackupUtil; import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.Util; +import network.loki.messenger.R; + public class BackupDialog { public static void showEnableBackupDialog(@NonNull Context context, @NonNull SwitchPreferenceCompat preference) { @@ -77,7 +79,7 @@ public class BackupDialog { .setPositiveButton(R.string.BackupDialog_delete_backups_statement, (dialog, which) -> { BackupPassphrase.set(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 dc0d8b1f4b..2d935c10fa 100644 --- a/src/org/thoughtcrime/securesms/jobs/LocalBackupJob.java +++ b/src/org/thoughtcrime/securesms/jobs/LocalBackupJob.java @@ -71,7 +71,7 @@ public class LocalBackupJob extends BaseJob { try { String backupPassword = BackupPassphrase.get(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("session-%s.backup", timestamp); File backupFile = new File(backupDirectory, fileName); @@ -97,7 +97,7 @@ public class LocalBackupJob extends BaseJob { 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 d3b4f4a112..21a983d31e 100644 --- a/src/org/thoughtcrime/securesms/util/BackupUtil.java +++ b/src/org/thoughtcrime/securesms/util/BackupUtil.java @@ -2,12 +2,12 @@ package org.thoughtcrime.securesms.util; import android.content.Context; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import org.thoughtcrime.securesms.logging.Log; -import network.loki.messenger.R; import org.thoughtcrime.securesms.database.NoExternalStorageException; +import org.thoughtcrime.securesms.logging.Log; import org.whispersystems.libsignal.util.ByteUtil; import java.io.File; @@ -16,13 +16,15 @@ import java.util.Arrays; import java.util.Calendar; import java.util.Locale; +import network.loki.messenger.R; + public class BackupUtil { private static final String TAG = BackupUtil.class.getSimpleName(); 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 +34,8 @@ public class BackupUtil { } } - public static @Nullable BackupInfo getLatestBackup() throws NoExternalStorageException { - File backupDirectory = StorageUtil.getBackupDirectory(); + public static @Nullable BackupInfo getLatestBackup(Context context) throws NoExternalStorageException { + File backupDirectory = StorageUtil.getBackupDirectory(context); File[] backups = backupDirectory.listFiles(); BackupInfo latestBackup = null; @@ -49,9 +51,9 @@ public class BackupUtil { } @SuppressWarnings("ResultOfMethodCallIgnored") - public static void deleteAllBackups() { + public static void deleteAllBackups(Context context) { try { - File backupDirectory = StorageUtil.getBackupDirectory(); + File backupDirectory = StorageUtil.getBackupDirectory(context); File[] backups = backupDirectory.listFiles(); for (File backup : backups) { @@ -62,9 +64,9 @@ public class BackupUtil { } } - public static void deleteOldBackups() { + public static void deleteOldBackups(Context context) { try { - File backupDirectory = StorageUtil.getBackupDirectory(); + File backupDirectory = StorageUtil.getBackupDirectory(context); File[] backups = backupDirectory.listFiles(); if (backups != null && backups.length > 2) { diff --git a/src/org/thoughtcrime/securesms/util/SaveAttachmentTask.java b/src/org/thoughtcrime/securesms/util/SaveAttachmentTask.java index f1afec8de8..86a2bc0d7b 100644 --- a/src/org/thoughtcrime/securesms/util/SaveAttachmentTask.java +++ b/src/org/thoughtcrime/securesms/util/SaveAttachmentTask.java @@ -4,14 +4,14 @@ import android.content.Context; import android.content.DialogInterface.OnClickListener; import android.media.MediaScannerConnection; import android.net.Uri; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.appcompat.app.AlertDialog; import android.text.TextUtils; import android.webkit.MimeTypeMap; import android.widget.Toast; -import network.loki.messenger.R; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; + import org.thoughtcrime.securesms.database.NoExternalStorageException; import org.thoughtcrime.securesms.logging.Log; import org.thoughtcrime.securesms.mms.PartAuthority; @@ -26,6 +26,8 @@ import java.io.OutputStream; import java.lang.ref.WeakReference; import java.text.SimpleDateFormat; +import network.loki.messenger.R; + public class SaveAttachmentTask extends ProgressDialogAsyncTask> { private static final String TAG = SaveAttachmentTask.class.getSimpleName(); @@ -59,7 +61,7 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask(WRITE_ACCESS_FAILURE, null); } @@ -111,16 +113,17 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask