Skip to content

Commit

Permalink
Fixed a bug that prevented the cookie banner from closing #16
Browse files Browse the repository at this point in the history
  • Loading branch information
sakusaku3939 committed Aug 2, 2024
1 parent 140e892 commit 858f8f2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 33 deletions.
50 changes: 50 additions & 0 deletions app/src/main/java/com/example/deeplviewer/CookieManagerHelper.kt
Original file line number Diff line number Diff line change
@@ -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.")
}
}
}
}
35 changes: 2 additions & 33 deletions app/src/main/java/com/example/deeplviewer/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 858f8f2

Please sign in to comment.