feat: 优化代码

pull/133/head
kunfei 5 years ago
parent c0b4b44f6c
commit 3bc2b9d514
  1. 50
      app/src/main/AndroidManifest.xml
  2. 55
      app/src/main/java/io/legado/app/service/CheckSourceService.kt

@ -133,7 +133,9 @@
android:name=".ui.about.DonateActivity" android:name=".ui.about.DonateActivity"
android:launchMode="singleTask" /> android:launchMode="singleTask" />
<!--书源管理--> <!--书源管理-->
<activity android:name=".ui.book.source.manage.BookSourceActivity"> <activity
android:name=".ui.book.source.manage.BookSourceActivity"
android:launchMode="singleTop">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
@ -144,7 +146,9 @@
</intent-filter> </intent-filter>
</activity> </activity>
<!--订阅源管理--> <!--订阅源管理-->
<activity android:name=".ui.rss.source.manage.RssSourceActivity"> <activity
android:name=".ui.rss.source.manage.RssSourceActivity"
android:launchMode="singleTop">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.VIEW" />
@ -159,7 +163,7 @@
<!--替换规则界面--> <!--替换规则界面-->
<activity <activity
android:name=".ui.replacerule.ReplaceRuleActivity" android:name=".ui.replacerule.ReplaceRuleActivity"
android:launchMode="singleTask"> android:launchMode="singleTop">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.VIEW" />
@ -171,16 +175,36 @@
android:scheme="yuedu" /> android:scheme="yuedu" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".ui.book.arrange.ArrangeBookActivity" /> <activity
<activity android:name=".ui.book.source.debug.BookSourceDebugActivity" /> android:name=".ui.book.arrange.ArrangeBookActivity"
<activity android:name=".ui.chapterlist.ChapterListActivity" /> android:launchMode="singleTop" />
<activity android:name=".ui.rss.read.ReadRssActivity" /> <activity
<activity android:name=".ui.importbook.ImportBookActivity" /> android:name=".ui.book.source.debug.BookSourceDebugActivity"
<activity android:name=".ui.explore.ExploreShowActivity" /> android:launchMode="singleTop" />
<activity android:name=".ui.rss.source.debug.RssSourceDebugActivity" /> <activity
<activity android:name=".ui.rss.article.RssArticlesActivity" /> android:name=".ui.chapterlist.ChapterListActivity"
<activity android:name=".ui.rss.favorites.RssFavoritesActivity" /> android:launchMode="singleTop" />
<activity android:name=".ui.download.DownloadActivity" /> <activity
android:name=".ui.rss.read.ReadRssActivity"
android:launchMode="singleTop" />
<activity
android:name=".ui.importbook.ImportBookActivity"
android:launchMode="singleTop" />
<activity
android:name=".ui.explore.ExploreShowActivity"
android:launchMode="singleTop" />
<activity
android:name=".ui.rss.source.debug.RssSourceDebugActivity"
android:launchMode="singleTop" />
<activity
android:name=".ui.rss.article.RssArticlesActivity"
android:launchMode="singleTop" />
<activity
android:name=".ui.rss.favorites.RssFavoritesActivity"
android:launchMode="singleTop" />
<activity
android:name=".ui.download.DownloadActivity"
android:launchMode="singleTop" />
<activity <activity
android:name=".receiver.SharedReceiverActivity" android:name=".receiver.SharedReceiverActivity"
android:label="@string/receiving_shared_label"> android:label="@string/receiving_shared_label">

@ -18,11 +18,12 @@ import org.jetbrains.anko.toast
import java.util.concurrent.Executors import java.util.concurrent.Executors
class CheckSourceService : BaseService() { class CheckSourceService : BaseService() {
private var searchPool = private val threadCount = AppConfig.threadCount
Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher() private var searchPool = Executors.newFixedThreadPool(threadCount).asCoroutineDispatcher()
private var task: Coroutine<*>? = null private var task: Coroutine<*>? = null
private val allIds = LinkedHashSet<String>() private val allIds = ArrayList<String>()
private val checkedIds = LinkedHashSet<String>() private val checkedIds = ArrayList<String>()
private var processIndex = 0
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
@ -50,30 +51,40 @@ class CheckSourceService : BaseService() {
allIds.clear() allIds.clear()
checkedIds.clear() checkedIds.clear()
allIds.addAll(ids) allIds.addAll(ids)
processIndex = 0
updateNotification(0, getString(R.string.progress_show, 0, allIds.size)) updateNotification(0, getString(R.string.progress_show, 0, allIds.size))
task = execute(context = searchPool) { task = execute {
allIds.forEach { sourceUrl -> for (i in 0 until threadCount) {
App.db.bookSourceDao().getBookSource(sourceUrl)?.let { source -> check()
val webBook = WebBook(source)
webBook.searchBook("我的", scope = this, context = searchPool)
.onError(IO) {
source.addGroup("失效")
App.db.bookSourceDao().update(source)
}.onFinally {
checkedIds.add(sourceUrl)
updateNotification(
checkedIds.size,
getString(R.string.progress_show, checkedIds.size, allIds.size)
)
}
}
} }
}.onError { }.onError {
toast("校验书源出错:${it.localizedMessage}") toast("校验书源出错:${it.localizedMessage}")
} }
}
task?.invokeOnCompletion { private fun check() {
stopSelf() processIndex++
if (processIndex < allIds.size) {
val sourceUrl = allIds[processIndex]
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 {
checkedIds.add(sourceUrl)
updateNotification(
checkedIds.size,
getString(R.string.progress_show, checkedIds.size, allIds.size)
)
if (processIndex >= allIds.size + threadCount - 1) {
stopSelf()
}
}
}
} }
} }

Loading…
Cancel
Save