小说阅读接口 优化

添加 epub 未完成
pull/274/head
gedoor 4 years ago
parent 8b76649226
commit 62a649052c
  1. 56
      app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt
  2. 3
      app/src/main/res/layout/dialog_change_source.xml

@ -1,13 +1,14 @@
package io.legado.app.model.localBook
import android.content.Context
import android.net.Uri
import android.text.TextUtils
import io.legado.app.data.entities.BookChapter
import net.sf.jazzlib.ZipFile
import io.legado.app.utils.htmlFormat
import io.legado.app.utils.isContentPath
import nl.siegmann.epublib.domain.Book
import nl.siegmann.epublib.domain.TOCReference
import nl.siegmann.epublib.epub.EpubReader
import nl.siegmann.epublib.service.MediatypeService
import org.jsoup.Jsoup
import java.io.File
import java.io.IOException
@ -19,28 +20,43 @@ class EPUBFile(context: Context, val book: io.legado.app.data.entities.Book) {
private lateinit var mCharset: Charset
init {
}
fun readBook(file: File?): Book? {
return try {
try {
val epubReader = EpubReader()
val lazyTypes =
arrayOf(
MediatypeService.CSS,
MediatypeService.GIF,
MediatypeService.JPG,
MediatypeService.PNG,
MediatypeService.MP3,
MediatypeService.MP4
)
val zipFile = ZipFile(file)
epubReader.readEpubLazy(zipFile, "utf-8", Arrays.asList(*lazyTypes))
val inputStream = if (book.bookUrl.isContentPath()) {
val uri = Uri.parse(book.bookUrl)
context.contentResolver.openInputStream(uri)
} else {
File(book.bookUrl).inputStream()
}
epubBook = epubReader.readEpub(inputStream)
} catch (e: Exception) {
null
}
}
fun getContent(chapter: BookChapter): String {
val resource = epubBook!!.resources.getByHref(chapter.url)
val content = StringBuilder()
val doc = Jsoup.parse(String(resource.data, mCharset))
val elements = doc.allElements
for (element in elements) {
val contentEs = element.textNodes()
for (i in contentEs.indices) {
val text = contentEs[i].text().trim { it <= ' ' }.htmlFormat()
if (elements.size > 1) {
if (text.isNotEmpty()) {
if (content.isNotEmpty()) {
content.append("\r\n")
}
content.append("\u3000\u3000").append(text)
}
} else {
content.append(text)
}
}
}
return content.toString()
}
fun getChapterList(epubBook: Book): ArrayList<BookChapter> {
val metadata = epubBook.metadata
book.name = metadata.firstTitle
@ -113,7 +129,7 @@ class EPUBFile(context: Context, val book: io.legado.app.data.entities.Book) {
chapter.url = ref.completeHref
chapterList.add(chapter)
}
if (ref.children != null && !ref.children.isEmpty()) {
if (ref.children != null && ref.children.isNotEmpty()) {
parseMenu(chapterList, ref.children, level + 1)
}
}

@ -3,7 +3,8 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:background="@color/background">
<androidx.appcompat.widget.Toolbar
android:id="@+id/tool_bar"

Loading…
Cancel
Save