From 43e560a893ffe18a9746f8ed4485807d89e9b952 Mon Sep 17 00:00:00 2001 From: rebelonion <87634197+rebelonion@users.noreply.github.com> Date: Mon, 27 May 2024 05:11:39 -0500 Subject: [PATCH] fix: Synchronized --- .../ani/dantotsu/download/DownloadsManager.kt | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/ani/dantotsu/download/DownloadsManager.kt b/app/src/main/java/ani/dantotsu/download/DownloadsManager.kt index 3b44e66d..11b6ad4c 100644 --- a/app/src/main/java/ani/dantotsu/download/DownloadsManager.kt +++ b/app/src/main/java/ani/dantotsu/download/DownloadsManager.kt @@ -13,7 +13,6 @@ import ani.dantotsu.snackString import ani.dantotsu.util.Logger import com.anggrayudi.storage.callback.FolderCallback import com.anggrayudi.storage.file.deleteRecursively -import com.anggrayudi.storage.file.findFolder import com.anggrayudi.storage.file.moveFolderTo import com.google.gson.Gson import com.google.gson.reflect.TypeToken @@ -279,6 +278,7 @@ class DownloadsManager(private val context: Context) { * @param type the type of media * @return the base directory */ + @Synchronized private fun getBaseDirectory(context: Context, type: MediaType): DocumentFile? { val baseDirectory = Uri.parse(PrefManager.getVal(PrefName.DownloadsDir)) if (baseDirectory == Uri.EMPTY) return null @@ -307,6 +307,7 @@ class DownloadsManager(private val context: Context) { * @param chapter the chapter of the media * @return the subdirectory */ + @Synchronized fun getSubDirectory( context: Context, type: MediaType, @@ -344,23 +345,33 @@ class DownloadsManager(private val context: Context) { } } + @Synchronized private fun getBaseDirectory(context: Context): DocumentFile? { val baseDirectory = Uri.parse(PrefManager.getVal(PrefName.DownloadsDir)) if (baseDirectory == Uri.EMPTY) return null return DocumentFile.fromTreeUri(context, baseDirectory) } + private val lock = Any() + private fun DocumentFile.findOrCreateFolder( name: String, overwrite: Boolean ): DocumentFile? { - return if (overwrite) { - findFolder(name.findValidName())?.delete() - createDirectory(name.findValidName()) - } else { - findFolder(name.findValidName()) ?: createDirectory(name.findValidName()) + val validName = name.findValidName() + synchronized(lock) { + return if (overwrite) { + findFolder(validName)?.delete() + createDirectory(validName) + } else { + val folder = findFolder(validName) + return folder ?: createDirectory(validName) + } } } + private fun DocumentFile.findFolder(name: String): DocumentFile? = + listFiles().find { it.name == name && it.isDirectory } + private const val RATIO_THRESHOLD = 95 fun Media.compareName(name: String): Boolean { val mainName = mainName().findValidName().lowercase() @@ -379,7 +390,7 @@ class DownloadsManager(private val context: Context) { private const val RESERVED_CHARS = "|\\?*<\":>+[]/'" fun String?.findValidName(): String { - return this?.replace("/","_")?.filterNot { RESERVED_CHARS.contains(it) } ?: "" + return this?.replace("/", "_")?.filterNot { RESERVED_CHARS.contains(it) } ?: "" } data class DownloadedType(