From 4ff399b4bb3785a759f509c1bc2ce9867905dbe5 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 27 Mar 2021 00:42:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=89=93=E5=BC=80=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 35 ++++++++++- .../association/FileAssociationViewModel.kt | 59 ++++++++++++------- .../association/ImportBookSourceActivity.kt | 10 +--- .../association/ImportBookSourceViewModel.kt | 33 +---------- 4 files changed, 74 insertions(+), 63 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 85914dd9d..8e07090d0 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -367,16 +367,47 @@ android:name=".ui.association.FileAssociationActivity" android:launchMode="singleTop" android:theme="@style/AppTheme.Transparent"> + + + - + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt b/app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt index a230b4450..4885e8ffc 100644 --- a/app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt @@ -6,6 +6,7 @@ import android.net.Uri import androidx.documentfile.provider.DocumentFile import androidx.lifecycle.MutableLiveData import io.legado.app.base.BaseViewModel +import io.legado.app.help.IntentDataHelp import io.legado.app.model.localBook.LocalBook import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.utils.isJsonArray @@ -23,7 +24,6 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica execute { //如果是普通的url,需要根据返回的内容判断是什么 if (uri.scheme == "file" || uri.scheme == "content") { - var scheme = "" val content = if (uri.scheme == "file") { File(uri.path.toString()).readText() } else { @@ -34,36 +34,51 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica //暂时根据文件内容判断属于什么 when { content.contains("bookSourceUrl") -> { - scheme = "booksource" + successLiveData.postValue( + Intent( + context, + ImportBookSourceActivity::class.java + ).apply { + val dataKey = IntentDataHelp.putData(content) + putExtra("dataKey", dataKey) + } + ) + return@execute } content.contains("sourceUrl") -> { - scheme = "rsssource" + successLiveData.postValue( + Intent( + context, + ImportRssSourceActivity::class.java + ).apply { + val dataKey = IntentDataHelp.putData(content) + putExtra("dataKey", dataKey) + } + ) + return@execute } content.contains("pattern") -> { - scheme = "replace" + successLiveData.postValue( + Intent( + context, + ImportReplaceRuleActivity::class.java + ).apply { + val dataKey = IntentDataHelp.putData(content) + putExtra("dataKey", dataKey) + } + ) + return@execute } } } - if (scheme.isEmpty()) { - val book = if (uri.scheme == "content") { - LocalBook.importFile(uri) - } else { - LocalBook.importFile(uri) - } - val intent = Intent(context, ReadBookActivity::class.java) - intent.putExtra("bookUrl", book.bookUrl) - successLiveData.postValue(intent) + val book = if (uri.scheme == "content") { + LocalBook.importFile(uri) } else { - val url = if (uri.scheme == "content") { - "yuedu://${scheme}/importonline?src=$uri" - } else { - "yuedu://${scheme}/importonline?src=${uri.path}" - } - val data = Uri.parse(url) - val newIndent = Intent(Intent.ACTION_VIEW) - newIndent.data = data - successLiveData.postValue(newIndent) + LocalBook.importFile(uri) } + val intent = Intent(context, ReadBookActivity::class.java) + intent.putExtra("bookUrl", book.bookUrl) + successLiveData.postValue(intent) } else { throw Exception("文件不存在") } diff --git a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceActivity.kt index 09007b795..c5a060d81 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceActivity.kt @@ -52,18 +52,10 @@ class ImportBookSourceActivity : viewModel.importSource(it) return } - intent.getStringExtra("filePath")?.let { - viewModel.importSourceFromFilePath(it) - return - } intent.data?.let { when (it.path) { "/importonline" -> it.getQueryParameter("src")?.let { url -> - if (url.startsWith("http", false)) { - viewModel.importSource(url) - } else { - viewModel.importSourceFromFilePath(url) - } + viewModel.importSource(url) } else -> { binding.rotateLoading.hide() diff --git a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt index 42647cf6e..7a08cacb1 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt @@ -1,8 +1,6 @@ package io.legado.app.ui.association import android.app.Application -import android.net.Uri -import androidx.documentfile.provider.DocumentFile import androidx.lifecycle.MutableLiveData import com.jayway.jsonpath.JsonPath import io.legado.app.R @@ -13,10 +11,11 @@ import io.legado.app.help.AppConfig import io.legado.app.help.SourceHelp import io.legado.app.help.storage.OldRule import io.legado.app.help.storage.Restore -import io.legado.app.utils.* +import io.legado.app.utils.isAbsUrl +import io.legado.app.utils.isJsonArray +import io.legado.app.utils.isJsonObject import rxhttp.wrapper.param.RxHttp import rxhttp.wrapper.param.toText -import java.io.File class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { var groupName: String? = null @@ -72,32 +71,6 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { } } - - fun importSourceFromFilePath(path: String) { - execute { - val content = if (path.isContentScheme()) { - //在前面被解码了,如果不进行编码,中文会无法识别 - 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) - } else { - errorLiveData.postValue(context.getString(R.string.error_read_file)) - } - }.onError { - it.printStackTrace() - errorLiveData.postValue(context.getString(R.string.error_read_file)) - } - } - fun importSource(text: String) { execute { val mText = text.trim()