mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
@@ -48,7 +48,6 @@ import io.legado.app.utils.flowWithLifecycleAndDatabaseChange
|
||||
import io.legado.app.utils.iconItemOnLongClick
|
||||
import io.legado.app.utils.isContentScheme
|
||||
import io.legado.app.utils.observeEvent
|
||||
import io.legado.app.utils.parseToUri
|
||||
import io.legado.app.utils.showDialogFragment
|
||||
import io.legado.app.utils.startService
|
||||
import io.legado.app.utils.toastOnUi
|
||||
@@ -184,6 +183,7 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
|
||||
CacheBook.stop(this@CacheActivity)
|
||||
}
|
||||
}
|
||||
|
||||
R.id.menu_download_all -> {
|
||||
if (!CacheBook.isRun) {
|
||||
adapter.getItems().forEach { book ->
|
||||
@@ -321,9 +321,7 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
|
||||
|
||||
override fun export(position: Int) {
|
||||
val path = ACache.get().getAsString(exportBookPathKey)
|
||||
if (path.isNullOrEmpty()) {
|
||||
selectExportFolder(position)
|
||||
} else if (FileDoc.fromUri(path.parseToUri(), true).checkWrite() != true) {
|
||||
if (path.isNullOrEmpty() || !FileDoc.fromDir(path).checkWrite()) {
|
||||
selectExportFolder(position)
|
||||
} else if (enableCustomExport()) {// 启用自定义导出 and 导出类型为Epub
|
||||
configExportSection(path, position)
|
||||
|
||||
@@ -304,14 +304,14 @@ fun FileDoc.delete() {
|
||||
asDocumentFile()?.delete()
|
||||
}
|
||||
|
||||
fun FileDoc.checkWrite(): Boolean? {
|
||||
fun FileDoc.checkWrite(): Boolean {
|
||||
if (!isDir) {
|
||||
throw NoStackTraceException("只能检查目录")
|
||||
}
|
||||
asFile()?.let {
|
||||
return it.checkWrite()
|
||||
}
|
||||
return asDocumentFile()?.checkWrite()
|
||||
return asDocumentFile()!!.checkWrite()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -358,17 +358,22 @@ fun DocumentFile.readBytes(context: Context): ByteArray {
|
||||
}
|
||||
|
||||
fun DocumentFile.checkWrite(): Boolean {
|
||||
var file: DocumentFile? = null
|
||||
return try {
|
||||
val filename = System.currentTimeMillis().toString()
|
||||
createFile(FileUtils.getMimeType(filename), filename)?.let {
|
||||
it.openOutputStream()?.let { out ->
|
||||
out.use { }
|
||||
it.delete()
|
||||
return true
|
||||
file = createFile(FileUtils.getMimeType(filename), filename)
|
||||
file?.openOutputStream()?.let { out ->
|
||||
out.bufferedWriter().use { it.write(filename) }
|
||||
file.openInputStream()?.let { input ->
|
||||
input.bufferedReader().use {
|
||||
return it.readText() == filename
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
} catch (e: Exception) {
|
||||
false
|
||||
} finally {
|
||||
file?.delete()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,14 +70,16 @@ fun File.createFolderReplace(): File {
|
||||
}
|
||||
|
||||
fun File.checkWrite(): Boolean {
|
||||
var file: File? = null
|
||||
return try {
|
||||
val filename = System.currentTimeMillis().toString()
|
||||
val file = FileUtils.createFileIfNotExist(this, filename)
|
||||
file.outputStream().use { }
|
||||
file.delete()
|
||||
true
|
||||
file = FileUtils.createFileIfNotExist(this, filename)
|
||||
file.outputStream().bufferedWriter().use { it.write(filename) }
|
||||
file.inputStream().bufferedReader().use { it.readText() == filename }
|
||||
} catch (e: Exception) {
|
||||
false
|
||||
} finally {
|
||||
file?.delete()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user