feat: 修复崩溃bug

pull/238/head
gedoor 5 years ago
parent 1d292eb97a
commit dc6efbb514
  1. 105
      app/src/main/java/io/legado/app/ui/config/FileAssociationViewModel.kt

@ -13,66 +13,73 @@ import io.legado.app.utils.readText
import java.io.File import java.io.File
class FileAssociationViewModel(application: Application) : BaseViewModel(application) { class FileAssociationViewModel(application: Application) : BaseViewModel(application) {
fun dispatchIndent(uri: Uri): Intent? { fun dispatchIndent(uri: Uri): Intent? {
val url: String try {
//如果是普通的url,需要根据返回的内容判断是什么 val url: String
if (uri.scheme == "file" || uri.scheme == "content") { //如果是普通的url,需要根据返回的内容判断是什么
val content = if (uri.scheme == "file") { if (uri.scheme == "file" || uri.scheme == "content") {
val file = File(uri.path.toString()) val content = if (uri.scheme == "file") {
if (file.exists()) { val file = File(uri.path.toString())
file.readText() if (file.exists()) {
file.readText()
} else {
null
}
} else { } else {
null DocumentFile.fromSingleUri(context, uri)?.readText(context)
} }
} else { var scheme = ""
DocumentFile.fromSingleUri(context, uri)?.readText(context) if (content != null) {
} if (content.isJsonObject() || content.isJsonArray()) {
var scheme = "" //暂时根据文件内容判断属于什么
if (content != null) { when {
if (content.isJsonObject() || content.isJsonArray()) { content.contains("bookSourceUrl") -> {
//暂时根据文件内容判断属于什么 scheme = "booksource"
when { }
content.contains("bookSourceUrl") -> { content.contains("sourceUrl") -> {
scheme = "booksource" scheme = "rsssource"
} }
content.contains("sourceUrl") -> { content.contains("pattern") -> {
scheme = "rsssource" scheme = "replace"
} }
content.contains("pattern") -> {
scheme = "replace"
} }
} }
} if (TextUtils.isEmpty(scheme)) {
if (TextUtils.isEmpty(scheme)) { execute {
execute { if (uri.scheme == "content") {
if (uri.scheme == "content"){ LocalBook.importFile(uri.toString())
LocalBook.importFile(uri.toString()) } else {
}else{ LocalBook.importFile(uri.path.toString())
LocalBook.importFile(uri.path.toString()) }
toast("添加本地文件成功${uri.path}")
} }
toast("添加本地文件成功${uri.path}") return null
} }
} else {
toast("文件不存在")
return null return null
} }
// content模式下,需要传递完整的路径,方便后续解析
url = if (uri.scheme == "content") {
"yuedu://${scheme}/importonline?src=$uri"
} else {
"yuedu://${scheme}/importonline?src=${uri.path}"
}
} else if (uri.scheme == "yuedu") {
url = uri.toString()
} else { } else {
toast("文件不存在") url = "yuedu://booksource/importonline?src=${uri.path}"
return null
}
// content模式下,需要传递完整的路径,方便后续解析
url = if (uri.scheme == "content"){
"yuedu://${scheme}/importonline?src=$uri"
}else{
"yuedu://${scheme}/importonline?src=${uri.path}"
} }
val data = Uri.parse(url)
} else if (uri.scheme == "yuedu") { val newIndent = Intent(Intent.ACTION_VIEW)
url = uri.toString() newIndent.data = data
} else { return newIndent
url = "yuedu://booksource/importonline?src=${uri.path}" } catch (e: Exception) {
e.printStackTrace()
toast(e.localizedMessage)
return null
} }
val data = Uri.parse(url)
val newIndent = Intent(Intent.ACTION_VIEW)
newIndent.data = data
return newIndent
} }
} }
Loading…
Cancel
Save