diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt index 81f1e13fd..ae0bdcd5e 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt @@ -46,19 +46,19 @@ class CacheActivity : VMBaseActivity() private val adapter by lazy { CacheAdapter(this, this) } private var booksFlowJob: Job? = null private var menu: Menu? = null - private var exportPosition = -1 private val groupList: ArrayList = arrayListOf() private var groupId: Long = -1 - private val exportDir = registerForActivityResult(HandleFileContract()) { uri -> - uri ?: return@registerForActivityResult - if (uri.isContentScheme()) { - ACache.get(this@CacheActivity).put(exportBookPathKey, uri.toString()) - startExport(uri.toString()) - } else { - uri.path?.let { path -> - ACache.get(this@CacheActivity).put(exportBookPathKey, path) - startExport(path) + private val exportDir = registerForActivityResult(HandleFileContract()) { result -> + result.uri?.let { uri -> + if (uri.isContentScheme()) { + ACache.get(this@CacheActivity).put(exportBookPathKey, uri.toString()) + startExport(uri.toString(), result.requestCode) + } else { + uri.path?.let { path -> + ACache.get(this@CacheActivity).put(exportBookPathKey, path) + startExport(path, result.requestCode) + } } } } @@ -128,8 +128,7 @@ class CacheActivity : VMBaseActivity() R.id.menu_export_no_chapter_name -> AppConfig.exportNoChapterName = !item.isChecked R.id.menu_export_web_dav -> AppConfig.exportToWebDav = !item.isChecked R.id.menu_export_folder -> { - exportPosition = -1 - selectExportFolder() + selectExportFolder(-1) } R.id.menu_export_file_name -> alertExportFileName() R.id.menu_export_type -> showExportTypeConfig() @@ -236,26 +235,24 @@ class CacheActivity : VMBaseActivity() } override fun export(position: Int) { - exportPosition = position val path = ACache.get(this@CacheActivity).getAsString(exportBookPathKey) if (path.isNullOrEmpty()) { - selectExportFolder() + selectExportFolder(position) } else { - startExport(path) + startExport(path, position) } } private fun exportAll() { - exportPosition = -10 val path = ACache.get(this@CacheActivity).getAsString(exportBookPathKey) if (path.isNullOrEmpty()) { - selectExportFolder() + selectExportFolder(-10) } else { - startExport(path) + startExport(path, -10) } } - private fun selectExportFolder() { + private fun selectExportFolder(exportPosition: Int) { val default = arrayListOf>() val path = ACache.get(this@CacheActivity).getAsString(exportBookPathKey) if (!path.isNullOrEmpty()) { @@ -263,10 +260,11 @@ class CacheActivity : VMBaseActivity() } exportDir.launch { otherActions = default + requestCode = exportPosition } } - private fun startExport(path: String) { + private fun startExport(path: String, exportPosition: Int) { if (exportPosition == -10) { if (adapter.getItems().isNotEmpty()) { adapter.getItems().forEach { book -> diff --git a/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt index f71e4047f..1579213dc 100644 --- a/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt @@ -49,15 +49,16 @@ class ImportBookActivity : VMBaseActivity - uri ?: return@registerForActivityResult - if (uri.isContentScheme()) { - AppConfig.importBookPath = uri.toString() - initRootDoc() - } else { - uri.path?.let { path -> - AppConfig.importBookPath = path + private val selectFolder = registerForActivityResult(HandleFileContract()) { + it.uri?.let { uri -> + if (uri.isContentScheme()) { + AppConfig.importBookPath = uri.toString() initRootDoc() + } else { + uri.path?.let { path -> + AppConfig.importBookPath = path + initRootDoc() + } } } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt index 3bee08141..61efde2aa 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt @@ -5,7 +5,10 @@ import android.content.DialogInterface import android.graphics.Color import android.net.Uri import android.os.Bundle -import android.view.* +import android.view.Gravity +import android.view.View +import android.view.ViewGroup +import android.view.WindowManager import androidx.documentfile.provider.DocumentFile import com.jaredrummler.android.colorpicker.ColorPickerDialog import io.legado.app.R @@ -50,15 +53,17 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) { } } private val selectExportDir = registerForActivityResult(HandleFileContract()) { - it ?: return@registerForActivityResult - exportConfig(it) + it.uri?.let { uri -> + exportConfig(uri) + } } private val selectImportDoc = registerForActivityResult(HandleFileContract()) { - it ?: return@registerForActivityResult - if (it.toString() == importFormNet) { - importNetConfigAlert() - } else { - importConfig(it) + it.uri?.let { uri -> + if (uri.toString() == importFormNet) { + importNetConfigAlert() + } else { + importConfig(uri) + } } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt index 285cef3c6..26d52ba59 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt @@ -39,25 +39,26 @@ class SpeakEngineDialog : BaseDialogFragment(R.layout.dialog_recycler_view), private val adapter by lazy { Adapter(requireContext()) } private var ttsEngine: String? = AppConfig.ttsEngine private val importDocResult = registerForActivityResult(HandleFileContract()) { - it?.let { - viewModel.importLocal(it) + it.uri?.let { uri -> + viewModel.importLocal(uri) } } - private val exportDirResult = registerForActivityResult(HandleFileContract()) { uri -> - uri ?: return@registerForActivityResult - alert(R.string.export_success) { - if (uri.toString().isAbsUrl()) { - DirectLinkUpload.getSummary()?.let { summary -> - setMessage(summary) + private val exportDirResult = registerForActivityResult(HandleFileContract()) { + it.uri?.let { uri -> + alert(R.string.export_success) { + if (uri.toString().isAbsUrl()) { + DirectLinkUpload.getSummary()?.let { summary -> + setMessage(summary) + } + } + val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply { + editView.hint = getString(R.string.path) + editView.setText(uri.toString()) + } + customView { alertBinding.root } + okButton { + requireContext().sendToClip(uri.toString()) } - } - val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply { - editView.hint = getString(R.string.path) - editView.setText(uri.toString()) - } - customView { alertBinding.root } - okButton { - requireContext().sendToClip(uri.toString()) } } } diff --git a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt index 2cc389c5b..e752d41ee 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt @@ -54,12 +54,13 @@ class BookSourceEditActivity : upRecyclerView(source) } } - private val selectDoc = registerForActivityResult(HandleFileContract()) { uri -> - uri ?: return@registerForActivityResult - if (uri.isContentScheme()) { - sendText(uri.toString()) - } else { - sendText(uri.path.toString()) + private val selectDoc = registerForActivityResult(HandleFileContract()) { + it.uri?.let { uri -> + if (uri.isContentScheme()) { + sendText(uri.toString()) + } else { + sendText(uri.path.toString()) + } } } diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt index 38f194b0f..1383de194 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt @@ -64,29 +64,31 @@ class BookSourceActivity : VMBaseActivity - uri ?: return@registerForActivityResult - try { - showDialogFragment(ImportBookSourceDialog(uri.readText(this))) - } catch (e: Exception) { - toastOnUi("readTextError:${e.localizedMessage}") + private val importDoc = registerForActivityResult(HandleFileContract()) { + it.uri?.let { uri -> + try { + showDialogFragment(ImportBookSourceDialog(uri.readText(this))) + } catch (e: Exception) { + toastOnUi("readTextError:${e.localizedMessage}") + } } } - private val exportDir = registerForActivityResult(HandleFileContract()) { uri -> - uri ?: return@registerForActivityResult - alert(R.string.export_success) { - if (uri.toString().isAbsUrl()) { - DirectLinkUpload.getSummary()?.let { summary -> - setMessage(summary) + private val exportDir = registerForActivityResult(HandleFileContract()) { + it.uri?.let { uri -> + alert(R.string.export_success) { + if (uri.toString().isAbsUrl()) { + DirectLinkUpload.getSummary()?.let { summary -> + setMessage(summary) + } + } + val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply { + editView.hint = getString(R.string.path) + editView.setText(uri.toString()) + } + customView { alertBinding.root } + okButton { + sendToClip(uri.toString()) } - } - val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply { - editView.hint = getString(R.string.path) - editView.setText(uri.toString()) - } - customView { alertBinding.root } - okButton { - sendToClip(uri.toString()) } } } diff --git a/app/src/main/java/io/legado/app/ui/browser/WebViewActivity.kt b/app/src/main/java/io/legado/app/ui/browser/WebViewActivity.kt index e77a34e1d..fb493067f 100644 --- a/app/src/main/java/io/legado/app/ui/browser/WebViewActivity.kt +++ b/app/src/main/java/io/legado/app/ui/browser/WebViewActivity.kt @@ -33,9 +33,10 @@ class WebViewActivity : VMBaseActivity() { private var customWebViewCallback: WebChromeClient.CustomViewCallback? = null private var webPic: String? = null private val saveImage = registerForActivityResult(HandleFileContract()) { - it ?: return@registerForActivityResult - ACache.get(this).put(imagePathKey, it.toString()) - viewModel.saveImage(webPic, it.toString()) + it.uri?.let { uri -> + ACache.get(this).put(imagePathKey, uri.toString()) + viewModel.saveImage(webPic, uri.toString()) + } } override fun onActivityCreated(savedInstanceState: Bundle?) { diff --git a/app/src/main/java/io/legado/app/ui/config/BackupConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/BackupConfigFragment.kt index 7e15f212d..77e842228 100644 --- a/app/src/main/java/io/legado/app/ui/config/BackupConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/BackupConfigFragment.kt @@ -38,56 +38,59 @@ import splitties.init.appCtx class BackupConfigFragment : BasePreferenceFragment(), SharedPreferences.OnSharedPreferenceChangeListener { - private val selectBackupPath = registerForActivityResult(HandleFileContract()) { uri -> - uri ?: return@registerForActivityResult - if (uri.isContentScheme()) { - AppConfig.backupPath = uri.toString() - } else { - AppConfig.backupPath = uri.path + private val selectBackupPath = registerForActivityResult(HandleFileContract()) { + it.uri?.let { uri -> + if (uri.isContentScheme()) { + AppConfig.backupPath = uri.toString() + } else { + AppConfig.backupPath = uri.path + } } } - private val backupDir = registerForActivityResult(HandleFileContract()) { uri -> - uri ?: return@registerForActivityResult - if (uri.isContentScheme()) { - AppConfig.backupPath = uri.toString() - Coroutine.async { - Backup.backup(appCtx, uri.toString()) - }.onSuccess { - appCtx.toastOnUi(R.string.backup_success) - }.onError { - appCtx.toastOnUi(R.string.backup_fail) - } - } else { - uri.path?.let { path -> - AppConfig.backupPath = path + private val backupDir = registerForActivityResult(HandleFileContract()) { + it.uri?.let { uri -> + if (uri.isContentScheme()) { + AppConfig.backupPath = uri.toString() Coroutine.async { - Backup.backup(appCtx, path) + Backup.backup(appCtx, uri.toString()) }.onSuccess { appCtx.toastOnUi(R.string.backup_success) }.onError { appCtx.toastOnUi(R.string.backup_fail) } + } else { + uri.path?.let { path -> + AppConfig.backupPath = path + Coroutine.async { + Backup.backup(appCtx, path) + }.onSuccess { + appCtx.toastOnUi(R.string.backup_success) + }.onError { + appCtx.toastOnUi(R.string.backup_fail) + } + } } } } - private val restoreDir = registerForActivityResult(HandleFileContract()) { uri -> - uri ?: return@registerForActivityResult - if (uri.isContentScheme()) { - AppConfig.backupPath = uri.toString() - Coroutine.async { - Restore.restore(appCtx, uri.toString()) - } - } else { - uri.path?.let { path -> - AppConfig.backupPath = path + private val restoreDir = registerForActivityResult(HandleFileContract()) { + it.uri?.let { uri -> + if (uri.isContentScheme()) { + AppConfig.backupPath = uri.toString() Coroutine.async { - Restore.restore(appCtx, path) + Restore.restore(appCtx, uri.toString()) + } + } else { + uri.path?.let { path -> + AppConfig.backupPath = path + Coroutine.async { + Restore.restore(appCtx, path) + } } } } } - private val restoreOld = registerForActivityResult(HandleFileContract()) { uri -> - uri?.let { + private val restoreOld = registerForActivityResult(HandleFileContract()) { + it.uri?.let { uri -> ImportOldData.importUri(appCtx, uri) } } 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 402d9ed63..b33d2ef70 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 @@ -11,13 +11,9 @@ import io.legado.app.utils.putJson @Suppress("unused") class HandleFileContract : - ActivityResultContract Unit, Uri?>() { + ActivityResultContract Unit, HandleFileContract.Result>() { - companion object { - const val DIR = 0 - const val FILE = 1 - const val EXPORT = 3 - } + private var requestCode: Int = 0 override fun createIntent(context: Context, input: (HandleFileParam.() -> Unit)?): Intent { val intent = Intent(context, HandleFileActivity::class.java) @@ -26,6 +22,7 @@ class HandleFileContract : handleFileParam.apply(input) } handleFileParam.let { + requestCode = it.requestCode intent.putExtra("mode", it.mode) intent.putExtra("title", it.title) intent.putExtra("allowExtensions", it.allowExtensions) @@ -39,11 +36,17 @@ class HandleFileContract : return intent } - override fun parseResult(resultCode: Int, intent: Intent?): Uri? { + override fun parseResult(resultCode: Int, intent: Intent?): Result { if (resultCode == RESULT_OK) { - return intent?.data + return Result(intent?.data, requestCode) } - return null + return Result(null, requestCode) + } + + companion object { + const val DIR = 0 + const val FILE = 1 + const val EXPORT = 3 } @Suppress("ArrayInDataClass") @@ -53,6 +56,12 @@ class HandleFileContract : var allowExtensions: Array = arrayOf(), var otherActions: ArrayList>? = null, var fileData: Triple? = null, + var requestCode: Int = 0 + ) + + data class Result( + val uri: Uri?, + val requestCode: Int ) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt index 098f01563..1b14f43d6 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt @@ -31,12 +31,16 @@ abstract class BaseBookshelfFragment(layoutId: Int) : VMBaseFragment() private val importBookshelf = registerForActivityResult(HandleFileContract()) { - it?.readText(requireContext())?.let { text -> - viewModel.importBookshelf(text, groupId) + kotlin.runCatching { + it.uri?.readText(requireContext())?.let { text -> + viewModel.importBookshelf(text, groupId) + } + }.onFailure { + toastOnUi(it.localizedMessage ?: "ERROR") } } private val exportResult = registerForActivityResult(HandleFileContract()) { - it?.let { uri -> + it.uri?.let { uri -> alert(R.string.export_success) { if (uri.toString().isAbsUrl()) { DirectLinkUpload.getSummary()?.let { summary -> diff --git a/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt b/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt index 611708aea..a595b2bd8 100644 --- a/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt @@ -71,9 +71,9 @@ class ReplaceRuleActivity : VMBaseActivity + private val importDoc = registerForActivityResult(HandleFileContract()) { kotlin.runCatching { - uri?.readText(this)?.let { + it.uri?.readText(this)?.let { showDialogFragment( ImportReplaceRuleDialog(it) ) @@ -82,21 +82,22 @@ class ReplaceRuleActivity : VMBaseActivity - uri ?: return@registerForActivityResult - alert(R.string.export_success) { - if (uri.toString().isAbsUrl()) { - DirectLinkUpload.getSummary()?.let { summary -> - setMessage(summary) + private val exportResult = registerForActivityResult(HandleFileContract()) { + it.uri?.let { uri -> + alert(R.string.export_success) { + if (uri.toString().isAbsUrl()) { + DirectLinkUpload.getSummary()?.let { summary -> + setMessage(summary) + } + } + val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply { + editView.hint = getString(R.string.path) + editView.setText(uri.toString()) + } + customView { alertBinding.root } + okButton { + sendToClip(uri.toString()) } - } - val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply { - editView.hint = getString(R.string.path) - editView.setText(uri.toString()) - } - customView { alertBinding.root } - okButton { - sendToClip(uri.toString()) } } } diff --git a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt index fcd1bc500..72fb8d97b 100644 --- a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt @@ -42,9 +42,10 @@ class ReadRssActivity : VMBaseActivity private var customWebViewCallback: WebChromeClient.CustomViewCallback? = null private var webPic: String? = null private val saveImage = registerForActivityResult(HandleFileContract()) { - it ?: return@registerForActivityResult - ACache.get(this).put(imagePathKey, it.toString()) - viewModel.saveImage(webPic, it.toString()) + it.uri?.let { uri -> + ACache.get(this).put(imagePathKey, uri.toString()) + viewModel.saveImage(webPic, uri.toString()) + } } override fun onActivityCreated(savedInstanceState: Bundle?) { diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt index 217b3d372..6a7d970ae 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt @@ -57,9 +57,9 @@ class RssSourceActivity : VMBaseActivity + private val importDoc = registerForActivityResult(HandleFileContract()) { kotlin.runCatching { - uri?.readText(this)?.let { + it.uri?.readText(this)?.let { showDialogFragment( ImportRssSourceDialog(it) ) @@ -68,21 +68,22 @@ class RssSourceActivity : VMBaseActivity - uri ?: return@registerForActivityResult - alert(R.string.export_success) { - if (uri.toString().isAbsUrl()) { - DirectLinkUpload.getSummary()?.let { summary -> - setMessage(summary) + private val exportResult = registerForActivityResult(HandleFileContract()) { + it.uri?.let { uri -> + alert(R.string.export_success) { + if (uri.toString().isAbsUrl()) { + DirectLinkUpload.getSummary()?.let { summary -> + setMessage(summary) + } + } + val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply { + editView.hint = getString(R.string.path) + editView.setText(uri.toString()) + } + customView { alertBinding.root } + okButton { + sendToClip(uri.toString()) } - } - val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply { - editView.hint = getString(R.string.path) - editView.setText(uri.toString()) - } - customView { alertBinding.root } - okButton { - sendToClip(uri.toString()) } } } diff --git a/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt b/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt index f055b22bd..d1a023c88 100644 --- a/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt +++ b/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt @@ -40,27 +40,28 @@ class FontSelectDialog : BaseDialogFragment(R.layout.dialog_font_select), val curFontPath = callBack?.curFontPath ?: "" FontAdapter(requireContext(), curFontPath, this) } - private val selectFontDir = registerForActivityResult(HandleFileContract()) { uri -> - uri ?: return@registerForActivityResult - if (uri.toString().isContentScheme()) { - putPrefString(PreferKey.fontFolder, uri.toString()) - val doc = DocumentFile.fromTreeUri(requireContext(), uri) - if (doc != null) { - context?.contentResolver?.takePersistableUriPermission( - uri, - Intent.FLAG_GRANT_READ_URI_PERMISSION - ) - loadFontFiles(doc) + private val selectFontDir = registerForActivityResult(HandleFileContract()) { + it.uri?.let { uri -> + if (uri.toString().isContentScheme()) { + putPrefString(PreferKey.fontFolder, uri.toString()) + val doc = DocumentFile.fromTreeUri(requireContext(), uri) + if (doc != null) { + context?.contentResolver?.takePersistableUriPermission( + uri, + Intent.FLAG_GRANT_READ_URI_PERMISSION + ) + loadFontFiles(doc) + } else { + RealPathUtil.getPath(requireContext(), uri)?.let { + loadFontFilesByPermission(it) + } + } } else { - RealPathUtil.getPath(requireContext(), uri)?.let { - loadFontFilesByPermission(it) + uri.path?.let { path -> + putPrefString(PreferKey.fontFolder, path) + loadFontFilesByPermission(path) } } - } else { - uri.path?.let { path -> - putPrefString(PreferKey.fontFolder, path) - loadFontFilesByPermission(path) - } } }