From 803eea9ac39d8ed447524ae5146f496f4a4c03ee Mon Sep 17 00:00:00 2001 From: wqfantexi Date: Mon, 23 Mar 2020 00:32:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=96=87=E4=BB=B6=E5=85=B3?= =?UTF-8?q?=E8=81=94content=E6=A8=A1=E5=BC=8F=EF=BC=8C=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E6=89=93=E5=BC=80=E6=96=87=E4=BB=B6=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../book/source/manage/BookSourceViewModel.kt | 19 ++++++++++++++++--- .../app/ui/config/FileAssociationViewModel.kt | 13 +++++++++++-- .../ui/replacerule/ReplaceRuleViewModel.kt | 18 +++++++++++++++--- .../rss/source/manage/RssSourceViewModel.kt | 19 ++++++++++++++++--- 4 files changed, 58 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt index c84497536..8f0f46d94 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt @@ -1,6 +1,7 @@ package io.legado.app.ui.book.source.manage import android.app.Application +import android.net.Uri import android.text.TextUtils import androidx.documentfile.provider.DocumentFile import com.jayway.jsonpath.JsonPath @@ -15,6 +16,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.jetbrains.anko.toast import java.io.File +import java.net.URLEncoder class BookSourceViewModel(application: Application) : BaseViewModel(application) { @@ -156,9 +158,20 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) fun importSourceFromFilePath(path: String, finally: (msg: String) -> Unit) { execute { - val file = File(path) - if (file.exists()) { - importSource(file.readText(), finally) + val content = if (path.isContentPath()) { + //在前面被解码了,如果不进行编码,中文会无法识别 + 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 { withContext(Dispatchers.Main) { finally("打开文件出错") diff --git a/app/src/main/java/io/legado/app/ui/config/FileAssociationViewModel.kt b/app/src/main/java/io/legado/app/ui/config/FileAssociationViewModel.kt index 80db1af37..02aab3668 100644 --- a/app/src/main/java/io/legado/app/ui/config/FileAssociationViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/config/FileAssociationViewModel.kt @@ -45,7 +45,11 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica } if (TextUtils.isEmpty(scheme)) { execute { - LocalBook.importFile(uri.path.toString()) + if (uri.scheme == "content"){ + LocalBook.importFile(uri.toString()) + }else{ + LocalBook.importFile(uri.path.toString()) + } toast("添加本地文件成功${uri.path}") } return null @@ -54,8 +58,13 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica toast("文件不存在") 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") { url = uri.toString() } else { diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt index ec75e78ed..d11d04d4a 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt @@ -1,6 +1,7 @@ package io.legado.app.ui.replacerule import android.app.Application +import android.net.Uri import android.text.TextUtils import androidx.documentfile.provider.DocumentFile import io.legado.app.App @@ -18,9 +19,20 @@ import java.io.File class ReplaceRuleViewModel(application: Application) : BaseViewModel(application) { fun importSourceFromFilePath(path: String, finally: (msg: String) -> Unit) { execute { - val file = File(path) - if (file.exists()) { - importSource(file.readText(), finally) + val content = if (path.isContentPath()) { + //在前面被解码了,如果不进行编码,中文会无法识别 + 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 { withContext(Dispatchers.Main) { finally("打开文件出错") diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt index 300d7468c..b4f20e20a 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt @@ -1,6 +1,7 @@ package io.legado.app.ui.rss.source.manage import android.app.Application +import android.net.Uri import android.text.TextUtils import androidx.documentfile.provider.DocumentFile import com.jayway.jsonpath.JsonPath @@ -134,9 +135,21 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application) fun importSourceFromFilePath(path: String, finally: (msg: String) -> Unit) { execute { - val file = File(path) - if (file.exists()) { - GSON.fromJsonArray(file.readText())?.let { + val content = if (path.isContentPath()) { + //在前面被解码了,如果不进行编码,中文会无法识别 + 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(content)?.let { App.db.rssSourceDao().insert(*it.toTypedArray()) } }