From 858f8f28125f8e14b6b7ab29a76b2bc2c350b7f9 Mon Sep 17 00:00:00 2001 From: sakusaku3939 Date: Sat, 3 Aug 2024 01:31:24 +0900 Subject: [PATCH] Fixed a bug that prevented the cookie banner from closing #16 --- .../deeplviewer/CookieManagerHelper.kt | 50 +++++++++++++++++++ .../com/example/deeplviewer/MainActivity.kt | 35 +------------ 2 files changed, 52 insertions(+), 33 deletions(-) create mode 100644 app/src/main/java/com/example/deeplviewer/CookieManagerHelper.kt diff --git a/app/src/main/java/com/example/deeplviewer/CookieManagerHelper.kt b/app/src/main/java/com/example/deeplviewer/CookieManagerHelper.kt new file mode 100644 index 0000000..0b3ca4f --- /dev/null +++ b/app/src/main/java/com/example/deeplviewer/CookieManagerHelper.kt @@ -0,0 +1,50 @@ +package com.example.deeplviewer + +import android.content.Context +import android.util.Log +import android.webkit.CookieManager +import android.webkit.WebView + +class CookieManagerHelper { + companion object { + private fun getCookieManager(): CookieManager { + return CookieManager.getInstance() + } + } + + fun saveCookies(context: Context, webView: WebView) { + val cookieManager = getCookieManager() + val isCookieAutoDeleted = context.getSharedPreferences("config", Context.MODE_PRIVATE) + .getBoolean(context.getString(R.string.key_auto_delete_cookies), false) + + if (isCookieAutoDeleted) { + clearCookies() + } else { + cookieManager.acceptCookie() + cookieManager.setAcceptThirdPartyCookies(webView, true) + cookieManager.flush() + } + } + + + // Disable cookie values in SharedPreferences according to the bug fix for cookie expiration in v8.5 + fun migrateCookie(context: Context) { + val sharedPreferences = context.getSharedPreferences("DeepLCookies", Context.MODE_PRIVATE) + val savedCookie = sharedPreferences.getString("cookie", null) + + if (savedCookie != null) { + clearCookies() + sharedPreferences.edit().clear().apply() + } + } + + private fun clearCookies() { + getCookieManager().removeAllCookies { success -> + if (success) { + Log.d("CookieManagerHelper", "Cookies successfully removed.") + } else { + Log.e("CookieManagerHelper", "Failed to remove cookies.") + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/deeplviewer/MainActivity.kt b/app/src/main/java/com/example/deeplviewer/MainActivity.kt index 87ff124..38924f9 100644 --- a/app/src/main/java/com/example/deeplviewer/MainActivity.kt +++ b/app/src/main/java/com/example/deeplviewer/MainActivity.kt @@ -6,7 +6,6 @@ import android.content.Intent import android.net.Uri import android.os.Bundle import android.view.animation.AlphaAnimation -import android.webkit.CookieManager import android.webkit.ValueCallback import android.webkit.WebChromeClient import android.webkit.WebSettings @@ -23,12 +22,6 @@ class MainActivity : AppCompatActivity() { defParamValue ) } - private val cookiesDisabled by lazy { - return@lazy getSharedPreferences( - "config", - Context.MODE_PRIVATE - ).getBoolean(getString(R.string.key_auto_delete_cookies), false) - } companion object { private const val REQUEST_SELECT_FILE = 100 @@ -63,18 +56,7 @@ class MainActivity : AppCompatActivity() { webView.alpha = 1.0F } - if (!cookiesDisabled) { - val cookieManager = CookieManager.getInstance() - cookieManager.acceptCookie() - cookieManager.setAcceptThirdPartyCookies(webView, true) - - // Load cookies from SharedPreferences - val sharedPreferences = getSharedPreferences("DeepLCookies", Context.MODE_PRIVATE) - val savedCookie = sharedPreferences.getString("cookie", null) - if (savedCookie != null) { - cookieManager.setCookie(startUrl, savedCookie) - } - } + CookieManagerHelper().migrateCookie(this) webView.settings.javaScriptEnabled = true webView.settings.domStorageEnabled = true @@ -117,20 +99,7 @@ class MainActivity : AppCompatActivity() { outState.putString("SavedText", inputText) } - val cookieManager = CookieManager.getInstance() - if (cookiesDisabled) { - cookieManager.removeAllCookies { } - } else { - // Save cookies - val cookies = cookieManager.getCookie(originUrl) - if (cookies != null) { - val sharedPreferences = getSharedPreferences("DeepLCookies", Context.MODE_PRIVATE) - val editor = sharedPreferences.edit() - editor.putString("cookie", cookies) - editor.apply() - } - cookieManager.flush() - } + CookieManagerHelper().saveCookies(this, webView) } inner class MyWebChromeClient : WebChromeClient() {