离线下载改为离线缓存

pull/374/head
gedoor 4 years ago
parent f1897ae596
commit 186924f9d4
  1. 4
      app/src/main/AndroidManifest.xml
  2. 2
      app/src/main/java/io/legado/app/App.kt
  3. 18
      app/src/main/java/io/legado/app/service/CacheBookService.kt
  4. 10
      app/src/main/java/io/legado/app/service/help/CacheBook.kt
  5. 30
      app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt
  6. 10
      app/src/main/java/io/legado/app/ui/book/cache/CacheAdapter.kt
  7. 4
      app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt
  8. 6
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivityHelp.kt
  9. 4
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt
  10. 2
      app/src/main/res/layout/activity_download.xml
  11. 2
      app/src/main/res/menu/main_bookshelf.xml
  12. 2
      app/src/main/res/menu/read_book.xml
  13. 6
      app/src/main/res/values-zh-rHK/strings.xml
  14. 6
      app/src/main/res/values-zh-rTW/strings.xml
  15. 6
      app/src/main/res/values-zh/strings.xml
  16. 6
      app/src/main/res/values/strings.xml

@ -250,7 +250,7 @@
android:launchMode="singleTop" />
<!-- 下载界面 -->
<activity
android:name=".ui.book.download.DownloadActivity"
android:name=".ui.book.cache.CacheActivity"
android:launchMode="singleTop" />
<!-- 书源登录 -->
<activity
@ -349,7 +349,7 @@
</activity>
<service android:name=".service.CheckSourceService" />
<service android:name=".service.DownloadService" />
<service android:name=".service.CacheBookService" />
<service android:name=".service.WebService" />
<service android:name=".service.TTSReadAloudService" />
<service android:name=".service.HttpReadAloudService" />

@ -156,7 +156,7 @@ class App : MultiDexApplication() {
//用唯一的ID创建渠道对象
val downloadChannel = NotificationChannel(
channelIdDownload,
getString(R.string.download_offline),
getString(R.string.offline_cache),
NotificationManager.IMPORTANCE_LOW
)
//初始化channel

@ -17,7 +17,7 @@ import io.legado.app.help.IntentHelp
import io.legado.app.help.coroutine.CompositeCoroutine
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.webBook.WebBook
import io.legado.app.service.help.Download
import io.legado.app.service.help.CacheBook
import io.legado.app.utils.postEvent
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.asCoroutineDispatcher
@ -27,7 +27,7 @@ import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CopyOnWriteArraySet
import java.util.concurrent.Executors
class DownloadService : BaseService() {
class CacheBookService : BaseService() {
private val threadCount = AppConfig.threadCount
private var searchPool =
Executors.newFixedThreadPool(threadCount).asCoroutineDispatcher()
@ -49,11 +49,11 @@ class DownloadService : BaseService() {
val builder = NotificationCompat.Builder(this, AppConst.channelIdDownload)
.setSmallIcon(R.drawable.ic_download)
.setOngoing(true)
.setContentTitle(getString(R.string.download_offline))
.setContentTitle(getString(R.string.offline_cache))
builder.addAction(
R.drawable.ic_stop_black_24dp,
getString(R.string.cancel),
IntentHelp.servicePendingIntent<DownloadService>(this, IntentAction.stop)
IntentHelp.servicePendingIntent<CacheBookService>(this, IntentAction.stop)
)
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
}
@ -138,7 +138,7 @@ class DownloadService : BaseService() {
chapters.addAll(it)
downloadMap[bookUrl] = chapters
} else {
Download.addLog("${getBook(bookUrl)?.name} is empty")
CacheBook.addLog("${getBook(bookUrl)?.name} is empty")
}
}
for (i in 0 until threadCount) {
@ -158,7 +158,7 @@ class DownloadService : BaseService() {
downloadingCount += 1
val task = Coroutine.async(this, context = searchPool) {
if (!isActive) return@async
val bookChapter: BookChapter? = synchronized(this@DownloadService) {
val bookChapter: BookChapter? = synchronized(this@CacheBookService) {
downloadMap.forEach {
it.value.forEach { chapter ->
if (!downloadingList.contains(chapter.url)) {
@ -193,12 +193,12 @@ class DownloadService : BaseService() {
synchronized(this) {
downloadingList.remove(bookChapter.url)
}
Download.addLog("getContentError${it.localizedMessage}")
CacheBook.addLog("getContentError${it.localizedMessage}")
updateNotification("getContentError${it.localizedMessage}")
}
.onSuccess(IO) { content ->
BookHelp.saveContent(book, bookChapter, content)
synchronized(this@DownloadService) {
synchronized(this@CacheBookService) {
downloadCount[book.bookUrl]?.increaseSuccess()
downloadCount[book.bookUrl]?.increaseFinished()
downloadCount[book.bookUrl]?.let {
@ -231,7 +231,7 @@ class DownloadService : BaseService() {
}
}
}.onError {
Download.addLog("ERROR:${it.localizedMessage}")
CacheBook.addLog("ERROR:${it.localizedMessage}")
updateNotification("ERROR:${it.localizedMessage}")
}
tasks.add(task)

@ -3,9 +3,9 @@ package io.legado.app.service.help
import android.content.Context
import android.content.Intent
import io.legado.app.constant.IntentAction
import io.legado.app.service.DownloadService
import io.legado.app.service.CacheBookService
object Download {
object CacheBook {
val logs = arrayListOf<String>()
@ -20,7 +20,7 @@ object Download {
}
fun start(context: Context, bookUrl: String, start: Int, end: Int) {
Intent(context, DownloadService::class.java).let {
Intent(context, CacheBookService::class.java).let {
it.action = IntentAction.start
it.putExtra("bookUrl", bookUrl)
it.putExtra("start", start)
@ -30,7 +30,7 @@ object Download {
}
fun remove(context: Context, bookUrl: String) {
Intent(context, DownloadService::class.java).let {
Intent(context, CacheBookService::class.java).let {
it.action = IntentAction.remove
it.putExtra("bookUrl", bookUrl)
context.startService(it)
@ -38,7 +38,7 @@ object Download {
}
fun stop(context: Context) {
Intent(context, DownloadService::class.java).let {
Intent(context, CacheBookService::class.java).let {
it.action = IntentAction.stop
context.startService(it)
}

@ -1,4 +1,4 @@
package io.legado.app.ui.book.download
package io.legado.app.ui.book.cache
import android.app.Activity
import android.content.Intent
@ -18,7 +18,7 @@ import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.BookGroup
import io.legado.app.help.BookHelp
import io.legado.app.service.help.Download
import io.legado.app.service.help.CacheBook
import io.legado.app.ui.filechooser.FileChooserDialog
import io.legado.app.ui.filechooser.FilePicker
import io.legado.app.ui.widget.dialog.TextListDialog
@ -32,12 +32,12 @@ import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CopyOnWriteArraySet
class DownloadActivity : VMBaseActivity<DownloadViewModel>(R.layout.activity_download),
class CacheActivity : VMBaseActivity<CacheViewModel>(R.layout.activity_download),
FileChooserDialog.CallBack,
DownloadAdapter.CallBack {
CacheAdapter.CallBack {
private val exportRequestCode = 32
private val exportBookPathKey = "exportBookPath"
lateinit var adapter: DownloadAdapter
lateinit var adapter: CacheAdapter
private var groupLiveData: LiveData<List<BookGroup>>? = null
private var booksLiveData: LiveData<List<Book>>? = null
private var menu: Menu? = null
@ -45,8 +45,8 @@ class DownloadActivity : VMBaseActivity<DownloadViewModel>(R.layout.activity_dow
private val groupList: ArrayList<BookGroup> = arrayListOf()
private var groupId: Int = -1
override val viewModel: DownloadViewModel
get() = getViewModel(DownloadViewModel::class.java)
override val viewModel: CacheViewModel
get() = getViewModel(CacheViewModel::class.java)
override fun onActivityCreated(savedInstanceState: Bundle?) {
groupId = intent.getIntExtra("groupId", -1)
@ -81,19 +81,19 @@ class DownloadActivity : VMBaseActivity<DownloadViewModel>(R.layout.activity_dow
R.id.menu_download -> launch(IO) {
if (adapter.downloadMap.isNullOrEmpty()) {
adapter.getItems().forEach { book ->
Download.start(
this@DownloadActivity,
CacheBook.start(
this@CacheActivity,
book.bookUrl,
book.durChapterIndex,
book.totalChapterNum
)
}
} else {
Download.stop(this@DownloadActivity)
CacheBook.stop(this@CacheActivity)
}
}
R.id.menu_log -> {
TextListDialog.show(supportFragmentManager, getString(R.string.log), Download.logs)
TextListDialog.show(supportFragmentManager, getString(R.string.log), CacheBook.logs)
}
R.id.menu_no_group -> {
title_bar.subtitle = getString(R.string.no_group)
@ -116,7 +116,7 @@ class DownloadActivity : VMBaseActivity<DownloadViewModel>(R.layout.activity_dow
private fun initRecyclerView() {
recycler_view.layoutManager = LinearLayoutManager(this)
adapter = DownloadAdapter(this, this)
adapter = CacheAdapter(this, this)
recycler_view.adapter = adapter
}
@ -191,7 +191,7 @@ class DownloadActivity : VMBaseActivity<DownloadViewModel>(R.layout.activity_dow
override fun export(position: Int) {
exportPosition = position
val default = arrayListOf<String>()
val path = ACache.get(this@DownloadActivity).getAsString(exportBookPathKey)
val path = ACache.get(this@CacheActivity).getAsString(exportBookPathKey)
if (!path.isNullOrEmpty()) {
default.add(path)
}
@ -213,7 +213,7 @@ class DownloadActivity : VMBaseActivity<DownloadViewModel>(R.layout.activity_dow
override fun onFilePicked(requestCode: Int, currentPath: String) {
when (requestCode) {
exportRequestCode -> {
ACache.get(this@DownloadActivity).put(exportBookPathKey, currentPath)
ACache.get(this@CacheActivity).put(exportBookPathKey, currentPath)
startExport(currentPath)
}
}
@ -228,7 +228,7 @@ class DownloadActivity : VMBaseActivity<DownloadViewModel>(R.layout.activity_dow
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
ACache.get(this@DownloadActivity).put(exportBookPathKey, uri.toString())
ACache.get(this@CacheActivity).put(exportBookPathKey, uri.toString())
startExport(uri.toString())
}
}

@ -1,4 +1,4 @@
package io.legado.app.ui.book.download
package io.legado.app.ui.book.cache
import android.content.Context
import android.widget.ImageView
@ -7,14 +7,14 @@ import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
import io.legado.app.service.help.Download
import io.legado.app.service.help.CacheBook
import kotlinx.android.synthetic.main.item_download.view.*
import org.jetbrains.anko.sdk27.listeners.onClick
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CopyOnWriteArraySet
class DownloadAdapter(context: Context, private val callBack: CallBack) :
class CacheAdapter(context: Context, private val callBack: CallBack) :
SimpleRecyclerAdapter<Book>(context, R.layout.item_download) {
val cacheChapters = hashMapOf<String, HashSet<String>>()
@ -47,9 +47,9 @@ class DownloadAdapter(context: Context, private val callBack: CallBack) :
iv_download.onClick {
getItem(holder.layoutPosition)?.let {
if (downloadMap?.containsKey(it.bookUrl) == true) {
Download.remove(context, it.bookUrl)
CacheBook.remove(context, it.bookUrl)
} else {
Download.start(context, it.bookUrl, 0, it.totalChapterNum)
CacheBook.start(context, it.bookUrl, 0, it.totalChapterNum)
}
}
}

@ -1,4 +1,4 @@
package io.legado.app.ui.book.download
package io.legado.app.ui.book.cache
import android.app.Application
import android.net.Uri
@ -13,7 +13,7 @@ import io.legado.app.utils.*
import java.io.File
class DownloadViewModel(application: Application) : BaseViewModel(application) {
class CacheViewModel(application: Application) : BaseViewModel(application) {
fun export(path: String, book: Book, finally: (msg: String) -> Unit) {

@ -20,7 +20,7 @@ import io.legado.app.lib.dialogs.*
import io.legado.app.lib.theme.ATH
import io.legado.app.lib.theme.ThemeStore
import io.legado.app.lib.theme.backgroundColor
import io.legado.app.service.help.Download
import io.legado.app.service.help.CacheBook
import io.legado.app.service.help.ReadBook
import io.legado.app.ui.widget.text.AutoCompleteTextView
import io.legado.app.utils.applyTint
@ -108,7 +108,7 @@ object ReadBookActivityHelp {
@SuppressLint("InflateParams")
fun showDownloadDialog(context: Context) {
ReadBook.book?.let { book ->
context.alert(titleResource = R.string.download_offline) {
context.alert(titleResource = R.string.offline_cache) {
var view: View? = null
customView {
LayoutInflater.from(context).inflate(R.layout.dialog_download_choice, null)
@ -123,7 +123,7 @@ object ReadBookActivityHelp {
view?.apply {
val start = edit_start?.text?.toString()?.toInt() ?: 0
val end = edit_end?.text?.toString()?.toInt() ?: book.totalChapterNum
Download.start(context, book.bookUrl, start - 1, end - 1)
CacheBook.start(context, book.bookUrl, start - 1, end - 1)
}
}
noButton()

@ -23,7 +23,7 @@ import io.legado.app.lib.dialogs.okButton
import io.legado.app.lib.theme.ATH
import io.legado.app.lib.theme.accentColor
import io.legado.app.ui.book.arrange.ArrangeBookActivity
import io.legado.app.ui.book.download.DownloadActivity
import io.legado.app.ui.book.cache.CacheActivity
import io.legado.app.ui.book.group.GroupManageDialog
import io.legado.app.ui.book.local.ImportBookActivity
import io.legado.app.ui.book.search.SearchActivity
@ -87,7 +87,7 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
Pair("groupId", selectedGroup?.groupId ?: 0),
Pair("groupName", selectedGroup?.groupName ?: 0)
)
R.id.menu_download -> startActivity<DownloadActivity>(
R.id.menu_download -> startActivity<CacheActivity>(
Pair("groupId", selectedGroup?.groupId ?: 0),
Pair("groupName", selectedGroup?.groupName ?: 0)
)

@ -9,7 +9,7 @@
android:id="@+id/title_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="@string/download_offline" />
app:title="@string/offline_cache" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"

@ -29,7 +29,7 @@
<item
android:id="@+id/menu_download"
android:icon="@drawable/ic_download_line"
android:title="@string/download_offline"
android:title="@string/offline_cache"
app:showAsAction="never" />
<item

@ -19,7 +19,7 @@
<item
android:id="@+id/menu_download"
android:icon="@drawable/ic_download_line"
android:title="@string/download_offline"
android:title="@string/offline_cache"
app:showAsAction="ifRoom" />
<item

@ -83,9 +83,9 @@
<string name="web_service_desc">啟用 Web 服務</string>
<string name="web_edit_source">web 編輯書源</string>
<string name="http_ip">http://%1$s:%2$d</string>
<string name="download_offline">離線下載</string>
<string name="download_offline_t">離線下載</string>
<string name="download_offline_s">下載已選擇的章節到本地</string>
<string name="offline_cache">離線緩存</string>
<string name="offline_cache_t">離線緩存</string>
<string name="offline_cache_s">緩存已選擇的章節到本地</string>
<string name="change_origin">換源</string>
<string name="about_description">
\u3000\u3000這是一款使用 Kotlin 全新開發的開源的閲讀應用程式,歡迎你的加入。關注公眾號[legado-top]!

@ -83,9 +83,9 @@
<string name="web_service_desc">啟用Web服務</string>
<string name="web_edit_source">web編輯書源</string>
<string name="http_ip">http://%1$s:%2$d</string>
<string name="download_offline">離線下載</string>
<string name="download_offline_t">離線下載</string>
<string name="download_offline_s">下載選擇的章節到本機</string>
<string name="offline_cache">離線緩存</string>
<string name="offline_cache_t">離線緩存</string>
<string name="offline_cache_s">緩存選擇的章節到本機</string>
<string name="change_origin">換源</string>
<string name="about_description">
\u3000\u3000這是一款使用Kotlin全新開發的開源的閱讀軟體,歡迎您的加入。關注公眾號[legado-top]!

@ -85,9 +85,9 @@
<string name="web_service_desc">启用Web服务</string>
<string name="web_edit_source">web编辑书源</string>
<string name="http_ip">http://%1$s:%2$d</string>
<string name="download_offline">离线下载</string>
<string name="download_offline_t">离线下载</string>
<string name="download_offline_s">下载选择的章节到本地</string>
<string name="offline_cache">离线缓存</string>
<string name="offline_cache_t">离线缓存</string>
<string name="offline_cache_s">缓存选择的章节到本地</string>
<string name="change_origin">换源</string>
<string name="about_description">
\u3000\u3000这是一款使用Kotlin全新开发的开源的阅读软件,欢迎您的加入。关注公众号[开源阅读]!

@ -85,9 +85,9 @@
<string name="web_service_desc">Enable web service</string>
<string name="web_edit_source">Edit book sources on the web</string>
<string name="http_ip">http://%1$s:%2$d</string>
<string name="download_offline">Offline download</string>
<string name="download_offline_t">Offline download</string>
<string name="download_offline_s">Download the selected chapter(s) to Storage</string>
<string name="offline_cache">Offline cache</string>
<string name="offline_cache_t">Offline cache</string>
<string name="offline_cache_s">cache the selected chapter(s) to Storage</string>
<string name="change_origin">Change Origin</string>
<string name="about_description">
\u3000\u3000 This is an open source reading software newly developed by Kotlin, welcome to join us. Follow the WeChat Official Account [开源阅读]!

Loading…
Cancel
Save