diff --git a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt index 59cfe9729..62ef5dcea 100644 --- a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt +++ b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt @@ -29,7 +29,7 @@ object LocalBook { FileUtils.createFolderIfNotExist(appCtx.externalFiles, folderName) } - @Throws(FileNotFoundException::class) + @Throws(FileNotFoundException::class, SecurityException::class) fun getBookInputStream(book: Book): InputStream { if (book.bookUrl.isContentScheme()) { val uri = Uri.parse(book.bookUrl) diff --git a/app/src/main/java/io/legado/app/ui/book/read/BaseReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/BaseReadBookActivity.kt index eaf81ce9e..ec77fbbd7 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/BaseReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/BaseReadBookActivity.kt @@ -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.ClickActionConfigDialog 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.viewbindingdelegate.viewBinding @@ -41,6 +42,13 @@ abstract class BaseReadBookActivity : override val binding by viewBinding(ActivityBookReadBinding::inflate) override val viewModel by viewModels() 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?) { ReadBook.msg = null @@ -51,6 +59,12 @@ abstract class BaseReadBookActivity : override fun onActivityCreated(savedInstanceState: Bundle?) { binding.navigationBar.setBackgroundColor(bottomBackground) + viewModel.permissionDenialLiveData.observe(this) { + selectBookFolderResult.launch { + mode = HandleFileContract.SYS_DIR + title = "选择书籍所在文件夹" + } + } if (!LocalConfig.readHelpVersionIsLast) { showClickRegionalConfig() } diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index 21c6830be..d4312e82b 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -2,6 +2,7 @@ package io.legado.app.ui.book.read import android.app.Application import android.content.Intent +import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope import io.legado.app.R import io.legado.app.base.BaseViewModel @@ -30,6 +31,7 @@ import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.ensureActive class ReadBookViewModel(application: Application) : BaseViewModel(application) { + val permissionDenialLiveData = MutableLiveData() var isInitFinish = false var searchContentQuery = "" private var changeSourceCoroutine: Coroutine<*>? = null @@ -121,7 +123,12 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { ReadBook.loadContent(resetPageOffset = true) } }.onError { - ReadBook.upMsg("LoadTocError:${it.localizedMessage}") + when (it) { + is SecurityException -> { + permissionDenialLiveData.postValue(1) + } + else -> ReadBook.upMsg("LoadTocError:${it.localizedMessage}") + } } } else { ReadBook.bookSource?.let { diff --git a/app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt b/app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt index bbe22ccf4..2f4cebcb9 100644 --- a/app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt +++ b/app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt @@ -58,6 +58,7 @@ class HandleFileActivity : } val allowExtensions = intent.getStringArrayExtra("allowExtensions") val selectList: ArrayList> = when (mode) { + HandleFileContract.SYS_DIR -> getDirActions(true) HandleFileContract.DIR -> getDirActions() HandleFileContract.FILE -> getFileActions() HandleFileContract.EXPORT -> arrayListOf( @@ -141,14 +142,14 @@ class HandleFileActivity : return null } - private fun getDirActions(): ArrayList> { - return if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { + private fun getDirActions(onlySys: Boolean = false): ArrayList> { + return if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q || onlySys) { + arrayListOf(SelectItem(getString(R.string.sys_folder_picker), HandleFileContract.DIR)) + } else { arrayListOf( SelectItem(getString(R.string.sys_folder_picker), HandleFileContract.DIR), SelectItem(getString(R.string.app_folder_picker), 10) ) - } else { - arrayListOf(SelectItem(getString(R.string.sys_folder_picker), HandleFileContract.DIR)) } } diff --git a/app/src/main/java/io/legado/app/ui/document/HandleFileContract.kt b/app/src/main/java/io/legado/app/ui/document/HandleFileContract.kt index 1c0a6738f..76d13d73d 100644 --- a/app/src/main/java/io/legado/app/ui/document/HandleFileContract.kt +++ b/app/src/main/java/io/legado/app/ui/document/HandleFileContract.kt @@ -46,6 +46,7 @@ class HandleFileContract : companion object { const val DIR = 0 const val FILE = 1 + const val SYS_DIR = 2 const val EXPORT = 3 }