diff --git a/app/src/main/java/io/legado/app/data/AppDatabase.kt b/app/src/main/java/io/legado/app/data/AppDatabase.kt index be65f7730..a91db75c4 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -13,7 +13,7 @@ import io.legado.app.data.entities.* @Database( entities = [Book::class, BookGroup::class, BookSource::class, BookChapter::class, ReplaceRule::class, SearchBook::class, SearchKeyword::class, SourceCookie::class, RssSource::class, Bookmark::class, - RssArticle::class], + RssArticle::class, Cookie::class], version = 1, exportSchema = true ) @@ -60,4 +60,5 @@ abstract class AppDatabase : RoomDatabase() { abstract fun rssSourceDao(): RssSourceDao abstract fun bookmarkDao(): BookmarkDao abstract fun rssArticleDao(): RssArticleDao + abstract fun cookieDao(): CookieDao } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/dao/CookieDao.kt b/app/src/main/java/io/legado/app/data/dao/CookieDao.kt index 93f613ae4..f2adba4aa 100644 --- a/app/src/main/java/io/legado/app/data/dao/CookieDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/CookieDao.kt @@ -1,13 +1,21 @@ package io.legado.app.data.dao -import androidx.room.Dao -import androidx.room.Query +import androidx.room.* +import io.legado.app.data.entities.Cookie @Dao interface CookieDao { @Query("select * from cookies where url = :url") - fun get(url: String) + fun get(url: String): Cookie? + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insert(vararg cookie: Cookie) + + @Update + fun update(vararg cookie: Cookie) + + @Query("delete from cookies where url = :url") + fun delete(url: String) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/entities/Cookie.kt b/app/src/main/java/io/legado/app/data/entities/Cookie.kt index fd1ec1840..d43fbf7e0 100644 --- a/app/src/main/java/io/legado/app/data/entities/Cookie.kt +++ b/app/src/main/java/io/legado/app/data/entities/Cookie.kt @@ -7,5 +7,5 @@ import androidx.room.PrimaryKey data class Cookie( @PrimaryKey var url: String, - var cookie: String + var cookie: String? = null ) \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/http/AjaxWebView.kt b/app/src/main/java/io/legado/app/help/http/AjaxWebView.kt index 81d8f3099..837c8d62c 100644 --- a/app/src/main/java/io/legado/app/help/http/AjaxWebView.kt +++ b/app/src/main/java/io/legado/app/help/http/AjaxWebView.kt @@ -246,18 +246,6 @@ class AjaxWebView { const val DESTROY_WEB_VIEW = 4 } - interface CookieStore { - fun setCookie(url: String, cookie: String) - - fun replaceCookie(url: String, cookie: String) - - fun getCookie(url: String): String - - fun removeCookie(url: String) - - fun clearCookies() - } - abstract class Callback { abstract fun onResult(result: String) diff --git a/app/src/main/java/io/legado/app/help/http/CookieStore.kt b/app/src/main/java/io/legado/app/help/http/CookieStore.kt new file mode 100644 index 000000000..56b7aff28 --- /dev/null +++ b/app/src/main/java/io/legado/app/help/http/CookieStore.kt @@ -0,0 +1,86 @@ +package io.legado.app.help.http + +import android.text.TextUtils +import io.legado.app.App +import io.legado.app.data.entities.Cookie + + +object CookieStore { + + fun setCookie(url: String, cookie: String?) { + try { + val cookieBean = Cookie(url, cookie) + App.db.cookieDao().insert(cookieBean) + } catch (ignore: Exception) { + } + } + + fun replaceCookie(url: String, cookie: String) { + if (TextUtils.isEmpty(url) || TextUtils.isEmpty(cookie)) { + return + } + val oldCookie = getCookie(url) + + if (TextUtils.isEmpty(oldCookie)) { + setCookie(url, cookie) + } else { + val cookieMap = cookieToMap(oldCookie) + cookieMap.putAll(cookieToMap(cookie)) + val newCookie = mapToCookie(cookieMap) + + setCookie(url, newCookie) + } + } + + fun getCookie(url: String): String { + val cookieBean = App.db.cookieDao().get(url) + return cookieBean?.cookie ?: "" + } + + fun removeCookie(url: String) { + App.db.cookieDao().delete(url) + } + + fun clearCookies() { + + } + + private fun cookieToMap(cookie: String): MutableMap { + val cookieMap = mutableMapOf() + if (cookie.isBlank()) { + return cookieMap + } + val pairArray = cookie.split(";".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() + for (pair in pairArray) { + val pairs = pair.split("=".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() + if (pairs.size == 1) { + continue + } + val key = pairs[0].trim { it <= ' ' } + val value = pairs[1] + if (value.isNotBlank() || value.trim { it <= ' ' } == "null") { + cookieMap.put(key, value.trim { it <= ' ' }) + } + } + return cookieMap + } + + private fun mapToCookie(cookieMap: Map?): String? { + if (cookieMap == null || cookieMap.isEmpty()) { + return null + } + val builder = StringBuilder() + for (key in cookieMap.keys) { + val value = cookieMap[key] + if (value?.isNotBlank() == true) { + builder.append(key) + .append("=") + .append(value) + .append(";") + } + } + return builder.deleteCharAt(builder.lastIndexOf(";")).toString() + } + + +} \ No newline at end of file