|  |  | @ -1,8 +1,8 @@ | 
			
		
	
		
		
			
				
					
					|  |  |  | package io.legado.app.model.localBook |  |  |  | package io.legado.app.model.localBook | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | import android.content.Context |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | import android.net.Uri |  |  |  | import android.net.Uri | 
			
		
	
		
		
			
				
					
					|  |  |  | import android.text.TextUtils |  |  |  | import android.text.TextUtils | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | import io.legado.app.App | 
			
		
	
		
		
			
				
					
					|  |  |  | import io.legado.app.data.entities.BookChapter |  |  |  | import io.legado.app.data.entities.BookChapter | 
			
		
	
		
		
			
				
					
					|  |  |  | import io.legado.app.utils.htmlFormat |  |  |  | import io.legado.app.utils.htmlFormat | 
			
		
	
		
		
			
				
					
					|  |  |  | import io.legado.app.utils.isContentPath |  |  |  | import io.legado.app.utils.isContentPath | 
			
		
	
	
		
		
			
				
					|  |  | @ -15,8 +15,30 @@ import java.io.IOException | 
			
		
	
		
		
			
				
					
					|  |  |  | import java.nio.charset.Charset |  |  |  | import java.nio.charset.Charset | 
			
		
	
		
		
			
				
					
					|  |  |  | import java.util.* |  |  |  | import java.util.* | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | class EPUBFile(context: Context, val book: io.legado.app.data.entities.Book) { |  |  |  | class EPUBFile(val book: io.legado.app.data.entities.Book) { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     var epubBook: Book? = null |  |  |  | 
 | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     companion object { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         private var eFile: EPUBFile? = null | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         @Synchronized | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         fun getEFile(book: io.legado.app.data.entities.Book): EPUBFile { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             if (eFile == null || eFile?.book?.bookUrl == book.bookUrl) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 eFile = EPUBFile(book) | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 return eFile!! | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             return eFile!! | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         fun getChapterList(book: io.legado.app.data.entities.Book): ArrayList<BookChapter> { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             return getEFile(book).getChapterList() | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         fun getContent(book: io.legado.app.data.entities.Book, chapter: BookChapter): String? { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             return getEFile(book).getContent(chapter) | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     private var epubBook: Book? = null | 
			
		
	
		
		
			
				
					
					|  |  |  |     private lateinit var mCharset: Charset |  |  |  |     private lateinit var mCharset: Charset | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     init { |  |  |  |     init { | 
			
		
	
	
		
		
			
				
					|  |  | @ -24,7 +46,7 @@ class EPUBFile(context: Context, val book: io.legado.app.data.entities.Book) { | 
			
		
	
		
		
			
				
					
					|  |  |  |             val epubReader = EpubReader() |  |  |  |             val epubReader = EpubReader() | 
			
		
	
		
		
			
				
					
					|  |  |  |             val inputStream = if (book.bookUrl.isContentPath()) { |  |  |  |             val inputStream = if (book.bookUrl.isContentPath()) { | 
			
		
	
		
		
			
				
					
					|  |  |  |                 val uri = Uri.parse(book.bookUrl) |  |  |  |                 val uri = Uri.parse(book.bookUrl) | 
			
		
	
		
		
			
				
					
					|  |  |  |                 context.contentResolver.openInputStream(uri) |  |  |  |                 App.INSTANCE.contentResolver.openInputStream(uri) | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |             } else { |  |  |  |             } else { | 
			
		
	
		
		
			
				
					
					|  |  |  |                 File(book.bookUrl).inputStream() |  |  |  |                 File(book.bookUrl).inputStream() | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |             } | 
			
		
	
	
		
		
			
				
					|  |  | @ -33,8 +55,9 @@ class EPUBFile(context: Context, val book: io.legado.app.data.entities.Book) { | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     fun getContent(chapter: BookChapter): String { |  |  |  |     fun getContent(chapter: BookChapter): String? { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         val resource = epubBook!!.resources.getByHref(chapter.url) |  |  |  |         epubBook?.let { eBook -> | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             val resource = eBook.resources.getByHref(chapter.url) | 
			
		
	
		
		
			
				
					
					|  |  |  |             val content = StringBuilder() |  |  |  |             val content = StringBuilder() | 
			
		
	
		
		
			
				
					
					|  |  |  |             val doc = Jsoup.parse(String(resource.data, mCharset)) |  |  |  |             val doc = Jsoup.parse(String(resource.data, mCharset)) | 
			
		
	
		
		
			
				
					
					|  |  |  |             val elements = doc.allElements |  |  |  |             val elements = doc.allElements | 
			
		
	
	
		
		
			
				
					|  |  | @ -56,9 +79,13 @@ class EPUBFile(context: Context, val book: io.legado.app.data.entities.Book) { | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |             return content.toString() |  |  |  |             return content.toString() | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         return null | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     fun getChapterList(epubBook: Book): ArrayList<BookChapter> { |  |  |  |     fun getChapterList(): ArrayList<BookChapter> { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         val metadata = epubBook.metadata |  |  |  |         val chapterList = ArrayList<BookChapter>() | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         epubBook?.let { eBook -> | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             val metadata = eBook.metadata | 
			
		
	
		
		
			
				
					
					|  |  |  |             book.name = metadata.firstTitle |  |  |  |             book.name = metadata.firstTitle | 
			
		
	
		
		
			
				
					
					|  |  |  |             if (metadata.authors.size > 0) { |  |  |  |             if (metadata.authors.size > 0) { | 
			
		
	
		
		
			
				
					
					|  |  |  |                 val author = |  |  |  |                 val author = | 
			
		
	
	
		
		
			
				
					|  |  | @ -68,12 +95,10 @@ class EPUBFile(context: Context, val book: io.legado.app.data.entities.Book) { | 
			
		
	
		
		
			
				
					
					|  |  |  |             if (metadata.descriptions.size > 0) { |  |  |  |             if (metadata.descriptions.size > 0) { | 
			
		
	
		
		
			
				
					
					|  |  |  |                 book.intro = Jsoup.parse(metadata.descriptions[0]).text() |  |  |  |                 book.intro = Jsoup.parse(metadata.descriptions[0]).text() | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |         val chapterList = ArrayList<BookChapter>() |  |  |  | 
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         val refs = |  |  |  |             val refs = eBook.tableOfContents.tocReferences | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             epubBook.tableOfContents.tocReferences |  |  |  |  | 
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |             if (refs == null || refs.isEmpty()) { |  |  |  |             if (refs == null || refs.isEmpty()) { | 
			
		
	
		
		
			
				
					
					|  |  |  |             val spineReferences = |  |  |  |                 val spineReferences = eBook.spine.spineReferences | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 epubBook.spine.spineReferences |  |  |  |  | 
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |                 var i = 0 |  |  |  |                 var i = 0 | 
			
		
	
		
		
			
				
					
					|  |  |  |                 val size = spineReferences.size |  |  |  |                 val size = spineReferences.size | 
			
		
	
		
		
			
				
					
					|  |  |  |                 while (i < size) { |  |  |  |                 while (i < size) { | 
			
		
	
	
		
		
			
				
					|  |  | @ -110,11 +135,10 @@ class EPUBFile(context: Context, val book: io.legado.app.data.entities.Book) { | 
			
		
	
		
		
			
				
					
					|  |  |  |                     chapterList[i].index = i |  |  |  |                     chapterList[i].index = i | 
			
		
	
		
		
			
				
					
					|  |  |  |                 } |  |  |  |                 } | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |         } | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |         return chapterList |  |  |  |         return chapterList | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     private fun parseMenu( |  |  |  |     private fun parseMenu( | 
			
		
	
		
		
			
				
					
					|  |  |  |         chapterList: ArrayList<BookChapter>, |  |  |  |         chapterList: ArrayList<BookChapter>, | 
			
		
	
		
		
			
				
					
					|  |  |  |         refs: List<TOCReference>?, |  |  |  |         refs: List<TOCReference>?, | 
			
		
	
	
		
		
			
				
					|  |  | 
 |