From 2aa0be91ea7fbe9625c3b34a04bf587742846148 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 2 Feb 2020 20:10:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/data/entities/Book.kt | 5 ++ .../main/java/io/legado/app/help/BookHelp.kt | 5 +- .../app/model/localBook/AnalyzeTxtFile.kt | 31 ++++++++---- .../io/legado/app/service/help/ReadBook.kt | 1 + .../app/ui/book/read/ReadBookViewModel.kt | 47 ++++++++++++------- 5 files changed, 63 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/Book.kt b/app/src/main/java/io/legado/app/data/entities/Book.kt index 17198cdda..0d853d2cf 100644 --- a/app/src/main/java/io/legado/app/data/entities/Book.kt +++ b/app/src/main/java/io/legado/app/data/entities/Book.kt @@ -10,6 +10,7 @@ import io.legado.app.utils.GSON import io.legado.app.utils.fromJsonObject import kotlinx.android.parcel.IgnoredOnParcel import kotlinx.android.parcel.Parcelize +import java.nio.charset.Charset import kotlin.math.max @Parcelize @@ -81,6 +82,10 @@ data class Book( variable = GSON.toJson(variableMap) } + fun getCharset(): Charset { + return charset(charset ?: "UTF-8") + } + fun toSearchBook(): SearchBook { return SearchBook( name = name, diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt index 7635df1f7..018b99605 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -7,6 +7,7 @@ import io.legado.app.constant.PreferKey import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.ReplaceRule +import io.legado.app.model.localBook.AnalyzeTxtFile import io.legado.app.utils.* import org.apache.commons.text.similarity.JaccardSimilarity import java.io.File @@ -126,7 +127,9 @@ object BookHelp { } fun getContent(book: Book, bookChapter: BookChapter): String? { - if (downloadUri.isDocumentUri(App.INSTANCE)) { + if (book.isLocalBook()) { + AnalyzeTxtFile.getContent(book, bookChapter) + } else if (downloadUri.isDocumentUri(App.INSTANCE)) { DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { root -> return DocumentUtils.getDirDocument( root, diff --git a/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt b/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt index 85c83c514..35c394251 100644 --- a/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt +++ b/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt @@ -28,14 +28,7 @@ object AnalyzeTxtFile { } fun analyze(context: Context, book: Book): ArrayList { - val uri = Uri.parse(book.bookUrl) - val bookFile = FileUtils.getFile(cacheFolder, book.originName, subDirs = *arrayOf()) - if (!bookFile.exists()) { - bookFile.createNewFile() - DocumentUtils.readBytes(context, uri)?.let { - bookFile.writeBytes(it) - } - } + val bookFile = getBookFile(context, book) val charset = charset(EncodingDetect.getEncode(bookFile)) book.charset = charset.name() val toc = arrayListOf() @@ -189,6 +182,28 @@ object AnalyzeTxtFile { return toc } + fun getContent(book: Book, bookChapter: BookChapter): String { + val bookFile = getBookFile(App.INSTANCE, book) + //获取文件流 + val bookStream = RandomAccessFile(bookFile, "r") + bookStream.seek(bookChapter.start ?: 0) + val extent = (bookChapter.end!! - bookChapter.start!!).toInt() + val content = ByteArray(extent) + bookStream.read(content, 0, extent) + return String(content, book.getCharset()) + } + + private fun getBookFile(context: Context, book: Book): File { + val uri = Uri.parse(book.bookUrl) + val bookFile = FileUtils.getFile(cacheFolder, book.originName, subDirs = *arrayOf()) + if (!bookFile.exists()) { + bookFile.createNewFile() + DocumentUtils.readBytes(context, uri)?.let { + bookFile.writeBytes(it) + } + } + return bookFile + } private fun getTocRule(bookStream: RandomAccessFile, charset: Charset): Pattern? { val tocRules = getTocRules() diff --git a/app/src/main/java/io/legado/app/service/help/ReadBook.kt b/app/src/main/java/io/legado/app/service/help/ReadBook.kt index 451883b55..770b21e18 100644 --- a/app/src/main/java/io/legado/app/service/help/ReadBook.kt +++ b/app/src/main/java/io/legado/app/service/help/ReadBook.kt @@ -197,6 +197,7 @@ object ReadBook { private fun download(index: Int) { book?.let { book -> + if (book.isLocalBook()) return if (addLoading(index)) { Coroutine.async { App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index 9b987e505..646aa3702 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -10,6 +10,7 @@ import io.legado.app.data.entities.BookChapter import io.legado.app.help.BookHelp import io.legado.app.help.IntentDataHelp import io.legado.app.model.WebBook +import io.legado.app.model.localBook.AnalyzeTxtFile import io.legado.app.service.BaseReadAloudService import io.legado.app.service.help.ReadAloud import io.legado.app.service.help.ReadBook @@ -79,10 +80,14 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { changeDruChapterIndex: ((chapters: List) -> Unit)? = null ) { execute { - ReadBook.webBook?.getBookInfo(book, this) - ?.onSuccess { - loadChapterList(book, changeDruChapterIndex) - } + if (book.isLocalBook()) { + loadChapterList(book, changeDruChapterIndex) + } else { + ReadBook.webBook?.getBookInfo(book, this) + ?.onSuccess { + loadChapterList(book, changeDruChapterIndex) + } + } } } @@ -91,22 +96,30 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { changeDruChapterIndex: ((chapters: List) -> Unit)? = null ) { execute { - ReadBook.webBook?.getChapterList(book, this) - ?.onSuccess(IO) { cList -> - if (!cList.isNullOrEmpty()) { - if (changeDruChapterIndex == null) { - App.db.bookChapterDao().insert(*cList.toTypedArray()) - ReadBook.chapterSize = cList.size - ReadBook.loadContent() + if (book.isLocalBook()) { + AnalyzeTxtFile.analyze(context, book).let { + App.db.bookChapterDao().insert(*it.toTypedArray()) + ReadBook.chapterSize = it.size + ReadBook.loadContent() + } + } else { + ReadBook.webBook?.getChapterList(book, this) + ?.onSuccess(IO) { cList -> + if (!cList.isNullOrEmpty()) { + if (changeDruChapterIndex == null) { + App.db.bookChapterDao().insert(*cList.toTypedArray()) + ReadBook.chapterSize = cList.size + ReadBook.loadContent() + } else { + changeDruChapterIndex(cList) + } } else { - changeDruChapterIndex(cList) + toast(R.string.error_load_toc) } - } else { + }?.onError { toast(R.string.error_load_toc) - } - }?.onError { - toast(R.string.error_load_toc) - } ?: autoChangeSource() + } ?: autoChangeSource() + } } }