From 0194f33fd983c5b5d6cca17fac80850c41a34a5a Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 16 Mar 2022 22:40:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BB=85=E5=9C=A8wifi?= =?UTF-8?q?=E4=B8=8B=E5=8A=A0=E8=BD=BD=E5=B0=81=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/constant/PreferKey.kt | 1 + .../io/legado/app/help/glide/OkHttpModelLoader.kt | 5 ++++- .../io/legado/app/help/glide/OkHttpStreamFetcher.kt | 13 ++++++++++++- .../io/legado/app/ui/widget/image/CoverImageView.kt | 6 ++++++ app/src/main/res/values-es-rES/strings.xml | 4 ++++ app/src/main/res/values-ja-rJP/strings.xml | 4 ++++ app/src/main/res/values-pt-rBR/strings.xml | 4 ++++ app/src/main/res/values-zh-rHK/strings.xml | 4 ++++ app/src/main/res/values-zh-rTW/strings.xml | 4 ++++ app/src/main/res/values-zh/strings.xml | 4 ++++ app/src/main/res/values/strings.xml | 4 ++++ app/src/main/res/xml/pref_config_cover.xml | 10 ++++++++-- 12 files changed, 59 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/constant/PreferKey.kt b/app/src/main/java/io/legado/app/constant/PreferKey.kt index aac30e901..79dae28d8 100644 --- a/app/src/main/java/io/legado/app/constant/PreferKey.kt +++ b/app/src/main/java/io/legado/app/constant/PreferKey.kt @@ -8,6 +8,7 @@ object PreferKey { const val showUnread = "showUnread" const val bookGroupStyle = "bookGroupStyle" const val useDefaultCover = "useDefaultCover" + const val loadCoverOnlyWifi = "loadCoverOnlyWifi" const val coverShowName = "coverShowName" const val coverShowAuthor = "coverShowAuthor" const val coverShowNameN = "coverShowNameN" diff --git a/app/src/main/java/io/legado/app/help/glide/OkHttpModelLoader.kt b/app/src/main/java/io/legado/app/help/glide/OkHttpModelLoader.kt index 2d3daa60f..90bec21b0 100644 --- a/app/src/main/java/io/legado/app/help/glide/OkHttpModelLoader.kt +++ b/app/src/main/java/io/legado/app/help/glide/OkHttpModelLoader.kt @@ -1,5 +1,6 @@ package io.legado.app.help.glide +import com.bumptech.glide.load.Option import com.bumptech.glide.load.Options import com.bumptech.glide.load.model.GlideUrl import com.bumptech.glide.load.model.ModelLoader @@ -7,13 +8,15 @@ import java.io.InputStream object OkHttpModelLoader : ModelLoader { + val loadOnlyWifiOption = Option.memory("loadOnlyWifi", false) + override fun buildLoadData( model: GlideUrl, width: Int, height: Int, options: Options ): ModelLoader.LoadData { - return ModelLoader.LoadData(model, OkHttpStreamFetcher(model)) + return ModelLoader.LoadData(model, OkHttpStreamFetcher(model, options)) } override fun handles(model: GlideUrl): Boolean { diff --git a/app/src/main/java/io/legado/app/help/glide/OkHttpStreamFetcher.kt b/app/src/main/java/io/legado/app/help/glide/OkHttpStreamFetcher.kt index f9127e065..1d9d26be2 100644 --- a/app/src/main/java/io/legado/app/help/glide/OkHttpStreamFetcher.kt +++ b/app/src/main/java/io/legado/app/help/glide/OkHttpStreamFetcher.kt @@ -3,27 +3,38 @@ package io.legado.app.help.glide import com.bumptech.glide.Priority import com.bumptech.glide.load.DataSource import com.bumptech.glide.load.HttpException +import com.bumptech.glide.load.Options import com.bumptech.glide.load.data.DataFetcher import com.bumptech.glide.load.model.GlideUrl import com.bumptech.glide.util.ContentLengthInputStream import com.bumptech.glide.util.Preconditions import io.legado.app.help.http.okHttpClient +import io.legado.app.model.NoStackTraceException +import io.legado.app.utils.isWifiConnect import okhttp3.Call import okhttp3.Request import okhttp3.Response import okhttp3.ResponseBody +import splitties.init.appCtx import java.io.IOException import java.io.InputStream -class OkHttpStreamFetcher(private val url: GlideUrl) : +class OkHttpStreamFetcher(private val url: GlideUrl, private val options: Options) : DataFetcher, okhttp3.Callback { private var stream: InputStream? = null private var responseBody: ResponseBody? = null private var callback: DataFetcher.DataCallback? = null + + @Volatile private var call: Call? = null override fun loadData(priority: Priority, callback: DataFetcher.DataCallback) { + val loadOnlyWifi = options.get(OkHttpModelLoader.loadOnlyWifiOption) ?: false + if (loadOnlyWifi && !appCtx.isWifiConnect) { + callback.onLoadFailed(NoStackTraceException("只在wifi加载图片")) + return + } val requestBuilder: Request.Builder = Request.Builder().url(url.toStringUrl()) for ((key, value) in url.headers.entries) { requestBuilder.addHeader(key, value) diff --git a/app/src/main/java/io/legado/app/ui/widget/image/CoverImageView.kt b/app/src/main/java/io/legado/app/ui/widget/image/CoverImageView.kt index f7199c650..ea5e0ddda 100644 --- a/app/src/main/java/io/legado/app/ui/widget/image/CoverImageView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/image/CoverImageView.kt @@ -8,12 +8,16 @@ import android.util.AttributeSet import com.bumptech.glide.load.DataSource import com.bumptech.glide.load.engine.GlideException import com.bumptech.glide.request.RequestListener +import com.bumptech.glide.request.RequestOptions import com.bumptech.glide.request.target.Target import io.legado.app.constant.AppPattern +import io.legado.app.constant.PreferKey import io.legado.app.help.config.AppConfig import io.legado.app.help.glide.ImageLoader +import io.legado.app.help.glide.OkHttpModelLoader import io.legado.app.lib.theme.accentColor import io.legado.app.model.BookCover +import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.textHeight import io.legado.app.utils.toStringArray @@ -179,7 +183,9 @@ class CoverImageView @JvmOverloads constructor( .centerCrop() .into(this) } else { + val loadOnlyWifi = context.getPrefBoolean(PreferKey.loadCoverOnlyWifi, false) ImageLoader.load(context, path)//Glide自动识别http://,content://和file:// + .apply(RequestOptions().set(OkHttpModelLoader.loadOnlyWifiOption, loadOnlyWifi)) .placeholder(BookCover.defaultDrawable) .error(BookCover.defaultDrawable) .listener(glideListener) diff --git a/app/src/main/res/values-es-rES/strings.xml b/app/src/main/res/values-es-rES/strings.xml index 1670dc97e..10bf02c46 100644 --- a/app/src/main/res/values-es-rES/strings.xml +++ b/app/src/main/res/values-es-rES/strings.xml @@ -957,5 +957,9 @@ 缓存/导出 辅助按键配置 Url参数 + 仅WIFI + 仅在wifi下加载网络封面 + 封面规则 + 进入详情页时使用封面规则重新获取封面 diff --git a/app/src/main/res/values-ja-rJP/strings.xml b/app/src/main/res/values-ja-rJP/strings.xml index 1fc350346..b7dc408c8 100644 --- a/app/src/main/res/values-ja-rJP/strings.xml +++ b/app/src/main/res/values-ja-rJP/strings.xml @@ -960,5 +960,9 @@ 缓存/导出 辅助按键配置 Url参数 + 仅WIFI + 仅在wifi下加载网络封面 + 封面规则 + 进入详情页时使用封面规则重新获取封面 diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index 1e9f96217..09b72966e 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -960,5 +960,9 @@ 缓存/导出 辅助按键配置 Url参数 + 仅WIFI + 仅在wifi下加载网络封面 + 封面规则 + 进入详情页时使用封面规则重新获取封面 diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index e0737af7a..02661958d 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -957,5 +957,9 @@ 缓存/导出 辅助按键配置 Url参数 + 仅WIFI + 仅在wifi下加载网络封面 + 封面规则 + 进入详情页时使用封面规则重新获取封面 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index e76a8156d..a4740207b 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -959,5 +959,9 @@ 缓存/导出 辅助按键配置 Url参数 + 仅WIFI + 仅在wifi下加载网络封面 + 封面规则 + 进入详情页时使用封面规则重新获取封面 diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index d279e4f0c..26718d53e 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -959,5 +959,9 @@ 缓存/导出 辅助按键配置 Url参数 + 仅WIFI + 仅在wifi下加载网络封面 + 封面规则 + 进入详情页时使用封面规则重新获取封面 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a431b66e0..3c783ff95 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -960,5 +960,9 @@ 缓存/导出 辅助按键配置 Url参数 + 仅WIFI + 仅在wifi下加载网络封面 + 封面规则 + 进入详情页时使用封面规则重新获取封面 diff --git a/app/src/main/res/xml/pref_config_cover.xml b/app/src/main/res/xml/pref_config_cover.xml index b2afd21aa..ca692fb57 100644 --- a/app/src/main/res/xml/pref_config_cover.xml +++ b/app/src/main/res/xml/pref_config_cover.xml @@ -2,10 +2,16 @@ + +