fix: Synchronized
This commit is contained in:
parent
7b36cd0d29
commit
43e560a893
1 changed files with 18 additions and 7 deletions
|
@ -13,7 +13,6 @@ import ani.dantotsu.snackString
|
||||||
import ani.dantotsu.util.Logger
|
import ani.dantotsu.util.Logger
|
||||||
import com.anggrayudi.storage.callback.FolderCallback
|
import com.anggrayudi.storage.callback.FolderCallback
|
||||||
import com.anggrayudi.storage.file.deleteRecursively
|
import com.anggrayudi.storage.file.deleteRecursively
|
||||||
import com.anggrayudi.storage.file.findFolder
|
|
||||||
import com.anggrayudi.storage.file.moveFolderTo
|
import com.anggrayudi.storage.file.moveFolderTo
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import com.google.gson.reflect.TypeToken
|
import com.google.gson.reflect.TypeToken
|
||||||
|
@ -279,6 +278,7 @@ class DownloadsManager(private val context: Context) {
|
||||||
* @param type the type of media
|
* @param type the type of media
|
||||||
* @return the base directory
|
* @return the base directory
|
||||||
*/
|
*/
|
||||||
|
@Synchronized
|
||||||
private fun getBaseDirectory(context: Context, type: MediaType): DocumentFile? {
|
private fun getBaseDirectory(context: Context, type: MediaType): DocumentFile? {
|
||||||
val baseDirectory = Uri.parse(PrefManager.getVal<String>(PrefName.DownloadsDir))
|
val baseDirectory = Uri.parse(PrefManager.getVal<String>(PrefName.DownloadsDir))
|
||||||
if (baseDirectory == Uri.EMPTY) return null
|
if (baseDirectory == Uri.EMPTY) return null
|
||||||
|
@ -307,6 +307,7 @@ class DownloadsManager(private val context: Context) {
|
||||||
* @param chapter the chapter of the media
|
* @param chapter the chapter of the media
|
||||||
* @return the subdirectory
|
* @return the subdirectory
|
||||||
*/
|
*/
|
||||||
|
@Synchronized
|
||||||
fun getSubDirectory(
|
fun getSubDirectory(
|
||||||
context: Context,
|
context: Context,
|
||||||
type: MediaType,
|
type: MediaType,
|
||||||
|
@ -344,22 +345,32 @@ class DownloadsManager(private val context: Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
private fun getBaseDirectory(context: Context): DocumentFile? {
|
private fun getBaseDirectory(context: Context): DocumentFile? {
|
||||||
val baseDirectory = Uri.parse(PrefManager.getVal<String>(PrefName.DownloadsDir))
|
val baseDirectory = Uri.parse(PrefManager.getVal<String>(PrefName.DownloadsDir))
|
||||||
if (baseDirectory == Uri.EMPTY) return null
|
if (baseDirectory == Uri.EMPTY) return null
|
||||||
return DocumentFile.fromTreeUri(context, baseDirectory)
|
return DocumentFile.fromTreeUri(context, baseDirectory)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val lock = Any()
|
||||||
|
|
||||||
private fun DocumentFile.findOrCreateFolder(
|
private fun DocumentFile.findOrCreateFolder(
|
||||||
name: String, overwrite: Boolean
|
name: String, overwrite: Boolean
|
||||||
): DocumentFile? {
|
): DocumentFile? {
|
||||||
|
val validName = name.findValidName()
|
||||||
|
synchronized(lock) {
|
||||||
return if (overwrite) {
|
return if (overwrite) {
|
||||||
findFolder(name.findValidName())?.delete()
|
findFolder(validName)?.delete()
|
||||||
createDirectory(name.findValidName())
|
createDirectory(validName)
|
||||||
} else {
|
} else {
|
||||||
findFolder(name.findValidName()) ?: createDirectory(name.findValidName())
|
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
|
private const val RATIO_THRESHOLD = 95
|
||||||
fun Media.compareName(name: String): Boolean {
|
fun Media.compareName(name: String): Boolean {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue