From 8618670b82641d5fbaec9c333f1290bab429ce27 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Wed, 31 May 2017 17:11:46 -0700 Subject: add more cookie handling --- .../com/pitchedapps/frost/facebook/FbCookie.kt | 45 +++++++++++++++------- .../com/pitchedapps/frost/facebook/UrlData.kt | 28 -------------- 2 files changed, 32 insertions(+), 41 deletions(-) delete mode 100644 app/src/main/kotlin/com/pitchedapps/frost/facebook/UrlData.kt (limited to 'app/src/main/kotlin/com/pitchedapps/frost/facebook') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt index 80fc3b72..3316bb65 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt @@ -3,24 +3,30 @@ 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.removeCookie import com.pitchedapps.frost.dbflow.saveFbCookie +import com.pitchedapps.frost.events.WebEvent +import com.pitchedapps.frost.utils.GlideUtils import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.Prefs +import org.greenrobot.eventbus.EventBus /** * 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) + set(value) { + CookieManager.getInstance().setCookie(FB_URL_BASE, value) + CookieManager.getInstance().flush() + } - fun init() { - userId = Prefs.userId - dbCookie = loadFbCookie()?.cookie + operator fun invoke() { + L.d("User ${Prefs.userId}") + dbCookie = loadFbCookie(Prefs.userId)?.cookie if (dbCookie != null && webCookie == null) { L.d("DbCookie found & WebCookie is null; setting webcookie") webCookie = dbCookie @@ -30,34 +36,47 @@ object FbCookie { private val userMatcher: Regex by lazy { Regex("c_user=([0-9]*);") } fun checkUserId(url: String, cookie: String?) { - if (userId != Prefs.userIdDefault || cookie == null) return + if (Prefs.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() + save(id.toLong()) } catch (e: NumberFormatException) { //todo send report that id has changed } } } - fun save() { - L.d("New cookie found for $userId") - Prefs.userId = userId + fun save(id: Long) { + L.d("New cookie found for $id") + Prefs.userId = id CookieManager.getInstance().flush() - saveFbCookie() + EventBus.getDefault().post(WebEvent(WebEvent.REFRESH_BASE)) + saveFbCookie(Prefs.userId, webCookie) + GlideUtils.downloadProfile(id) } //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() } } + + fun switchUser(id: Long) { + val cookie = loadFbCookie(id) ?: return + Prefs.userId = id + dbCookie = cookie.cookie + webCookie = dbCookie + } + + fun logout() { + L.d("Logging out user ${Prefs.userId}") + removeCookie(Prefs.userId) + reset() + } } \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/UrlData.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/UrlData.kt deleted file mode 100644 index c5c2a86b..00000000 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/UrlData.kt +++ /dev/null @@ -1,28 +0,0 @@ -package com.pitchedapps.frost.facebook - -import android.content.Context -import android.support.annotation.StringRes -import com.mikepenz.community_material_typeface_library.CommunityMaterial -import com.mikepenz.google_material_typeface_library.GoogleMaterial -import com.mikepenz.iconics.typeface.IIcon -import com.mikepenz.material_design_iconic_typeface_library.MaterialDesignIconic -import com.pitchedapps.frost.R -import com.pitchedapps.frost.dbflow.FbTab - -/** - * Created by Allan Wang on 2017-05-29. - */ -enum class FbUrl(@StringRes val titleId: Int, val icon: IIcon, val url: String) { - LOGIN(R.string.feed, CommunityMaterial.Icon.cmd_newspaper, "https://www.facebook.com/v2.9/dialog/oauth?client_id=$FB_KEY&redirect_uri=https://touch.facebook.com/&response_type=token,granted_scopes"), - FEED(R.string.feed, CommunityMaterial.Icon.cmd_newspaper, "https://touch.facebook.com/"), - PROFILE(R.string.profile, CommunityMaterial.Icon.cmd_account, "https://touch.facebook.com/me/"), - EVENTS(R.string.events, GoogleMaterial.Icon.gmd_event, "https://touch.facebook.com/events/upcoming"), - FRIENDS(R.string.friends, GoogleMaterial.Icon.gmd_people, "https://touch.facebook.com/friends/center/requests/"), - MESSAGES(R.string.messages, MaterialDesignIconic.Icon.gmi_comments, "https://touch.facebook.com/messages"), - NOTIFICATIONS(R.string.notifications, MaterialDesignIconic.Icon.gmi_globe, "https://touch.facebook.com/notifications"); - - fun tabInfo(c: Context) = FbTab(c.getString(titleId), icon, url) -} - -//BOOKMARKS("https://touch.facebook.com/bookmarks"), -//SEARCH("https://touch.facebook.com/search"), \ No newline at end of file -- cgit v1.2.3