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

@ -2,9 +2,39 @@ package io.legado.app.utils
import android.content.Context import android.content.Context
import android.net.Uri import android.net.Uri
import androidx.documentfile.provider.DocumentFile
object DocumentUtils { 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 @JvmStatic
@Throws(Exception::class) @Throws(Exception::class)
fun writeText(context: Context, data: String, fileUri: Uri): Boolean { fun writeText(context: Context, data: String, fileUri: Uri): Boolean {

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

Loading…
Cancel
Save