pull/90/head^2
kunfei 5 years ago
parent 5999e21043
commit a286831fd1
  1. 12
      app/src/main/java/io/legado/app/base/BaseService.kt
  2. 10
      app/src/main/java/io/legado/app/data/entities/BookSource.kt
  3. 31
      app/src/main/java/io/legado/app/service/CheckSourceService.kt
  4. 5
      app/src/main/java/io/legado/app/service/help/CheckSource.kt
  5. 1
      app/src/main/res/values/strings.xml

@ -3,17 +3,27 @@ package io.legado.app.base
import android.app.Service import android.app.Service
import android.content.Intent import android.content.Intent
import android.os.IBinder import android.os.IBinder
import io.legado.app.help.coroutine.Coroutine
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancel
import kotlin.coroutines.CoroutineContext
abstract class BaseService : Service(), CoroutineScope by MainScope() { abstract class BaseService : Service(), CoroutineScope by MainScope() {
fun <T> execute(
scope: CoroutineScope = this,
context: CoroutineContext = Dispatchers.IO,
block: suspend CoroutineScope.() -> T
): Coroutine<T> {
return Coroutine.async(scope, context) { block() }
}
override fun onBind(intent: Intent?): IBinder? { override fun onBind(intent: Intent?): IBinder? {
return null return null
} }
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
cancel() cancel()

@ -126,6 +126,16 @@ data class BookSource(
return contentRuleV!! return contentRuleV!!
} }
fun addGroup(group: String) {
bookSourceGroup?.let {
if (!it.contains(group)) {
bookSourceGroup = "$it;$group"
}
} ?: let {
bookSourceGroup = group
}
}
fun getExploreKinds(): ArrayList<ExploreKind>? { fun getExploreKinds(): ArrayList<ExploreKind>? {
val exploreKinds = arrayListOf<ExploreKind>() val exploreKinds = arrayListOf<ExploreKind>()
exploreUrl?.let { exploreUrl?.let {

@ -2,19 +2,24 @@ package io.legado.app.service
import android.content.Intent import android.content.Intent
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseService import io.legado.app.base.BaseService
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
import io.legado.app.constant.IntentAction import io.legado.app.constant.IntentAction
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.help.IntentHelp import io.legado.app.help.IntentHelp
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.WebBook
import io.legado.app.ui.book.source.manage.BookSourceActivity import io.legado.app.ui.book.source.manage.BookSourceActivity
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher
import java.util.concurrent.Executors import java.util.concurrent.Executors
class CheckSourceService : BaseService() { class CheckSourceService : BaseService() {
private var searchPool = private var searchPool =
Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher() Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher()
private var task: Coroutine<*>? = null
private val allIds = LinkedHashSet<String>() private val allIds = LinkedHashSet<String>()
private val unCheckIds = LinkedHashSet<String>() private val unCheckIds = LinkedHashSet<String>()
@ -35,13 +40,39 @@ class CheckSourceService : BaseService() {
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
task?.cancel()
searchPool.close() searchPool.close()
} }
private fun check(ids: List<String>) { private fun check(ids: List<String>) {
task?.cancel()
allIds.clear()
unCheckIds.clear()
allIds.addAll(ids) allIds.addAll(ids)
unCheckIds.addAll(ids) unCheckIds.addAll(ids)
task = execute {
unCheckIds.forEach { sourceUrl ->
App.db.bookSourceDao().getBookSource(sourceUrl)?.let { source ->
val webBook = WebBook(source)
webBook.searchBook("我的", scope = this, context = searchPool)
.onError(IO) {
source.addGroup("失效")
App.db.bookSourceDao().update(source)
}.onFinally {
unCheckIds.remove(sourceUrl)
val checkedCount = allIds.size - unCheckIds.size
updateNotification(
checkedCount,
getString(R.string.progress_show, checkedCount, unCheckIds.size)
)
}
}
}
}
task?.invokeOnCompletion {
stopSelf()
}
} }
/** /**

@ -2,12 +2,17 @@ package io.legado.app.service.help
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import io.legado.app.R
import io.legado.app.constant.IntentAction import io.legado.app.constant.IntentAction
import io.legado.app.service.CheckSourceService import io.legado.app.service.CheckSourceService
import org.jetbrains.anko.toast
object CheckSource { object CheckSource {
fun start(context: Context, selectedIds: ArrayList<String>) { fun start(context: Context, selectedIds: ArrayList<String>) {
if (selectedIds.isEmpty()) {
context.toast(R.string.non_select)
}
Intent(context, CheckSourceService::class.java).let { Intent(context, CheckSourceService::class.java).let {
it.action = IntentAction.start it.action = IntentAction.start
it.putExtra("selectIds", selectedIds) it.putExtra("selectIds", selectedIds)

@ -252,6 +252,7 @@
<string name="text_bg_style">文字颜色和背景(长按自定义)</string> <string name="text_bg_style">文字颜色和背景(长按自定义)</string>
<string name="immersion_status_bar">沉浸式状态栏</string> <string name="immersion_status_bar">沉浸式状态栏</string>
<string name="un_download">还剩%d章未下载</string> <string name="un_download">还剩%d章未下载</string>
<string name="non_select">没有选择</string>
<string name="long_click_input_color">长按输入颜色值</string> <string name="long_click_input_color">长按输入颜色值</string>
<string name="loading">加载中…</string> <string name="loading">加载中…</string>
<string name="group_zg">追更区</string> <string name="group_zg">追更区</string>

Loading…
Cancel
Save