mirror of
https://github.com/gedoor/legado.git
synced 2024-07-06 23:47:49 +08:00
优化
This commit is contained in:
parent
9de9ac4cf8
commit
aa0cc6ec91
@ -18,21 +18,20 @@ class CronetInterceptor(private val cookieJar: CookieJar) : Interceptor {
|
||||
}
|
||||
val original: Request = chain.request()
|
||||
//Cronet未初始化
|
||||
return if (!CronetLoader.install() || cronetEngine == null) {
|
||||
chain.proceed(original)
|
||||
} else try {
|
||||
if (!CronetLoader.install() || cronetEngine == null) {
|
||||
return chain.proceed(original)
|
||||
}
|
||||
val cronetException: Exception
|
||||
try {
|
||||
val builder: Request.Builder = original.newBuilder()
|
||||
//移除Keep-Alive,手动设置会导致400 BadRequest
|
||||
builder.removeHeader("Keep-Alive")
|
||||
builder.removeHeader("Accept-Encoding")
|
||||
|
||||
val newReq = builder.build()
|
||||
proceedWithCronet(newReq, chain.call(), chain.readTimeoutMillis())/*?.let { response ->
|
||||
//从Response 中保存Cookie到CookieJar
|
||||
//cookieJar.receiveHeaders(newReq.url, response.headers)
|
||||
response
|
||||
}*/ ?: chain.proceed(original)
|
||||
return proceedWithCronet(newReq, chain.call(), chain.readTimeoutMillis())!!
|
||||
} catch (e: Exception) {
|
||||
cronetException = e
|
||||
//不能抛出错误,抛出错误会导致应用崩溃
|
||||
//遇到Cronet处理有问题时的情况,如证书过期等等,回退到okhttp处理
|
||||
if (!e.message.toString().contains("ERR_CERT_", true)
|
||||
@ -40,7 +39,12 @@ class CronetInterceptor(private val cookieJar: CookieJar) : Interceptor {
|
||||
) {
|
||||
e.printOnDebug()
|
||||
}
|
||||
chain.proceed(original)
|
||||
}
|
||||
try {
|
||||
return chain.proceed(original)
|
||||
} catch (e: Exception) {
|
||||
e.addSuppressed(cronetException)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
@ -51,8 +55,8 @@ class CronetInterceptor(private val cookieJar: CookieJar) : Interceptor {
|
||||
} else {
|
||||
OldCallback(request, call, readTimeoutMillis)
|
||||
}
|
||||
buildRequest(request, callBack)?.runCatching {
|
||||
return callBack.waitForDone(this)
|
||||
buildRequest(request, callBack)?.let {
|
||||
return callBack.waitForDone(it)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
@ -38,8 +38,8 @@ import kotlin.math.min
|
||||
|
||||
class MainViewModel(application: Application) : BaseViewModel(application) {
|
||||
private var threadCount = AppConfig.threadCount
|
||||
private var upTocPool =
|
||||
Executors.newFixedThreadPool(min(threadCount, AppConst.MAX_THREAD)).asCoroutineDispatcher()
|
||||
private var poolSize = min(threadCount, AppConst.MAX_THREAD)
|
||||
private var upTocPool = Executors.newFixedThreadPool(poolSize).asCoroutineDispatcher()
|
||||
private val waitUpTocBooks = LinkedList<String>()
|
||||
private val onUpTocBooks = ConcurrentHashMap.newKeySet<String>()
|
||||
val onUpBooksLiveData = MutableLiveData<Int>()
|
||||
@ -53,9 +53,16 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
|
||||
|
||||
fun upPool() {
|
||||
threadCount = AppConfig.threadCount
|
||||
if (upTocJob?.isActive == true || cacheBookJob?.isActive == true) {
|
||||
return
|
||||
}
|
||||
val newPoolSize = min(threadCount, AppConst.MAX_THREAD)
|
||||
if (poolSize == newPoolSize) {
|
||||
return
|
||||
}
|
||||
poolSize = newPoolSize
|
||||
upTocPool.close()
|
||||
upTocPool = Executors
|
||||
.newFixedThreadPool(min(threadCount, AppConst.MAX_THREAD)).asCoroutineDispatcher()
|
||||
upTocPool = Executors.newFixedThreadPool(poolSize).asCoroutineDispatcher()
|
||||
}
|
||||
|
||||
fun isUpdate(bookUrl: String): Boolean {
|
||||
@ -91,6 +98,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
|
||||
}
|
||||
|
||||
private fun startUpTocJob() {
|
||||
upPool()
|
||||
postUpBooksLiveData()
|
||||
upTocJob = viewModelScope.launch(upTocPool) {
|
||||
while (isActive) {
|
||||
|
Loading…
Reference in New Issue
Block a user