|
|
@ -5,8 +5,10 @@ import android.content.Intent |
|
|
|
import android.net.Uri |
|
|
|
import android.net.Uri |
|
|
|
import android.text.TextUtils |
|
|
|
import android.text.TextUtils |
|
|
|
import androidx.documentfile.provider.DocumentFile |
|
|
|
import androidx.documentfile.provider.DocumentFile |
|
|
|
|
|
|
|
import androidx.lifecycle.MutableLiveData |
|
|
|
import io.legado.app.base.BaseViewModel |
|
|
|
import io.legado.app.base.BaseViewModel |
|
|
|
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.utils.isJsonArray |
|
|
|
import io.legado.app.utils.isJsonArray |
|
|
|
import io.legado.app.utils.isJsonObject |
|
|
|
import io.legado.app.utils.isJsonObject |
|
|
|
import io.legado.app.utils.readText |
|
|
|
import io.legado.app.utils.readText |
|
|
@ -14,8 +16,11 @@ import java.io.File |
|
|
|
|
|
|
|
|
|
|
|
class FileAssociationViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
class FileAssociationViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
|
|
|
|
|
|
|
|
fun dispatchIndent(uri: Uri): Intent? { |
|
|
|
val successLiveData = MutableLiveData<Intent>() |
|
|
|
try { |
|
|
|
val errorLiveData = MutableLiveData<String>() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun dispatchIndent(uri: Uri) { |
|
|
|
|
|
|
|
execute { |
|
|
|
val url: String |
|
|
|
val url: String |
|
|
|
//如果是普通的url,需要根据返回的内容判断是什么 |
|
|
|
//如果是普通的url,需要根据返回的内容判断是什么 |
|
|
|
if (uri.scheme == "file" || uri.scheme == "content") { |
|
|
|
if (uri.scheme == "file" || uri.scheme == "content") { |
|
|
@ -46,19 +51,19 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (TextUtils.isEmpty(scheme)) { |
|
|
|
if (TextUtils.isEmpty(scheme)) { |
|
|
|
execute { |
|
|
|
val book = if (uri.scheme == "content") { |
|
|
|
if (uri.scheme == "content") { |
|
|
|
LocalBook.importFile(uri.toString()) |
|
|
|
LocalBook.importFile(uri.toString()) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
LocalBook.importFile(uri.path.toString()) |
|
|
|
LocalBook.importFile(uri.path.toString()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
toast("添加本地文件成功${uri.path}") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return null |
|
|
|
val intent = Intent(context, ReadBookActivity::class.java) |
|
|
|
|
|
|
|
intent.putExtra("bookUrl", book.bookUrl) |
|
|
|
|
|
|
|
successLiveData.postValue(intent) |
|
|
|
|
|
|
|
return@execute |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
toast("文件不存在") |
|
|
|
errorLiveData.postValue("文件不存在") |
|
|
|
return null |
|
|
|
return@execute |
|
|
|
} |
|
|
|
} |
|
|
|
// content模式下,需要传递完整的路径,方便后续解析 |
|
|
|
// content模式下,需要传递完整的路径,方便后续解析 |
|
|
|
url = if (uri.scheme == "content") { |
|
|
|
url = if (uri.scheme == "content") { |
|
|
@ -75,11 +80,11 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica |
|
|
|
val data = Uri.parse(url) |
|
|
|
val data = Uri.parse(url) |
|
|
|
val newIndent = Intent(Intent.ACTION_VIEW) |
|
|
|
val newIndent = Intent(Intent.ACTION_VIEW) |
|
|
|
newIndent.data = data |
|
|
|
newIndent.data = data |
|
|
|
return newIndent |
|
|
|
successLiveData.postValue(newIndent) |
|
|
|
} catch (e: Exception) { |
|
|
|
return@execute |
|
|
|
e.printStackTrace() |
|
|
|
}.onError { |
|
|
|
toast(e.localizedMessage) |
|
|
|
it.printStackTrace() |
|
|
|
return null |
|
|
|
toast(it.localizedMessage) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |