优化打开文件

pull/915/head
gedoor 4 years ago
parent 6fbcce8af2
commit 4ff399b4bb
  1. 35
      app/src/main/AndroidManifest.xml
  2. 59
      app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt
  3. 10
      app/src/main/java/io/legado/app/ui/association/ImportBookSourceActivity.kt
  4. 33
      app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt

@ -367,16 +367,47 @@
android:name=".ui.association.FileAssociationActivity" android:name=".ui.association.FileAssociationActivity"
android:launchMode="singleTop" android:launchMode="singleTop"
android:theme="@style/AppTheme.Transparent"> android:theme="@style/AppTheme.Transparent">
<!-- VIEW (Open with) action -->
<!-- Works when an app knows the media type of a file, e.g. Gmail or Chrome. -->
<intent-filter> <intent-filter>
<action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="file" /> <data android:scheme="app" />
<data android:scheme="content" /> <data android:scheme="content" />
<data android:scheme="file" />
<!-- text -->
<data android:mimeType="text/plain" /> <data android:mimeType="text/plain" />
<!-- json -->
<data android:mimeType="application/json" /> <data android:mimeType="application/json" />
<data android:mimeType="application/epub" /> <!-- EPUB -->
<data android:mimeType="application/epub+zip" />
</intent-filter>
<!-- Works when an app doesn't know the media type, e.g. Dropbox -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:host="*" />
<data android:scheme="app" />
<data android:scheme="content" />
<data android:scheme="file" />
<!-- This media type is necessary, otherwise it won't match on the file extension -->
<data android:mimeType="*/*" />
<!--TXT-->
<data android:pathPattern=".*\\.txt" />
<data android:pathPattern=".*\\.TXT" />
<!--JSON-->
<data android:pathPattern=".*\\.json" />
<data android:pathPattern=".*\\.JSON" />
<!-- EPUB -->
<data android:pathPattern=".*\\.epub" />
<data android:pathPattern=".*\\.EPUB" />
</intent-filter> </intent-filter>
</activity> </activity>

@ -6,6 +6,7 @@ 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.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.help.IntentDataHelp
import io.legado.app.model.localBook.LocalBook import io.legado.app.model.localBook.LocalBook
import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.ui.book.read.ReadBookActivity
import io.legado.app.utils.isJsonArray import io.legado.app.utils.isJsonArray
@ -23,7 +24,6 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica
execute { execute {
//如果是普通的url,需要根据返回的内容判断是什么 //如果是普通的url,需要根据返回的内容判断是什么
if (uri.scheme == "file" || uri.scheme == "content") { if (uri.scheme == "file" || uri.scheme == "content") {
var scheme = ""
val content = if (uri.scheme == "file") { val content = if (uri.scheme == "file") {
File(uri.path.toString()).readText() File(uri.path.toString()).readText()
} else { } else {
@ -34,36 +34,51 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica
//暂时根据文件内容判断属于什么 //暂时根据文件内容判断属于什么
when { when {
content.contains("bookSourceUrl") -> { 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") -> { 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") -> { 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") {
val book = if (uri.scheme == "content") { LocalBook.importFile(uri)
LocalBook.importFile(uri)
} else {
LocalBook.importFile(uri)
}
val intent = Intent(context, ReadBookActivity::class.java)
intent.putExtra("bookUrl", book.bookUrl)
successLiveData.postValue(intent)
} else { } else {
val url = if (uri.scheme == "content") { LocalBook.importFile(uri)
"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)
} }
val intent = Intent(context, ReadBookActivity::class.java)
intent.putExtra("bookUrl", book.bookUrl)
successLiveData.postValue(intent)
} else { } else {
throw Exception("文件不存在") throw Exception("文件不存在")
} }

@ -52,18 +52,10 @@ class ImportBookSourceActivity :
viewModel.importSource(it) viewModel.importSource(it)
return return
} }
intent.getStringExtra("filePath")?.let {
viewModel.importSourceFromFilePath(it)
return
}
intent.data?.let { intent.data?.let {
when (it.path) { when (it.path) {
"/importonline" -> it.getQueryParameter("src")?.let { url -> "/importonline" -> it.getQueryParameter("src")?.let { url ->
if (url.startsWith("http", false)) { viewModel.importSource(url)
viewModel.importSource(url)
} else {
viewModel.importSourceFromFilePath(url)
}
} }
else -> { else -> {
binding.rotateLoading.hide() binding.rotateLoading.hide()

@ -1,8 +1,6 @@
package io.legado.app.ui.association package io.legado.app.ui.association
import android.app.Application import android.app.Application
import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import com.jayway.jsonpath.JsonPath import com.jayway.jsonpath.JsonPath
import io.legado.app.R 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.SourceHelp
import io.legado.app.help.storage.OldRule import io.legado.app.help.storage.OldRule
import io.legado.app.help.storage.Restore 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.RxHttp
import rxhttp.wrapper.param.toText import rxhttp.wrapper.param.toText
import java.io.File
class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
var groupName: String? = null 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) { fun importSource(text: String) {
execute { execute {
val mText = text.trim() val mText = text.trim()

Loading…
Cancel
Save