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