优化
This commit is contained in:
@@ -103,7 +103,16 @@ class FileAssociationActivity :
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
intent.data?.let { data ->
|
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)
|
importBook(Uri.parse(treeUriStr), uri)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
PermissionsCompat.Builder(this)
|
viewModel.importBook(uri)
|
||||||
.addPermissions(*Permissions.Group.STORAGE)
|
|
||||||
.rationale(R.string.tip_perm_request_storage)
|
|
||||||
.onGranted {
|
|
||||||
viewModel.importBook(uri)
|
|
||||||
}.request()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,13 +4,14 @@ import android.app.Application
|
|||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import androidx.documentfile.provider.DocumentFile
|
import androidx.documentfile.provider.DocumentFile
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
|
import io.legado.app.constant.AppLog
|
||||||
import io.legado.app.constant.AppPattern.bookFileRegex
|
import io.legado.app.constant.AppPattern.bookFileRegex
|
||||||
import io.legado.app.exception.NoStackTraceException
|
import io.legado.app.exception.NoStackTraceException
|
||||||
import io.legado.app.model.localBook.LocalBook
|
import io.legado.app.model.localBook.LocalBook
|
||||||
import io.legado.app.utils.isJson
|
import io.legado.app.utils.isJson
|
||||||
import io.legado.app.utils.printOnDebug
|
import io.legado.app.utils.printOnDebug
|
||||||
import io.legado.app.utils.readText
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.io.InputStream
|
||||||
|
|
||||||
class FileAssociationViewModel(application: Application) : BaseAssociationViewModel(application) {
|
class FileAssociationViewModel(application: Application) : BaseAssociationViewModel(application) {
|
||||||
val importBookLiveData = MutableLiveData<Uri>()
|
val importBookLiveData = MutableLiveData<Uri>()
|
||||||
@@ -23,28 +24,34 @@ class FileAssociationViewModel(application: Application) : BaseAssociationViewMo
|
|||||||
execute {
|
execute {
|
||||||
lateinit var fileName: String
|
lateinit var fileName: String
|
||||||
lateinit var content: String
|
lateinit var content: String
|
||||||
|
lateinit var fileStream: InputStream
|
||||||
//如果是普通的url,需要根据返回的内容判断是什么
|
//如果是普通的url,需要根据返回的内容判断是什么
|
||||||
if (uri.scheme == "file" || uri.scheme == "content") {
|
if (uri.scheme == "file" || uri.scheme == "content") {
|
||||||
if (uri.scheme == "file") {
|
if (uri.scheme == "file") {
|
||||||
val file = File(uri.path.toString())
|
val file = File(uri.path.toString())
|
||||||
content = file.readText()
|
fileStream = file.inputStream()
|
||||||
fileName = file.name
|
fileName = file.name
|
||||||
} else {
|
} else {
|
||||||
val file = DocumentFile.fromSingleUri(context, uri)
|
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 ?: ""
|
fileName = file.name ?: ""
|
||||||
}
|
}
|
||||||
when {
|
kotlin.runCatching {
|
||||||
content.isJson() -> {
|
content = fileStream.reader(Charsets.UTF_8).use { it.readText() }
|
||||||
|
if (content.isJson()) {
|
||||||
importJson(content)
|
importJson(content)
|
||||||
|
return@execute
|
||||||
}
|
}
|
||||||
fileName.matches(bookFileRegex) -> {
|
}.onFailure {
|
||||||
importBookLiveData.postValue(uri)
|
it.printOnDebug()
|
||||||
}
|
AppLog.put("尝试导入为JSON文件失败\n${it.localizedMessage}", it)
|
||||||
else -> {
|
|
||||||
notSupportedLiveData.postValue(Pair(uri, fileName))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if (fileName.matches(bookFileRegex)) {
|
||||||
|
importBookLiveData.postValue(uri)
|
||||||
|
return@execute
|
||||||
|
}
|
||||||
|
notSupportedLiveData.postValue(Pair(uri, fileName))
|
||||||
} else {
|
} else {
|
||||||
onLineImportLive.postValue(uri)
|
onLineImportLive.postValue(uri)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user