重新安装应用后没有权限的本地书籍会提示选择文件所在文件夹,不需要重新添加

pull/1487/head
kunfei 3 years ago
parent 9d5a5f9ddc
commit f3c952ed56
  1. 2
      app/src/main/java/io/legado/app/model/localBook/LocalBook.kt
  2. 14
      app/src/main/java/io/legado/app/ui/book/read/BaseReadBookActivity.kt
  3. 9
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt
  4. 9
      app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt
  5. 1
      app/src/main/java/io/legado/app/ui/document/HandleFileContract.kt

@ -29,7 +29,7 @@ object LocalBook {
FileUtils.createFolderIfNotExist(appCtx.externalFiles, folderName) FileUtils.createFolderIfNotExist(appCtx.externalFiles, folderName)
} }
@Throws(FileNotFoundException::class) @Throws(FileNotFoundException::class, SecurityException::class)
fun getBookInputStream(book: Book): InputStream { fun getBookInputStream(book: Book): InputStream {
if (book.bookUrl.isContentScheme()) { if (book.bookUrl.isContentScheme()) {
val uri = Uri.parse(book.bookUrl) val uri = Uri.parse(book.bookUrl)

@ -29,6 +29,7 @@ import io.legado.app.model.ReadBook
import io.legado.app.ui.book.read.config.BgTextConfigDialog import io.legado.app.ui.book.read.config.BgTextConfigDialog
import io.legado.app.ui.book.read.config.ClickActionConfigDialog import io.legado.app.ui.book.read.config.ClickActionConfigDialog
import io.legado.app.ui.book.read.config.PaddingConfigDialog import io.legado.app.ui.book.read.config.PaddingConfigDialog
import io.legado.app.ui.document.HandleFileContract
import io.legado.app.utils.* import io.legado.app.utils.*
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
@ -41,6 +42,13 @@ abstract class BaseReadBookActivity :
override val binding by viewBinding(ActivityBookReadBinding::inflate) override val binding by viewBinding(ActivityBookReadBinding::inflate)
override val viewModel by viewModels<ReadBookViewModel>() override val viewModel by viewModels<ReadBookViewModel>()
var bottomDialog = 0 var bottomDialog = 0
private val selectBookFolderResult = registerForActivityResult(HandleFileContract()){
it.uri?.let {
ReadBook.book?.let { book ->
viewModel.loadChapterList(book)
}
} ?: ReadBook.upMsg("没有权限访问")
}
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
ReadBook.msg = null ReadBook.msg = null
@ -51,6 +59,12 @@ abstract class BaseReadBookActivity :
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
binding.navigationBar.setBackgroundColor(bottomBackground) binding.navigationBar.setBackgroundColor(bottomBackground)
viewModel.permissionDenialLiveData.observe(this) {
selectBookFolderResult.launch {
mode = HandleFileContract.SYS_DIR
title = "选择书籍所在文件夹"
}
}
if (!LocalConfig.readHelpVersionIsLast) { if (!LocalConfig.readHelpVersionIsLast) {
showClickRegionalConfig() showClickRegionalConfig()
} }

@ -2,6 +2,7 @@ package io.legado.app.ui.book.read
import android.app.Application import android.app.Application
import android.content.Intent import android.content.Intent
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
@ -30,6 +31,7 @@ import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.ensureActive import kotlinx.coroutines.ensureActive
class ReadBookViewModel(application: Application) : BaseViewModel(application) { class ReadBookViewModel(application: Application) : BaseViewModel(application) {
val permissionDenialLiveData = MutableLiveData<Int>()
var isInitFinish = false var isInitFinish = false
var searchContentQuery = "" var searchContentQuery = ""
private var changeSourceCoroutine: Coroutine<*>? = null private var changeSourceCoroutine: Coroutine<*>? = null
@ -121,7 +123,12 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
ReadBook.loadContent(resetPageOffset = true) ReadBook.loadContent(resetPageOffset = true)
} }
}.onError { }.onError {
ReadBook.upMsg("LoadTocError:${it.localizedMessage}") when (it) {
is SecurityException -> {
permissionDenialLiveData.postValue(1)
}
else -> ReadBook.upMsg("LoadTocError:${it.localizedMessage}")
}
} }
} else { } else {
ReadBook.bookSource?.let { ReadBook.bookSource?.let {

@ -58,6 +58,7 @@ class HandleFileActivity :
} }
val allowExtensions = intent.getStringArrayExtra("allowExtensions") val allowExtensions = intent.getStringArrayExtra("allowExtensions")
val selectList: ArrayList<SelectItem<Int>> = when (mode) { val selectList: ArrayList<SelectItem<Int>> = when (mode) {
HandleFileContract.SYS_DIR -> getDirActions(true)
HandleFileContract.DIR -> getDirActions() HandleFileContract.DIR -> getDirActions()
HandleFileContract.FILE -> getFileActions() HandleFileContract.FILE -> getFileActions()
HandleFileContract.EXPORT -> arrayListOf( HandleFileContract.EXPORT -> arrayListOf(
@ -141,14 +142,14 @@ class HandleFileActivity :
return null return null
} }
private fun getDirActions(): ArrayList<SelectItem<Int>> { private fun getDirActions(onlySys: Boolean = false): ArrayList<SelectItem<Int>> {
return if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { return if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q || onlySys) {
arrayListOf(SelectItem(getString(R.string.sys_folder_picker), HandleFileContract.DIR))
} else {
arrayListOf( arrayListOf(
SelectItem(getString(R.string.sys_folder_picker), HandleFileContract.DIR), SelectItem(getString(R.string.sys_folder_picker), HandleFileContract.DIR),
SelectItem(getString(R.string.app_folder_picker), 10) SelectItem(getString(R.string.app_folder_picker), 10)
) )
} else {
arrayListOf(SelectItem(getString(R.string.sys_folder_picker), HandleFileContract.DIR))
} }
} }

@ -46,6 +46,7 @@ class HandleFileContract :
companion object { companion object {
const val DIR = 0 const val DIR = 0
const val FILE = 1 const val FILE = 1
const val SYS_DIR = 2
const val EXPORT = 3 const val EXPORT = 3
} }

Loading…
Cancel
Save