pull/83/head
kunfei 6 years ago
parent 5e2f4ad06b
commit de35674c43
  1. 16
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 21
      app/src/main/java/io/legado/app/help/storage/Backup.kt
  3. 21
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  4. 2
      app/src/main/java/io/legado/app/service/HttpReadAloudService.kt
  5. 2
      app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt
  6. 2
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt
  7. 2
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt
  8. 2
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt
  9. 5
      app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt
  10. 12
      app/src/main/java/io/legado/app/utils/FileUtils.kt
  11. 2
      app/src/main/java/io/legado/app/utils/LogUtils.kt

@ -41,8 +41,14 @@ object BookHelp {
} }
fun clearCache() { fun clearCache() {
FileUtils.deleteFile(getBookCachePath()) if (downloadUri.isDocumentUri(App.INSTANCE)) {
FileUtils.getFolder(getBookCachePath()) DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)
?.findFile(cacheFolderName)
?.delete()
} else {
FileUtils.deleteFile(getBookCachePath())
FileUtils.createFolderIfNotExist(getBookCachePath())
}
} }
@Synchronized @Synchronized
@ -132,11 +138,11 @@ object BookHelp {
fun delContent(book: Book, bookChapter: BookChapter) { fun delContent(book: Book, bookChapter: BookChapter) {
if (downloadUri.isDocumentUri(App.INSTANCE)) { if (downloadUri.isDocumentUri(App.INSTANCE)) {
DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let {
DocumentUtils.createFileIfNotExist( DocumentUtils.getDirDocument(
it, it,
"${bookChapterName(bookChapter)}.nb",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
)?.delete() )?.findFile("${bookChapterName(bookChapter)}.nb")
?.delete()
} }
} else { } else {
FileUtils.createFileIfNotExist( FileUtils.createFileIfNotExist(

@ -47,40 +47,40 @@ object Backup {
App.db.bookDao().allBooks.let { App.db.bookDao().allBooks.let {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
val json = GSON.toJson(it) val json = GSON.toJson(it)
FileUtils.getFile(backupPath + File.separator + "bookshelf.json") FileUtils.createFileIfNotExist(backupPath + File.separator + "bookshelf.json")
.writeText(json) .writeText(json)
} }
} }
App.db.bookGroupDao().all().let { App.db.bookGroupDao().all().let {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
val json = GSON.toJson(it) val json = GSON.toJson(it)
FileUtils.getFile(backupPath + File.separator + "bookGroup.json") FileUtils.createFileIfNotExist(backupPath + File.separator + "bookGroup.json")
.writeText(json) .writeText(json)
} }
} }
App.db.bookSourceDao().all.let { App.db.bookSourceDao().all.let {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
val json = GSON.toJson(it) val json = GSON.toJson(it)
FileUtils.getFile(backupPath + File.separator + "bookSource.json") FileUtils.createFileIfNotExist(backupPath + File.separator + "bookSource.json")
.writeText(json) .writeText(json)
} }
} }
App.db.rssSourceDao().all.let { App.db.rssSourceDao().all.let {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
val json = GSON.toJson(it) val json = GSON.toJson(it)
FileUtils.getFile(backupPath + File.separator + "rssSource.json") FileUtils.createFileIfNotExist(backupPath + File.separator + "rssSource.json")
.writeText(json) .writeText(json)
} }
} }
App.db.replaceRuleDao().all.let { App.db.replaceRuleDao().all.let {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
val json = GSON.toJson(it) val json = GSON.toJson(it)
FileUtils.getFile(backupPath + File.separator + "replaceRule.json") FileUtils.createFileIfNotExist(backupPath + File.separator + "replaceRule.json")
.writeText(json) .writeText(json)
} }
} }
GSON.toJson(ReadBookConfig.configList)?.let { GSON.toJson(ReadBookConfig.configList)?.let {
FileUtils.getFile(backupPath + File.separator + ReadBookConfig.readConfigFileName) FileUtils.createFileIfNotExist(backupPath + File.separator + ReadBookConfig.readConfigFileName)
.writeText(it) .writeText(it)
} }
Preferences.getSharedPreferences(App.INSTANCE, backupPath, "config")?.let { sp -> Preferences.getSharedPreferences(App.INSTANCE, backupPath, "config")?.let { sp ->
@ -113,7 +113,7 @@ object Backup {
doc?.let { doc?.let {
DocumentUtils.writeText( DocumentUtils.writeText(
context, context,
FileUtils.getFile(backupPath + File.separator + fileName).readText(), FileUtils.createFileIfNotExist(backupPath + File.separator + fileName).readText(),
doc.uri doc.uri
) )
} }
@ -124,8 +124,11 @@ object Backup {
private fun copyBackup() { private fun copyBackup() {
try { try {
for (fileName in backupFileNames) { for (fileName in backupFileNames) {
FileUtils.getFile(backupPath + File.separator + "bookshelf.json") FileUtils.createFileIfNotExist(backupPath + File.separator + "bookshelf.json")
.copyTo(FileUtils.getFile(legadoPath + File.separator + "bookshelf.json"), true) .copyTo(
FileUtils.createFileIfNotExist(legadoPath + File.separator + "bookshelf.json"),
true
)
} }
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()

@ -37,7 +37,7 @@ object Restore {
for (fileName in Backup.backupFileNames) { for (fileName in Backup.backupFileNames) {
if (doc.name == fileName) { if (doc.name == fileName) {
DocumentUtils.readText(context, doc.uri)?.let { DocumentUtils.readText(context, doc.uri)?.let {
FileUtils.getFile(Backup.backupPath + File.separator + fileName) FileUtils.createFileIfNotExist(Backup.backupPath + File.separator + fileName)
.writeText(it) .writeText(it)
} }
} }
@ -50,7 +50,7 @@ object Restore {
suspend fun restore(path: String) { suspend fun restore(path: String) {
withContext(IO) { withContext(IO) {
try { try {
val file = FileUtils.getFile(path + File.separator + "bookshelf.json") val file = FileUtils.createFileIfNotExist(path + File.separator + "bookshelf.json")
val json = file.readText() val json = file.readText()
GSON.fromJsonArray<Book>(json)?.let { GSON.fromJsonArray<Book>(json)?.let {
App.db.bookDao().insert(*it.toTypedArray()) App.db.bookDao().insert(*it.toTypedArray())
@ -59,7 +59,7 @@ object Restore {
e.printStackTrace() e.printStackTrace()
} }
try { try {
val file = FileUtils.getFile(path + File.separator + "bookGroup.json") val file = FileUtils.createFileIfNotExist(path + File.separator + "bookGroup.json")
val json = file.readText() val json = file.readText()
GSON.fromJsonArray<BookGroup>(json)?.let { GSON.fromJsonArray<BookGroup>(json)?.let {
App.db.bookGroupDao().insert(*it.toTypedArray()) App.db.bookGroupDao().insert(*it.toTypedArray())
@ -68,7 +68,7 @@ object Restore {
e.printStackTrace() e.printStackTrace()
} }
try { try {
val file = FileUtils.getFile(path + File.separator + "bookSource.json") val file = FileUtils.createFileIfNotExist(path + File.separator + "bookSource.json")
val json = file.readText() val json = file.readText()
GSON.fromJsonArray<BookSource>(json)?.let { GSON.fromJsonArray<BookSource>(json)?.let {
App.db.bookSourceDao().insert(*it.toTypedArray()) App.db.bookSourceDao().insert(*it.toTypedArray())
@ -77,7 +77,7 @@ object Restore {
e.printStackTrace() e.printStackTrace()
} }
try { try {
val file = FileUtils.getFile(path + File.separator + "rssSource.json") val file = FileUtils.createFileIfNotExist(path + File.separator + "rssSource.json")
val json = file.readText() val json = file.readText()
GSON.fromJsonArray<RssSource>(json)?.let { GSON.fromJsonArray<RssSource>(json)?.let {
App.db.rssSourceDao().insert(*it.toTypedArray()) App.db.rssSourceDao().insert(*it.toTypedArray())
@ -86,7 +86,8 @@ object Restore {
e.printStackTrace() e.printStackTrace()
} }
try { try {
val file = FileUtils.getFile(path + File.separator + "replaceRule.json") val file =
FileUtils.createFileIfNotExist(path + File.separator + "replaceRule.json")
val json = file.readText() val json = file.readText()
GSON.fromJsonArray<ReplaceRule>(json)?.let { GSON.fromJsonArray<ReplaceRule>(json)?.let {
App.db.replaceRuleDao().insert(*it.toTypedArray()) App.db.replaceRuleDao().insert(*it.toTypedArray())
@ -96,7 +97,7 @@ object Restore {
} }
try { try {
val file = val file =
FileUtils.getFile(path + File.separator + ReadBookConfig.readConfigFileName) FileUtils.createFileIfNotExist(path + File.separator + ReadBookConfig.readConfigFileName)
val configFile = val configFile =
File(App.INSTANCE.filesDir.absolutePath + File.separator + ReadBookConfig.readConfigFileName) File(App.INSTANCE.filesDir.absolutePath + File.separator + ReadBookConfig.readConfigFileName)
if (file.exists()) { if (file.exists()) {
@ -125,7 +126,7 @@ object Restore {
GlobalScope.launch(IO) { GlobalScope.launch(IO) {
try {// 导入书架 try {// 导入书架
val shelfFile = val shelfFile =
FileUtils.getFile(Backup.defaultPath + File.separator + "myBookShelf.json") FileUtils.createFileIfNotExist(Backup.defaultPath + File.separator + "myBookShelf.json")
val json = shelfFile.readText() val json = shelfFile.readText()
val importCount = importOldBookshelf(json) val importCount = importOldBookshelf(json)
withContext(Main) { withContext(Main) {
@ -139,7 +140,7 @@ object Restore {
try {// Book source try {// Book source
val sourceFile = val sourceFile =
FileUtils.getFile(Backup.defaultPath + File.separator + "myBookSource.json") FileUtils.createFileIfNotExist(Backup.defaultPath + File.separator + "myBookSource.json")
val json = sourceFile.readText() val json = sourceFile.readText()
val importCount = importOldSource(json) val importCount = importOldSource(json)
withContext(Main) { withContext(Main) {
@ -153,7 +154,7 @@ object Restore {
try {// Replace rules try {// Replace rules
val ruleFile = val ruleFile =
FileUtils.getFile(Backup.defaultPath + File.separator + "myBookReplaceRule.json") FileUtils.createFileIfNotExist(Backup.defaultPath + File.separator + "myBookReplaceRule.json")
val json = ruleFile.readText() val json = ruleFile.readText()
val importCount = importOldReplaceRule(json) val importCount = importOldReplaceRule(json)
withContext(Main) { withContext(Main) {

@ -100,7 +100,7 @@ class HttpReadAloudService : BaseReadAloudService(),
} }
private fun getSpeakFile(index: Int = nowSpeak): File { private fun getSpeakFile(index: Int = nowSpeak): File {
return FileUtils.getFile("${ttsFolder}${File.separator}${index}.mp3") return FileUtils.createFileIfNotExist("${ttsFolder}${File.separator}${index}.mp3")
} }
private fun getAudioBody(content: String): Map<String, String> { private fun getAudioBody(content: String): Map<String, String> {

@ -173,7 +173,7 @@ class BgTextConfigDialog : DialogFragment() {
var file = requireContext().getExternalFilesDir(null) var file = requireContext().getExternalFilesDir(null)
?: requireContext().filesDir ?: requireContext().filesDir
file = file =
FileUtils.getFile(file.absolutePath + File.separator + "bg" + File.separator + doc.name) FileUtils.createFileIfNotExist(file.absolutePath + File.separator + "bg" + File.separator + doc.name)
DocumentUtils.readBytes(requireContext(), uri)?.let { DocumentUtils.readBytes(requireContext(), uri)?.let {
file.writeBytes(it) file.writeBytes(it)
ReadBookConfig.getConfig().setBg(2, file.absolutePath) ReadBookConfig.getConfig().setBg(2, file.absolutePath)

@ -91,7 +91,7 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application)
}.let { }.let {
val json = GSON.toJson(it) val json = GSON.toJson(it)
val file = val file =
FileUtils.getFile(Backup.exportPath + File.separator + "exportBookSource.json") FileUtils.createFileIfNotExist(Backup.exportPath + File.separator + "exportBookSource.json")
file.writeText(json) file.writeText(json)
} }
}.onSuccess { }.onSuccess {

@ -80,7 +80,7 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application
}.let { }.let {
val json = GSON.toJson(it) val json = GSON.toJson(it)
val file = val file =
FileUtils.getFile(Backup.exportPath + File.separator + "exportReplaceRule.json") FileUtils.createFileIfNotExist(Backup.exportPath + File.separator + "exportReplaceRule.json")
file.writeText(json) file.writeText(json)
} }
}.onSuccess { }.onSuccess {

@ -65,7 +65,7 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application)
App.db.rssSourceDao().getRssSources(*ids.toTypedArray()).let { App.db.rssSourceDao().getRssSources(*ids.toTypedArray()).let {
val json = GSON.toJson(it) val json = GSON.toJson(it)
val file = val file =
FileUtils.getFile(Backup.exportPath + File.separator + "exportRssSource.json") FileUtils.createFileIfNotExist(Backup.exportPath + File.separator + "exportRssSource.json")
file.writeText(json) file.writeText(json)
} }
}.onSuccess { }.onSuccess {

@ -126,7 +126,7 @@ class FontSelectDialog : DialogFragment(),
DocumentFile.fromTreeUri(App.INSTANCE, uri)?.listFiles()?.forEach { file -> DocumentFile.fromTreeUri(App.INSTANCE, uri)?.listFiles()?.forEach { file ->
if (file.name?.toLowerCase()?.matches(".*\\.[ot]tf".toRegex()) == true) { if (file.name?.toLowerCase()?.matches(".*\\.[ot]tf".toRegex()) == true) {
DocumentUtils.readBytes(App.INSTANCE, file.uri)?.let { DocumentUtils.readBytes(App.INSTANCE, file.uri)?.let {
FileUtils.getFile(fontCacheFolder + file.name).writeBytes(it) FileUtils.createFileIfNotExist(fontCacheFolder + file.name).writeBytes(it)
} }
} }
} }
@ -162,7 +162,8 @@ class FontSelectDialog : DialogFragment(),
override fun onClick(file: File) { override fun onClick(file: File) {
launch(IO) { launch(IO) {
file.copyTo(FileUtils.getFile(fontFolder + file.name), true).absolutePath.let { path -> file.copyTo(FileUtils.createFileIfNotExist(fontFolder + file.name), true)
.absolutePath.let { path ->
val cb = (parentFragment as? CallBack) ?: (activity as? CallBack) val cb = (parentFragment as? CallBack) ?: (activity as? CallBack)
cb?.let { cb?.let {
if (it.curFontPath != path) { if (it.curFontPath != path) {

@ -22,12 +22,12 @@ object FileUtils {
fun createFileIfNotExist(file: File, fileName: String, vararg subDirs: String): File { fun createFileIfNotExist(file: File, fileName: String, vararg subDirs: String): File {
val filePath = val filePath =
file.absolutePath + File.separator + subDirs.joinToString(File.separator) + File.separator + fileName file.absolutePath + File.separator + subDirs.joinToString(File.separator) + File.separator + fileName
return getFile(filePath) return createFileIfNotExist(filePath)
} }
fun createFileIfNotExist(file: File, vararg subDirs: String): File { fun createFileIfNotExist(file: File, vararg subDirs: String): File {
val filePath = file.absolutePath + File.separator + subDirs.joinToString(File.separator) val filePath = file.absolutePath + File.separator + subDirs.joinToString(File.separator)
return getFolder(filePath) return createFolderIfNotExist(filePath)
} }
fun getCachePath(): String { fun getCachePath(): String {
@ -35,8 +35,7 @@ object FileUtils {
?: App.INSTANCE.cacheDir.absolutePath ?: App.INSTANCE.cacheDir.absolutePath
} }
//获取文件夹 fun createFolderIfNotExist(filePath: String): File {
fun getFolder(filePath: String): File {
val file = File(filePath) val file = File(filePath)
//如果文件夹不存在,就创建它 //如果文件夹不存在,就创建它
if (!file.exists()) { if (!file.exists()) {
@ -45,15 +44,14 @@ object FileUtils {
return file return file
} }
//获取文件
@Synchronized @Synchronized
fun getFile(filePath: String): File { fun createFileIfNotExist(filePath: String): File {
val file = File(filePath) val file = File(filePath)
try { try {
if (!file.exists()) { if (!file.exists()) {
//创建父类文件夹 //创建父类文件夹
file.parent?.let { file.parent?.let {
getFolder(it) createFolderIfNotExist(it)
} }
//创建文件 //创建文件
file.createNewFile() file.createNewFile()

@ -30,7 +30,7 @@ object LogUtils {
private val fileHandler by lazy { private val fileHandler by lazy {
val logFolder = FileUtils.getCachePath() + File.separator + "logs" val logFolder = FileUtils.getCachePath() + File.separator + "logs"
FileUtils.getFolder(logFolder) FileUtils.createFolderIfNotExist(logFolder)
FileHandler( FileHandler(
logFolder + File.separator + "app.log", logFolder + File.separator + "app.log",
10240, 10240,

Loading…
Cancel
Save