From 0f8f6645339d2a1997b9b444f33ee156a91c2010 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sat, 3 Jun 2017 14:27:04 -0700 Subject: remap login cookies --- .../kotlin/com/pitchedapps/frost/LoginActivity.kt | 34 +++++++--------------- .../com/pitchedapps/frost/dbflow/CookiesDb.kt | 3 +- .../com/pitchedapps/frost/events/WebLaunchEvent.kt | 3 +- .../kotlin/com/pitchedapps/frost/facebook/FbTab.kt | 2 +- .../com/pitchedapps/frost/utils/ContextUtils.kt | 6 ++-- .../pitchedapps/frost/web/FrostWebViewClient.kt | 5 ++-- .../com/pitchedapps/frost/web/LoginWebView.kt | 2 +- 7 files changed, 23 insertions(+), 32 deletions(-) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/LoginActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/LoginActivity.kt index 8fca271a..fcda7fb4 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/LoginActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/LoginActivity.kt @@ -1,13 +1,8 @@ package com.pitchedapps.frost -import android.app.Activity -import android.content.Context -import android.content.Intent import android.graphics.drawable.Drawable import android.os.Bundle import android.os.Handler -import android.support.v4.app.ActivityOptionsCompat -import android.support.v4.content.ContextCompat import android.support.v4.widget.SwipeRefreshLayout import android.support.v7.app.AppCompatActivity import android.support.v7.widget.AppCompatTextView @@ -20,13 +15,13 @@ import com.bumptech.glide.load.engine.GlideException import com.bumptech.glide.request.RequestListener import com.bumptech.glide.request.target.Target import com.pitchedapps.frost.dbflow.CookieModel +import com.pitchedapps.frost.dbflow.loadFbCookiesAsync import com.pitchedapps.frost.dbflow.saveFbCookie import com.pitchedapps.frost.facebook.FACEBOOK_COM import com.pitchedapps.frost.facebook.FbTab import com.pitchedapps.frost.facebook.PROFILE_PICTURE_URL import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.bindView -import com.pitchedapps.frost.utils.cookies import com.pitchedapps.frost.utils.launchNewTask import com.pitchedapps.frost.views.fadeIn import com.pitchedapps.frost.views.fadeOut @@ -58,18 +53,6 @@ class LoginActivity : AppCompatActivity() { val profileObservable = SingleSubject.create()!! val usernameObservable = SingleSubject.create()!! - companion object { - const val EXTRA_COOKIES = "extra_cookies" - fun newInstance(context: Context, cookies: ArrayList = arrayListOf()) { - val intent = Intent(context, LoginActivity::class.java) - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) - intent.putExtra(EXTRA_COOKIES, cookies) - val bundle = ActivityOptionsCompat.makeCustomAnimation(context, R.anim.slide_in_right, R.anim.slide_out_right).toBundle() - ContextCompat.startActivity(context, intent, bundle) - if (context is Activity) context.finish() - } - } - // Helper to set and enable swipeRefresh var refresh: Boolean get() = swipeRefresh.isRefreshing @@ -108,11 +91,16 @@ class LoginActivity : AppCompatActivity() { L.d("Zip done") if (!foundImage) L.e("Could not get profile photo; Invalid id?\n\t$cookie") textview.setTextWithFade(String.format(getString(R.string.welcome), name), duration = 500) - Handler().postDelayed({ - val cookies = cookies() - cookies.add(cookie) - launchNewTask(MainActivity::class.java, cookies) - }, 1000) + /* + * The user may have logged into an account that is already in the database + * We will let the db handle duplicates and load it now after the new account has been saved + */ + loadFbCookiesAsync { + cookies -> + Handler().postDelayed({ + launchNewTask(MainActivity::class.java, ArrayList(cookies)) + }, 1000) + } } loadProfile(cookie.id) loadUsername(cookie) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt b/app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt index bb5b0115..b2f29656 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt @@ -40,9 +40,10 @@ fun loadFbCookiesAsync(callback: (cookies: List) -> Unit) { (select from CookieModel::class).async().queryListResultCallback { _, tResult -> callback.invoke(tResult) }.execute() } -fun saveFbCookie(cookie: CookieModel) { +fun saveFbCookie(cookie: CookieModel, callback: (() -> Unit)? = null) { cookie.async save { L.d("Fb cookie $cookie saved") + callback?.invoke() } } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/events/WebLaunchEvent.kt b/app/src/main/kotlin/com/pitchedapps/frost/events/WebLaunchEvent.kt index e32c01ae..13a8187a 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/events/WebLaunchEvent.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/events/WebLaunchEvent.kt @@ -2,10 +2,11 @@ package com.pitchedapps.frost.events import android.content.Context import com.pitchedapps.frost.WebOverlayActivity +import com.pitchedapps.frost.utils.launchWebOverlay /** * Created by Allan Wang on 2017-06-01. */ class WebLaunchEvent(val url: String) { - fun execute(context: Context) = WebOverlayActivity.newInstance(context, url) + fun execute(context: Context) = context.launchWebOverlay(url) } \ 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 index 6b4a2f35..c34a5cbe 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbTab.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbTab.kt @@ -8,7 +8,7 @@ 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, ""), + FEED(R.string.feed, CommunityMaterial.Icon.cmd_newspaper, "feed"), 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"), diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt index 1f0eb5d3..31055c4e 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt @@ -16,16 +16,16 @@ import com.pitchedapps.frost.facebook.FbTab private const val EXTRA_COOKIES = "extra_cookies" private const val ARG_URL = "arg_url" -fun Activity.launchNewTask(clazz: Class, cookieList: ArrayList = arrayListOf()) { +fun Context.launchNewTask(clazz: Class, cookieList: ArrayList = arrayListOf()) { val intent = (Intent(this, clazz)) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) intent.putParcelableArrayListExtra(EXTRA_COOKIES, cookieList) startActivity(intent) - finish() + if (this is Activity) finish() } fun Activity.cookies(): ArrayList { - return intent.extras?.getParcelableArrayList(EXTRA_COOKIES) ?: arrayListOf() + return intent?.extras?.getParcelableArrayList(EXTRA_COOKIES) ?: arrayListOf() } fun Context.launchWebOverlay(url: String) { diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClient.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClient.kt index 03bb5a60..70129429 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClient.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClient.kt @@ -9,6 +9,7 @@ import com.pitchedapps.frost.facebook.FbCookie import com.pitchedapps.frost.injectors.CssAssets import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.Prefs +import com.pitchedapps.frost.utils.launchNewTask import com.pitchedapps.frost.views.circularReveal import com.pitchedapps.frost.views.fadeOut @@ -32,10 +33,10 @@ class FrostWebViewClient(val position: () -> Int) : WebViewClient() { if (!url.contains(FACEBOOK_COM)) return if (url.contains("logout.php")) { FbCookie.logout(Prefs.userId) - LoginActivity.newInstance(view.context) + view.context.launchNewTask(LoginActivity::class.java) } else if (url.contains("login.php")) { FbCookie.reset() - LoginActivity.newInstance(view.context) + view.context.launchNewTask(LoginActivity::class.java) } view.fadeOut(duration = 200L) } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt index aa40f1f1..d7c16a20 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt @@ -81,11 +81,11 @@ class LoginWebView @JvmOverloads constructor( loadLogin() return } + cookieObservable.onNext(Pair(url, CookieManager.getInstance().getCookie(url))) CssAssets.LOGIN.inject(view, { if (view.visibility == View.INVISIBLE) view.circularReveal(offset = 150L) }) - cookieObservable.onNext(Pair(url, CookieManager.getInstance().getCookie(url))) } } -- cgit v1.2.3