From 5444758f300af3d478ce22eec8c5c7fba322e32e Mon Sep 17 00:00:00 2001 From: Horis <821938089@qq.com> Date: Mon, 26 Sep 2022 13:19:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/help/coroutine/OrderCoroutine.kt | 18 ++++++------------ .../app/ui/book/group/GroupEditDialog.kt | 6 +++++- .../ui/book/info/edit/BookInfoEditActivity.kt | 6 +++++- .../app/ui/config/CoverConfigFragment.kt | 6 +++++- .../app/ui/config/ThemeConfigFragment.kt | 6 +++++- .../app/ui/config/WelcomeConfigFragment.kt | 6 +++++- .../java/io/legado/app/utils/UriExtensions.kt | 3 +++ 7 files changed, 34 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/coroutine/OrderCoroutine.kt b/app/src/main/java/io/legado/app/help/coroutine/OrderCoroutine.kt index 3b0989588..315da7c34 100644 --- a/app/src/main/java/io/legado/app/help/coroutine/OrderCoroutine.kt +++ b/app/src/main/java/io/legado/app/help/coroutine/OrderCoroutine.kt @@ -2,30 +2,24 @@ package io.legado.app.help.coroutine import kotlinx.coroutines.* import kotlinx.coroutines.Dispatchers.IO -import kotlinx.coroutines.sync.Mutex -import kotlinx.coroutines.sync.withLock import java.util.concurrent.ConcurrentHashMap -import java.util.concurrent.ConcurrentLinkedQueue import java.util.concurrent.PriorityBlockingQueue +import java.util.concurrent.atomic.AtomicInteger class OrderCoroutine(val threadCount: Int) { - private val taskList = ConcurrentLinkedQueue T>() + private val taskList = ArrayList T>() private val taskResultMap = ConcurrentHashMap() private val finishTaskIndex = PriorityBlockingQueue() - private val mutex = Mutex() private suspend fun start() = coroutineScope { - var taskIndex = 0 + val taskIndex = AtomicInteger(0) + val tasks = taskList.toList() for (i in 1..threadCount) { launch { while (true) { ensureActive() - val task: suspend CoroutineScope.() -> T - val curIndex: Int - mutex.withLock { - task = taskList.poll() ?: return@launch - curIndex = taskIndex++ - } + val curIndex = taskIndex.getAndIncrement() + val task = tasks.getOrNull(curIndex) ?: return@launch taskResultMap[curIndex] = task.invoke(this) finishTaskIndex.add(curIndex) } diff --git a/app/src/main/java/io/legado/app/ui/book/group/GroupEditDialog.kt b/app/src/main/java/io/legado/app/ui/book/group/GroupEditDialog.kt index 068116e60..c50b73bfc 100644 --- a/app/src/main/java/io/legado/app/ui/book/group/GroupEditDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/group/GroupEditDialog.kt @@ -29,7 +29,11 @@ class GroupEditDialog() : BaseDialogFragment(R.layout.dialog_book_group_edit) { val selectImage = registerForActivityResult(SelectImageContract()) { readUri(it?.uri) { fileDoc, inputStream -> var file = requireContext().externalFiles - file = FileUtils.createFileIfNotExist(file, "covers", fileDoc.name) + val suffix = fileDoc.name.substringAfterLast(".") + val fileName = it.uri!!.inputStream(requireContext())!!.use { + MD5Utils.md5Encode(it) + ".$suffix" + } + file = FileUtils.createFileIfNotExist(file, "covers", fileName) FileOutputStream(file).use { outputStream -> inputStream.copyTo(outputStream) } diff --git a/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt index ad31c4bcc..c48954998 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt @@ -118,7 +118,11 @@ class BookInfoEditActivity : readUri(uri) { fileDoc, inputStream -> inputStream.use { var file = this.externalFiles - file = FileUtils.createFileIfNotExist(file, "covers", fileDoc.name) + val suffix = fileDoc.name.substringAfterLast(".") + val fileName = uri.inputStream(this)!!.use { + MD5Utils.md5Encode(it) + ".$suffix" + } + file = FileUtils.createFileIfNotExist(file, "covers", fileName) FileOutputStream(file).use { outputStream -> inputStream.copyTo(outputStream) } diff --git a/app/src/main/java/io/legado/app/ui/config/CoverConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/CoverConfigFragment.kt index 6931ed155..1866b5edb 100644 --- a/app/src/main/java/io/legado/app/ui/config/CoverConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/CoverConfigFragment.kt @@ -140,7 +140,11 @@ class CoverConfigFragment : PreferenceFragment(), private fun setCoverFromUri(preferenceKey: String, uri: Uri) { readUri(uri) { fileDoc, inputStream -> var file = requireContext().externalFiles - file = FileUtils.createFileIfNotExist(file, "covers", fileDoc.name) + val suffix = fileDoc.name.substringAfterLast(".") + val fileName = uri.inputStream(requireContext())!!.use { + MD5Utils.md5Encode(it) + ".$suffix" + } + file = FileUtils.createFileIfNotExist(file, "covers", fileName) FileOutputStream(file).use { inputStream.copyTo(it) } diff --git a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt index 5863fa600..a38f71d5c 100644 --- a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt @@ -304,7 +304,11 @@ class ThemeConfigFragment : PreferenceFragment(), private fun setBgFromUri(uri: Uri, preferenceKey: String, success: () -> Unit) { readUri(uri) { fileDoc, inputStream -> var file = requireContext().externalFiles - file = FileUtils.createFileIfNotExist(file, preferenceKey, fileDoc.name) + val suffix = fileDoc.name.substringAfterLast(".") + val fileName = uri.inputStream(requireContext())!!.use { + MD5Utils.md5Encode(it) + ".$suffix" + } + file = FileUtils.createFileIfNotExist(file, preferenceKey, fileName) FileOutputStream(file).use { inputStream.copyTo(it) } diff --git a/app/src/main/java/io/legado/app/ui/config/WelcomeConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/WelcomeConfigFragment.kt index 33b1b1689..680890c9b 100644 --- a/app/src/main/java/io/legado/app/ui/config/WelcomeConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/WelcomeConfigFragment.kt @@ -120,7 +120,11 @@ class WelcomeConfigFragment : PreferenceFragment(), private fun setCoverFromUri(preferenceKey: String, uri: Uri) { readUri(uri) { fileDoc, inputStream -> var file = requireContext().externalFiles - file = FileUtils.createFileIfNotExist(file, "covers", fileDoc.name) + val suffix = fileDoc.name.substringAfterLast(".") + val fileName = uri.inputStream(requireContext())!!.use { + MD5Utils.md5Encode(it) + ".$suffix" + } + file = FileUtils.createFileIfNotExist(file, "covers", fileName) FileOutputStream(file).use { inputStream.copyTo(it) } diff --git a/app/src/main/java/io/legado/app/utils/UriExtensions.kt b/app/src/main/java/io/legado/app/utils/UriExtensions.kt index db2d62f79..aea7beff2 100644 --- a/app/src/main/java/io/legado/app/utils/UriExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/UriExtensions.kt @@ -53,6 +53,9 @@ fun AppCompatActivity.readUri( } catch (e: Exception) { e.printOnDebug() toastOnUi(e.localizedMessage ?: "read uri error") + if (e is SecurityException) { + throw e + } } }