From 1a9a5770fe5857ab8be3531f25a4e94fe856ae11 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 22 Jan 2023 21:58:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 2 +- .../io/legado/app/lib}/cronet/AbsCallBack.kt | 2 +- .../app/lib}/cronet/BodyUploadProvider.kt | 2 +- .../lib}/cronet/CronetCoroutineInterceptor.kt | 7 +++--- .../io/legado/app/lib}/cronet/CronetHelper.kt | 2 +- .../app/lib}/cronet/CronetInterceptor.kt | 6 +++-- .../io/legado/app/lib}/cronet/CronetLoader.kt | 11 +++++----- .../io/legado/app/lib}/cronet/NewCallBack.kt | 2 +- .../io/legado/app/lib}/cronet/OldCallback.kt | 2 +- app/src/main/java/io/legado/app/App.kt | 4 ++-- .../io/legado/app/help/http/HttpHelper.kt | 11 ++++++---- .../io/legado/app/help/http/cronet/Cronet.kt | 22 +++++++++++++++++++ .../help/http/cronet/CronetLoaderInterface.kt | 9 ++++++++ 13 files changed, 60 insertions(+), 22 deletions(-) rename app/src/{main/java/io/legado/app/help/http => app/java/io/legado/app/lib}/cronet/AbsCallBack.kt (99%) rename app/src/{main/java/io/legado/app/help/http => app/java/io/legado/app/lib}/cronet/BodyUploadProvider.kt (97%) rename app/src/{main/java/io/legado/app/help/http => app/java/io/legado/app/lib}/cronet/CronetCoroutineInterceptor.kt (96%) rename app/src/{main/java/io/legado/app/help/http => app/java/io/legado/app/lib}/cronet/CronetHelper.kt (98%) rename app/src/{main/java/io/legado/app/help/http => app/java/io/legado/app/lib}/cronet/CronetInterceptor.kt (95%) rename app/src/{main/java/io/legado/app/help/http => app/java/io/legado/app/lib}/cronet/CronetLoader.kt (97%) rename app/src/{main/java/io/legado/app/help/http => app/java/io/legado/app/lib}/cronet/NewCallBack.kt (96%) rename app/src/{main/java/io/legado/app/help/http => app/java/io/legado/app/lib}/cronet/OldCallback.kt (97%) create mode 100644 app/src/main/java/io/legado/app/help/http/cronet/Cronet.kt create mode 100644 app/src/main/java/io/legado/app/help/http/cronet/CronetLoaderInterface.kt diff --git a/app/build.gradle b/app/build.gradle index 2d609fa43..fdf651653 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -200,7 +200,7 @@ dependencies { //网络 implementation('com.squareup.okhttp3:okhttp:4.10.0') - implementation(fileTree(dir: 'cronetlib', include: ['*.jar', '*.aar'])) + appImplementation(fileTree(dir: 'cronetlib', include: ['*.jar', '*.aar'])) //Glide def glideVersion = "4.14.2" diff --git a/app/src/main/java/io/legado/app/help/http/cronet/AbsCallBack.kt b/app/src/app/java/io/legado/app/lib/cronet/AbsCallBack.kt similarity index 99% rename from app/src/main/java/io/legado/app/help/http/cronet/AbsCallBack.kt rename to app/src/app/java/io/legado/app/lib/cronet/AbsCallBack.kt index 7d7ba8cb7..073970dfe 100644 --- a/app/src/main/java/io/legado/app/help/http/cronet/AbsCallBack.kt +++ b/app/src/app/java/io/legado/app/lib/cronet/AbsCallBack.kt @@ -1,4 +1,4 @@ -package io.legado.app.help.http.cronet +package io.legado.app.lib.cronet import io.legado.app.help.http.okHttpClient import io.legado.app.utils.DebugLog diff --git a/app/src/main/java/io/legado/app/help/http/cronet/BodyUploadProvider.kt b/app/src/app/java/io/legado/app/lib/cronet/BodyUploadProvider.kt similarity index 97% rename from app/src/main/java/io/legado/app/help/http/cronet/BodyUploadProvider.kt rename to app/src/app/java/io/legado/app/lib/cronet/BodyUploadProvider.kt index 00b2a5145..c8daf38d3 100644 --- a/app/src/main/java/io/legado/app/help/http/cronet/BodyUploadProvider.kt +++ b/app/src/app/java/io/legado/app/lib/cronet/BodyUploadProvider.kt @@ -1,4 +1,4 @@ -package io.legado.app.help.http.cronet +package io.legado.app.lib.cronet import okhttp3.RequestBody import okio.Buffer diff --git a/app/src/main/java/io/legado/app/help/http/cronet/CronetCoroutineInterceptor.kt b/app/src/app/java/io/legado/app/lib/cronet/CronetCoroutineInterceptor.kt similarity index 96% rename from app/src/main/java/io/legado/app/help/http/cronet/CronetCoroutineInterceptor.kt rename to app/src/app/java/io/legado/app/lib/cronet/CronetCoroutineInterceptor.kt index 542f8f504..8701f4c11 100644 --- a/app/src/main/java/io/legado/app/help/http/cronet/CronetCoroutineInterceptor.kt +++ b/app/src/app/java/io/legado/app/lib/cronet/CronetCoroutineInterceptor.kt @@ -1,5 +1,6 @@ -package io.legado.app.help.http.cronet +package io.legado.app.lib.cronet +import io.legado.app.help.http.cookieJar import io.legado.app.utils.printOnDebug import kotlinx.coroutines.runBlocking import kotlinx.coroutines.suspendCancellableCoroutine @@ -12,8 +13,8 @@ import java.io.IOException import kotlin.coroutines.resume import kotlin.coroutines.resumeWithException -class CronetCoroutineInterceptor(private val cookieJar: CookieJar = CookieJar.NO_COOKIES) : - Interceptor { +@Suppress("unused") +class CronetCoroutineInterceptor : Interceptor { override fun intercept(chain: Interceptor.Chain): Response { if (chain.call().isCanceled()) { throw IOException("Canceled") diff --git a/app/src/main/java/io/legado/app/help/http/cronet/CronetHelper.kt b/app/src/app/java/io/legado/app/lib/cronet/CronetHelper.kt similarity index 98% rename from app/src/main/java/io/legado/app/help/http/cronet/CronetHelper.kt rename to app/src/app/java/io/legado/app/lib/cronet/CronetHelper.kt index 37c34fc8d..7493a17de 100644 --- a/app/src/main/java/io/legado/app/help/http/cronet/CronetHelper.kt +++ b/app/src/app/java/io/legado/app/lib/cronet/CronetHelper.kt @@ -1,4 +1,4 @@ -package io.legado.app.help.http.cronet +package io.legado.app.lib.cronet import io.legado.app.constant.AppLog import io.legado.app.help.config.AppConfig diff --git a/app/src/main/java/io/legado/app/help/http/cronet/CronetInterceptor.kt b/app/src/app/java/io/legado/app/lib/cronet/CronetInterceptor.kt similarity index 95% rename from app/src/main/java/io/legado/app/help/http/cronet/CronetInterceptor.kt rename to app/src/app/java/io/legado/app/lib/cronet/CronetInterceptor.kt index daa774dfa..1d3c7076e 100644 --- a/app/src/main/java/io/legado/app/help/http/cronet/CronetInterceptor.kt +++ b/app/src/app/java/io/legado/app/lib/cronet/CronetInterceptor.kt @@ -1,13 +1,15 @@ -package io.legado.app.help.http.cronet +package io.legado.app.lib.cronet import android.os.Build +import io.legado.app.help.http.cookieJar import io.legado.app.utils.printOnDebug import okhttp3.* import okhttp3.internal.http.receiveHeaders import java.io.IOException -class CronetInterceptor(private val cookieJar: CookieJar = CookieJar.NO_COOKIES) : Interceptor { +@Suppress("unused") +class CronetInterceptor : Interceptor { @Throws(IOException::class) override fun intercept(chain: Interceptor.Chain): Response { if (chain.call().isCanceled()) { diff --git a/app/src/main/java/io/legado/app/help/http/cronet/CronetLoader.kt b/app/src/app/java/io/legado/app/lib/cronet/CronetLoader.kt similarity index 97% rename from app/src/main/java/io/legado/app/help/http/cronet/CronetLoader.kt rename to app/src/app/java/io/legado/app/lib/cronet/CronetLoader.kt index 34ad3e5f5..feebe84e1 100644 --- a/app/src/main/java/io/legado/app/help/http/cronet/CronetLoader.kt +++ b/app/src/app/java/io/legado/app/lib/cronet/CronetLoader.kt @@ -1,4 +1,4 @@ -package io.legado.app.help.http.cronet +package io.legado.app.lib.cronet import android.annotation.SuppressLint import android.content.Context @@ -8,6 +8,7 @@ import android.text.TextUtils import io.legado.app.BuildConfig import io.legado.app.help.config.AppConfig import io.legado.app.help.coroutine.Coroutine +import io.legado.app.help.http.cronet.CronetLoaderInterface import io.legado.app.utils.DebugLog import io.legado.app.utils.printOnDebug @@ -23,7 +24,7 @@ import java.security.MessageDigest import java.util.* -object CronetLoader : CronetEngine.Builder.LibraryLoader() { +object CronetLoader : CronetEngine.Builder.LibraryLoader(), CronetLoaderInterface { //https://storage.googleapis.com/chromium-cronet/android/92.0.4515.159/Release/cronet/libs/arm64-v8a/libcronet.92.0.4515.159.so private const val soVersion = BuildConfig.Cronet_Version @@ -55,7 +56,7 @@ object CronetLoader : CronetEngine.Builder.LibraryLoader() { /** * 判断Cronet是否安装完成 */ - fun install(): Boolean { + override fun install(): Boolean { synchronized(this) { if (cacheInstall) { return true @@ -77,7 +78,7 @@ object CronetLoader : CronetEngine.Builder.LibraryLoader() { /** * 预加载Cronet */ - fun preDownload() { + override fun preDownload() { if (AppConfig.isGooglePlay) { return } @@ -288,7 +289,7 @@ object CronetLoader : CronetEngine.Builder.LibraryLoader() { cacheInstall = false val parentFile = downloadTempFile.parentFile @Suppress("SameParameterValue") - deleteHistoryFile(parentFile!!, null) + (deleteHistoryFile(parentFile!!, null)) } // executor.execute { // diff --git a/app/src/main/java/io/legado/app/help/http/cronet/NewCallBack.kt b/app/src/app/java/io/legado/app/lib/cronet/NewCallBack.kt similarity index 96% rename from app/src/main/java/io/legado/app/help/http/cronet/NewCallBack.kt rename to app/src/app/java/io/legado/app/lib/cronet/NewCallBack.kt index 7960f54f8..67c518945 100644 --- a/app/src/main/java/io/legado/app/help/http/cronet/NewCallBack.kt +++ b/app/src/app/java/io/legado/app/lib/cronet/NewCallBack.kt @@ -1,4 +1,4 @@ -package io.legado.app.help.http.cronet +package io.legado.app.lib.cronet import android.os.Build diff --git a/app/src/main/java/io/legado/app/help/http/cronet/OldCallback.kt b/app/src/app/java/io/legado/app/lib/cronet/OldCallback.kt similarity index 97% rename from app/src/main/java/io/legado/app/help/http/cronet/OldCallback.kt rename to app/src/app/java/io/legado/app/lib/cronet/OldCallback.kt index 3107187df..c39888403 100644 --- a/app/src/main/java/io/legado/app/help/http/cronet/OldCallback.kt +++ b/app/src/app/java/io/legado/app/lib/cronet/OldCallback.kt @@ -1,4 +1,4 @@ -package io.legado.app.help.http.cronet +package io.legado.app.lib.cronet import android.os.ConditionVariable import okhttp3.Call diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index 8ed281747..8c82a99e3 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -24,7 +24,7 @@ import io.legado.app.help.book.BookHelp import io.legado.app.help.config.AppConfig import io.legado.app.help.config.ThemeConfig.applyDayNight import io.legado.app.help.coroutine.Coroutine -import io.legado.app.help.http.cronet.CronetLoader +import io.legado.app.help.http.cronet.Cronet import io.legado.app.model.BookCover import io.legado.app.utils.defaultSharedPreferences import io.legado.app.utils.getPrefBoolean @@ -42,7 +42,7 @@ class App : MultiDexApplication() { oldConfig = Configuration(resources.configuration) CrashHandler(this) //预下载Cronet so - CronetLoader.preDownload() + Cronet.preDownload() createNotificationChannels() applyDayNight(this) LiveEventBus.config() 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 3f571c789..7d5907c92 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 @@ -3,8 +3,7 @@ package io.legado.app.help.http import io.legado.app.constant.AppConst import io.legado.app.help.CacheManager import io.legado.app.help.config.AppConfig -import io.legado.app.help.http.cronet.CronetInterceptor -import io.legado.app.help.http.cronet.CronetLoader +import io.legado.app.help.http.cronet.Cronet import io.legado.app.utils.NetworkUtils import okhttp3.* import java.net.InetSocketAddress @@ -70,8 +69,12 @@ val okHttpClient: OkHttpClient by lazy { builder.addHeader("Cache-Control", "no-cache") chain.proceed(builder.build()) }) - if (!AppConfig.isGooglePlay && AppConfig.isCronet && CronetLoader.install()) { - builder.addInterceptor(CronetInterceptor(cookieJar = cookieJar)) + if (!AppConfig.isGooglePlay && AppConfig.isCronet) { + if (Cronet.loader?.install() == true) { + Cronet.interceptor?.let { + builder.addInterceptor(it) + } + } } builder.build() } diff --git a/app/src/main/java/io/legado/app/help/http/cronet/Cronet.kt b/app/src/main/java/io/legado/app/help/http/cronet/Cronet.kt new file mode 100644 index 000000000..d18abe083 --- /dev/null +++ b/app/src/main/java/io/legado/app/help/http/cronet/Cronet.kt @@ -0,0 +1,22 @@ +package io.legado.app.help.http.cronet + +import okhttp3.Interceptor + +object Cronet { + + val loader: CronetLoaderInterface? by lazy { + val cl = Class.forName("io.legado.app.lib.cronet.CronetLoader") + ?.kotlin?.objectInstance + cl as? CronetLoaderInterface + } + + fun preDownload() { + loader?.preDownload() + } + + val interceptor: Interceptor? by lazy { + val cl = Class.forName("io.legado.app.lib.cronet.CronetInterceptor")?.newInstance() + cl as? Interceptor + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/http/cronet/CronetLoaderInterface.kt b/app/src/main/java/io/legado/app/help/http/cronet/CronetLoaderInterface.kt new file mode 100644 index 000000000..2b4c35ba1 --- /dev/null +++ b/app/src/main/java/io/legado/app/help/http/cronet/CronetLoaderInterface.kt @@ -0,0 +1,9 @@ +package io.legado.app.help.http.cronet + +interface CronetLoaderInterface { + + fun install(): Boolean + + fun preDownload() + +} \ No newline at end of file