diff --git a/app/src/main/java/org/koitharu/kotatsu/browser/cloudflare/CloudFlareClient.kt b/app/src/main/java/org/koitharu/kotatsu/browser/cloudflare/CloudFlareClient.kt index 9b731229c..287c32c14 100644 --- a/app/src/main/java/org/koitharu/kotatsu/browser/cloudflare/CloudFlareClient.kt +++ b/app/src/main/java/org/koitharu/kotatsu/browser/cloudflare/CloudFlareClient.kt @@ -1,23 +1,19 @@ package org.koitharu.kotatsu.browser.cloudflare import android.graphics.Bitmap -import android.webkit.CookieManager import android.webkit.WebView -import android.webkit.WebViewClient -import okhttp3.Cookie -import okhttp3.CookieJar import okhttp3.HttpUrl.Companion.toHttpUrl +import org.koitharu.kotatsu.core.network.AndroidCookieJar +import org.koitharu.kotatsu.core.network.WebViewClientCompat class CloudFlareClient( - private val cookieJar: CookieJar, + private val cookieJar: AndroidCookieJar, private val callback: CloudFlareCallback, private val targetUrl: String -) : WebViewClient() { - - private val cookieManager = CookieManager.getInstance() +) : WebViewClientCompat() { init { - cookieManager.removeAllCookies(null) + cookieJar.remove(targetUrl, CF_UID, CF_CLEARANCE) } override fun onPageStarted(view: WebView, url: String?, favicon: Bitmap?) { @@ -36,16 +32,11 @@ class CloudFlareClient( } private fun checkClearance() { - val httpUrl = targetUrl.toHttpUrl() - val rawCookie = cookieManager.getCookie(targetUrl) ?: return - val cookies = rawCookie.split(';').mapNotNull { - Cookie.parse(httpUrl, it) + val cookies = cookieJar.loadForRequest(targetUrl.toHttpUrl()) + if (cookies.any { it.name == CF_CLEARANCE }) { + callback.onCheckPassed() } - if (cookies.none { it.name == CF_CLEARANCE }) { - return - } - cookieJar.saveFromResponse(httpUrl, cookies) - callback.onCheckPassed() + } private companion object { diff --git a/app/src/main/java/org/koitharu/kotatsu/core/network/AndroidCookieJar.kt b/app/src/main/java/org/koitharu/kotatsu/core/network/AndroidCookieJar.kt new file mode 100644 index 000000000..b8bfaada5 --- /dev/null +++ b/app/src/main/java/org/koitharu/kotatsu/core/network/AndroidCookieJar.kt @@ -0,0 +1,47 @@ +package org.koitharu.kotatsu.core.network + +import android.webkit.CookieManager +import okhttp3.Cookie +import okhttp3.CookieJar +import okhttp3.HttpUrl +import kotlin.coroutines.resume +import kotlin.coroutines.suspendCoroutine + +class AndroidCookieJar : CookieJar { + + private val cookieManager = CookieManager.getInstance() + + override fun loadForRequest(url: HttpUrl): List { + val rawCookie = cookieManager.getCookie(url.toString()) ?: return emptyList() + return rawCookie.split(';').mapNotNull { + Cookie.parse(url, it) + } + } + + override fun saveFromResponse(url: HttpUrl, cookies: List) { + if (cookies.isEmpty()) { + return + } + val urlString = url.toString() + for (cookie in cookies) { + cookieManager.setCookie(urlString, cookie.toString()) + } + } + + fun remove(url: String, vararg names: String) { + val cookies = cookieManager.getCookie(url) ?: return + val newCookies = cookies.split(";") + .filterNot { cookie -> + names.any { cookie.startsWith("$it=") } + }.joinToString(";") + cookieManager.setCookie(url, newCookies) + } + + fun clearAsync() { + cookieManager.removeAllCookies(null) + } + + suspend fun clear() = suspendCoroutine { continuation -> + cookieManager.removeAllCookies(continuation::resume) + } +} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/network/NetworkModule.kt b/app/src/main/java/org/koitharu/kotatsu/core/network/NetworkModule.kt index d1ffd108a..0eb68df3e 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/network/NetworkModule.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/network/NetworkModule.kt @@ -6,21 +6,12 @@ import org.koin.android.ext.koin.androidContext import org.koin.core.qualifier.named import org.koin.dsl.bind import org.koin.dsl.module -import org.koitharu.kotatsu.core.network.cookies.ClearableCookieJar -import org.koitharu.kotatsu.core.network.cookies.PersistentCookieJar -import org.koitharu.kotatsu.core.network.cookies.cache.SetCookieCache -import org.koitharu.kotatsu.core.network.cookies.persistence.SharedPrefsCookiePersistor import org.koitharu.kotatsu.utils.CacheUtils import java.util.concurrent.TimeUnit val networkModule get() = module { - single { - PersistentCookieJar( - SetCookieCache(), - SharedPrefsCookiePersistor(androidContext()) - ) - } bind ClearableCookieJar::class + single { AndroidCookieJar() } bind CookieJar::class single(named(CacheUtils.QUALIFIER_HTTP)) { CacheUtils.createHttpCache(androidContext()) } single { OkHttpClient.Builder().apply { diff --git a/app/src/main/java/org/koitharu/kotatsu/core/network/WebViewClientCompat.kt b/app/src/main/java/org/koitharu/kotatsu/core/network/WebViewClientCompat.kt new file mode 100644 index 000000000..8764a338d --- /dev/null +++ b/app/src/main/java/org/koitharu/kotatsu/core/network/WebViewClientCompat.kt @@ -0,0 +1,86 @@ +package org.koitharu.kotatsu.core.network + +import android.annotation.TargetApi +import android.os.Build +import android.webkit.* + +@Suppress("OverridingDeprecatedMember") +abstract class WebViewClientCompat : WebViewClient() { + + open fun shouldOverrideUrlCompat(view: WebView, url: String): Boolean { + return false + } + + open fun shouldInterceptRequestCompat(view: WebView, url: String): WebResourceResponse? { + return null + } + + open fun onReceivedErrorCompat( + view: WebView, + errorCode: Int, + description: String?, + failingUrl: String, + isMainFrame: Boolean + ) { + } + + @TargetApi(Build.VERSION_CODES.N) + final override fun shouldOverrideUrlLoading( + view: WebView, + request: WebResourceRequest + ): Boolean = shouldOverrideUrlCompat(view, request.url.toString()) + + final override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean { + return shouldOverrideUrlCompat(view, url) + } + + final override fun shouldInterceptRequest( + view: WebView, + request: WebResourceRequest + ): WebResourceResponse? = shouldInterceptRequestCompat(view, request.url.toString()) + + final override fun shouldInterceptRequest( + view: WebView, + url: String + ): WebResourceResponse? = shouldInterceptRequestCompat(view, url) + + @TargetApi(Build.VERSION_CODES.M) + final override fun onReceivedError( + view: WebView, + request: WebResourceRequest, + error: WebResourceError + ) { + onReceivedErrorCompat( + view, + error.errorCode, + error.description?.toString(), + request.url.toString(), + request.isForMainFrame + ) + } + + final override fun onReceivedError( + view: WebView, + errorCode: Int, + description: String?, + failingUrl: String + ) { + onReceivedErrorCompat(view, errorCode, description, failingUrl, failingUrl == view.url) + } + + @TargetApi(Build.VERSION_CODES.M) + final override fun onReceivedHttpError( + view: WebView, + request: WebResourceRequest, + error: WebResourceResponse + ) { + onReceivedErrorCompat( + view, + error.statusCode, + error.reasonPhrase, + request.url + .toString(), + request.isForMainFrame + ) + } +} diff --git a/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/ClearableCookieJar.kt b/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/ClearableCookieJar.kt deleted file mode 100644 index ced957752..000000000 --- a/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/ClearableCookieJar.kt +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (C) 2016 Francisco José Montiel Navarro. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.koitharu.kotatsu.core.network.cookies - -import okhttp3.CookieJar - -/** - * This interface extends [okhttp3.CookieJar] and adds methods to clear the cookies. - */ -interface ClearableCookieJar : CookieJar { - - /** - * Clear all the session cookies while maintaining the persisted ones. - */ - fun clearSession() - - /** - * Clear all the cookies from persistence and from the cache. - */ - fun clear() -} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/PersistentCookieJar.kt b/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/PersistentCookieJar.kt deleted file mode 100644 index 82dc45b86..000000000 --- a/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/PersistentCookieJar.kt +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (C) 2016 Francisco José Montiel Navarro. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.koitharu.kotatsu.core.network.cookies - -import okhttp3.Cookie -import okhttp3.HttpUrl -import org.koitharu.kotatsu.core.network.cookies.cache.CookieCache -import org.koitharu.kotatsu.core.network.cookies.persistence.CookiePersistor -import java.util.* -import java.util.concurrent.atomic.AtomicBoolean - -class PersistentCookieJar( - private val cache: CookieCache, - private val persistor: CookiePersistor -) : ClearableCookieJar { - - private var isLoaded = AtomicBoolean(false) - - @Synchronized - override fun saveFromResponse(url: HttpUrl, cookies: List) { - if (isLoaded.compareAndSet(false, true)) { - cache.addAll(persistor.loadAll()) - } - cache.addAll(cookies) - persistor.saveAll(cookies.filter { it.persistent }) - } - - @Synchronized - override fun loadForRequest(url: HttpUrl): List { - if (isLoaded.compareAndSet(false, true)) { - cache.addAll(persistor.loadAll()) - } - val cookiesToRemove: MutableList = ArrayList() - val validCookies: MutableList = ArrayList() - val it = cache.iterator() - while (it.hasNext()) { - val currentCookie = it.next() - when { - currentCookie.isExpired() -> { - cookiesToRemove.add(currentCookie) - it.remove() - } - currentCookie.matches(url) -> { - validCookies.add(currentCookie) - } - } - } - persistor.removeAll(cookiesToRemove) - return validCookies - } - - @Synchronized - override fun clearSession() { - cache.clear() - cache.addAll(persistor.loadAll()) - } - - @Synchronized - override fun clear() { - cache.clear() - persistor.clear() - } - - private companion object { - - fun Cookie.isExpired(): Boolean { - return expiresAt < System.currentTimeMillis() - } - } -} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/cache/CookieCache.kt b/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/cache/CookieCache.kt deleted file mode 100644 index 6eaacb539..000000000 --- a/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/cache/CookieCache.kt +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (C) 2016 Francisco José Montiel Navarro. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.koitharu.kotatsu.core.network.cookies.cache - -import okhttp3.Cookie - -/** - * A CookieCache handles the volatile cookie session storage. - */ -interface CookieCache : MutableIterable { - /** - * Add all the new cookies to the session, existing cookies will be overwritten. - * - * @param newCookies - */ - fun addAll(newCookies: Collection) - - /** - * Clear all the cookies from the session. - */ - fun clear() -} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/cache/IdentifiableCookie.kt b/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/cache/IdentifiableCookie.kt deleted file mode 100644 index 0bffe0b09..000000000 --- a/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/cache/IdentifiableCookie.kt +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (C) 2016 Francisco José Montiel Navarro. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.koitharu.kotatsu.core.network.cookies.cache - -import okhttp3.Cookie -import java.util.* - -/** - * This class decorates a Cookie to re-implements equals() and hashcode() methods in order to identify - * the cookie by the following attributes: name, domain, path, secure & hostOnly. - * - * - * - * This new behaviour will be useful in determining when an already existing cookie in session must be overwritten. - */ -internal class IdentifiableCookie(val cookie: Cookie) { - - override fun equals(other: Any?): Boolean { - if (other !is IdentifiableCookie) return false - return other.cookie.name == cookie.name && other.cookie.domain == cookie.domain - && other.cookie.path == cookie.path && other.cookie.secure == cookie.secure - && other.cookie.hostOnly == cookie.hostOnly - } - - override fun hashCode(): Int { - var hash = 17 - hash = 31 * hash + cookie.name.hashCode() - hash = 31 * hash + cookie.domain.hashCode() - hash = 31 * hash + cookie.path.hashCode() - hash = 31 * hash + if (cookie.secure) 0 else 1 - hash = 31 * hash + if (cookie.hostOnly) 0 else 1 - return hash - } - - companion object { - - fun decorateAll(cookies: Collection): List { - val identifiableCookies: MutableList = ArrayList(cookies.size) - for (cookie in cookies) { - identifiableCookies.add(IdentifiableCookie(cookie)) - } - return identifiableCookies - } - } - -} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/cache/SetCookieCache.kt b/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/cache/SetCookieCache.kt deleted file mode 100644 index e2c0804d8..000000000 --- a/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/cache/SetCookieCache.kt +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (C) 2016 Francisco José Montiel Navarro. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.koitharu.kotatsu.core.network.cookies.cache - -import okhttp3.Cookie -import org.koitharu.kotatsu.core.network.cookies.cache.IdentifiableCookie.Companion.decorateAll -import java.util.* -import java.util.concurrent.ConcurrentHashMap - -class SetCookieCache : CookieCache { - - private val cookies: MutableSet = - Collections.newSetFromMap(ConcurrentHashMap()) - - override fun addAll(newCookies: Collection) { - for (cookie in decorateAll(newCookies)) { - cookies.remove(cookie) - cookies.add(cookie) - } - } - - override fun clear() { - cookies.clear() - } - - override fun iterator(): MutableIterator = SetCookieCacheIterator() - - private inner class SetCookieCacheIterator : MutableIterator { - - private val iterator = cookies.iterator() - - override fun hasNext(): Boolean { - return iterator.hasNext() - } - - override fun next(): Cookie { - return iterator.next().cookie - } - - override fun remove() { - iterator.remove() - } - - } -} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/persistence/CookiePersistor.kt b/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/persistence/CookiePersistor.kt deleted file mode 100644 index fa7b1886a..000000000 --- a/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/persistence/CookiePersistor.kt +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (C) 2016 Francisco José Montiel Navarro. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.koitharu.kotatsu.core.network.cookies.persistence - -import okhttp3.Cookie - -/** - * A CookiePersistor handles the persistent cookie storage. - */ -interface CookiePersistor { - - fun loadAll(): List - - /** - * Persist all cookies, existing cookies will be overwritten. - * - * @param cookies cookies persist - */ - fun saveAll(cookies: Collection) - - /** - * Removes indicated cookies from persistence. - * - * @param cookies cookies to remove from persistence - */ - fun removeAll(cookies: Collection) - - /** - * Clear all cookies from persistence. - */ - fun clear() -} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/persistence/SerializableCookie.kt b/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/persistence/SerializableCookie.kt deleted file mode 100644 index 529313d92..000000000 --- a/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/persistence/SerializableCookie.kt +++ /dev/null @@ -1,150 +0,0 @@ -/* - * Copyright (C) 2016 Francisco José Montiel Navarro. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.koitharu.kotatsu.core.network.cookies.persistence - -import android.util.Log -import okhttp3.Cookie -import java.io.* - -class SerializableCookie : Serializable { - - @Transient - private var cookie: Cookie? = null - - fun encode(cookie: Cookie?): String? { - this.cookie = cookie - val byteArrayOutputStream = ByteArrayOutputStream() - var objectOutputStream: ObjectOutputStream? = null - try { - objectOutputStream = ObjectOutputStream(byteArrayOutputStream) - objectOutputStream.writeObject(this) - } catch (e: IOException) { - Log.d(TAG, "IOException in encodeCookie", e) - return null - } finally { - if (objectOutputStream != null) { - try { // Closing a ByteArrayOutputStream has no effect, it can be used later (and is used in the return statement) - objectOutputStream.close() - } catch (e: IOException) { - Log.d(TAG, "Stream not closed in encodeCookie", e) - } - } - } - return byteArrayToHexString(byteArrayOutputStream.toByteArray()) - } - - fun decode(encodedCookie: String): Cookie? { - val bytes = hexStringToByteArray(encodedCookie) - val byteArrayInputStream = ByteArrayInputStream( - bytes - ) - var cookie: Cookie? = null - var objectInputStream: ObjectInputStream? = null - try { - objectInputStream = ObjectInputStream(byteArrayInputStream) - cookie = (objectInputStream.readObject() as SerializableCookie).cookie - } catch (e: IOException) { - Log.d(TAG, "IOException in decodeCookie", e) - } catch (e: ClassNotFoundException) { - Log.d(TAG, "ClassNotFoundException in decodeCookie", e) - } finally { - if (objectInputStream != null) { - try { - objectInputStream.close() - } catch (e: IOException) { - Log.d(TAG, "Stream not closed in decodeCookie", e) - } - } - } - return cookie - } - - @Throws(IOException::class) - private fun writeObject(out: ObjectOutputStream) { - out.writeObject(cookie!!.name) - out.writeObject(cookie!!.value) - out.writeLong(if (cookie!!.persistent) cookie!!.expiresAt else NON_VALID_EXPIRES_AT) - out.writeObject(cookie!!.domain) - out.writeObject(cookie!!.path) - out.writeBoolean(cookie!!.secure) - out.writeBoolean(cookie!!.httpOnly) - out.writeBoolean(cookie!!.hostOnly) - } - - @Throws(IOException::class, ClassNotFoundException::class) - private fun readObject(`in`: ObjectInputStream) { - val builder = Cookie.Builder() - builder.name((`in`.readObject() as String)) - builder.value((`in`.readObject() as String)) - val expiresAt = `in`.readLong() - if (expiresAt != NON_VALID_EXPIRES_AT) { - builder.expiresAt(expiresAt) - } - val domain = `in`.readObject() as String - builder.domain(domain) - builder.path((`in`.readObject() as String)) - if (`in`.readBoolean()) builder.secure() - if (`in`.readBoolean()) builder.httpOnly() - if (`in`.readBoolean()) builder.hostOnlyDomain(domain) - cookie = builder.build() - } - - private companion object { - - private val TAG = SerializableCookie::class.java.simpleName - - const val serialVersionUID = -8594045714036645534L - private const val NON_VALID_EXPIRES_AT = -1L - - /** - * Using some super basic byte array <-> hex conversions so we don't - * have to rely on any large Base64 libraries. Can be overridden if you - * like! - * - * @param bytes byte array to be converted - * @return string containing hex values - */ - private fun byteArrayToHexString(bytes: ByteArray): String { - val sb = StringBuilder(bytes.size * 2) - for (element in bytes) { - val v: Int = element.toInt() and 0xff - if (v < 16) { - sb.append('0') - } - sb.append(Integer.toHexString(v)) - } - return sb.toString() - } - - /** - * Converts hex values from strings to byte array - * - * @param hexString string of hex-encoded values - * @return decoded byte array - */ - private fun hexStringToByteArray(hexString: String): ByteArray { - val len = hexString.length - val data = ByteArray(len / 2) - var i = 0 - while (i < len) { - data[i / 2] = ((Character.digit(hexString[i], 16) shl 4) + Character - .digit(hexString[i + 1], 16)).toByte() - i += 2 - } - return data - } - } -} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/persistence/SharedPrefsCookiePersistor.kt b/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/persistence/SharedPrefsCookiePersistor.kt deleted file mode 100644 index 73ef2b6ea..000000000 --- a/app/src/main/java/org/koitharu/kotatsu/core/network/cookies/persistence/SharedPrefsCookiePersistor.kt +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2016 Francisco José Montiel Navarro. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.koitharu.kotatsu.core.network.cookies.persistence - -import android.content.Context -import okhttp3.Cookie -import java.util.* - -class SharedPrefsCookiePersistor(context: Context) : CookiePersistor { - - private val sharedPreferences by lazy { - context.getSharedPreferences("cookies", Context.MODE_PRIVATE) - } - - override fun loadAll(): List { - val cookies: MutableList = ArrayList(sharedPreferences.all.size) - for ((_, value) in sharedPreferences.all) { - val serializedCookie = value as? String - if (serializedCookie != null) { - val cookie = SerializableCookie().decode(serializedCookie) - if (cookie != null) { - cookies.add(cookie) - } - } - } - return cookies - } - - override fun saveAll(cookies: Collection) { - val editor = sharedPreferences.edit() - for (cookie in cookies) { - editor.putString(createCookieKey(cookie), SerializableCookie().encode(cookie)) - } - editor.apply() - } - - override fun removeAll(cookies: Collection) { - val editor = sharedPreferences.edit() - for (cookie in cookies) { - editor.remove(createCookieKey(cookie)) - } - editor.apply() - } - - override fun clear() { - sharedPreferences.edit().clear().apply() - } - - private companion object { - - fun createCookieKey(cookie: Cookie): String { - return (if (cookie.secure) "https" else "http") + "://" + cookie.domain + cookie.path + "|" + cookie.name - } - } - -} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/settings/HistorySettingsFragment.kt b/app/src/main/java/org/koitharu/kotatsu/settings/HistorySettingsFragment.kt index ab6a635d8..96f34a27a 100644 --- a/app/src/main/java/org/koitharu/kotatsu/settings/HistorySettingsFragment.kt +++ b/app/src/main/java/org/koitharu/kotatsu/settings/HistorySettingsFragment.kt @@ -11,7 +11,7 @@ import org.koin.android.ext.android.get import org.koin.android.ext.android.inject import org.koitharu.kotatsu.R import org.koitharu.kotatsu.base.ui.BasePreferenceFragment -import org.koitharu.kotatsu.core.network.cookies.ClearableCookieJar +import org.koitharu.kotatsu.core.network.AndroidCookieJar import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.local.data.Cache import org.koitharu.kotatsu.search.ui.MangaSuggestionsProvider @@ -75,10 +75,8 @@ class HistorySettingsFragment : BasePreferenceFragment(R.string.history_and_cach } AppSettings.KEY_COOKIES_CLEAR -> { viewLifecycleScope.launch { - val cookieJar = get() - withContext(Dispatchers.IO) { - cookieJar.clear() - } + val cookieJar = get() + cookieJar.clear() Snackbar.make( listView ?: return@launch, R.string.cookies_cleared,