From f3c952ed56241087b8516791de17482494155738 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 1 Jan 2022 19:17:09 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=96=B0=E5=AE=89=E8=A3=85=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E5=90=8E=E6=B2=A1=E6=9C=89=E6=9D=83=E9=99=90=E7=9A=84?= =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E4=B9=A6=E7=B1=8D=E4=BC=9A=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E6=96=87=E4=BB=B6=E6=89=80=E5=9C=A8=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=B9,=E4=B8=8D=E9=9C=80=E8=A6=81=E9=87=8D?= =?UTF-8?q?=E6=96=B0=E6=B7=BB=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/model/localBook/LocalBook.kt | 2 +- .../app/ui/book/read/BaseReadBookActivity.kt | 14 ++++++++++++++ .../legado/app/ui/book/read/ReadBookViewModel.kt | 9 ++++++++- .../legado/app/ui/document/HandleFileActivity.kt | 9 +++++---- .../legado/app/ui/document/HandleFileContract.kt | 1 + 5 files changed, 29 insertions(+), 6 deletions(-) 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 }