diff --git a/app/src/main/java/io/legado/app/base/BaseService.kt b/app/src/main/java/io/legado/app/base/BaseService.kt index 2922c9795..9db011cd7 100644 --- a/app/src/main/java/io/legado/app/base/BaseService.kt +++ b/app/src/main/java/io/legado/app/base/BaseService.kt @@ -18,8 +18,9 @@ abstract class BaseService : LifecycleService(), CoroutineScope by MainScope() { scope: CoroutineScope = this, context: CoroutineContext = Dispatchers.IO, start: CoroutineStart = CoroutineStart.DEFAULT, + executeContext: CoroutineContext = Dispatchers.Main, block: suspend CoroutineScope.() -> T - ) = Coroutine.async(scope, context, start) { block() } + ) = Coroutine.async(scope, context, start, executeContext, block) @CallSuper override fun onCreate() { @@ -57,7 +58,7 @@ abstract class BaseService : LifecycleService(), CoroutineScope by MainScope() { /** * 检测通知权限 */ - private fun checkNotificationPermission() { + private fun checkNotificationPermission() { PermissionsCompat.Builder() .addPermissions(Permissions.POST_NOTIFICATIONS) .rationale(R.string.notification_permission_rationale) diff --git a/app/src/main/java/io/legado/app/base/BaseViewModel.kt b/app/src/main/java/io/legado/app/base/BaseViewModel.kt index 2422a8a15..f62b8c954 100644 --- a/app/src/main/java/io/legado/app/base/BaseViewModel.kt +++ b/app/src/main/java/io/legado/app/base/BaseViewModel.kt @@ -7,6 +7,7 @@ import androidx.lifecycle.viewModelScope import io.legado.app.App import io.legado.app.help.coroutine.Coroutine import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.CoroutineStart import kotlinx.coroutines.Deferred import kotlinx.coroutines.Dispatchers import kotlin.coroutines.CoroutineContext @@ -19,9 +20,11 @@ open class BaseViewModel(application: Application) : AndroidViewModel(applicatio fun execute( scope: CoroutineScope = viewModelScope, context: CoroutineContext = Dispatchers.IO, + start: CoroutineStart = CoroutineStart.DEFAULT, + executeContext: CoroutineContext = Dispatchers.Main, block: suspend CoroutineScope.() -> T ): Coroutine { - return Coroutine.async(scope, context) { block() } + return Coroutine.async(scope, context, start, executeContext, block) } fun submit( diff --git a/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt b/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt index 31e220577..d96322276 100644 --- a/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt +++ b/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt @@ -25,6 +25,7 @@ class Coroutine( val scope: CoroutineScope, context: CoroutineContext = Dispatchers.IO, val startOption: CoroutineStart = CoroutineStart.DEFAULT, + val executeContext: CoroutineContext = Dispatchers.Main, block: suspend CoroutineScope.() -> T ) { @@ -36,9 +37,10 @@ class Coroutine( scope: CoroutineScope = DEFAULT, context: CoroutineContext = Dispatchers.IO, start: CoroutineStart = CoroutineStart.DEFAULT, + executeContext: CoroutineContext = Dispatchers.Main, block: suspend CoroutineScope.() -> T ): Coroutine { - return Coroutine(scope, context, start, block) + return Coroutine(scope, context, start, executeContext, block) } } @@ -158,7 +160,7 @@ class Coroutine( context: CoroutineContext, block: suspend CoroutineScope.() -> T ): Job { - return (scope.plus(Dispatchers.Main)).launch(start = startOption) { + return (scope.plus(executeContext)).launch(start = startOption) { try { start?.let { dispatchVoidCallback(this, it) } ensureActive() diff --git a/app/src/main/java/io/legado/app/service/CheckSourceService.kt b/app/src/main/java/io/legado/app/service/CheckSourceService.kt index c0409eae6..143496350 100644 --- a/app/src/main/java/io/legado/app/service/CheckSourceService.kt +++ b/app/src/main/java/io/legado/app/service/CheckSourceService.kt @@ -122,7 +122,11 @@ class CheckSourceService : BaseService() { *校验书源 */ private fun check(source: BookSource) { - execute(context = searchCoroutine, start = CoroutineStart.LAZY) { + execute( + context = searchCoroutine, + start = CoroutineStart.LAZY, + executeContext = IO + ) { Debug.startChecking(source) var searchWord = CheckSource.keyword source.ruleSearch?.checkKeyWord?.let { diff --git a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt index 6bb144051..b9b0d1cf8 100644 --- a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt @@ -76,7 +76,6 @@ class HttpReadAloudService : BaseReadAloudService(), override fun play() { pageChanged = false exoPlayer.stop() - exoPlayer.clearMediaItems() if (!requestFocus()) return if (contentList.isEmpty()) { AppLog.putDebug("朗读列表为空") @@ -101,6 +100,7 @@ class HttpReadAloudService : BaseReadAloudService(), } private fun downloadAndPlayAudios() { + exoPlayer.clearMediaItems() downloadTask?.cancel() downloadTask = execute { downloadTaskActiveLock.withLock { @@ -133,8 +133,12 @@ class HttpReadAloudService : BaseReadAloudService(), val file = getSpeakFileAsMd5(fileName) val mediaItem = MediaItem.fromUri(Uri.fromFile(file)) launch(Main) { + if (exoPlayer.playbackState == Player.STATE_ENDED) { + exoPlayer.stop() + exoPlayer.clearMediaItems() + } exoPlayer.addMediaItem(mediaItem) - if (!exoPlayer.isPlaying && nowSpeak == index) { + if (!exoPlayer.isPlaying) { exoPlayer.playWhenReady = !pause exoPlayer.prepare() } diff --git a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt index 49e0a5482..4024a62f3 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt @@ -20,6 +20,7 @@ import io.legado.app.model.webBook.WebBook import io.legado.app.service.CacheBookService import io.legado.app.utils.postEvent import kotlinx.coroutines.* +import kotlinx.coroutines.Dispatchers.IO import java.util.concurrent.CopyOnWriteArraySet import java.util.concurrent.Executors import kotlin.math.min @@ -85,9 +86,11 @@ class MainViewModel(application: Application) : BaseViewModel(application) { upTocJob?.cancel() upTocJob = null } + onUpTocBooks.size < threadCount -> { updateToc() } + else -> { delay(500) } @@ -119,7 +122,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) { } waitUpTocBooks.remove(bookUrl) upTocAdd(bookUrl) - execute(context = upTocPool) { + execute(context = upTocPool, executeContext = IO) { kotlin.runCatching { val oldBook = book.copy() WebBook.runPreUpdateJs(source, book)