使用本地书籍导入和webDav书籍时请求设置书籍保存位置 (#2386)

* refactor: remove storage setting request

* refactor: request storage setting when needed
pull/2389/head^2
Xwite 2 years ago committed by GitHub
parent ca2381e8b6
commit 75bd60ad19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      app/src/main/AndroidManifest.xml
  2. 4
      app/src/main/assets/storageHelp.md
  3. 4
      app/src/main/java/io/legado/app/model/localBook/LocalBook.kt
  4. 2
      app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt
  5. 45
      app/src/main/java/io/legado/app/ui/book/import/BaseImportBookActivity.kt
  6. 7
      app/src/main/java/io/legado/app/ui/book/import/local/ImportBookActivity.kt
  7. 2
      app/src/main/java/io/legado/app/ui/book/import/local/ImportBookAdapter.kt
  8. 2
      app/src/main/java/io/legado/app/ui/book/import/local/ImportBookViewModel.kt
  9. 2
      app/src/main/java/io/legado/app/ui/book/import/local/rule/TxtTocRuleActivity.kt
  10. 2
      app/src/main/java/io/legado/app/ui/book/import/local/rule/TxtTocRuleAdapter.kt
  11. 2
      app/src/main/java/io/legado/app/ui/book/import/local/rule/TxtTocRuleViewModel.kt
  12. 7
      app/src/main/java/io/legado/app/ui/book/import/remote/RemoteBookActivity.kt
  13. 2
      app/src/main/java/io/legado/app/ui/book/import/remote/RemoteBookAdapter.kt
  14. 2
      app/src/main/java/io/legado/app/ui/book/import/remote/RemoteBookSort.kt
  15. 2
      app/src/main/java/io/legado/app/ui/book/import/remote/RemoteBookViewModel.kt
  16. 2
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt
  17. 31
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt
  18. 4
      app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt

@ -201,7 +201,7 @@
android:screenOrientation="behind" /> android:screenOrientation="behind" />
<!-- txt目录规则管理 --> <!-- txt目录规则管理 -->
<activity <activity
android:name=".ui.book.local.rule.TxtTocRuleActivity" android:name=".ui.book.import.local.rule.TxtTocRuleActivity"
android:launchMode="singleTop" android:launchMode="singleTop"
android:screenOrientation="behind" /> android:screenOrientation="behind" />
<!-- 替换规则界面 --> <!-- 替换规则界面 -->
@ -241,11 +241,11 @@
android:launchMode="singleTop" /> android:launchMode="singleTop" />
<!-- 导入书籍 --> <!-- 导入书籍 -->
<activity <activity
android:name=".ui.book.local.ImportBookActivity" android:name=".ui.book.import.local.ImportBookActivity"
android:launchMode="singleTop" /> android:launchMode="singleTop" />
<!-- 添加远程 --> <!-- 添加远程 -->
<activity <activity
android:name=".ui.book.remote.RemoteBookActivity" android:name=".ui.book.import.remote.RemoteBookActivity"
android:launchMode="singleTop" /> android:launchMode="singleTop" />
<!-- 发现界面 --> <!-- 发现界面 -->
<activity <activity

@ -1,2 +1,2 @@
* 由于安卓的存储访问限制,阅读需要设置**公共目录下的子目录**来实现书籍拷贝、下载 * 由于安卓的存储访问限制,阅读需要设置**公共目录下的子目录**来实现书籍拷贝、下载,例如Documents/Books、Download/Books
* 如不设置,可能影响本地书籍、webDav书籍的正常阅读 * 如不设置,将无法正常使用本地书籍、webDav书籍的相关功能

@ -42,7 +42,7 @@ object LocalBook {
@Throws(FileNotFoundException::class, SecurityException::class) @Throws(FileNotFoundException::class, SecurityException::class)
fun getBookInputStream(book: Book): InputStream { fun getBookInputStream(book: Book): InputStream {
val uri = book.getLocalUri() val uri = book.getLocalUri()
//文件不存在 尝试下载webDav文件 book.remoteUrl //文件不存在 尝试下载webDav文件
val inputStream = uri.inputStream(appCtx) ?: downloadRemoteBook(book) val inputStream = uri.inputStream(appCtx) ?: downloadRemoteBook(book)
if (inputStream != null) return inputStream if (inputStream != null) return inputStream
throw FileNotFoundException("${uri.path} 文件不存在") throw FileNotFoundException("${uri.path} 文件不存在")
@ -303,7 +303,7 @@ object LocalBook {
return localBook return localBook
} }
//下载book.remoteUrl对应的远程文件并更新bookUrl 返回inputStream //下载book对应的远程文件并更新bookUrl 返回inputStream
private fun downloadRemoteBook(localBook: Book): InputStream? { private fun downloadRemoteBook(localBook: Book): InputStream? {
val webDavUrl = localBook.getRemoteUrl() val webDavUrl = localBook.getRemoteUrl()
if (webDavUrl.isNullOrBlank()) return null if (webDavUrl.isNullOrBlank()) return null

@ -172,7 +172,7 @@ class FileAssociationActivity :
}.onFailure { }.onFailure {
when (it) { when (it) {
is SecurityException -> localBookTreeSelect.launch { is SecurityException -> localBookTreeSelect.launch {
title = "选择保存书籍的文件夹" title = getString(R.string.select_book_folder)
mode = HandleFileContract.DIR_SYS mode = HandleFileContract.DIR_SYS
} }
else -> { else -> {

@ -0,0 +1,45 @@
package io.legado.app.ui.book.import
import androidx.lifecycle.ViewModel
import androidx.viewbinding.ViewBinding
import io.legado.app.R
import io.legado.app.base.VMBaseActivity
import io.legado.app.help.config.AppConfig
import io.legado.app.lib.dialogs.alert
import io.legado.app.ui.document.HandleFileContract
abstract class BaseImportBookActivity<VB : ViewBinding, VM : ViewModel>(): VMBaseActivity<VB, VM>() {
private val localBookTreeSelect = registerForActivityResult(HandleFileContract()) {
it.uri?.let { treeUri ->
AppConfig.defaultBookTreeUri = treeUri.toString()
}
}
/**
* 设置书籍保存位置
*/
protected fun setBookStorage() {
//测试书籍保存位置是否设置
if (!AppConfig.defaultBookTreeUri.isNullOrBlank()) return
//测试读写??
val storageHelp = String(assets.open("storageHelp.md").readBytes())
val hint = getString(R.string.select_book_folder)
alert(hint, storageHelp) {
yesButton {
localBookTreeSelect.launch {
title = hint
mode = HandleFileContract.DIR_SYS
}
}
noButton {
finish()
}
onCancelled {
finish()
}
}
}
}

@ -1,4 +1,4 @@
package io.legado.app.ui.book.local package io.legado.app.ui.book.import.local
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.net.Uri import android.net.Uri
@ -11,7 +11,6 @@ import androidx.appcompat.widget.PopupMenu
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseActivity
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.databinding.ActivityImportBookBinding import io.legado.app.databinding.ActivityImportBookBinding
@ -21,6 +20,7 @@ import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.permission.Permissions import io.legado.app.lib.permission.Permissions
import io.legado.app.lib.permission.PermissionsCompat import io.legado.app.lib.permission.PermissionsCompat
import io.legado.app.lib.theme.backgroundColor import io.legado.app.lib.theme.backgroundColor
import io.legado.app.ui.book.import.BaseImportBookActivity
import io.legado.app.ui.document.HandleFileContract import io.legado.app.ui.document.HandleFileContract
import io.legado.app.ui.widget.SelectActionBar import io.legado.app.ui.widget.SelectActionBar
import io.legado.app.utils.* import io.legado.app.utils.*
@ -34,7 +34,7 @@ import java.io.File
/** /**
* 导入本地书籍界面 * 导入本地书籍界面
*/ */
class ImportBookActivity : VMBaseActivity<ActivityImportBookBinding, ImportBookViewModel>(), class ImportBookActivity : BaseImportBookActivity<ActivityImportBookBinding, ImportBookViewModel>(),
PopupMenu.OnMenuItemClickListener, PopupMenu.OnMenuItemClickListener,
ImportBookAdapter.CallBack, ImportBookAdapter.CallBack,
SelectActionBar.CallBack { SelectActionBar.CallBack {
@ -61,6 +61,7 @@ class ImportBookActivity : VMBaseActivity<ActivityImportBookBinding, ImportBookV
initView() initView()
initEvent() initEvent()
initData() initData()
setBookStorage()
} }
override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {

@ -1,4 +1,4 @@
package io.legado.app.ui.book.local package io.legado.app.ui.book.import.local
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Context import android.content.Context

@ -1,4 +1,4 @@
package io.legado.app.ui.book.local package io.legado.app.ui.book.import.local
import android.app.Application import android.app.Application
import android.net.Uri import android.net.Uri

@ -1,4 +1,4 @@
package io.legado.app.ui.book.local.rule package io.legado.app.ui.book.import.local.rule
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.os.Bundle import android.os.Bundle

@ -1,4 +1,4 @@
package io.legado.app.ui.book.local.rule package io.legado.app.ui.book.import.local.rule
import android.content.Context import android.content.Context
import android.os.Bundle import android.os.Bundle

@ -1,4 +1,4 @@
package io.legado.app.ui.book.local.rule package io.legado.app.ui.book.import.local.rule
import android.app.Application import android.app.Application
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel

@ -1,4 +1,4 @@
package io.legado.app.ui.book.remote package io.legado.app.ui.book.import.remote
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.os.Bundle import android.os.Bundle
@ -9,11 +9,11 @@ import androidx.activity.viewModels
import androidx.core.view.isGone import androidx.core.view.isGone
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseActivity
import io.legado.app.databinding.ActivityImportBookBinding import io.legado.app.databinding.ActivityImportBookBinding
import io.legado.app.lib.theme.backgroundColor import io.legado.app.lib.theme.backgroundColor
import io.legado.app.model.remote.RemoteBook import io.legado.app.model.remote.RemoteBook
import io.legado.app.ui.about.AppLogDialog import io.legado.app.ui.about.AppLogDialog
import io.legado.app.ui.book.import.BaseImportBookActivity
import io.legado.app.ui.widget.SelectActionBar import io.legado.app.ui.widget.SelectActionBar
import io.legado.app.ui.widget.dialog.WaitDialog import io.legado.app.ui.widget.dialog.WaitDialog
import io.legado.app.utils.showDialogFragment import io.legado.app.utils.showDialogFragment
@ -28,7 +28,7 @@ import java.io.File
* @author qianfanguojin * @author qianfanguojin
* @time 2022/05/12 * @time 2022/05/12
*/ */
class RemoteBookActivity : VMBaseActivity<ActivityImportBookBinding, RemoteBookViewModel>(), class RemoteBookActivity : BaseImportBookActivity<ActivityImportBookBinding, RemoteBookViewModel>(),
RemoteBookAdapter.CallBack, RemoteBookAdapter.CallBack,
SelectActionBar.CallBack { SelectActionBar.CallBack {
override val binding by viewBinding(ActivityImportBookBinding::inflate) override val binding by viewBinding(ActivityImportBookBinding::inflate)
@ -41,6 +41,7 @@ class RemoteBookActivity : VMBaseActivity<ActivityImportBookBinding, RemoteBookV
initView() initView()
initData() initData()
initEvent() initEvent()
setBookStorage()
} }
private fun initView() { private fun initView() {

@ -1,4 +1,4 @@
package io.legado.app.ui.book.remote package io.legado.app.ui.book.import.remote
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Context import android.content.Context

@ -1,4 +1,4 @@
package io.legado.app.ui.book.remote package io.legado.app.ui.book.import.remote
enum class RemoteBookSort { enum class RemoteBookSort {
Default, Name Default, Name

@ -1,4 +1,4 @@
package io.legado.app.ui.book.remote package io.legado.app.ui.book.import.remote
import android.app.Application import android.app.Application
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel

@ -28,7 +28,7 @@ import io.legado.app.lib.theme.primaryTextColor
import io.legado.app.model.CheckSource import io.legado.app.model.CheckSource
import io.legado.app.model.Debug import io.legado.app.model.Debug
import io.legado.app.ui.association.ImportBookSourceDialog import io.legado.app.ui.association.ImportBookSourceDialog
import io.legado.app.ui.book.local.rule.TxtTocRuleActivity import io.legado.app.ui.book.import.local.rule.TxtTocRuleActivity
import io.legado.app.ui.book.source.debug.BookSourceDebugActivity import io.legado.app.ui.book.source.debug.BookSourceDebugActivity
import io.legado.app.ui.book.source.edit.BookSourceEditActivity import io.legado.app.ui.book.source.edit.BookSourceEditActivity
import io.legado.app.ui.config.CheckSourceConfig import io.legado.app.ui.config.CheckSourceConfig

@ -31,7 +31,6 @@ import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.theme.elevation import io.legado.app.lib.theme.elevation
import io.legado.app.lib.theme.primaryColor import io.legado.app.lib.theme.primaryColor
import io.legado.app.service.BaseReadAloudService import io.legado.app.service.BaseReadAloudService
import io.legado.app.ui.document.HandleFileContract
import io.legado.app.ui.main.bookshelf.BaseBookshelfFragment import io.legado.app.ui.main.bookshelf.BaseBookshelfFragment
import io.legado.app.ui.main.bookshelf.style1.BookshelfFragment1 import io.legado.app.ui.main.bookshelf.style1.BookshelfFragment1
import io.legado.app.ui.main.bookshelf.style2.BookshelfFragment2 import io.legado.app.ui.main.bookshelf.style2.BookshelfFragment2
@ -73,12 +72,6 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
private var bottomMenuCount = 4 private var bottomMenuCount = 4
private val realPositions = arrayOf(idBookshelf, idExplore, idRss, idMy) private val realPositions = arrayOf(idBookshelf, idExplore, idRss, idMy)
private val localBookTreeSelect = registerForActivityResult(HandleFileContract()) {
it.uri?.let { treeUri ->
AppConfig.defaultBookTreeUri = treeUri.toString()
}
}
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
upBottomMenu() upBottomMenu()
binding.run { binding.run {
@ -99,8 +92,6 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
if (!privacyPolicy()) return@launch if (!privacyPolicy()) return@launch
//版本更新 //版本更新
upVersion() upVersion()
//请求设置书籍保存位置
setBookStorage()
//自动更新书籍 //自动更新书籍
val isAutoRefreshedBook = savedInstanceState?.getBoolean("isAutoRefreshedBook") ?: false val isAutoRefreshedBook = savedInstanceState?.getBoolean("isAutoRefreshedBook") ?: false
if (AppConfig.autoRefreshBook && !isAutoRefreshedBook) { if (AppConfig.autoRefreshBook && !isAutoRefreshedBook) {
@ -219,28 +210,6 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
} }
} }
/**
* 设置书籍保存位置
*/
private fun setBookStorage() {
//测试书籍保存位置是否设置
if (!AppConfig.defaultBookTreeUri.isNullOrBlank()) return
//测试读写??
val storageHelp = String(assets.open("storageHelp.md").readBytes())
alert("设置书籍保存位置", storageHelp) {
yesButton {
localBookTreeSelect.launch {
title = getString(R.string.select_book_folder)
mode = HandleFileContract.DIR_SYS
}
}
neutralButton("不想设置") {
AppConfig.defaultBookTreeUri = "null"
}
noButton()
}
}
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean { override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
event?.let { event?.let {
when (keyCode) { when (keyCode) {

@ -21,9 +21,9 @@ import io.legado.app.lib.dialogs.alert
import io.legado.app.ui.about.AppLogDialog import io.legado.app.ui.about.AppLogDialog
import io.legado.app.ui.book.cache.CacheActivity import io.legado.app.ui.book.cache.CacheActivity
import io.legado.app.ui.book.group.GroupManageDialog import io.legado.app.ui.book.group.GroupManageDialog
import io.legado.app.ui.book.local.ImportBookActivity import io.legado.app.ui.book.import.local.ImportBookActivity
import io.legado.app.ui.book.import.remote.RemoteBookActivity
import io.legado.app.ui.book.manage.BookshelfManageActivity import io.legado.app.ui.book.manage.BookshelfManageActivity
import io.legado.app.ui.book.remote.RemoteBookActivity
import io.legado.app.ui.book.search.SearchActivity import io.legado.app.ui.book.search.SearchActivity
import io.legado.app.ui.document.HandleFileContract import io.legado.app.ui.document.HandleFileContract
import io.legado.app.ui.main.MainViewModel import io.legado.app.ui.main.MainViewModel

Loading…
Cancel
Save