pull/2337/head
Horis 2 years ago
parent 7ae769bddb
commit 5444758f30
  1. 18
      app/src/main/java/io/legado/app/help/coroutine/OrderCoroutine.kt
  2. 6
      app/src/main/java/io/legado/app/ui/book/group/GroupEditDialog.kt
  3. 6
      app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt
  4. 6
      app/src/main/java/io/legado/app/ui/config/CoverConfigFragment.kt
  5. 6
      app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt
  6. 6
      app/src/main/java/io/legado/app/ui/config/WelcomeConfigFragment.kt
  7. 3
      app/src/main/java/io/legado/app/utils/UriExtensions.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<T>(val threadCount: Int) {
private val taskList = ConcurrentLinkedQueue<suspend CoroutineScope.() -> T>()
private val taskList = ArrayList<suspend CoroutineScope.() -> T>()
private val taskResultMap = ConcurrentHashMap<Int, T>()
private val finishTaskIndex = PriorityBlockingQueue<Int>()
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)
}

@ -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)
}

@ -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)
}

@ -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)
}

@ -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)
}

@ -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)
}

@ -53,6 +53,9 @@ fun AppCompatActivity.readUri(
} catch (e: Exception) {
e.printOnDebug()
toastOnUi(e.localizedMessage ?: "read uri error")
if (e is SecurityException) {
throw e
}
}
}

Loading…
Cancel
Save