From abc0af7103615d5f990adfea7669a0e5bef19bd9 Mon Sep 17 00:00:00 2001 From: 821938089 <821938089@qq.com> Date: Sat, 30 Jul 2022 16:54:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/association/FileAssociationActivity.kt | 18 +++++++----- .../association/FileAssociationViewModel.kt | 29 ++++++++++++------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt b/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt index d8bf85a1f..f050002c5 100644 --- a/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt +++ b/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt @@ -103,7 +103,16 @@ class FileAssociationActivity : } } intent.data?.let { data -> - viewModel.dispatchIndent(data) + if (!data.isContentScheme()) { + PermissionsCompat.Builder(this) + .addPermissions(*Permissions.Group.STORAGE) + .rationale(R.string.tip_perm_request_storage) + .onGranted { + viewModel.dispatchIndent(data) + }.request() + } else { + viewModel.dispatchIndent(data) + } } } @@ -119,12 +128,7 @@ class FileAssociationActivity : importBook(Uri.parse(treeUriStr), uri) } } else { - PermissionsCompat.Builder(this) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { - viewModel.importBook(uri) - }.request() + viewModel.importBook(uri) } } 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 f2e5896a6..f5b0bd562 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 @@ -4,13 +4,14 @@ import android.app.Application import android.net.Uri import androidx.documentfile.provider.DocumentFile import androidx.lifecycle.MutableLiveData +import io.legado.app.constant.AppLog import io.legado.app.constant.AppPattern.bookFileRegex import io.legado.app.exception.NoStackTraceException import io.legado.app.model.localBook.LocalBook import io.legado.app.utils.isJson import io.legado.app.utils.printOnDebug -import io.legado.app.utils.readText import java.io.File +import java.io.InputStream class FileAssociationViewModel(application: Application) : BaseAssociationViewModel(application) { val importBookLiveData = MutableLiveData() @@ -23,28 +24,34 @@ class FileAssociationViewModel(application: Application) : BaseAssociationViewMo execute { lateinit var fileName: String lateinit var content: String + lateinit var fileStream: InputStream //如果是普通的url,需要根据返回的内容判断是什么 if (uri.scheme == "file" || uri.scheme == "content") { if (uri.scheme == "file") { val file = File(uri.path.toString()) - content = file.readText() + fileStream = file.inputStream() fileName = file.name } else { val file = DocumentFile.fromSingleUri(context, uri) - content = file?.readText(context) ?: throw NoStackTraceException("文件不存在") + if (file?.exists() != true) throw NoStackTraceException("文件不存在") + fileStream = context.contentResolver.openInputStream(uri)!! fileName = file.name ?: "" } - when { - content.isJson() -> { + kotlin.runCatching { + content = fileStream.reader(Charsets.UTF_8).use { it.readText() } + if (content.isJson()) { importJson(content) + return@execute } - fileName.matches(bookFileRegex) -> { - importBookLiveData.postValue(uri) - } - else -> { - notSupportedLiveData.postValue(Pair(uri, fileName)) - } + }.onFailure { + it.printOnDebug() + AppLog.put("尝试导入为JSON文件失败\n${it.localizedMessage}", it) + } + if (fileName.matches(bookFileRegex)) { + importBookLiveData.postValue(uri) + return@execute } + notSupportedLiveData.postValue(Pair(uri, fileName)) } else { onLineImportLive.postValue(uri) }