小说阅读接口 优化

添加 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 package io.legado.app.model.localBook
import android.content.Context import android.content.Context
import android.net.Uri
import android.text.TextUtils import android.text.TextUtils
import io.legado.app.data.entities.BookChapter 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.Book
import nl.siegmann.epublib.domain.TOCReference import nl.siegmann.epublib.domain.TOCReference
import nl.siegmann.epublib.epub.EpubReader import nl.siegmann.epublib.epub.EpubReader
import nl.siegmann.epublib.service.MediatypeService
import org.jsoup.Jsoup import org.jsoup.Jsoup
import java.io.File import java.io.File
import java.io.IOException 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 private lateinit var mCharset: Charset
init { init {
try {
}
fun readBook(file: File?): Book? {
return try {
val epubReader = EpubReader() val epubReader = EpubReader()
val lazyTypes = val inputStream = if (book.bookUrl.isContentPath()) {
arrayOf( val uri = Uri.parse(book.bookUrl)
MediatypeService.CSS, context.contentResolver.openInputStream(uri)
MediatypeService.GIF, } else {
MediatypeService.JPG, File(book.bookUrl).inputStream()
MediatypeService.PNG, }
MediatypeService.MP3, epubBook = epubReader.readEpub(inputStream)
MediatypeService.MP4
)
val zipFile = ZipFile(file)
epubReader.readEpubLazy(zipFile, "utf-8", Arrays.asList(*lazyTypes))
} catch (e: Exception) { } 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> { fun getChapterList(epubBook: Book): ArrayList<BookChapter> {
val metadata = epubBook.metadata val metadata = epubBook.metadata
book.name = metadata.firstTitle book.name = metadata.firstTitle
@ -113,7 +129,7 @@ class EPUBFile(context: Context, val book: io.legado.app.data.entities.Book) {
chapter.url = ref.completeHref chapter.url = ref.completeHref
chapterList.add(chapter) chapterList.add(chapter)
} }
if (ref.children != null && !ref.children.isEmpty()) { if (ref.children != null && ref.children.isNotEmpty()) {
parseMenu(chapterList, ref.children, level + 1) parseMenu(chapterList, ref.children, level + 1)
} }
} }

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

Loading…
Cancel
Save