修复文件关联content模式,无法打开文件的问题

pull/173/head
wqfantexi 5 years ago
parent 2ceb71ee1a
commit 803eea9ac3
  1. 19
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt
  2. 13
      app/src/main/java/io/legado/app/ui/config/FileAssociationViewModel.kt
  3. 18
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt
  4. 19
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt

@ -1,6 +1,7 @@
package io.legado.app.ui.book.source.manage package io.legado.app.ui.book.source.manage
import android.app.Application import android.app.Application
import android.net.Uri
import android.text.TextUtils import android.text.TextUtils
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import com.jayway.jsonpath.JsonPath import com.jayway.jsonpath.JsonPath
@ -15,6 +16,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.jetbrains.anko.toast import org.jetbrains.anko.toast
import java.io.File import java.io.File
import java.net.URLEncoder
class BookSourceViewModel(application: Application) : BaseViewModel(application) { class BookSourceViewModel(application: Application) : BaseViewModel(application) {
@ -156,9 +158,20 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application)
fun importSourceFromFilePath(path: String, finally: (msg: String) -> Unit) { fun importSourceFromFilePath(path: String, finally: (msg: String) -> Unit) {
execute { execute {
val file = File(path) val content = if (path.isContentPath()) {
if (file.exists()) { //在前面被解码了,如果不进行编码,中文会无法识别
importSource(file.readText(), finally) val newPath = Uri.encode(path, ":/.")
DocumentFile.fromSingleUri(context, Uri.parse(newPath))?.readText(context)
} else {
val file = File(path)
if (file.exists()) {
file.readText()
} else {
null
}
}
if (content != null) {
importSource(content, finally)
} else { } else {
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
finally("打开文件出错") finally("打开文件出错")

@ -45,7 +45,11 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica
} }
if (TextUtils.isEmpty(scheme)) { if (TextUtils.isEmpty(scheme)) {
execute { execute {
LocalBook.importFile(uri.path.toString()) if (uri.scheme == "content"){
LocalBook.importFile(uri.toString())
}else{
LocalBook.importFile(uri.path.toString())
}
toast("添加本地文件成功${uri.path}") toast("添加本地文件成功${uri.path}")
} }
return null return null
@ -54,8 +58,13 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica
toast("文件不存在") toast("文件不存在")
return null return null
} }
// content模式下,需要传递完整的路径,方便后续解析
url = if (uri.scheme == "content"){
"yuedu://${scheme}/importonline?src=$uri"
}else{
"yuedu://${scheme}/importonline?src=${uri.path}"
}
url = "yuedu://${scheme}/importonline?src=${uri.path}"
} else if (uri.scheme == "yuedu") { } else if (uri.scheme == "yuedu") {
url = uri.toString() url = uri.toString()
} else { } else {

@ -1,6 +1,7 @@
package io.legado.app.ui.replacerule package io.legado.app.ui.replacerule
import android.app.Application import android.app.Application
import android.net.Uri
import android.text.TextUtils import android.text.TextUtils
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import io.legado.app.App import io.legado.app.App
@ -18,9 +19,20 @@ import java.io.File
class ReplaceRuleViewModel(application: Application) : BaseViewModel(application) { class ReplaceRuleViewModel(application: Application) : BaseViewModel(application) {
fun importSourceFromFilePath(path: String, finally: (msg: String) -> Unit) { fun importSourceFromFilePath(path: String, finally: (msg: String) -> Unit) {
execute { execute {
val file = File(path) val content = if (path.isContentPath()) {
if (file.exists()) { //在前面被解码了,如果不进行编码,中文会无法识别
importSource(file.readText(), finally) val newPath = Uri.encode(path, ":/.")
DocumentFile.fromSingleUri(context, Uri.parse(newPath))?.readText(context)
} else {
val file = File(path)
if (file.exists()) {
file.readText()
} else {
null
}
}
if (content != null) {
importSource(content, finally)
} else { } else {
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
finally("打开文件出错") finally("打开文件出错")

@ -1,6 +1,7 @@
package io.legado.app.ui.rss.source.manage package io.legado.app.ui.rss.source.manage
import android.app.Application import android.app.Application
import android.net.Uri
import android.text.TextUtils import android.text.TextUtils
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import com.jayway.jsonpath.JsonPath import com.jayway.jsonpath.JsonPath
@ -134,9 +135,21 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application)
fun importSourceFromFilePath(path: String, finally: (msg: String) -> Unit) { fun importSourceFromFilePath(path: String, finally: (msg: String) -> Unit) {
execute { execute {
val file = File(path) val content = if (path.isContentPath()) {
if (file.exists()) { //在前面被解码了,如果不进行编码,中文会无法识别
GSON.fromJsonArray<RssSource>(file.readText())?.let { val newPath = Uri.encode(path, ":/.")
DocumentFile.fromSingleUri(context, Uri.parse(newPath))?.readText(context)
} else {
val file = File(path)
if (file.exists()) {
file.readText()
} else {
null
}
}
if (null != content) {
GSON.fromJsonArray<RssSource>(content)?.let {
App.db.rssSourceDao().insert(*it.toTypedArray()) App.db.rssSourceDao().insert(*it.toTypedArray())
} }
} }

Loading…
Cancel
Save