-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed a bug that prevented the cookie banner from closing #16
- Loading branch information
1 parent
140e892
commit 858f8f2
Showing
2 changed files
with
52 additions
and
33 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
app/src/main/java/com/example/deeplviewer/CookieManagerHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters