Merge branch 'master' of https://github.com/gedoor/legado
	
		
	
				
					
				
			# Conflicts: # app/src/main/res/values/colors.xmlpull/169/head
						commit
						78573d8e77
					
				@ -0,0 +1,39 @@ | 
				
			||||
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 FileAssociationActivity : | 
				
			||||
    VMBaseActivity<FileAssociationViewModel>(R.layout.activity_file_association) { | 
				
			||||
    override val viewModel: FileAssociationViewModel | 
				
			||||
        get() = getViewModel(FileAssociationViewModel::class.java) | 
				
			||||
 | 
				
			||||
    override fun onActivityCreated(savedInstanceState: Bundle?) { | 
				
			||||
        intent.data?.let { data -> | 
				
			||||
            val newIntent = viewModel.dispatchIndent(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,69 @@ | 
				
			||||
package io.legado.app.ui.config | 
				
			||||
 | 
				
			||||
import android.app.Application | 
				
			||||
import android.content.Intent | 
				
			||||
import android.net.Uri | 
				
			||||
import android.text.TextUtils | 
				
			||||
import androidx.documentfile.provider.DocumentFile | 
				
			||||
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 io.legado.app.utils.readText | 
				
			||||
import java.io.File | 
				
			||||
 | 
				
			||||
class FileAssociationViewModel(application: Application) : BaseViewModel(application) { | 
				
			||||
    fun dispatchIndent(uri: Uri): Intent? { | 
				
			||||
        val url: String | 
				
			||||
        //如果是普通的url,需要根据返回的内容判断是什么 | 
				
			||||
        if (uri.scheme == "file" || uri.scheme == "content") { | 
				
			||||
            val content = if (uri.scheme == "file") { | 
				
			||||
                val file = File(uri.path.toString()) | 
				
			||||
                if (file.exists()) { | 
				
			||||
                    file.readText() | 
				
			||||
                } else { | 
				
			||||
                    null | 
				
			||||
                } | 
				
			||||
            } else { | 
				
			||||
                DocumentFile.fromSingleUri(context, uri)?.readText(context) | 
				
			||||
            } | 
				
			||||
            var scheme = "" | 
				
			||||
            if (content != null) { | 
				
			||||
                if (content.isJsonObject() || content.isJsonArray()) { | 
				
			||||
                    //暂时根据文件内容判断属于什么 | 
				
			||||
                    when { | 
				
			||||
                        content.contains("bookSourceUrl") -> { | 
				
			||||
                            scheme = "booksource" | 
				
			||||
                        } | 
				
			||||
                        content.contains("sourceUrl") -> { | 
				
			||||
                            scheme = "rsssource" | 
				
			||||
                        } | 
				
			||||
                        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 | 
				
			||||
    } | 
				
			||||
} | 
				
			||||
@ -0,0 +1,8 @@ | 
				
			||||
<?xml version="1.0" encoding="utf-8"?> | 
				
			||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | 
				
			||||
    xmlns:tools="http://schemas.android.com/tools" | 
				
			||||
    android:layout_width="match_parent" | 
				
			||||
    android:layout_height="match_parent" | 
				
			||||
    tools:context="io.legado.app.ui.config.FileAssociationActivity"> | 
				
			||||
 | 
				
			||||
</androidx.constraintlayout.widget.ConstraintLayout> | 
				
			||||
					Loading…
					
					
				
		Reference in new issue