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

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

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

@ -100,7 +100,7 @@ class HttpReadAloudService : BaseReadAloudService(),
}
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> {

@ -173,7 +173,7 @@ class BgTextConfigDialog : DialogFragment() {
var file = requireContext().getExternalFilesDir(null)
?: requireContext().filesDir
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 {
file.writeBytes(it)
ReadBookConfig.getConfig().setBg(2, file.absolutePath)

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

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

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

@ -126,7 +126,7 @@ class FontSelectDialog : DialogFragment(),
DocumentFile.fromTreeUri(App.INSTANCE, uri)?.listFiles()?.forEach { file ->
if (file.name?.toLowerCase()?.matches(".*\\.[ot]tf".toRegex()) == true) {
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) {
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)
cb?.let {
if (it.curFontPath != path) {

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

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

Loading…
Cancel
Save