Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Jan 22, 2024
1 parent 9de9ac4 commit aa0cc6e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
26 changes: 15 additions & 11 deletions app/src/main/java/io/legado/app/lib/cronet/CronetInterceptor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,33 @@ 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)
&& !e.message.toString().contains("ERR_SSL_", true)
) {
e.printOnDebug()
}
chain.proceed(original)
}
try {
return chain.proceed(original)
} catch (e: Exception) {
e.addSuppressed(cronetException)
throw e
}
}

Expand All @@ -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
}
Expand Down
16 changes: 12 additions & 4 deletions app/src/main/java/io/legado/app/ui/main/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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>()
Expand All @@ -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 {
Expand Down Expand Up @@ -91,6 +98,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
}

private fun startUpTocJob() {
upPool()
postUpBooksLiveData()
upTocJob = viewModelScope.launch(upTocPool) {
while (isActive) {
Expand Down

0 comments on commit aa0cc6e

Please sign in to comment.