pull/83/head
kunfei 5 years ago
parent 43d3b981df
commit 95ef2d9a03
  1. 29
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 30
      app/src/main/java/io/legado/app/utils/DocumentUtils.kt
  3. 3
      app/src/main/java/io/legado/app/utils/UriExtensions.kt

@ -1,20 +1,19 @@
package io.legado.app.help
import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import io.legado.app.App
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.utils.MD5Utils
import io.legado.app.utils.getPrefInt
import io.legado.app.utils.getPrefString
import io.legado.app.utils.*
import org.apache.commons.text.similarity.JaccardSimilarity
import java.io.File
import kotlin.math.min
object BookHelp {
private const val cacheFolderName = "book_cache"
private var downloadPath: String =
App.INSTANCE.getPrefString(PreferKey.downloadPath)
?: App.INSTANCE.getExternalFilesDir(null)?.absolutePath
@ -27,7 +26,7 @@ object BookHelp {
?: App.INSTANCE.cacheDir.absolutePath
}
private val dwnloadUri get() = Uri.parse(downloadPath)
private val downloadUri get() = Uri.parse(downloadPath)
private fun bookFolderName(book: Book): String {
return formatFolderName(book.name) + MD5Utils.md5Encode16(book.bookUrl)
@ -38,7 +37,7 @@ object BookHelp {
}
private fun getBookCachePath(): String {
return "$downloadPath${File.separator}book_cache"
return "$downloadPath${File.separator}$cacheFolderName"
}
fun clearCache() {
@ -49,6 +48,15 @@ object BookHelp {
@Synchronized
fun saveContent(book: Book, bookChapter: BookChapter, content: String) {
if (content.isEmpty()) return
if (downloadUri.isDocumentUri(App.INSTANCE)) {
DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let {
DocumentUtils.createFileIfNotExist(
it,
bookChapterName(bookChapter),
subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
)
}
} else {
FileHelp.getFolder(getBookFolder(book)).listFiles()?.forEach {
if (it.name.startsWith(String.format("%05d", bookChapter.index))) {
it.delete()
@ -59,6 +67,7 @@ object BookHelp {
val file = FileHelp.getFile(filePath)
file.writeText(content)
}
}
fun getChapterCount(book: Book): Int {
return FileHelp.getFolder(getBookFolder(book)).list()?.size ?: 0
@ -132,16 +141,16 @@ object BookHelp {
}
var newIndex = 0
val jaccardSimilarity = JaccardSimilarity()
val jSimilarity = JaccardSimilarity()
var similarity = if (chapters.size > index) {
jaccardSimilarity.apply(title, chapters[index].title)
jSimilarity.apply(title, chapters[index].title)
} else 0.0
if (similarity == 1.0) {
return index
} else {
for (i in 1..50) {
if (index - i in chapters.indices) {
jaccardSimilarity.apply(title, chapters[index - i].title).let {
jSimilarity.apply(title, chapters[index - i].title).let {
if (it > similarity) {
similarity = it
newIndex = index - i
@ -152,7 +161,7 @@ object BookHelp {
}
}
if (index + i in chapters.indices) {
jaccardSimilarity.apply(title, chapters[index + i].title).let {
jSimilarity.apply(title, chapters[index + i].title).let {
if (it > similarity) {
similarity = it
newIndex = index + i

@ -2,9 +2,39 @@ package io.legado.app.utils
import android.content.Context
import android.net.Uri
import androidx.documentfile.provider.DocumentFile
object DocumentUtils {
fun createFileIfNotExist(
root: DocumentFile,
fileName: String,
mimeType: String = "",
vararg subDirs: String
): DocumentFile? {
val parent: DocumentFile? = createFileIfNotExist(root, *subDirs)
return parent?.createFile(mimeType, fileName)
}
fun createFileIfNotExist(root: DocumentFile, vararg subDirs: String): DocumentFile? {
var parent: DocumentFile? = root
for (subDirName in subDirs) {
val subDir = parent?.findFile(subDirName)
?: parent?.createDirectory(subDirName)
parent = subDir
}
return parent
}
fun getDirDocument(root: DocumentFile, vararg subDirs: String): DocumentFile? {
var parent = root
for (subDirName in subDirs) {
val subDir = parent.findFile(subDirName)
parent = subDir ?: return null
}
return parent
}
@JvmStatic
@Throws(Exception::class)
fun writeText(context: Context, data: String, fileUri: Uri): Boolean {

@ -5,6 +5,9 @@ import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import java.io.File
fun Uri.isDocumentUri(context: Context): Boolean {
return DocumentFile.isDocumentUri(context, this)
}
@Throws(Exception::class)
fun Uri.readBytes(context: Context): ByteArray? {

Loading…
Cancel
Save