From ef9391b13748a3f66d38d3e125e1a38f89602f9f Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Fri, 6 May 2022 23:05:53 +0800 Subject: [PATCH 1/9] =?UTF-8?q?fix:=E4=B8=8D=E4=B8=8B=E8=BD=BD=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E7=A0=81=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/ui/association/VerificationCodeDialog.kt | 7 ++++--- app/src/main/res/layout/dialog_verification_code_view.xml | 7 +++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/association/VerificationCodeDialog.kt b/app/src/main/java/io/legado/app/ui/association/VerificationCodeDialog.kt index 85a16fdfd..9f531ff32 100644 --- a/app/src/main/java/io/legado/app/ui/association/VerificationCodeDialog.kt +++ b/app/src/main/java/io/legado/app/ui/association/VerificationCodeDialog.kt @@ -7,6 +7,7 @@ import android.view.ViewGroup import android.view.MenuItem import androidx.appcompat.widget.Toolbar import com.bumptech.glide.request.RequestOptions +import com.bumptech.glide.load.engine.DiskCacheStrategy import io.legado.app.R import io.legado.app.base.BaseDialogFragment import io.legado.app.databinding.DialogVerificationCodeViewBinding @@ -46,7 +47,6 @@ class VerificationCodeDialog() : BaseDialogFragment(R.layout.dialog_verification binding.run { toolBar.setBackgroundColor(primaryColor) val sourceOrigin = arguments?.getString("sourceOrigin") - val key = "${sourceOrigin}_verificationResult" arguments?.getString("imageUrl")?.let { imageUrl -> ImageLoader.load(requireContext(), imageUrl).apply { sourceOrigin?.let { @@ -58,8 +58,9 @@ class VerificationCodeDialog() : BaseDialogFragment(R.layout.dialog_verification ) } }.error(R.drawable.image_loading_error) - .into(ivImage) - ivImage.setOnClickListener { + .diskCacheStrategy(DiskCacheStrategy.NONE) + .into(verificationCodeImageView) + verificationCodeImageView.setOnClickListener { showDialogFragment(PhotoDialog(imageUrl, sourceOrigin)) } } diff --git a/app/src/main/res/layout/dialog_verification_code_view.xml b/app/src/main/res/layout/dialog_verification_code_view.xml index 707e28051..f3ca7c528 100644 --- a/app/src/main/res/layout/dialog_verification_code_view.xml +++ b/app/src/main/res/layout/dialog_verification_code_view.xml @@ -18,11 +18,10 @@ app:titleTextAppearance="@style/ToolbarTitle" /> From ffba859da75b996f9d33738fcbf98d2a1a012cec Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Sat, 7 May 2022 06:26:15 +0800 Subject: [PATCH 2/9] =?UTF-8?q?fix:=E4=B8=8D=E7=BC=93=E5=AD=98=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E7=A0=81bitmap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/ui/association/VerificationCodeDialog.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/io/legado/app/ui/association/VerificationCodeDialog.kt b/app/src/main/java/io/legado/app/ui/association/VerificationCodeDialog.kt index 9f531ff32..57460eb09 100644 --- a/app/src/main/java/io/legado/app/ui/association/VerificationCodeDialog.kt +++ b/app/src/main/java/io/legado/app/ui/association/VerificationCodeDialog.kt @@ -59,6 +59,7 @@ class VerificationCodeDialog() : BaseDialogFragment(R.layout.dialog_verification } }.error(R.drawable.image_loading_error) .diskCacheStrategy(DiskCacheStrategy.NONE) + .skipMemoryCache(true) .into(verificationCodeImageView) verificationCodeImageView.setOnClickListener { showDialogFragment(PhotoDialog(imageUrl, sourceOrigin)) From 872fa8577d742ff1eaac7583056e5cfb46fb89c8 Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Sat, 7 May 2022 06:27:00 +0800 Subject: [PATCH 3/9] =?UTF-8?q?fix:glide=E9=87=8D=E5=A4=8D=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0ua?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/help/glide/OkHttpStreamFetcher.kt | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) 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 a101d4413..d76bc6f33 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,9 +8,11 @@ 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.constant.AppConst import io.legado.app.data.appDb import io.legado.app.exception.NoStackTraceException import io.legado.app.help.http.okHttpClient +import io.legado.app.help.http.addHeaders import io.legado.app.utils.isWifiConnect import okhttp3.Call import okhttp3.Request @@ -37,16 +39,19 @@ class OkHttpStreamFetcher(private val url: GlideUrl, private val options: Option return } val requestBuilder: Request.Builder = Request.Builder().url(url.toStringUrl()) + val headerMap = HashMap() + 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) + source?.getHeaderMap(true)?.let { + headerMap.putAll(it) } } for ((key, value) in url.headers.entries) { - requestBuilder.addHeader(key, value) + headerMap.put(key, value) } + requestBuilder.addHeaders(headerMap) val request: Request = requestBuilder.build() this.callback = callback call = okHttpClient.newCall(request) From 4618b1328956eeb5b052be1683a0edd32b058dc8 Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Sat, 7 May 2022 07:05:17 +0800 Subject: [PATCH 4/9] =?UTF-8?q?fix:okhttp=E8=87=AA=E5=8A=A8=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E8=BF=94=E5=9B=9E=E7=9A=84cookie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/help/http/HttpHelper.kt | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt index 07a7dd7a6..a1db99199 100644 --- a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt +++ b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt @@ -6,6 +6,9 @@ import io.legado.app.help.http.cronet.CronetInterceptor import io.legado.app.help.http.cronet.CronetLoader import okhttp3.ConnectionSpec import okhttp3.Credentials +import okhttp3.CookieJar +import okhttp3.Cookie +import okhttp3.HttpUrl import okhttp3.Interceptor import okhttp3.OkHttpClient import java.net.InetSocketAddress @@ -29,6 +32,16 @@ val okHttpClient: OkHttpClient by lazy { .writeTimeout(15, TimeUnit.SECONDS) .readTimeout(15, TimeUnit.SECONDS) .callTimeout(60, TimeUnit.SECONDS) + .cookieJar(object : CookieJar { + override fun saveFromResponse(url: HttpUrl, cookies: List) { + cookies.forEach { + CookieStore.setCookie(url.toString(), it.value) + } + } + override fun loadForRequest(url: HttpUrl): List { + return ArrayList() + } + }) .sslSocketFactory(SSLHelper.unsafeSSLSocketFactory, SSLHelper.unsafeTrustManager) .retryOnConnectionFailure(true) .hostnameVerifier(SSLHelper.unsafeHostnameVerifier) From da0e451233106536c7031603c6efad9684280110 Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Sat, 7 May 2022 08:35:19 +0800 Subject: [PATCH 5/9] =?UTF-8?q?fix:okhttp=E8=87=AA=E5=8A=A8=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E8=BF=94=E5=9B=9E=E7=9A=84cookie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/help/CacheManager.kt | 28 ++++++------------- .../io/legado/app/help/http/CookieStore.kt | 7 ++++- .../io/legado/app/help/http/HttpHelper.kt | 2 +- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/CacheManager.kt b/app/src/main/java/io/legado/app/help/CacheManager.kt index e9e597648..f628cb599 100644 --- a/app/src/main/java/io/legado/app/help/CacheManager.kt +++ b/app/src/main/java/io/legado/app/help/CacheManager.kt @@ -11,11 +11,7 @@ import splitties.init.appCtx object CacheManager { private val queryTTFMap = hashMapOf>() - private val memoryLruCache = object : LruCache(100) { - override fun sizeOf(key: String, value: Cache): Int { - return 1 - } - } + private val memoryLruCache = object : LruCache(100) /** * saveTime 单位为秒 @@ -29,15 +25,14 @@ object CacheManager { is ByteArray -> ACache.get(appCtx).put(key, value, saveTime) else -> { val cache = Cache(key, value.toString(), deadline) - memoryLruCache.put(key, cache) + putMemory(key, value.toString()) appDb.cacheDao.insert(cache) } } } - fun putMemory(key: String, value: Any) { - val cache = Cache(key, value.toString(), 0) - memoryLruCache.put(key, cache) + fun putMemory(key: String, value: String) { + memoryLruCache.put(key, value) } fun get(key: String): String? { @@ -46,22 +41,15 @@ object CacheManager { } val cache = appDb.cacheDao.get(key) if (cache != null && (cache.deadline == 0L || cache.deadline > System.currentTimeMillis())) { - memoryLruCache.put(key, cache) + putMemory(key, cache.value ?: "") return cache.value } return null } - //从内存中获取数据 使用lruCache 支持过期功能 - private fun getFromMemory(key: String): String? { - val cache = memoryLruCache.get(key) ?: return null - val deadline = cache.deadline - return if (deadline == 0L || deadline > System.currentTimeMillis()) { - cache.value - } else { - memoryLruCache.remove(key) - null - } + //从内存中获取数据 使用lruCache + fun getFromMemory(key: String): String? { + return memoryLruCache.get(key) } fun getInt(key: String): Int? { diff --git a/app/src/main/java/io/legado/app/help/http/CookieStore.kt b/app/src/main/java/io/legado/app/help/http/CookieStore.kt index 0da6c0d97..b0d892b36 100644 --- a/app/src/main/java/io/legado/app/help/http/CookieStore.kt +++ b/app/src/main/java/io/legado/app/help/http/CookieStore.kt @@ -6,6 +6,7 @@ import android.text.TextUtils import io.legado.app.data.appDb import io.legado.app.data.entities.Cookie import io.legado.app.help.http.api.CookieManager +import io.legado.app.help.CacheManager import io.legado.app.utils.NetworkUtils object CookieStore : CookieManager { @@ -14,6 +15,7 @@ object CookieStore : CookieManager { *保存cookie到数据库,会自动识别url的二级域名 */ override fun setCookie(url: String, cookie: String?) { + CacheManager.putMemory(url, cookie ?: "") val cookieBean = Cookie(NetworkUtils.getSubDomain(url), cookie ?: "") appDb.cookieDao.insert(cookieBean) } @@ -37,8 +39,11 @@ object CookieStore : CookieManager { *获取url所属的二级域名的cookie */ override fun getCookie(url: String): String { + CacheManager.getFromMemory(url)?.let { return it } val cookieBean = appDb.cookieDao.get(NetworkUtils.getSubDomain(url)) - return cookieBean?.cookie ?: "" + val cookie = cookieBean?.cookie ?: "" + CacheManager.putMemory(url, cookie ?: "") + return cookie } fun getKey(url: String, key: String): String { diff --git a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt index a1db99199..a19e2b03c 100644 --- a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt +++ b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt @@ -35,7 +35,7 @@ val okHttpClient: OkHttpClient by lazy { .cookieJar(object : CookieJar { override fun saveFromResponse(url: HttpUrl, cookies: List) { cookies.forEach { - CookieStore.setCookie(url.toString(), it.value) + CookieStore.replaceCookie(url.toString(), "${it.name}=${it.value}") } } override fun loadForRequest(url: HttpUrl): List { From 3017a6a398f75ee28a3e5993c9167c51acf46dde Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Sat, 7 May 2022 08:35:19 +0800 Subject: [PATCH 6/9] =?UTF-8?q?fix:okhttp=E8=87=AA=E5=8A=A8=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E8=BF=94=E5=9B=9E=E7=9A=84cookie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/help/CacheManager.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/help/CacheManager.kt b/app/src/main/java/io/legado/app/help/CacheManager.kt index 03b655676..b4bab56f5 100644 --- a/app/src/main/java/io/legado/app/help/CacheManager.kt +++ b/app/src/main/java/io/legado/app/help/CacheManager.kt @@ -11,7 +11,7 @@ import splitties.init.appCtx object CacheManager { private val queryTTFMap = hashMapOf>() - private val memoryLruCache = object : LruCache(100) + private val memoryLruCache = object : LruCache(100) {} /** * saveTime 单位为秒 From d79b5e8139991040af70921dc9feaeff4004d79c Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Sat, 7 May 2022 09:27:15 +0800 Subject: [PATCH 7/9] refactor:remove duplicated pollyfill --- .../main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index fd005ff70..0b4c5d6de 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -192,9 +192,6 @@ class AnalyzeUrl( } } } - headerMap[UA_NAME] ?: let { - headerMap[UA_NAME] = AppConfig.userAgent - } urlNoQuery = url when (method) { RequestMethod.GET -> { From 39f368817b79a4126572b1a555e0b78df2784a1b Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Sat, 7 May 2022 10:26:30 +0800 Subject: [PATCH 8/9] =?UTF-8?q?fix:=20=E7=89=B9=E5=AE=9A=E6=83=85=E5=86=B5?= =?UTF-8?q?=E4=B8=8B=E7=99=BB=E5=BD=95UI=E6=97=A0=E6=B3=95=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E7=99=BB=E5=BD=95=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 数据库中存在cookie,登录头也有cookie,后者会被前者覆盖 --- app/src/main/java/io/legado/app/data/entities/BaseSource.kt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/src/main/java/io/legado/app/data/entities/BaseSource.kt b/app/src/main/java/io/legado/app/data/entities/BaseSource.kt index 4bda7986c..45466f2a2 100644 --- a/app/src/main/java/io/legado/app/data/entities/BaseSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/BaseSource.kt @@ -95,11 +95,17 @@ interface BaseSource : JsExtensions { * 保存登录头部信息,map格式,访问时自动添加 */ fun putLoginHeader(header: String) { + val headerMap = GSON.fromJsonObject>(header).getOrNull() + val cookie = headerMap?.get("Cookie") ?: headerMap?.get("cookie") + cookie?.let { + CookieStore.replaceCookie(getKey(), it) + } CacheManager.put("loginHeader_${getKey()}", header) } fun removeLoginHeader() { CacheManager.delete("loginHeader_${getKey()}") + CookieStore.removeCookie(getKey()) } /** From 75ef8a17b8d810ac12d134ab91c9be68273e41c3 Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Sat, 7 May 2022 13:10:05 +0800 Subject: [PATCH 9/9] =?UTF-8?q?fix=EF=BC=9Acookie=E5=86=85=E5=AD=98?= =?UTF-8?q?=E7=BC=93=E5=AD=98bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/help/CacheManager.kt | 16 ++++++++++------ .../java/io/legado/app/help/http/CookieStore.kt | 14 +++++++++----- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/CacheManager.kt b/app/src/main/java/io/legado/app/help/CacheManager.kt index b4bab56f5..9cdac800d 100644 --- a/app/src/main/java/io/legado/app/help/CacheManager.kt +++ b/app/src/main/java/io/legado/app/help/CacheManager.kt @@ -35,6 +35,15 @@ object CacheManager { memoryLruCache.put(key, value) } + //从内存中获取数据 使用lruCache + fun getFromMemory(key: String): String? { + return memoryLruCache.get(key) + } + + fun deleteMemory(key: String) { + memoryLruCache.remove(key) + } + fun get(key: String): String? { getFromMemory(key)?.let { return it @@ -47,11 +56,6 @@ object CacheManager { return null } - //从内存中获取数据 使用lruCache - fun getFromMemory(key: String): String? { - return memoryLruCache.get(key) - } - fun getInt(key: String): Int? { return get(key)?.toIntOrNull() } @@ -92,7 +96,7 @@ object CacheManager { fun delete(key: String) { appDb.cacheDao.delete(key) - memoryLruCache.remove(key) + deleteMemory(key) ACache.get(appCtx).remove(key) } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/http/CookieStore.kt b/app/src/main/java/io/legado/app/help/http/CookieStore.kt index b0d892b36..ace5ac5ca 100644 --- a/app/src/main/java/io/legado/app/help/http/CookieStore.kt +++ b/app/src/main/java/io/legado/app/help/http/CookieStore.kt @@ -15,8 +15,9 @@ object CookieStore : CookieManager { *保存cookie到数据库,会自动识别url的二级域名 */ override fun setCookie(url: String, cookie: String?) { - CacheManager.putMemory(url, cookie ?: "") - val cookieBean = Cookie(NetworkUtils.getSubDomain(url), cookie ?: "") + val domain = NetworkUtils.getSubDomain(url) + CacheManager.putMemory("${domain}_cookie", cookie ?: "") + val cookieBean = Cookie(domain, cookie ?: "") appDb.cookieDao.insert(cookieBean) } @@ -39,8 +40,9 @@ object CookieStore : CookieManager { *获取url所属的二级域名的cookie */ override fun getCookie(url: String): String { - CacheManager.getFromMemory(url)?.let { return it } - val cookieBean = appDb.cookieDao.get(NetworkUtils.getSubDomain(url)) + val domain = NetworkUtils.getSubDomain(url) + CacheManager.getFromMemory("${domain}_cookie")?.let { return it } + val cookieBean = appDb.cookieDao.get(domain) val cookie = cookieBean?.cookie ?: "" CacheManager.putMemory(url, cookie ?: "") return cookie @@ -53,7 +55,9 @@ object CookieStore : CookieManager { } override fun removeCookie(url: String) { - appDb.cookieDao.delete(NetworkUtils.getSubDomain(url)) + val domain = NetworkUtils.getSubDomain(url) + CacheManager.deleteMemory("${domain}_cookie") + appDb.cookieDao.delete(domain) } override fun cookieToMap(cookie: String): MutableMap {