pull/46/head
kunfei 5 years ago
parent 456a2e3043
commit 2bb7d2502d
  1. 3
      app/src/main/java/io/legado/app/data/AppDatabase.kt
  2. 14
      app/src/main/java/io/legado/app/data/dao/CookieDao.kt
  3. 2
      app/src/main/java/io/legado/app/data/entities/Cookie.kt
  4. 12
      app/src/main/java/io/legado/app/help/http/AjaxWebView.kt
  5. 86
      app/src/main/java/io/legado/app/help/http/CookieStore.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
}

@ -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)
}

@ -7,5 +7,5 @@ import androidx.room.PrimaryKey
data class Cookie(
@PrimaryKey
var url: String,
var cookie: String
var cookie: String? = null
)

@ -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)

@ -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<String, String> {
val cookieMap = mutableMapOf<String, String>()
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, String>?): 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()
}
}
Loading…
Cancel
Save