From 7004b47898e5533b4f9c887bd06241f24b4af281 Mon Sep 17 00:00:00 2001 From: wqfantexi Date: Sat, 21 Mar 2020 20:31:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=87=E4=BB=B6=E5=85=B3?= =?UTF-8?q?=E8=81=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 12 +++- .../book/source/manage/BookSourceActivity.kt | 10 +++- .../legado/app/ui/config/FileAssociation.kt | 37 ++++++++++++ .../app/ui/config/FileAssociationViewModel.kt | 60 +++++++++++++++++++ .../app/ui/replacerule/ReplaceRuleActivity.kt | 11 +++- .../ui/replacerule/ReplaceRuleViewModel.kt | 17 +++++- .../ui/rss/source/manage/RssSourceActivity.kt | 11 +++- .../res/layout/activity_file_association.xml | 9 +++ 8 files changed, 159 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/config/FileAssociation.kt create mode 100644 app/src/main/java/io/legado/app/ui/config/FileAssociationViewModel.kt create mode 100644 app/src/main/res/layout/activity_file_association.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9d778fbb3..71fa00c62 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -244,7 +244,17 @@ - + + + + + + + + + + + diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt index 385088640..d3cb63af3 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt @@ -104,8 +104,14 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity when (it.path) { "/importonline" -> it.getQueryParameter("src")?.let { url -> Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show() - viewModel.importSource(url) { msg -> - title_bar.snackbar(msg) + if (url.startsWith("http", false)){ + viewModel.importSource(url) { msg -> + title_bar.snackbar(msg) + } + } + else{ + viewModel.importSourceFromFilePath(url){msg -> + title_bar.snackbar(msg)} } } else -> { diff --git a/app/src/main/java/io/legado/app/ui/config/FileAssociation.kt b/app/src/main/java/io/legado/app/ui/config/FileAssociation.kt new file mode 100644 index 000000000..0b2a3c749 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/config/FileAssociation.kt @@ -0,0 +1,37 @@ +package io.legado.app.ui.config + +import android.content.Intent +import android.os.Bundle +import io.legado.app.R +import io.legado.app.base.VMBaseActivity +import io.legado.app.ui.main.MainActivity +import io.legado.app.utils.getViewModel + + +class FileAssociation : VMBaseActivity(R.layout.activity_file_association) { + override val viewModel: FileAssociationViewModel + get() = getViewModel(FileAssociationViewModel::class.java) + + override fun onActivityCreated(savedInstanceState: Bundle?) { + if (null != intent.data){ + val newIntent = viewModel.dispatchIndent(intent.data!!) + if (newIntent != null){ + this.startActivityForResult(newIntent, 100) + }else{ + gotoMainActivity() + } + } + } + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + + //返回后直接跳转到主页面 + gotoMainActivity() + } + + private fun gotoMainActivity(){ + val mIntent = Intent() + mIntent.setClass(this, MainActivity::class.java) + startActivity(mIntent) + } +} 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 new file mode 100644 index 000000000..4a09b4325 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/config/FileAssociationViewModel.kt @@ -0,0 +1,60 @@ +package io.legado.app.ui.config + +import android.app.Application +import android.content.Intent +import android.net.Uri +import android.text.TextUtils +import io.legado.app.base.BaseViewModel +import io.legado.app.model.localBook.LocalBook +import io.legado.app.utils.isJsonArray +import io.legado.app.utils.isJsonObject +import org.jetbrains.anko.toast +import java.io.File + +class FileAssociationViewModel(application: Application) : BaseViewModel(application) { + fun dispatchIndent(uri:Uri):Intent?{ + var url = ""; + //如果是普通的url,需要根据返回的内容判断是什么 + if(uri.scheme == "file" || uri.scheme == "content"){ + val file = File(uri.path.toString()) + var scheme = "" + if (file.exists()) { + val content = file.readText() + if (content.isJsonObject() || content.isJsonArray()){ + //暂时根据文件内容判断属于什么 + if (content.contains("bookSourceUrl")){ + scheme = "booksource" + }else if (content.contains("sourceUrl")){ + scheme = "rsssource" + }else if (content.contains("pattern")){ + scheme = "replace" + } + } + if (TextUtils.isEmpty(scheme)){ + execute{ + LocalBook.importFile(uri.path.toString()) + toast("添加本地文件成功${uri.path}") + } + + return null + } + } + else{ + toast("文件不存在") + return null + } + + url = "yuedu://${scheme}/importonline?src=${uri.path}" + } + else if (uri.scheme == "yuedu"){ + url = uri.toString() + } + else{ + url = "yuedu://booksource/importonline?src=${uri.path}" + } + val data = Uri.parse(url) + val newIndent = Intent(Intent.ACTION_VIEW) + newIndent.data = data; + return newIndent + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt index fd0a591b6..1f9057e35 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt @@ -81,8 +81,15 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi when (it.path) { "/importonline" -> it.getQueryParameter("src")?.let { url -> Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show() - viewModel.importSource(url) { msg -> - title_bar.snackbar(msg) + if (url.startsWith("http", false)){ + viewModel.importSource(url) { msg -> + title_bar.snackbar(msg) + } + } + else{ + viewModel.importSourceFromFilePath(url) { msg -> + title_bar.snackbar(msg) + } } } 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 a63d7ea38..8f0e848f0 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 @@ -11,11 +11,26 @@ import io.legado.app.help.http.HttpHelper import io.legado.app.help.storage.Backup import io.legado.app.help.storage.ImportOldData import io.legado.app.utils.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import org.jetbrains.anko.toast 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) + } else { + withContext(Dispatchers.Main) { + finally("打开文件出错") + } + } + }.onError { + finally(it.localizedMessage ?: "打开文件出错") + } + } fun importSource(text: String, showMsg: (msg: String) -> Unit) { execute { if (text.isAbsUrl()) { diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt index fb54d0cae..1f7503e47 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt @@ -117,8 +117,15 @@ class RssSourceActivity : VMBaseActivity(R.layout.activity_r when (it.path) { "/importonline" -> it.getQueryParameter("src")?.let { url -> Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show() - viewModel.importSource(url) { msg -> - title_bar.snackbar(msg) + if (url.startsWith("http", false)){ + viewModel.importSource(url) { msg -> + title_bar.snackbar(msg) + } + } + else{ + viewModel.importSourceFromFilePath(url) { msg -> + title_bar.snackbar(msg) + } } } else -> { diff --git a/app/src/main/res/layout/activity_file_association.xml b/app/src/main/res/layout/activity_file_association.xml new file mode 100644 index 000000000..5c147517b --- /dev/null +++ b/app/src/main/res/layout/activity_file_association.xml @@ -0,0 +1,9 @@ + + + +