mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.legado.app.help.http.cronet
|
||||
package io.legado.app.lib.cronet
|
||||
|
||||
import okhttp3.RequestBody
|
||||
import okio.Buffer
|
||||
@@ -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")
|
||||
@@ -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
|
||||
@@ -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()) {
|
||||
@@ -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 {
|
||||
//
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.legado.app.help.http.cronet
|
||||
package io.legado.app.lib.cronet
|
||||
|
||||
|
||||
import android.os.Build
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.legado.app.help.http.cronet
|
||||
package io.legado.app.lib.cronet
|
||||
|
||||
import android.os.ConditionVariable
|
||||
import okhttp3.Call
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
22
app/src/main/java/io/legado/app/help/http/cronet/Cronet.kt
Normal file
22
app/src/main/java/io/legado/app/help/http/cronet/Cronet.kt
Normal file
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package io.legado.app.help.http.cronet
|
||||
|
||||
interface CronetLoaderInterface {
|
||||
|
||||
fun install(): Boolean
|
||||
|
||||
fun preDownload()
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user