From 1fbf8ca0790bcf2177e6f8850b51b9fb43c66d6e Mon Sep 17 00:00:00 2001 From: arkon Date: Thu, 26 Oct 2023 13:43:42 -0400 Subject: [PATCH] Use unified storage location for automatic backups --- .../tachiyomi/data/backup/BackupCreateJob.kt | 13 +++++++++- .../data/download/DownloadProvider.kt | 1 - .../kanade/tachiyomi/di/PreferenceModule.kt | 9 +------ .../provider/AndroidBackupFolderProvider.kt | 25 ------------------- .../backup/service/BackupPreferences.kt | 4 --- .../storage/service/StoragePreferences.kt | 1 + .../commonMain/resources/MR/base/strings.xml | 1 - 7 files changed, 14 insertions(+), 40 deletions(-) delete mode 100644 core/src/main/java/tachiyomi/core/provider/AndroidBackupFolderProvider.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreateJob.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreateJob.kt index 4226e77fca..c28d12f263 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreateJob.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreateJob.kt @@ -21,6 +21,7 @@ import eu.kanade.tachiyomi.util.system.workManager import logcat.LogPriority import tachiyomi.core.util.system.logcat import tachiyomi.domain.backup.service.BackupPreferences +import tachiyomi.domain.storage.service.StoragePreferences import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get import java.util.Date @@ -39,8 +40,9 @@ class BackupCreateJob(private val context: Context, workerParams: WorkerParamete if (isAutoBackup && BackupRestoreJob.isRunning(context)) return Result.retry() val backupPreferences = Injekt.get() + val uri = inputData.getString(LOCATION_URI_KEY)?.toUri() - ?: backupPreferences.backupsDirectory().get().toUri() + ?: getAutomaticBackupLocation() val flags = inputData.getInt(BACKUP_FLAGS_KEY, BackupCreateFlags.AutomaticDefaults) try { @@ -73,6 +75,15 @@ class BackupCreateJob(private val context: Context, workerParams: WorkerParamete ) } + private fun getAutomaticBackupLocation(): Uri { + val storagePreferences = Injekt.get() + return storagePreferences.baseStorageDirectory().get().let { + val dir = UniFile.fromUri(context, it.toUri()) + .createDirectory(StoragePreferences.BACKUP_DIR) + dir.uri + } + } + companion object { fun isManualJobRunning(context: Context): Boolean { return context.workManager.isRunning(TAG_MANUAL) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt index cbb4d9dd35..eb444398ac 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt @@ -47,7 +47,6 @@ class DownloadProvider( val dir = UniFile.fromUri(context, it.toUri()) .createDirectory(StoragePreferences.DOWNLOADS_DIR) DiskUtil.createNoMediaFile(dir, context) - logcat { "downloadsDir: ${dir.filePath}" } dir } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/di/PreferenceModule.kt b/app/src/main/java/eu/kanade/tachiyomi/di/PreferenceModule.kt index 48dfc91a95..91c128ae80 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/di/PreferenceModule.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/di/PreferenceModule.kt @@ -11,7 +11,6 @@ import eu.kanade.tachiyomi.ui.reader.setting.ReaderPreferences import eu.kanade.tachiyomi.util.system.isDevFlavor import tachiyomi.core.preference.AndroidPreferenceStore import tachiyomi.core.preference.PreferenceStore -import tachiyomi.core.provider.AndroidBackupFolderProvider import tachiyomi.core.provider.AndroidStorageFolderProvider import tachiyomi.domain.backup.service.BackupPreferences import tachiyomi.domain.download.service.DownloadPreferences @@ -53,13 +52,7 @@ class PreferenceModule(val app: Application) : InjektModule { DownloadPreferences(get()) } addSingletonFactory { - AndroidBackupFolderProvider(app) - } - addSingletonFactory { - BackupPreferences( - folderProvider = get(), - preferenceStore = get(), - ) + BackupPreferences(get()) } addSingletonFactory { AndroidStorageFolderProvider(app) diff --git a/core/src/main/java/tachiyomi/core/provider/AndroidBackupFolderProvider.kt b/core/src/main/java/tachiyomi/core/provider/AndroidBackupFolderProvider.kt deleted file mode 100644 index afed0d95c7..0000000000 --- a/core/src/main/java/tachiyomi/core/provider/AndroidBackupFolderProvider.kt +++ /dev/null @@ -1,25 +0,0 @@ -package tachiyomi.core.provider - -import android.content.Context -import android.os.Environment -import androidx.core.net.toUri -import tachiyomi.core.i18n.stringResource -import tachiyomi.i18n.MR -import java.io.File - -class AndroidBackupFolderProvider( - private val context: Context, -) : FolderProvider { - - override fun directory(): File { - return File( - Environment.getExternalStorageDirectory().absolutePath + File.separator + - context.stringResource(MR.strings.app_name), - "backup", - ) - } - - override fun path(): String { - return directory().toUri().toString() - } -} diff --git a/domain/src/main/java/tachiyomi/domain/backup/service/BackupPreferences.kt b/domain/src/main/java/tachiyomi/domain/backup/service/BackupPreferences.kt index 6144709de7..ba00faa184 100644 --- a/domain/src/main/java/tachiyomi/domain/backup/service/BackupPreferences.kt +++ b/domain/src/main/java/tachiyomi/domain/backup/service/BackupPreferences.kt @@ -2,15 +2,11 @@ package tachiyomi.domain.backup.service import tachiyomi.core.preference.Preference import tachiyomi.core.preference.PreferenceStore -import tachiyomi.core.provider.FolderProvider class BackupPreferences( - private val folderProvider: FolderProvider, private val preferenceStore: PreferenceStore, ) { - fun backupsDirectory() = preferenceStore.getString("backup_directory", folderProvider.path()) - fun backupInterval() = preferenceStore.getInt("backup_interval", 12) fun lastAutoBackupTimestamp() = preferenceStore.getLong(Preference.appStateKey("last_auto_backup_timestamp"), 0L) diff --git a/domain/src/main/java/tachiyomi/domain/storage/service/StoragePreferences.kt b/domain/src/main/java/tachiyomi/domain/storage/service/StoragePreferences.kt index 1746a37238..c336825b82 100644 --- a/domain/src/main/java/tachiyomi/domain/storage/service/StoragePreferences.kt +++ b/domain/src/main/java/tachiyomi/domain/storage/service/StoragePreferences.kt @@ -11,6 +11,7 @@ class StoragePreferences( fun baseStorageDirectory() = preferenceStore.getString("storage_dir", folderProvider.path()) companion object { + const val BACKUP_DIR = "backup" const val DOWNLOADS_DIR = "downloads" } } diff --git a/i18n/src/commonMain/resources/MR/base/strings.xml b/i18n/src/commonMain/resources/MR/base/strings.xml index 1074a85572..9428945667 100644 --- a/i18n/src/commonMain/resources/MR/base/strings.xml +++ b/i18n/src/commonMain/resources/MR/base/strings.xml @@ -469,7 +469,6 @@ Can be used to restore current library Restore backup Restore library from backup file - Backup location Automatic backup frequency Create Backup created