添加仅在wifi下加载封面

pull/1738/head
kunfei 3 years ago
parent 71493cd913
commit 0194f33fd9
  1. 1
      app/src/main/java/io/legado/app/constant/PreferKey.kt
  2. 5
      app/src/main/java/io/legado/app/help/glide/OkHttpModelLoader.kt
  3. 13
      app/src/main/java/io/legado/app/help/glide/OkHttpStreamFetcher.kt
  4. 6
      app/src/main/java/io/legado/app/ui/widget/image/CoverImageView.kt
  5. 4
      app/src/main/res/values-es-rES/strings.xml
  6. 4
      app/src/main/res/values-ja-rJP/strings.xml
  7. 4
      app/src/main/res/values-pt-rBR/strings.xml
  8. 4
      app/src/main/res/values-zh-rHK/strings.xml
  9. 4
      app/src/main/res/values-zh-rTW/strings.xml
  10. 4
      app/src/main/res/values-zh/strings.xml
  11. 4
      app/src/main/res/values/strings.xml
  12. 10
      app/src/main/res/xml/pref_config_cover.xml

@ -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"

@ -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<GlideUrl?, InputStream?> {
val loadOnlyWifiOption = Option.memory("loadOnlyWifi", false)
override fun buildLoadData(
model: GlideUrl,
width: Int,
height: Int,
options: Options
): ModelLoader.LoadData<InputStream?> {
return ModelLoader.LoadData(model, OkHttpStreamFetcher(model))
return ModelLoader.LoadData(model, OkHttpStreamFetcher(model, options))
}
override fun handles(model: GlideUrl): Boolean {

@ -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<InputStream>, okhttp3.Callback {
private var stream: InputStream? = null
private var responseBody: ResponseBody? = null
private var callback: DataFetcher.DataCallback<in InputStream>? = null
@Volatile
private var call: Call? = null
override fun loadData(priority: Priority, callback: DataFetcher.DataCallback<in InputStream>) {
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)

@ -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)

@ -957,5 +957,9 @@
<string name="cache_export">缓存/导出</string>
<string name="assists_key_config">辅助按键配置</string>
<string name="url_option">Url参数</string>
<string name="only_wifi">仅WIFI</string>
<string name="only_wifi_summary">仅在wifi下加载网络封面</string>
<string name="cover_rule">封面规则</string>
<string name="cover_rule_summary">进入详情页时使用封面规则重新获取封面</string>
<!-- string end -->
</resources>

@ -960,5 +960,9 @@
<string name="cache_export">缓存/导出</string>
<string name="assists_key_config">辅助按键配置</string>
<string name="url_option">Url参数</string>
<string name="only_wifi">仅WIFI</string>
<string name="only_wifi_summary">仅在wifi下加载网络封面</string>
<string name="cover_rule">封面规则</string>
<string name="cover_rule_summary">进入详情页时使用封面规则重新获取封面</string>
<!-- string end -->
</resources>

@ -960,5 +960,9 @@
<string name="cache_export">缓存/导出</string>
<string name="assists_key_config">辅助按键配置</string>
<string name="url_option">Url参数</string>
<string name="only_wifi">仅WIFI</string>
<string name="only_wifi_summary">仅在wifi下加载网络封面</string>
<string name="cover_rule">封面规则</string>
<string name="cover_rule_summary">进入详情页时使用封面规则重新获取封面</string>
<!-- string end -->
</resources>

@ -957,5 +957,9 @@
<string name="cache_export">缓存/导出</string>
<string name="assists_key_config">辅助按键配置</string>
<string name="url_option">Url参数</string>
<string name="only_wifi">仅WIFI</string>
<string name="only_wifi_summary">仅在wifi下加载网络封面</string>
<string name="cover_rule">封面规则</string>
<string name="cover_rule_summary">进入详情页时使用封面规则重新获取封面</string>
<!-- string end -->
</resources>

@ -959,5 +959,9 @@
<string name="cache_export">缓存/导出</string>
<string name="assists_key_config">辅助按键配置</string>
<string name="url_option">Url参数</string>
<string name="only_wifi">仅WIFI</string>
<string name="only_wifi_summary">仅在wifi下加载网络封面</string>
<string name="cover_rule">封面规则</string>
<string name="cover_rule_summary">进入详情页时使用封面规则重新获取封面</string>
<!-- string end -->
</resources>

@ -959,5 +959,9 @@
<string name="cache_export">缓存/导出</string>
<string name="assists_key_config">辅助按键配置</string>
<string name="url_option">Url参数</string>
<string name="only_wifi">仅WIFI</string>
<string name="only_wifi_summary">仅在wifi下加载网络封面</string>
<string name="cover_rule">封面规则</string>
<string name="cover_rule_summary">进入详情页时使用封面规则重新获取封面</string>
<!-- string end -->
</resources>

@ -960,5 +960,9 @@
<string name="cache_export">缓存/导出</string>
<string name="assists_key_config">辅助按键配置</string>
<string name="url_option">Url参数</string>
<string name="only_wifi">仅WIFI</string>
<string name="only_wifi_summary">仅在wifi下加载网络封面</string>
<string name="cover_rule">封面规则</string>
<string name="cover_rule_summary">进入详情页时使用封面规则重新获取封面</string>
<!-- string end -->
</resources>

@ -2,10 +2,16 @@
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<io.legado.app.ui.widget.prefs.SwitchPreference
android:key="loadCoverOnlyWifi"
android:defaultValue="false"
android:title="@string/only_wifi"
android:summary="@string/only_wifi_summary" />
<io.legado.app.ui.widget.prefs.Preference
android:key="coverRule"
android:title="封面规则"
android:summary="进入详情页时使用封面规则重新获取封面"
android:title="@string/cover_rule"
android:summary="@string/cover_rule_summary"
app:allowDividerAbove="false"
app:allowDividerBelow="false"
app:iconSpaceReserved="false" />

Loading…
Cancel
Save