From 72fbcd4e0e1872a5f4b5f58d7c57eb2571a4dab1 Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Mon, 4 Nov 2024 17:43:07 +0800 Subject: [PATCH] Refine Cronet workaround --- .../main/kotlin/com/hippo/ehviewer/EhApplication.kt | 2 +- app/src/main/kotlin/com/hippo/ehviewer/Settings.kt | 2 +- .../kotlin/com/hippo/ehviewer/ktor/CronetEngine.kt | 10 ++++++++-- .../com/hippo/ehviewer/ui/settings/AdvancedScreen.kt | 8 +++++--- .../main/kotlin/com/hippo/ehviewer/util/ContextUtil.kt | 8 ++++++++ 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/EhApplication.kt b/app/src/main/kotlin/com/hippo/ehviewer/EhApplication.kt index 3f5460b24..3c5e9d457 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/EhApplication.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/EhApplication.kt @@ -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() diff --git a/app/src/main/kotlin/com/hippo/ehviewer/Settings.kt b/app/src/main/kotlin/com/hippo/ehviewer/Settings.kt index cd842ab5c..dbbf08688 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/Settings.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/Settings.kt @@ -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) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ktor/CronetEngine.kt b/app/src/main/kotlin/com/hippo/ehviewer/ktor/CronetEngine.kt index 53af726f8..3fb13c45d 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ktor/CronetEngine.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ktor/CronetEngine.kt @@ -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) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/AdvancedScreen.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/AdvancedScreen.kt index 3fb6c59fb..e921c6b6b 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/AdvancedScreen.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/AdvancedScreen.kt @@ -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) { diff --git a/app/src/main/kotlin/com/hippo/ehviewer/util/ContextUtil.kt b/app/src/main/kotlin/com/hippo/ehviewer/util/ContextUtil.kt index 85a0c6fa6..de9785e65 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/util/ContextUtil.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/util/ContextUtil.kt @@ -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 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) + } +}