pull/357/head
gedoor 4 years ago
parent 5036d15b84
commit d7f7c91936
  1. 25
      app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt
  2. 2
      app/src/main/java/io/legado/app/utils/UriExtensions.kt

@ -197,6 +197,7 @@ class BgTextConfigDialog : BaseDialogFragment(), FileChooserDialog.CallBack {
@Suppress("BlockingMethodInNonBlockingContext") @Suppress("BlockingMethodInNonBlockingContext")
private fun exportConfig(uri: Uri) { private fun exportConfig(uri: Uri) {
execute { execute {
val exportFiles = arrayListOf<File>()
val configDirPath = FileUtils.getPath(requireContext().eCacheDir, "readConfig") val configDirPath = FileUtils.getPath(requireContext().eCacheDir, "readConfig")
FileUtils.deleteFile(configDirPath) FileUtils.deleteFile(configDirPath)
val configDir = FileUtils.createFolderIfNotExist(configDirPath) val configDir = FileUtils.createFolderIfNotExist(configDirPath)
@ -204,6 +205,7 @@ class BgTextConfigDialog : BaseDialogFragment(), FileChooserDialog.CallBack {
FileUtils.deleteFile(configExportPath) FileUtils.deleteFile(configExportPath)
val configExportFile = FileUtils.createFileIfNotExist(configExportPath) val configExportFile = FileUtils.createFileIfNotExist(configExportPath)
configExportFile.writeText(GSON.toJson(ReadBookConfig.durConfig)) configExportFile.writeText(GSON.toJson(ReadBookConfig.durConfig))
exportFiles.add(configExportFile)
val fontPath = ReadBookConfig.textFont val fontPath = ReadBookConfig.textFont
if (fontPath.isNotEmpty()) { if (fontPath.isNotEmpty()) {
val fontName = FileUtils.getName(fontPath) val fontName = FileUtils.getName(fontPath)
@ -211,6 +213,7 @@ class BgTextConfigDialog : BaseDialogFragment(), FileChooserDialog.CallBack {
fontBytes?.let { fontBytes?.let {
val fontExportFile = FileUtils.createFileIfNotExist(configDir, fontName) val fontExportFile = FileUtils.createFileIfNotExist(configDir, fontName)
fontExportFile.writeBytes(it) fontExportFile.writeBytes(it)
exportFiles.add(fontExportFile)
} }
} }
if (ReadBookConfig.durConfig.bgType() == 2) { if (ReadBookConfig.durConfig.bgType() == 2) {
@ -219,16 +222,30 @@ class BgTextConfigDialog : BaseDialogFragment(), FileChooserDialog.CallBack {
if (bgFile.exists()) { if (bgFile.exists()) {
val bgExportFile = FileUtils.createFileIfNotExist(configDir, bgName) val bgExportFile = FileUtils.createFileIfNotExist(configDir, bgName)
bgFile.copyTo(bgExportFile) bgFile.copyTo(bgExportFile)
exportFiles.add(bgExportFile)
} }
} }
val configZipPath = FileUtils.getPath(requireContext().eCacheDir, "readConfig.zip") val configFileName = "readConfig.zip"
if (ZipUtils.zipFile(configDirPath, configZipPath)) { val configZipPath = FileUtils.getPath(requireContext().eCacheDir, configFileName)
uri.writeBytes(requireContext(), File(configZipPath).readBytes()) if (ZipUtils.zipFiles(exportFiles, File(configZipPath))) {
if (uri.isContentPath()) {
DocumentFile.fromTreeUri(requireContext(), uri)?.let { treeDoc ->
treeDoc.findFile(configFileName)?.delete()
treeDoc.createFile("", configFileName)
?.writeBytes(requireContext(), File(configZipPath).readBytes())
}
} else {
val exportPath = FileUtils.getPath(File(uri.toString()), configFileName)
FileUtils.deleteFile(exportPath)
FileUtils.createFileIfNotExist(exportPath)
.writeBytes(File(configZipPath).readBytes())
}
} }
}.onSuccess { }.onSuccess {
toast("导出成功") toast("导出成功")
}.onError { }.onError {
toast("导出失败:${it.localizedMessage}") it.printStackTrace()
longToast("导出失败:${it.localizedMessage}")
} }
} }

@ -4,6 +4,8 @@ import android.content.Context
import android.net.Uri import android.net.Uri
import java.io.File import java.io.File
fun Uri.isContentPath() = this.toString().isContentPath()
@Throws(Exception::class) @Throws(Exception::class)
fun Uri.readBytes(context: Context): ByteArray? { fun Uri.readBytes(context: Context): ByteArray? {
if (this.toString().isContentPath()) { if (this.toString().isContentPath()) {

Loading…
Cancel
Save