From 2a070c0b1e658c50006ec9231a900210bc2e9464 Mon Sep 17 00:00:00 2001 From: arkon Date: Wed, 13 Apr 2022 17:48:05 -0400 Subject: [PATCH] Add clear cookies option to WebView menu --- .../kanade/tachiyomi/network/AndroidCookieJar.kt | 7 ++++--- .../details/ExtensionDetailsController.kt | 4 ++-- .../tachiyomi/ui/webview/WebViewActivity.kt | 15 +++++++++++++-- app/src/main/res/menu/webview.xml | 5 +++++ 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/network/AndroidCookieJar.kt b/app/src/main/java/eu/kanade/tachiyomi/network/AndroidCookieJar.kt index c436b0dbd..f9322e840 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/network/AndroidCookieJar.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/network/AndroidCookieJar.kt @@ -29,9 +29,9 @@ class AndroidCookieJar : CookieJar { } } - fun remove(url: HttpUrl, cookieNames: List? = null, maxAge: Int = -1) { + fun remove(url: HttpUrl, cookieNames: List? = null, maxAge: Int = -1): Int { val urlString = url.toString() - val cookies = manager.getCookie(urlString) ?: return + val cookies = manager.getCookie(urlString) ?: return 0 fun List.filterNames(): List { return if (cookieNames != null) { @@ -41,10 +41,11 @@ class AndroidCookieJar : CookieJar { } } - cookies.split(";") + return cookies.split(";") .map { it.substringBefore("=") } .filterNames() .onEach { manager.setCookie(urlString, "$it=;Max-Age=$maxAge") } + .count() } fun removeAll() { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt index 02747b2ea..7d76967e1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt @@ -259,11 +259,11 @@ class ExtensionDetailsController(bundle: Bundle? = null) : ?.map { it.baseUrl } ?.distinct() ?: emptyList() - urls.forEach { + val cleared = urls.sumOf { network.cookieManager.remove(it.toHttpUrl()) } - logcat { "Cleared cookies for: ${urls.joinToString()}" } + logcat { "Cleared $cleared cookies for: ${urls.joinToString()}" } } private fun Source.isEnabled(): Boolean { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt index 353da4493..794df9097 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt @@ -17,17 +17,20 @@ import androidx.lifecycle.lifecycleScope import eu.kanade.tachiyomi.BuildConfig import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.databinding.WebviewActivityBinding +import eu.kanade.tachiyomi.network.NetworkHelper import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.ui.base.activity.BaseActivity import eu.kanade.tachiyomi.util.system.WebViewClientCompat import eu.kanade.tachiyomi.util.system.WebViewUtil import eu.kanade.tachiyomi.util.system.getResourceColor +import eu.kanade.tachiyomi.util.system.logcat import eu.kanade.tachiyomi.util.system.openInBrowser import eu.kanade.tachiyomi.util.system.setDefaultSettings import eu.kanade.tachiyomi.util.system.toast import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach +import okhttp3.HttpUrl.Companion.toHttpUrl import reactivecircus.flowbinding.appcompat.navigationClicks import reactivecircus.flowbinding.swiperefreshlayout.refreshes import uy.kohesive.injekt.injectLazy @@ -37,11 +40,16 @@ class WebViewActivity : BaseActivity() { private lateinit var binding: WebviewActivityBinding private val sourceManager: SourceManager by injectLazy() + private val network: NetworkHelper by injectLazy() private var bundle: Bundle? = null private var isRefreshing: Boolean = false + init { + registerSecureActivity(this) + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -176,6 +184,7 @@ class WebViewActivity : BaseActivity() { R.id.action_web_refresh -> refreshPage() R.id.action_web_share -> shareWebpage() R.id.action_web_browser -> openInBrowser() + R.id.action_clear_cookies -> clearCookies() } return super.onOptionsItemSelected(item) } @@ -207,8 +216,10 @@ class WebViewActivity : BaseActivity() { openInBrowser(binding.webview.url!!, forceDefaultBrowser = true) } - init { - registerSecureActivity(this) + private fun clearCookies() { + val url = binding.webview.url!! + val cleared = network.cookieManager.remove(url.toHttpUrl()) + logcat { "Cleared $cleared cookies for: $url" } } companion object { diff --git a/app/src/main/res/menu/webview.xml b/app/src/main/res/menu/webview.xml index bd8aa3ad2..df91be82b 100644 --- a/app/src/main/res/menu/webview.xml +++ b/app/src/main/res/menu/webview.xml @@ -31,4 +31,9 @@ android:title="@string/action_open_in_browser" app:showAsAction="never" /> + +