From 5796566137995c8d244720f87ba85bce0e0d2f00 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sat, 3 Jun 2017 00:22:10 -0700 Subject: css updates and beginning login migration --- .../com/pitchedapps/frost/facebook/FbConst.kt | 8 +++++ .../com/pitchedapps/frost/facebook/FbCookie.kt | 39 +++++++++++++--------- .../kotlin/com/pitchedapps/frost/facebook/FbTab.kt | 30 +++++++++++++++++ .../com/pitchedapps/frost/facebook/FbTabs.kt | 24 ------------- .../pitchedapps/frost/facebook/UsernameFetcher.kt | 36 ++++++++++++++++++++ 5 files changed, 97 insertions(+), 40 deletions(-) create mode 100644 app/src/main/kotlin/com/pitchedapps/frost/facebook/FbConst.kt create mode 100644 app/src/main/kotlin/com/pitchedapps/frost/facebook/FbTab.kt delete mode 100644 app/src/main/kotlin/com/pitchedapps/frost/facebook/FbTabs.kt create mode 100644 app/src/main/kotlin/com/pitchedapps/frost/facebook/UsernameFetcher.kt (limited to 'app/src/main/kotlin/com/pitchedapps/frost/facebook') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbConst.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbConst.kt new file mode 100644 index 00000000..59d76954 --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbConst.kt @@ -0,0 +1,8 @@ +package com.pitchedapps.frost.facebook + +/** + * Created by Allan Wang on 2017-06-01. + */ +const val FACEBOOK_COM = "facebook.com" +const val FB_URL_BASE = "https://m.facebook.com/" +fun PROFILE_PICTURE_URL(id: Long) = "https://graph.facebook.com/$id/picture?type=large" \ 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 index e44b872a..7829998f 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt @@ -1,11 +1,11 @@ package com.pitchedapps.frost.facebook import android.webkit.CookieManager +import com.pitchedapps.frost.dbflow.CookieModel 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.events.FbAccountEvent import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.Prefs import org.greenrobot.eventbus.EventBus @@ -34,48 +34,55 @@ object FbCookie { private val userMatcher: Regex by lazy { Regex("c_user=([0-9]*);") } - fun checkUserId(url: String, cookie: String?) { - if (Prefs.userId != Prefs.userIdDefault || cookie == null) return + fun hasLoggedIn(url: String, cookie: String?):Boolean { + if (cookie == null || !url.contains("facebook") || !cookie.contains(userMatcher)) return false 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 { - save(id.toLong()) + save(id.toLong(), -1) + return true } catch (e: NumberFormatException) { //todo send report that id has changed } } + return false } - fun save(id: Long) { + fun save(id: Long, sender: Int) { L.d("New cookie found for $id") Prefs.userId = id CookieManager.getInstance().flush() - EventBus.getDefault().post(WebEvent(WebEvent.REFRESH_BASE)) - saveFbCookie(Prefs.userId, webCookie) - GlideUtils.downloadProfile(id) + val cookie = CookieModel(Prefs.userId, "", webCookie) + EventBus.getDefault().post(FbAccountEvent(cookie, sender, FbAccountEvent.FLAG_NEW)) + saveFbCookie(cookie) } //TODO reset when new account is added; reset and clear when account is logged out - fun reset() { + fun reset(loggedOut: Boolean = false, sender: Int) { Prefs.userId = Prefs.userIdDefault with(CookieManager.getInstance()) { removeAllCookies(null) flush() } + EventBus.getDefault().post(FbAccountEvent(CookieModel(), sender, if (loggedOut) FbAccountEvent.FLAG_LOGOUT else FbAccountEvent.FLAG_RESET)) } - fun switchUser(id: Long) { - val cookie = loadFbCookie(id) ?: return - Prefs.userId = id + fun switchUser(id: Long, sender: Int) = switchUser(loadFbCookie(id), sender) + + fun switchUser(name: String, sender: Int) = switchUser(loadFbCookie(name), sender) + + fun switchUser(cookie: CookieModel?, sender: Int) { + if (cookie == null) return + Prefs.userId = cookie.id dbCookie = cookie.cookie webCookie = dbCookie + EventBus.getDefault().post(FbAccountEvent(cookie, sender, FbAccountEvent.FLAG_SWITCH)) } - fun logout() { + fun logout(sender: Int) { L.d("Logging out user ${Prefs.userId}") removeCookie(Prefs.userId) - reset() + reset(true, sender) } } \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbTab.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbTab.kt new file mode 100644 index 00000000..6b4a2f35 --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbTab.kt @@ -0,0 +1,30 @@ +package com.pitchedapps.frost.facebook + +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 + +enum class FbTab(@StringRes val titleId: Int, val icon: IIcon, relativeUrl: String) { + FEED(R.string.feed, CommunityMaterial.Icon.cmd_newspaper, ""), + PROFILE(R.string.profile, CommunityMaterial.Icon.cmd_account, "me"), + EVENTS(R.string.events, GoogleMaterial.Icon.gmd_event, "events/upcoming"), + FRIENDS(R.string.friends, GoogleMaterial.Icon.gmd_people, "friends/center/requests"), + MESSAGES(R.string.messages, MaterialDesignIconic.Icon.gmi_comments, "messages?disable_interstitial=1&rdr"), + NOTIFICATIONS(R.string.notifications, MaterialDesignIconic.Icon.gmi_globe, "notifications"), + ACTIVITY_LOG(R.string.activity_log, GoogleMaterial.Icon.gmd_list, "me/allactivity"), + PAGES(R.string.pages, GoogleMaterial.Icon.gmd_flag, "pages"), + GROUPS(R.string.groups, GoogleMaterial.Icon.gmd_group, "groups"), + SAVED(R.string.saved, GoogleMaterial.Icon.gmd_bookmark, "saved"), + BIRTHDAYS(R.string.birthdays, GoogleMaterial.Icon.gmd_cake, "events/birthdays"), + CHAT(R.string.chat, GoogleMaterial.Icon.gmd_chat, "buddylist"), + PHOTOS(R.string.photos, GoogleMaterial.Icon.gmd_photo, "me/photos"), + ; + + val url = "$FB_URL_BASE$relativeUrl" +} + +fun defaultTabs():List = listOf(FbTab.FEED, FbTab.MESSAGES, FbTab.FRIENDS, FbTab.NOTIFICATIONS) +fun defaultDrawers():List = listOf(FbTab.ACTIVITY_LOG, FbTab.PAGES, FbTab.GROUPS, FbTab.SAVED) \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbTabs.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbTabs.kt deleted file mode 100644 index d391d20f..00000000 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbTabs.kt +++ /dev/null @@ -1,24 +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 - -enum class FbTab(@StringRes val titleId: Int, val icon: IIcon, relativeUrl: 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, ""), - PROFILE(R.string.profile, CommunityMaterial.Icon.cmd_account, "me"), - EVENTS(R.string.events, GoogleMaterial.Icon.gmd_event, "events/upcoming"), - FRIENDS(R.string.friends, GoogleMaterial.Icon.gmd_people, "friends/center/requests"), - MESSAGES(R.string.messages, MaterialDesignIconic.Icon.gmi_comments, "messages?disable_interstitial=1&rdr"), - NOTIFICATIONS(R.string.notifications, MaterialDesignIconic.Icon.gmi_globe, "notifications"); - - val url = "$FB_URL_BASE$relativeUrl" -} - -const val FACEBOOK_COM = "facebook.com" -const val FB_URL_BASE = "https://m.facebook.com/" \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/UsernameFetcher.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/UsernameFetcher.kt new file mode 100644 index 00000000..da244ba3 --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/UsernameFetcher.kt @@ -0,0 +1,36 @@ +package com.pitchedapps.frost.facebook + +import com.pitchedapps.frost.dbflow.CookieModel +import com.pitchedapps.frost.dbflow.saveFbCookie +import com.pitchedapps.frost.events.FbAccountEvent +import com.pitchedapps.frost.utils.L +import org.greenrobot.eventbus.EventBus +import org.jsoup.Jsoup +import kotlin.concurrent.thread + +/** + * Created by Allan Wang on 2017-06-02. + */ +object UsernameFetcher { + + fun fetch(data: CookieModel, sender: Int) { + thread { + try { + val title = Jsoup.connect(FbTab.PROFILE.url) + .cookie(FACEBOOK_COM, data.cookie) + .get().title() + L.d("User name found: $title") + data.name = title + } catch (e: Exception) { + L.e("User name fetching failed: ${e.message}") + data.name = "" + } finally { + if (data.name != null) { + saveFbCookie(data) + EventBus.getDefault().post(FbAccountEvent(data, sender, FbAccountEvent.FLAG_USER_NAME)) + } + } + } + } + +} \ No newline at end of file -- cgit v1.2.3