From 12a69e1e5299022c40d5b5f64177bcf6583a457c Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 13 Apr 2022 18:48:00 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=9B=BE=E7=89=87=E7=9A=84?= =?UTF-8?q?=E6=BA=90=E6=94=BE=E5=88=B0=E8=AE=BF=E9=97=AE=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E6=97=B6,=E8=BF=99=E6=A0=B7=E5=A6=82=E6=9E=9C=E6=9C=89?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E5=B0=B1=E4=B8=8D=E4=BC=9A=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=BA=90,=E8=8E=B7=E5=8F=96=E6=BA=90?= =?UTF-8?q?=E6=98=AF=E8=80=97=E6=97=B6=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/help/glide/ImageLoader.kt | 15 ++------------- .../io/legado/app/help/glide/OkHttpModelLoader.kt | 1 + .../legado/app/help/glide/OkHttpStreamFetcher.kt | 8 ++++++++ .../java/io/legado/app/ui/main/rss/RssAdapter.kt | 7 ++++++- .../app/ui/rss/article/RssArticlesAdapter.kt | 6 +++++- .../app/ui/rss/article/RssArticlesAdapter1.kt | 8 +++++++- .../app/ui/rss/article/RssArticlesAdapter2.kt | 8 +++++++- .../app/ui/rss/favorites/RssFavoritesAdapter.kt | 7 ++++++- .../legado/app/ui/widget/image/CoverImageView.kt | 8 ++++++-- 9 files changed, 48 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/glide/ImageLoader.kt b/app/src/main/java/io/legado/app/help/glide/ImageLoader.kt index b60131995..625086c0a 100644 --- a/app/src/main/java/io/legado/app/help/glide/ImageLoader.kt +++ b/app/src/main/java/io/legado/app/help/glide/ImageLoader.kt @@ -7,7 +7,6 @@ import android.net.Uri import androidx.annotation.DrawableRes import com.bumptech.glide.RequestBuilder import io.legado.app.constant.AppPattern.dataUriRegex -import io.legado.app.data.appDb import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.utils.isAbsUrl import io.legado.app.utils.isContentScheme @@ -19,24 +18,14 @@ object ImageLoader { /** * 自动判断path类型 */ - fun load(context: Context, path: String?, sourceOrigin: String? = null): RequestBuilder { + fun load(context: Context, path: String?): RequestBuilder { return when { path.isNullOrEmpty() -> GlideApp.with(context).load(path) dataUriRegex.find(path) != null -> { //glide内部已经实现dataUri解析 GlideApp.with(context).load(path) } - path.isAbsUrl() -> { - kotlin.runCatching { - val source = sourceOrigin?.let { - appDb.bookSourceDao.getBookSource(it) ?: appDb.rssSourceDao.getByKey(it) - } - val url = AnalyzeUrl(path, source = source).getGlideUrl() - GlideApp.with(context).load(url) - }.getOrDefault( - GlideApp.with(context).load(path) - ) - } + path.isAbsUrl() -> GlideApp.with(context).load(path) path.isContentScheme() -> GlideApp.with(context).load(Uri.parse(path)) else -> kotlin.runCatching { val file = File(path) 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 90bec21b0..b8cd1ba9a 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 @@ -9,6 +9,7 @@ import java.io.InputStream object OkHttpModelLoader : ModelLoader { val loadOnlyWifiOption = Option.memory("loadOnlyWifi", false) + val sourceOriginOption = Option.memory("sourceOrigin") override fun buildLoadData( model: GlideUrl, 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 2cde4f8fc..a101d4413 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 @@ -8,6 +8,7 @@ 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.data.appDb import io.legado.app.exception.NoStackTraceException import io.legado.app.help.http.okHttpClient import io.legado.app.utils.isWifiConnect @@ -36,6 +37,13 @@ class OkHttpStreamFetcher(private val url: GlideUrl, private val options: Option return } val requestBuilder: Request.Builder = Request.Builder().url(url.toStringUrl()) + options.get(OkHttpModelLoader.sourceOriginOption)?.let { sourceUrl -> + val source = appDb.bookSourceDao.getBookSource(sourceUrl) + ?: appDb.rssSourceDao.getByKey(sourceUrl) + source?.getHeaderMap(true)?.forEach { + requestBuilder.addHeader(it.key, it.value) + } + } for ((key, value) in url.headers.entries) { requestBuilder.addHeader(key, value) } diff --git a/app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt b/app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt index 0fa56a4a4..2fb232c24 100644 --- a/app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt @@ -4,12 +4,14 @@ import android.content.Context import android.view.View import android.view.ViewGroup import androidx.appcompat.widget.PopupMenu +import com.bumptech.glide.request.RequestOptions import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.RecyclerAdapter import io.legado.app.data.entities.RssSource import io.legado.app.databinding.ItemRssBinding import io.legado.app.help.glide.ImageLoader +import io.legado.app.help.glide.OkHttpModelLoader import splitties.views.onLongClick class RssAdapter(context: Context, val callBack: CallBack) : @@ -27,7 +29,10 @@ class RssAdapter(context: Context, val callBack: CallBack) : ) { binding.apply { tvName.text = item.sourceName - ImageLoader.load(context, item.sourceIcon, item.sourceUrl) + val options = RequestOptions() + .set(OkHttpModelLoader.sourceOriginOption, item.sourceUrl) + ImageLoader.load(context, item.sourceIcon) + .apply(options) .centerCrop() .placeholder(R.drawable.image_rss) .error(R.drawable.image_rss) diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter.kt index 85c39d029..006da715c 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter.kt @@ -7,12 +7,14 @@ import android.view.ViewGroup 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.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.data.entities.RssArticle import io.legado.app.databinding.ItemRssArticleBinding import io.legado.app.help.glide.ImageLoader +import io.legado.app.help.glide.OkHttpModelLoader import io.legado.app.utils.getCompatColor import io.legado.app.utils.gone import io.legado.app.utils.visible @@ -38,7 +40,9 @@ class RssArticlesAdapter(context: Context, callBack: CallBack) : if (item.image.isNullOrBlank() && !callBack.isGridLayout) { imageView.gone() } else { - ImageLoader.load(context, item.image, item.origin).apply { + val options = + RequestOptions().set(OkHttpModelLoader.sourceOriginOption, item.origin) + ImageLoader.load(context, item.image).apply(options).apply { if (callBack.isGridLayout) { placeholder(R.drawable.image_rss_article) } else { diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter1.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter1.kt index 40a53984d..9e70877f8 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter1.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter1.kt @@ -1,17 +1,20 @@ package io.legado.app.ui.rss.article +import android.annotation.SuppressLint import android.content.Context import android.graphics.drawable.Drawable import android.view.ViewGroup 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.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.data.entities.RssArticle import io.legado.app.databinding.ItemRssArticle1Binding import io.legado.app.help.glide.ImageLoader +import io.legado.app.help.glide.OkHttpModelLoader import io.legado.app.utils.getCompatColor import io.legado.app.utils.gone import io.legado.app.utils.visible @@ -24,6 +27,7 @@ class RssArticlesAdapter1(context: Context, callBack: CallBack) : return ItemRssArticle1Binding.inflate(inflater, parent, false) } + @SuppressLint("CheckResult") override fun convert( holder: ItemViewHolder, binding: ItemRssArticle1Binding, @@ -36,7 +40,9 @@ class RssArticlesAdapter1(context: Context, callBack: CallBack) : if (item.image.isNullOrBlank() && !callBack.isGridLayout) { imageView.gone() } else { - ImageLoader.load(context, item.image, item.origin).apply { + val options = + RequestOptions().set(OkHttpModelLoader.sourceOriginOption, item.origin) + ImageLoader.load(context, item.image).apply(options).apply { if (callBack.isGridLayout) { placeholder(R.drawable.image_rss_article) } else { diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter2.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter2.kt index 99f892bda..353cfda91 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter2.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter2.kt @@ -1,17 +1,20 @@ package io.legado.app.ui.rss.article +import android.annotation.SuppressLint import android.content.Context import android.graphics.drawable.Drawable import android.view.ViewGroup 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.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.data.entities.RssArticle import io.legado.app.databinding.ItemRssArticle2Binding import io.legado.app.help.glide.ImageLoader +import io.legado.app.help.glide.OkHttpModelLoader import io.legado.app.utils.getCompatColor import io.legado.app.utils.gone import io.legado.app.utils.visible @@ -24,6 +27,7 @@ class RssArticlesAdapter2(context: Context, callBack: CallBack) : return ItemRssArticle2Binding.inflate(inflater, parent, false) } + @SuppressLint("CheckResult") override fun convert( holder: ItemViewHolder, binding: ItemRssArticle2Binding, @@ -36,7 +40,9 @@ class RssArticlesAdapter2(context: Context, callBack: CallBack) : if (item.image.isNullOrBlank() && !callBack.isGridLayout) { imageView.gone() } else { - ImageLoader.load(context, item.image, item.origin).apply { + val options = + RequestOptions().set(OkHttpModelLoader.sourceOriginOption, item.origin) + ImageLoader.load(context, item.image).apply(options).apply { if (callBack.isGridLayout) { placeholder(R.drawable.image_rss_article) } else { diff --git a/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesAdapter.kt b/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesAdapter.kt index 25cde23c0..5b816462f 100644 --- a/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesAdapter.kt @@ -6,12 +6,14 @@ import android.view.ViewGroup 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.base.adapter.ItemViewHolder import io.legado.app.base.adapter.RecyclerAdapter import io.legado.app.data.entities.RssStar import io.legado.app.databinding.ItemRssArticleBinding import io.legado.app.help.glide.ImageLoader +import io.legado.app.help.glide.OkHttpModelLoader import io.legado.app.utils.gone import io.legado.app.utils.visible @@ -35,7 +37,10 @@ class RssFavoritesAdapter(context: Context, val callBack: CallBack) : if (item.image.isNullOrBlank()) { imageView.gone() } else { - ImageLoader.load(context, item.image, item.origin) + val options = + RequestOptions().set(OkHttpModelLoader.sourceOriginOption, item.origin) + ImageLoader.load(context, item.image) + .apply(options) .addListener(object : RequestListener { override fun onLoadFailed( e: GlideException?, 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 995b1a120..78e18a8da 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 @@ -187,8 +187,12 @@ class CoverImageView @JvmOverloads constructor( .centerCrop() .into(this) } else { - ImageLoader.load(context, path, sourceOrigin)//Glide自动识别http://,content://和file:// - .apply(RequestOptions().set(OkHttpModelLoader.loadOnlyWifiOption, loadOnlyWifi)) + var options = RequestOptions().set(OkHttpModelLoader.loadOnlyWifiOption, loadOnlyWifi) + if (sourceOrigin != null) { + options = options.set(OkHttpModelLoader.sourceOriginOption, sourceOrigin) + } + ImageLoader.load(context, path)//Glide自动识别http://,content://和file:// + .apply(options) .placeholder(BookCover.defaultDrawable) .error(BookCover.defaultDrawable) .listener(glideListener)