添加文件关联

pull/172/head
wqfantexi 5 years ago
parent deaceb9840
commit 7004b47898
  1. 12
      app/src/main/AndroidManifest.xml
  2. 10
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt
  3. 37
      app/src/main/java/io/legado/app/ui/config/FileAssociation.kt
  4. 60
      app/src/main/java/io/legado/app/ui/config/FileAssociationViewModel.kt
  5. 11
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt
  6. 17
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt
  7. 11
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt
  8. 9
      app/src/main/res/layout/activity_file_association.xml

@ -244,7 +244,17 @@
<data android:mimeType="text/plain" /> <data android:mimeType="text/plain" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".ui.config.FileAssociation" android:launchMode="singleTop">
<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:scheme="file"/>
<data android:scheme="content"/>
<data android:mimeType="text/plain" />
<data android:mimeType="application/json" />
</intent-filter>
</activity>
<service android:name=".service.CheckSourceService" /> <service android:name=".service.CheckSourceService" />
<service android:name=".service.DownloadService" /> <service android:name=".service.DownloadService" />
<service android:name=".service.WebService" /> <service android:name=".service.WebService" />

@ -104,8 +104,14 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
when (it.path) { when (it.path) {
"/importonline" -> it.getQueryParameter("src")?.let { url -> "/importonline" -> it.getQueryParameter("src")?.let { url ->
Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show() Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show()
viewModel.importSource(url) { msg -> if (url.startsWith("http", false)){
title_bar.snackbar(msg) viewModel.importSource(url) { msg ->
title_bar.snackbar(msg)
}
}
else{
viewModel.importSourceFromFilePath(url){msg ->
title_bar.snackbar(msg)}
} }
} }
else -> { else -> {

@ -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<FileAssociationViewModel>(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)
}
}

@ -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
}
}

@ -81,8 +81,15 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
when (it.path) { when (it.path) {
"/importonline" -> it.getQueryParameter("src")?.let { url -> "/importonline" -> it.getQueryParameter("src")?.let { url ->
Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show() Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show()
viewModel.importSource(url) { msg -> if (url.startsWith("http", false)){
title_bar.snackbar(msg) viewModel.importSource(url) { msg ->
title_bar.snackbar(msg)
}
}
else{
viewModel.importSourceFromFilePath(url) { msg ->
title_bar.snackbar(msg)
}
} }
} }
else -> { else -> {

@ -11,11 +11,26 @@ import io.legado.app.help.http.HttpHelper
import io.legado.app.help.storage.Backup import io.legado.app.help.storage.Backup
import io.legado.app.help.storage.ImportOldData import io.legado.app.help.storage.ImportOldData
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jetbrains.anko.toast import org.jetbrains.anko.toast
import java.io.File import java.io.File
class ReplaceRuleViewModel(application: Application) : BaseViewModel(application) { 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) { fun importSource(text: String, showMsg: (msg: String) -> Unit) {
execute { execute {
if (text.isAbsUrl()) { if (text.isAbsUrl()) {

@ -117,8 +117,15 @@ class RssSourceActivity : VMBaseActivity<RssSourceViewModel>(R.layout.activity_r
when (it.path) { when (it.path) {
"/importonline" -> it.getQueryParameter("src")?.let { url -> "/importonline" -> it.getQueryParameter("src")?.let { url ->
Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show() Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show()
viewModel.importSource(url) { msg -> if (url.startsWith("http", false)){
title_bar.snackbar(msg) viewModel.importSource(url) { msg ->
title_bar.snackbar(msg)
}
}
else{
viewModel.importSourceFromFilePath(url) { msg ->
title_bar.snackbar(msg)
}
} }
} }
else -> { else -> {

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="io.legado.app.ui.config.FileAssociation">
</androidx.constraintlayout.widget.ConstraintLayout>
Loading…
Cancel
Save