|
|
@ -7,7 +7,9 @@ import androidx.lifecycle.MutableLiveData |
|
|
|
import io.legado.app.constant.AppLog |
|
|
|
import io.legado.app.constant.AppLog |
|
|
|
import io.legado.app.constant.AppPattern.bookFileRegex |
|
|
|
import io.legado.app.constant.AppPattern.bookFileRegex |
|
|
|
import io.legado.app.exception.NoStackTraceException |
|
|
|
import io.legado.app.exception.NoStackTraceException |
|
|
|
|
|
|
|
import io.legado.app.help.IntentData |
|
|
|
import io.legado.app.model.localBook.LocalBook |
|
|
|
import io.legado.app.model.localBook.LocalBook |
|
|
|
|
|
|
|
import io.legado.app.utils.inputStream |
|
|
|
import io.legado.app.utils.isJson |
|
|
|
import io.legado.app.utils.isJson |
|
|
|
import io.legado.app.utils.printOnDebug |
|
|
|
import io.legado.app.utils.printOnDebug |
|
|
|
import java.io.File |
|
|
|
import java.io.File |
|
|
@ -23,24 +25,19 @@ class FileAssociationViewModel(application: Application) : BaseAssociationViewMo |
|
|
|
fun dispatchIndent(uri: Uri) { |
|
|
|
fun dispatchIndent(uri: Uri) { |
|
|
|
execute { |
|
|
|
execute { |
|
|
|
lateinit var fileName: String |
|
|
|
lateinit var fileName: String |
|
|
|
lateinit var content: String |
|
|
|
|
|
|
|
lateinit var fileStream: InputStream |
|
|
|
|
|
|
|
//如果是普通的url,需要根据返回的内容判断是什么 |
|
|
|
//如果是普通的url,需要根据返回的内容判断是什么 |
|
|
|
if (uri.scheme == "file" || uri.scheme == "content") { |
|
|
|
if (uri.scheme == "file" || uri.scheme == "content") { |
|
|
|
if (uri.scheme == "file") { |
|
|
|
fileName = if (uri.scheme == "file") { |
|
|
|
val file = File(uri.path.toString()) |
|
|
|
val file = File(uri.path.toString()) |
|
|
|
fileStream = file.inputStream() |
|
|
|
file.name |
|
|
|
fileName = file.name |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
val file = DocumentFile.fromSingleUri(context, uri) |
|
|
|
val file = DocumentFile.fromSingleUri(context, uri) |
|
|
|
if (file?.exists() != true) throw NoStackTraceException("文件不存在") |
|
|
|
if (file?.exists() != true) throw NoStackTraceException("文件不存在") |
|
|
|
fileStream = context.contentResolver.openInputStream(uri)!! |
|
|
|
file.name ?: "" |
|
|
|
fileName = file.name ?: "" |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
kotlin.runCatching { |
|
|
|
kotlin.runCatching { |
|
|
|
content = fileStream.reader(Charsets.UTF_8).use { it.readText() } |
|
|
|
if (uri.inputStream(context).isJson()) { |
|
|
|
if (content.isJson()) { |
|
|
|
importJson(uri) |
|
|
|
importJson(content) |
|
|
|
|
|
|
|
return@execute |
|
|
|
return@execute |
|
|
|
} |
|
|
|
} |
|
|
|
}.onFailure { |
|
|
|
}.onFailure { |
|
|
@ -58,6 +55,7 @@ class FileAssociationViewModel(application: Application) : BaseAssociationViewMo |
|
|
|
}.onError { |
|
|
|
}.onError { |
|
|
|
it.printOnDebug() |
|
|
|
it.printOnDebug() |
|
|
|
errorLive.postValue(it.localizedMessage) |
|
|
|
errorLive.postValue(it.localizedMessage) |
|
|
|
|
|
|
|
AppLog.put("无法打开文件\n${it.localizedMessage}", it) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|