diff --git a/app/src/main/java/io/legado/app/constant/AppConst.kt b/app/src/main/java/io/legado/app/constant/AppConst.kt index 49220b429..2e8eb69c3 100644 --- a/app/src/main/java/io/legado/app/constant/AppConst.kt +++ b/app/src/main/java/io/legado/app/constant/AppConst.kt @@ -50,15 +50,17 @@ object AppConst { const val notificationIdDownload = -1122395 const val notificationIdCheckSource = -1122395 + const val imagePathKey = "imagePath" + val menuViewNames = arrayOf( "com.android.internal.view.menu.ListMenuItemView", "androidx.appcompat.view.menu.ListMenuItemView" ) @SuppressLint("PrivateResource") - val sysElevation = - appCtx.resources.getDimension(com.google.android.material.R.dimen.design_appbar_elevation) - .toInt() + val sysElevation = appCtx.resources + .getDimension(com.google.android.material.R.dimen.design_appbar_elevation) + .toInt() val androidId: String by lazy { Settings.System.getString(appCtx.contentResolver, Settings.Secure.ANDROID_ID) ?: "null" diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index cb31722c0..f610bc3a1 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -4,6 +4,7 @@ import android.annotation.SuppressLint import android.app.Activity import android.content.Intent import android.content.res.Configuration +import android.net.Uri import android.os.Bundle import android.view.* import androidx.activity.result.contract.ActivityResultContracts @@ -14,10 +15,7 @@ import androidx.core.view.size import com.jaredrummler.android.colorpicker.ColorPickerDialogListener import io.legado.app.BuildConfig import io.legado.app.R -import io.legado.app.constant.BookType -import io.legado.app.constant.EventBus -import io.legado.app.constant.PreferKey -import io.legado.app.constant.Status +import io.legado.app.constant.* import io.legado.app.data.appDb import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter @@ -58,6 +56,7 @@ import io.legado.app.ui.book.source.edit.BookSourceEditActivity import io.legado.app.ui.book.toc.TocActivityResult import io.legado.app.ui.browser.WebViewActivity import io.legado.app.ui.dict.DictDialog +import io.legado.app.ui.document.HandleFileContract import io.legado.app.ui.login.SourceLoginActivity import io.legado.app.ui.replace.ReplaceRuleActivity import io.legado.app.ui.replace.edit.ReplaceEditActivity @@ -129,6 +128,12 @@ class ReadBookActivity : BaseReadBookActivity(), } } } + private val selectImageDir = registerForActivityResult(HandleFileContract()) { + it.uri?.let { uri -> + ACache.get().put(AppConst.imagePathKey, uri.toString()) + viewModel.saveImage(it.value, uri) + } + } private var menu: Menu? = null private var autoPageJob: Job? = null private var backupJob: Job? = null @@ -1001,13 +1006,26 @@ class ReadBookActivity : BaseReadBookActivity(), popupAction.setItems( listOf( SelectItem(getString(R.string.show), "show"), - SelectItem(getString(R.string.refresh), "refresh") + SelectItem(getString(R.string.refresh), "refresh"), + SelectItem(getString(R.string.action_save), "save"), + SelectItem(getString(R.string.select_folder), "selectFolder") ) ) popupAction.onActionClick = { when (it) { "show" -> showDialogFragment(PhotoDialog(src)) "refresh" -> viewModel.refreshImage(src) + "save" -> { + val path = ACache.get().getAsString(AppConst.imagePathKey) + if (path.isNullOrEmpty()) { + selectImageDir.launch { + value = src + } + } else { + viewModel.saveImage(src, Uri.parse(path)) + } + } + "selectFolder" -> selectImageDir.launch() } popupAction.dismiss() } 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 7ae2a1589..aabdac831 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 android.net.Uri import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope import io.legado.app.R @@ -425,6 +426,10 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } } + fun saveImage(src: String?, uri: Uri) { + + } + /** * 替换规则变化 */ 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 6b64ba3c7..7332f783f 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 @@ -223,6 +223,7 @@ class HandleFileActivity : } } } else { + data.putExtra("value", intent.getStringExtra("value")) setResult(RESULT_OK, data) finish() } 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 919e56690..665f2c765 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 @@ -32,15 +32,16 @@ class HandleFileContract : intent.putExtra("fileKey", IntentData.put(fileData.second)) intent.putExtra("contentType", fileData.third) } + intent.putExtra("value", it.value) } return intent } override fun parseResult(resultCode: Int, intent: Intent?): Result { if (resultCode == RESULT_OK) { - return Result(intent?.data, requestCode) + return Result(intent?.data, requestCode, intent?.getStringExtra("value")) } - return Result(null, requestCode) + return Result(null, requestCode, intent?.getStringExtra("value")) } companion object { @@ -57,12 +58,14 @@ class HandleFileContract : var allowExtensions: Array = arrayOf(), var otherActions: ArrayList>? = null, var fileData: Triple? = null, - var requestCode: Int = 0 + var requestCode: Int = 0, + var value: String? = null ) data class Result( val uri: Uri?, - val requestCode: Int + val requestCode: Int, + val value: String? ) } \ No newline at end of file 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 0e37f5787..9e9536741 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 @@ -14,9 +14,11 @@ import androidx.webkit.WebViewFeature import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.constant.AppConst +import io.legado.app.constant.AppConst.imagePathKey import io.legado.app.databinding.ActivityRssReadBinding import io.legado.app.help.config.AppConfig import io.legado.app.lib.dialogs.SelectItem +import io.legado.app.lib.dialogs.selector import io.legado.app.lib.theme.accentColor import io.legado.app.lib.theme.primaryTextColor import io.legado.app.model.Download @@ -38,15 +40,13 @@ class ReadRssActivity : VMBaseActivity override val binding by viewBinding(ActivityRssReadBinding::inflate) override val viewModel by viewModels() - private val imagePathKey = "imagePath" private var starMenuItem: MenuItem? = null private var ttsMenuItem: MenuItem? = null private var customWebViewCallback: WebChromeClient.CustomViewCallback? = null - private var webPic: String? = null - private val saveImage = registerForActivityResult(HandleFileContract()) { + private val selectImageDir = registerForActivityResult(HandleFileContract()) { it.uri?.let { uri -> ACache.get().put(imagePathKey, uri.toString()) - viewModel.saveImage(webPic, uri) + viewModel.saveImage(it.value, uri) } } @@ -135,8 +135,18 @@ class ReadRssActivity : VMBaseActivity if (hitTestResult.type == WebView.HitTestResult.IMAGE_TYPE || hitTestResult.type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE ) { - hitTestResult.extra?.let { - saveImage(it) + hitTestResult.extra?.let { webPic -> + selector( + arrayListOf( + SelectItem(getString(R.string.action_save), "save"), + SelectItem(getString(R.string.select_folder), "selectFolder") + ) + ) { _, charSequence, i -> + when (charSequence.value) { + "save" -> saveImage(webPic) + "selectFolder" -> selectSaveFolder(null) + } + } return@setOnLongClickListener true } } @@ -153,23 +163,23 @@ class ReadRssActivity : VMBaseActivity } private fun saveImage(webPic: String) { - this.webPic = webPic val path = ACache.get().getAsString(imagePathKey) if (path.isNullOrEmpty()) { - selectSaveFolder() + selectSaveFolder(webPic) } else { viewModel.saveImage(webPic, Uri.parse(path)) } } - private fun selectSaveFolder() { + private fun selectSaveFolder(webPic: String?) { val default = arrayListOf>() val path = ACache.get().getAsString(imagePathKey) if (!path.isNullOrEmpty()) { default.add(SelectItem(path, -1)) } - saveImage.launch { + selectImageDir.launch { otherActions = default + value = webPic } }