Refine Cronet workaround
This commit is contained in:
1 parent
a5719a9c1d
commit
72fbcd4e0e
5 files changed
+23
-7
No files matched your search
@@ -196,7 +196,7 @@ class EhApplication :
|
||||
|
||||
companion object {
|
||||
val ktorClient by lazy {
|
||||
if (isAtLeastSExtension7 && Settings.enableCronet) {
|
||||
if (isAtLeastSExtension7 && Settings.enableCronet.value) {
|
||||
HttpClient(Cronet) {
|
||||
engine { configureClient() }
|
||||
configureCommon()
|
||||
|
||||
@@ -126,6 +126,7 @@ object Settings : DataStorePreferences(null) {
|
||||
val qSSaveProgress = boolPref("qs_save_progress", true)
|
||||
val security = boolPref("require_unlock", false)
|
||||
val animateItems = boolPref("animate_items", true)
|
||||
val enableCronet = boolPref("enable_cronet", true)
|
||||
val displayName = stringOrNullPref("display_name", null)
|
||||
val avatar = stringOrNullPref("avatar", null)
|
||||
val recentDownloadLabel = stringOrNullPref("recent_download_label", null)
|
||||
@@ -164,7 +165,6 @@ object Settings : DataStorePreferences(null) {
|
||||
var downloadOriginImage by boolPref("download_origin_image", false)
|
||||
var saveAsCbz by boolPref("save_as_cbz", false)
|
||||
var archiveMetadata by boolPref("archive_metadata", true)
|
||||
var enableCronet by boolPref("enable_cronet", true)
|
||||
var desktopSite by boolPref("desktop_site", false)
|
||||
var recentFavCat by intPref("recent_fav_cat", FavListUrlBuilder.FAV_CAT_LOCAL)
|
||||
var defaultFavSlot by intPref("default_favorite_slot", -2)
|
||||
|
||||
@@ -4,6 +4,7 @@ package com.hippo.ehviewer.ktor
|
||||
|
||||
import android.net.http.HttpEngine
|
||||
import android.net.http.HttpException
|
||||
import android.net.http.NetworkException
|
||||
import android.net.http.UploadDataProvider
|
||||
import android.net.http.UploadDataSink
|
||||
import android.net.http.UrlRequest
|
||||
@@ -11,6 +12,7 @@ import android.net.http.UrlResponseInfo
|
||||
import android.os.Build
|
||||
import androidx.annotation.RequiresExtension
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.util.restartApplication
|
||||
import io.ktor.client.engine.HttpClientEngineBase
|
||||
import io.ktor.client.engine.callContext
|
||||
import io.ktor.client.plugins.HttpTimeoutCapability
|
||||
@@ -39,6 +41,7 @@ import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.channels.consumeEach
|
||||
import kotlinx.coroutines.job
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
import splitties.preferences.edit
|
||||
|
||||
class CronetEngine(override val config: CronetConfig) : HttpClientEngineBase("Cronet") {
|
||||
// Limit thread to 1 since we are async & non-blocking
|
||||
@@ -103,8 +106,11 @@ class CronetEngine(override val config: CronetConfig) : HttpClientEngineBase("Cr
|
||||
override fun onFailed(request: UrlRequest, info: UrlResponseInfo?, error: HttpException) {
|
||||
// Cronet may crash on some devices, fuck xiaomi
|
||||
// https://github.com/FooIbar/EhViewer/issues/1826
|
||||
if (error.message?.contains("ERR_FILE_NOT_FOUND") == true) {
|
||||
Settings.enableCronet = false
|
||||
if (error is NetworkException && error.errorCode == NetworkException.ERROR_OTHER) {
|
||||
Settings.edit(true) {
|
||||
enableCronet.value = false
|
||||
}
|
||||
config.context.restartApplication()
|
||||
}
|
||||
if (continuation.isActive) {
|
||||
continuation.resumeWithException(error)
|
||||
|
||||
@@ -175,10 +175,12 @@ fun AdvancedScreen(navigator: DestinationsNavigator) {
|
||||
useSelectedAsSummary = true,
|
||||
entries = languages,
|
||||
)
|
||||
if (BuildConfig.DEBUG) {
|
||||
SwitchPreference(
|
||||
var enableCronet by Settings.enableCronet.asMutableState()
|
||||
if (BuildConfig.DEBUG || !enableCronet) {
|
||||
SwitchPref(
|
||||
checked = enableCronet,
|
||||
onMutate = { enableCronet = !enableCronet },
|
||||
title = "Enable Cronet",
|
||||
value = Settings::enableCronet,
|
||||
)
|
||||
}
|
||||
if (isAtLeastO) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.hippo.ehviewer.util
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.ContextWrapper
|
||||
import android.content.Intent
|
||||
|
||||
/**
|
||||
* Find the closest Activity in a given Context.
|
||||
@@ -15,3 +16,10 @@ inline fun <reified T : Activity> Context.findActivity(): T {
|
||||
}
|
||||
throw IllegalStateException("findActivity() should be called in the context of an Activity")
|
||||
}
|
||||
|
||||
fun Context.restartApplication() {
|
||||
packageManager.getLaunchIntentForPackage(packageName)?.let {
|
||||
startActivity(Intent.makeRestartActivityTask(it.component))
|
||||
Runtime.getRuntime().exit(0)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user