Browse Source

Add clear cookies option to WebView menu

arkon 3 years ago
parent
commit
2a070c0b1e

+ 4 - 3
app/src/main/java/eu/kanade/tachiyomi/network/AndroidCookieJar.kt

@@ -29,9 +29,9 @@ class AndroidCookieJar : CookieJar {
         }
     }
 
-    fun remove(url: HttpUrl, cookieNames: List<String>? = null, maxAge: Int = -1) {
+    fun remove(url: HttpUrl, cookieNames: List<String>? = null, maxAge: Int = -1): Int {
         val urlString = url.toString()
-        val cookies = manager.getCookie(urlString) ?: return
+        val cookies = manager.getCookie(urlString) ?: return 0
 
         fun List<String>.filterNames(): List<String> {
             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() {

+ 2 - 2
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 {

+ 13 - 2
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 {

+ 5 - 0
app/src/main/res/menu/webview.xml

@@ -31,4 +31,9 @@
         android:title="@string/action_open_in_browser"
         app:showAsAction="never" />
 
+    <item
+        android:id="@+id/action_clear_cookies"
+        android:title="@string/pref_clear_cookies"
+        app:showAsAction="never" />
+
 </menu>