From 9a41937a33539dbfaae4d072361caaec79865c29 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Wed, 31 May 2017 01:31:02 -0700 Subject: implement cache db and start js injections --- .../com/pitchedapps/frost/facebook/CookieMap.kt | 45 ---------------- .../com/pitchedapps/frost/facebook/FbCookie.kt | 63 ++++++++++++++++++++++ 2 files changed, 63 insertions(+), 45 deletions(-) delete mode 100644 app/src/main/kotlin/com/pitchedapps/frost/facebook/CookieMap.kt create mode 100644 app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt (limited to 'app/src/main/kotlin/com/pitchedapps/frost/facebook') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/CookieMap.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/CookieMap.kt deleted file mode 100644 index 96b1f2de..00000000 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/CookieMap.kt +++ /dev/null @@ -1,45 +0,0 @@ -package com.pitchedapps.frost.facebook - -import android.webkit.CookieManager -import com.pitchedapps.frost.utils.Prefs - -/** - * Created by Allan Wang on 2017-05-30. - */ -object CookieMap { - - var userId: Int = -1 - private val userMatcher = "c_user=([0-9]*);" - private val map = HashMap() - - operator fun get(key: String) = map[key] - - operator fun set(key: String, value: String) { - map[key] = value - } - - fun put(url: String, cookie: String) { - map.put(url, cookie) - checkUserId(url, cookie) - } - - fun checkUserId(url: String, cookie: String) { - if (userId != -1) return - if (!url.contains("facebook") || !cookie.contains(userMatcher)) return - val id = Regex(userMatcher).find(cookie)?.value - if (id != null) { - userId = id.toInt() - save() - } - } - - fun save() { - Prefs.userId = userId - CookieManager.getInstance().flush() - - } - - fun reset() { - - } -} \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt new file mode 100644 index 00000000..80fc3b72 --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt @@ -0,0 +1,63 @@ +package com.pitchedapps.frost.facebook + +import android.webkit.CookieManager +import com.pitchedapps.frost.dbflow.FB_URL_BASE +import com.pitchedapps.frost.dbflow.loadFbCookie +import com.pitchedapps.frost.dbflow.saveFbCookie +import com.pitchedapps.frost.utils.L +import com.pitchedapps.frost.utils.Prefs + +/** + * Created by Allan Wang on 2017-05-30. + */ +object FbCookie { + + var userId: Long = Prefs.userIdDefault + var dbCookie: String? = null + var webCookie: String? + get() = CookieManager.getInstance().getCookie(FB_URL_BASE) + set(value) = CookieManager.getInstance().setCookie(FB_URL_BASE, value) + + fun init() { + userId = Prefs.userId + dbCookie = loadFbCookie()?.cookie + if (dbCookie != null && webCookie == null) { + L.d("DbCookie found & WebCookie is null; setting webcookie") + webCookie = dbCookie + } + } + + private val userMatcher: Regex by lazy { Regex("c_user=([0-9]*);") } + + fun checkUserId(url: String, cookie: String?) { + if (userId != Prefs.userIdDefault || cookie == null) return + L.d("Checking cookie for $url\n\t$cookie") + if (!url.contains("facebook") || !cookie.contains(userMatcher)) return + val id = userMatcher.find(cookie)?.groups?.get(1)?.value + if (id != null) { + try { + userId = id.toLong() + save() + } catch (e: NumberFormatException) { + //todo send report that id has changed + } + } + } + + fun save() { + L.d("New cookie found for $userId") + Prefs.userId = userId + CookieManager.getInstance().flush() + saveFbCookie() + } + + //TODO reset when new account is added; reset and clear when account is logged out + fun reset() { + Prefs.userId = Prefs.userIdDefault + userId = Prefs.userIdDefault + with(CookieManager.getInstance()) { + removeAllCookies(null) + flush() + } + } +} \ No newline at end of file -- cgit v1.2.3