From 736d16a7ab475c86c5306df17de5be8457533620 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sat, 17 Feb 2024 12:31:14 +0100 Subject: [PATCH] Make WebkitCookieManagerProxy no-op if webview is not available on the device (#2427) --- .../utils/WebkitCookieManagerProxy.kt | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/utils/WebkitCookieManagerProxy.kt b/app/src/main/java/io/github/wulkanowy/utils/WebkitCookieManagerProxy.kt index a54978717..3d41c711c 100644 --- a/app/src/main/java/io/github/wulkanowy/utils/WebkitCookieManagerProxy.kt +++ b/app/src/main/java/io/github/wulkanowy/utils/WebkitCookieManagerProxy.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.utils +import android.util.AndroidRuntimeException import java.net.CookiePolicy import java.net.CookieStore import java.net.HttpCookie @@ -9,7 +10,18 @@ import java.net.CookieManager as JavaCookieManager class WebkitCookieManagerProxy : JavaCookieManager(null, CookiePolicy.ACCEPT_ALL) { - private val webkitCookieManager: WebkitCookieManager = WebkitCookieManager.getInstance() + private val webkitCookieManager: WebkitCookieManager? = getWebkitCookieManager() + + /** + * @see [https://stackoverflow.com/a/70354583/6695449] + */ + private fun getWebkitCookieManager(): WebkitCookieManager? { + return try { + WebkitCookieManager.getInstance() + } catch (e: AndroidRuntimeException) { + null + } + } override fun put(uri: URI?, responseHeaders: Map>?) { if (uri == null || responseHeaders == null) return @@ -23,7 +35,7 @@ class WebkitCookieManagerProxy : JavaCookieManager(null, CookiePolicy.ACCEPT_ALL // process each of the headers for (headerValue in responseHeaders[headerKey].orEmpty()) { - webkitCookieManager.setCookie(url, headerValue) + webkitCookieManager?.setCookie(url, headerValue) } } } @@ -34,7 +46,7 @@ class WebkitCookieManagerProxy : JavaCookieManager(null, CookiePolicy.ACCEPT_ALL ): Map> { require(!(uri == null || requestHeaders == null)) { "Argument is null" } val res = mutableMapOf>() - val cookie = webkitCookieManager.getCookie(uri.toString()) + val cookie = webkitCookieManager?.getCookie(uri.toString()) if (cookie != null) res["Cookie"] = listOf(cookie) return res } @@ -50,7 +62,7 @@ class WebkitCookieManagerProxy : JavaCookieManager(null, CookiePolicy.ACCEPT_ALL cookies.remove(uri, cookie) override fun removeAll(): Boolean { - webkitCookieManager.removeAllCookies(null) + webkitCookieManager?.removeAllCookies(null) ?: return false return true } }