From 0dfc1b3e6542b9deca6c56236b46e71e4c6976f5 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sun, 23 Feb 2020 16:36:31 -0800 Subject: Remove singleton pattern for fbcookie --- .../kotlin/com/pitchedapps/frost/FrostTestApp.kt | 42 ++++++++++++++++++++++ .../main/kotlin/com/pitchedapps/frost/FrostApp.kt | 4 ++- .../kotlin/com/pitchedapps/frost/StartActivity.kt | 6 ++-- .../pitchedapps/frost/activities/BaseActivity.kt | 2 ++ .../frost/activities/BaseMainActivity.kt | 18 +++++----- .../pitchedapps/frost/activities/LoginActivity.kt | 2 +- .../frost/activities/SelectorActivity.kt | 2 +- .../frost/activities/SettingsActivity.kt | 2 ++ .../frost/activities/WebOverlayActivity.kt | 2 +- .../com/pitchedapps/frost/facebook/FbCookie.kt | 22 ++++++------ .../pitchedapps/frost/fragments/FragmentBase.kt | 2 ++ .../frost/fragments/RecyclerFragmentBase.kt | 2 +- .../frost/fragments/RecyclerFragments.kt | 2 +- .../com/pitchedapps/frost/glide/GlideUtils.kt | 9 +++-- .../com/pitchedapps/frost/iitems/GenericIItems.kt | 9 ++--- .../pitchedapps/frost/iitems/NotificationIItem.kt | 5 +-- .../kotlin/com/pitchedapps/frost/settings/Debug.kt | 5 +-- .../kotlin/com/pitchedapps/frost/utils/Utils.kt | 14 ++++---- .../com/pitchedapps/frost/utils/WebContextMenu.kt | 17 ++++----- .../com/pitchedapps/frost/views/FrostWebView.kt | 6 ++-- .../kotlin/com/pitchedapps/frost/web/FrostJSI.kt | 10 ++++-- .../frost/web/FrostUrlOverlayValidator.kt | 8 ++--- .../pitchedapps/frost/web/FrostWebViewClients.kt | 3 +- .../com/pitchedapps/frost/web/LoginWebView.kt | 3 +- 24 files changed, 133 insertions(+), 64 deletions(-) create mode 100644 app/src/androidTest/kotlin/com/pitchedapps/frost/FrostTestApp.kt (limited to 'app/src') diff --git a/app/src/androidTest/kotlin/com/pitchedapps/frost/FrostTestApp.kt b/app/src/androidTest/kotlin/com/pitchedapps/frost/FrostTestApp.kt new file mode 100644 index 00000000..528ff2a4 --- /dev/null +++ b/app/src/androidTest/kotlin/com/pitchedapps/frost/FrostTestApp.kt @@ -0,0 +1,42 @@ +package com.pitchedapps.frost + +import android.app.Application +import ca.allanwang.kau.kpref.KPrefFactory +import ca.allanwang.kau.kpref.KPrefFactoryInMemory +import com.pitchedapps.frost.db.FrostDatabase +import com.pitchedapps.frost.facebook.FbCookie +import com.pitchedapps.frost.utils.Prefs +import com.pitchedapps.frost.utils.Showcase +import org.koin.android.ext.koin.androidContext +import org.koin.android.ext.koin.androidLogger +import org.koin.core.context.startKoin +import org.koin.core.module.Module +import org.koin.dsl.module + +class FrostTestApp : Application() { + + override fun onCreate() { + super.onCreate() + startKoin { + androidLogger() + androidContext(this@FrostTestApp) + modules( + listOf( + FrostDatabase.module(), + prefFactoryModule(), + Prefs.module(), + Showcase.module(), + FbCookie.module() + ) + ) + } + } + + companion object { + fun prefFactoryModule(): Module = module { + single { + KPrefFactoryInMemory + } + } + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/FrostApp.kt b/app/src/main/kotlin/com/pitchedapps/frost/FrostApp.kt index 3bf07ac9..f7582f4f 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/FrostApp.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/FrostApp.kt @@ -26,6 +26,7 @@ import ca.allanwang.kau.utils.buildIsLollipopAndUp import com.bugsnag.android.Bugsnag import com.bugsnag.android.Configuration import com.pitchedapps.frost.db.FrostDatabase +import com.pitchedapps.frost.facebook.FbCookie import com.pitchedapps.frost.services.scheduleNotificationsFromPrefs import com.pitchedapps.frost.services.setupNotificationChannels import com.pitchedapps.frost.utils.BuildUtils @@ -60,7 +61,8 @@ class FrostApp : Application(), KoinComponent { FrostDatabase.module(), prefFactoryModule(), Prefs.module(), - Showcase.module() + Showcase.module(), + FbCookie.module() )) } if (!buildIsLollipopAndUp) { // not supported diff --git a/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt index c7d9d941..aa87483f 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt @@ -43,15 +43,16 @@ import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.Prefs import com.pitchedapps.frost.utils.launchNewTask import com.pitchedapps.frost.utils.loadAssets -import java.util.ArrayList import kotlinx.coroutines.launch import org.koin.android.ext.android.inject +import java.util.ArrayList /** * Created by Allan Wang on 2017-05-28. */ class StartActivity : KauBaseActivity() { + private val fbCookie: FbCookie by inject() private val prefs: Prefs by inject() private val cookieDao: CookieDao by inject() private val genericDao: GenericDao by inject() @@ -68,13 +69,14 @@ class StartActivity : KauBaseActivity() { // TODO add better descriptions CookieManager.getInstance() } catch (e: Exception) { + L.e(e) { "No cookiemanager instance" } showInvalidWebView() } launch { try { val authDefer = BiometricUtils.authenticate(this@StartActivity) - FbCookie.switchBackUser() + fbCookie.switchBackUser() val cookies = ArrayList(cookieDao.selectAll()) L.i { "Cookies loaded at time ${System.currentTimeMillis()}" } L._d { diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseActivity.kt index 3c2ad844..af49cb33 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseActivity.kt @@ -21,6 +21,7 @@ import android.os.Bundle import ca.allanwang.kau.internal.KauBaseActivity import ca.allanwang.kau.searchview.SearchViewHolder import com.pitchedapps.frost.contracts.VideoViewHolder +import com.pitchedapps.frost.facebook.FbCookie import com.pitchedapps.frost.utils.Prefs import com.pitchedapps.frost.utils.setFrostTheme import org.koin.android.ext.android.inject @@ -30,6 +31,7 @@ import org.koin.android.ext.android.inject */ abstract class BaseActivity : KauBaseActivity() { + val fbCookie: FbCookie by inject() val prefs: Prefs by inject() /** diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt index b3d4001f..ab6f41ca 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt @@ -314,7 +314,7 @@ abstract class BaseMainActivity : BaseActivity(), MainActivityContract, val item = FbItem.values[it.itemId] frostEvent("Drawer Tab", "name" to item.name) drawer.closeDrawer(navigation) - launchWebOverlay(item.url) + launchWebOverlay(item.url, fbCookie) false } val navBg = prefs.bgColor.withMinAlpha(200) @@ -468,7 +468,7 @@ abstract class BaseMainActivity : BaseActivity(), MainActivityContract, val currentCookie = cookieDao.currentCookie() if (currentCookie == null) { toast(R.string.account_not_found) - FbCookie.reset() + fbCookie.reset() launchLogin(cookies(), true) } else { materialDialog { @@ -482,7 +482,7 @@ abstract class BaseMainActivity : BaseActivity(), MainActivityContract, ) positiveButton(R.string.kau_yes) { this@BaseMainActivity.launch { - FbCookie.logout(this@BaseMainActivity) + fbCookie.logout(this@BaseMainActivity) } } negativeButton(R.string.kau_no) @@ -597,7 +597,7 @@ abstract class BaseMainActivity : BaseActivity(), MainActivityContract, .into(this) setOnClickListener { if (primary) { - launchWebOverlay(FbItem.PROFILE.url) + launchWebOverlay(FbItem.PROFILE.url, fbCookie) } else { switchAccount(cookie.id) } @@ -612,7 +612,7 @@ abstract class BaseMainActivity : BaseActivity(), MainActivityContract, pendingUpdate = true closeDrawer() launch { - FbCookie.switchUser(id) + fbCookie.switchUser(id) tabsForEachView { _, view -> view.badgeText = null } refreshAll() } @@ -644,7 +644,7 @@ abstract class BaseMainActivity : BaseActivity(), MainActivityContract, if (results != null) searchView.results = results else { - val data = SearchParser.query(FbCookie.webCookie, query)?.data?.results + val data = SearchParser.query(fbCookie.webCookie, query)?.data?.results if (data != null) { val items = data.mapTo(mutableListOf(), FrostSearch::toSearchItem) if (items.isNotEmpty()) @@ -662,11 +662,11 @@ abstract class BaseMainActivity : BaseActivity(), MainActivityContract, } textDebounceInterval = 300 searchCallback = - { query, _ -> launchWebOverlay("${FbItem._SEARCH.url}/?q=$query"); true } + { query, _ -> launchWebOverlay("${FbItem._SEARCH.url}/?q=$query", fbCookie); true } closeListener = { _ -> searchViewCache.clear() } foregroundColor = prefs.textColor backgroundColor = prefs.bgColor.withMinAlpha(200) - onItemClick = { _, key, _, _ -> launchWebOverlay(key) } + onItemClick = { _, key, _, _ -> launchWebOverlay(key, fbCookie) } } } } @@ -758,7 +758,7 @@ abstract class BaseMainActivity : BaseActivity(), MainActivityContract, controlWebview?.resumeTimers() launch { val authDefer = BiometricUtils.authenticate(this@BaseMainActivity) - FbCookie.switchBackUser() + fbCookie.switchBackUser() authDefer.await() if (shouldReload && prefs.autoRefreshFeed) { refreshAll() diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/LoginActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/LoginActivity.kt index efce11c6..23a247ad 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/LoginActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/LoginActivity.kt @@ -97,7 +97,7 @@ class LoginActivity : BaseActivity() { launch { val cookie = web.loadLogin { refresh(it != 100) }.await() L.d { "Login found" } - FbCookie.save(cookie.id) + fbCookie.save(cookie.id) webFadeOut() profile.fadeIn() loadInfo(cookie) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/SelectorActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/SelectorActivity.kt index 87fb1b17..357c159d 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/SelectorActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/SelectorActivity.kt @@ -63,7 +63,7 @@ class SelectorActivity : BaseActivity() { ) { if (item.cookie == null) this@SelectorActivity.launchNewTask() else launch { - FbCookie.switchUser(item.cookie) + fbCookie.switchUser(item.cookie) launchNewTask(cookies()) } } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/SettingsActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/SettingsActivity.kt index 028f8123..55ff3fba 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/SettingsActivity.kt @@ -41,6 +41,7 @@ import com.mikepenz.iconics.typeface.library.community.material.CommunityMateria import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial import com.pitchedapps.frost.R import com.pitchedapps.frost.enums.Support +import com.pitchedapps.frost.facebook.FbCookie import com.pitchedapps.frost.settings.getAppearancePrefs import com.pitchedapps.frost.settings.getBehaviourPrefs import com.pitchedapps.frost.settings.getDebugPrefs @@ -68,6 +69,7 @@ import org.koin.android.ext.android.inject */ class SettingsActivity : KPrefActivity() { + val fbCookie: FbCookie by inject() val prefs: Prefs by inject() private var resultFlag = Activity.RESULT_CANCELED diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt index 4de8d4f9..2b321d9e 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt @@ -228,7 +228,7 @@ abstract class WebOverlayActivityBase(private val userAgent: String = USER_AGENT launch { val authDefer = BiometricUtils.authenticate(this@WebOverlayActivityBase) if (userId != prefs.userId) { - FbCookie.switchUser(userId) + fbCookie.switchUser(userId) } authDefer.await() reloadBase(true) 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 3ff24ccb..aa12fbc5 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt @@ -28,34 +28,36 @@ import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.Prefs import com.pitchedapps.frost.utils.cookies import com.pitchedapps.frost.utils.launchLogin -import kotlin.coroutines.resume -import kotlin.coroutines.suspendCoroutine import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.async import kotlinx.coroutines.awaitAll import kotlinx.coroutines.withContext -import org.koin.core.KoinComponent -import org.koin.core.inject +import org.koin.dsl.module +import kotlin.coroutines.resume +import kotlin.coroutines.suspendCoroutine /** * Created by Allan Wang on 2017-05-30. * * The following component manages all cookie transfers. */ -object FbCookie : KoinComponent { +class FbCookie(private val prefs: Prefs, private val cookieDao: CookieDao) { - const val COOKIE_DOMAIN = FB_URL_BASE + companion object { + private const val COOKIE_DOMAIN = FB_URL_BASE + + fun module() = module { + single { FbCookie(get(), get()) } + } + } /** * Retrieves the facebook cookie if it exists * Note that this is a synchronized call */ - inline val webCookie: String? + val webCookie: String? get() = CookieManager.getInstance().getCookie(COOKIE_DOMAIN) - private val prefs: Prefs by inject() - private val cookieDao: CookieDao by inject() - private suspend fun CookieManager.suspendSetWebCookie(cookie: String?): Boolean { cookie ?: return true return withContext(NonCancellable) { diff --git a/app/src/main/kotlin/com/pitchedapps/frost/fragments/FragmentBase.kt b/app/src/main/kotlin/com/pitchedapps/frost/fragments/FragmentBase.kt index e1767848..84cd60c8 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/fragments/FragmentBase.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/fragments/FragmentBase.kt @@ -32,6 +32,7 @@ import com.pitchedapps.frost.contracts.FrostContentParent import com.pitchedapps.frost.contracts.MainActivityContract import com.pitchedapps.frost.contracts.MainFabContract import com.pitchedapps.frost.enums.FeedSort +import com.pitchedapps.frost.facebook.FbCookie import com.pitchedapps.frost.facebook.FbItem import com.pitchedapps.frost.utils.ARG_URL import com.pitchedapps.frost.utils.L @@ -82,6 +83,7 @@ abstract class BaseFragment : Fragment(), CoroutineScope, KoinComponent, Fragmen } } + protected val fbCookie: FbCookie by inject() protected val prefs: Prefs by inject() open lateinit var job: Job override val coroutineContext: CoroutineContext diff --git a/app/src/main/kotlin/com/pitchedapps/frost/fragments/RecyclerFragmentBase.kt b/app/src/main/kotlin/com/pitchedapps/frost/fragments/RecyclerFragmentBase.kt index 21ba17ae..22d2aa8c 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/fragments/RecyclerFragmentBase.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/fragments/RecyclerFragmentBase.kt @@ -130,7 +130,7 @@ abstract class FrostParserFragment : override suspend fun reloadImpl(progress: (Int) -> Unit): List? = withContext(Dispatchers.IO) { progress(10) - val cookie = FbCookie.webCookie + val cookie = fbCookie.webCookie val doc = getDoc(cookie) progress(60) val response = try { diff --git a/app/src/main/kotlin/com/pitchedapps/frost/fragments/RecyclerFragments.kt b/app/src/main/kotlin/com/pitchedapps/frost/fragments/RecyclerFragments.kt index f45ffa6d..35b390c4 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/fragments/RecyclerFragments.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/fragments/RecyclerFragments.kt @@ -40,6 +40,6 @@ class NotificationFragment : FrostParserFragment response.data.notifs.map { NotificationIItem(it, response.cookie) } override fun bindImpl(recyclerView: FrostRecyclerView) { - NotificationIItem.bindEvents(adapter) + NotificationIItem.bindEvents(adapter, fbCookie) } } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/glide/GlideUtils.kt b/app/src/main/kotlin/com/pitchedapps/frost/glide/GlideUtils.kt index 537ad89c..1ae46c15 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/glide/GlideUtils.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/glide/GlideUtils.kt @@ -30,6 +30,8 @@ import com.pitchedapps.frost.facebook.FbCookie import okhttp3.Interceptor import okhttp3.OkHttpClient import okhttp3.Response +import org.koin.core.KoinComponent +import org.koin.core.inject /** * Created by Allan Wang on 28/12/17. @@ -63,10 +65,13 @@ class FrostGlideModule : AppGlideModule() { private fun getFrostHttpClient(): OkHttpClient = OkHttpClient.Builder().addInterceptor(FrostCookieInterceptor()).build() -class FrostCookieInterceptor : Interceptor { +class FrostCookieInterceptor : Interceptor, KoinComponent { + + private val fbCookie: FbCookie by inject() + override fun intercept(chain: Interceptor.Chain): Response { val origRequest = chain.request() - val cookie = FbCookie.webCookie ?: return chain.proceed(origRequest) + val cookie = fbCookie.webCookie ?: return chain.proceed(origRequest) val request = origRequest.newBuilder().addHeader("Cookie", cookie).build() return chain.proceed(request) } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/iitems/GenericIItems.kt b/app/src/main/kotlin/com/pitchedapps/frost/iitems/GenericIItems.kt index 8459fdf0..1262c078 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/iitems/GenericIItems.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/iitems/GenericIItems.kt @@ -27,6 +27,7 @@ import com.mikepenz.fastadapter.GenericItem import com.mikepenz.fastadapter.IAdapter import com.mikepenz.fastadapter.select.selectExtension import com.pitchedapps.frost.R +import com.pitchedapps.frost.facebook.FbCookie import com.pitchedapps.frost.utils.Prefs import com.pitchedapps.frost.utils.launchWebOverlay import org.koin.core.KoinComponent @@ -43,20 +44,20 @@ interface ClickableIItemContract { val url: String? - fun click(context: Context) { + fun click(context: Context, fbCookie: FbCookie) { val url = url ?: return - context.launchWebOverlay(url) + context.launchWebOverlay(url, fbCookie) } companion object { - fun bindEvents(adapter: IAdapter) { + fun bindEvents(adapter: IAdapter, fbCookie: FbCookie) { adapter.fastAdapter?.apply { selectExtension { isSelectable = false } onClickListener = { v, _, item, _ -> if (item is ClickableIItemContract) { - item.click(v!!.context) + item.click(v!!.context, fbCookie) true } else false diff --git a/app/src/main/kotlin/com/pitchedapps/frost/iitems/NotificationIItem.kt b/app/src/main/kotlin/com/pitchedapps/frost/iitems/NotificationIItem.kt index 828d2484..8624fff0 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/iitems/NotificationIItem.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/iitems/NotificationIItem.kt @@ -31,6 +31,7 @@ import com.mikepenz.fastadapter.adapters.ItemAdapter import com.mikepenz.fastadapter.diff.DiffCallback import com.mikepenz.fastadapter.select.selectExtension import com.pitchedapps.frost.R +import com.pitchedapps.frost.facebook.FbCookie import com.pitchedapps.frost.facebook.FbItem import com.pitchedapps.frost.facebook.parsers.FrostNotif import com.pitchedapps.frost.glide.FrostGlide @@ -50,7 +51,7 @@ class NotificationIItem(val notification: FrostNotif, val cookie: String) : ) { companion object { - fun bindEvents(adapter: ItemAdapter) { + fun bindEvents(adapter: ItemAdapter, fbCookie: FbCookie) { adapter.fastAdapter?.apply { selectExtension { isSelectable = false @@ -64,7 +65,7 @@ class NotificationIItem(val notification: FrostNotif, val cookie: String) : ) } // TODO temp fix. If url is dependent, we cannot load it directly - v!!.context.launchWebOverlay(if (notif.url.isIndependent) notif.url else FbItem.NOTIFICATIONS.url) + v!!.context.launchWebOverlay(if (notif.url.isIndependent) notif.url else FbItem.NOTIFICATIONS.url, fbCookie) true } } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/settings/Debug.kt b/app/src/main/kotlin/com/pitchedapps/frost/settings/Debug.kt index d250f036..6c2f4e86 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/settings/Debug.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/settings/Debug.kt @@ -87,7 +87,7 @@ fun SettingsActivity.getDebugPrefs(): KPrefAdapterBuilder.() -> Unit = { attempt = launch(Dispatchers.IO) { try { - val data = parser.parse(FbCookie.webCookie) + val data = parser.parse(fbCookie.webCookie) withMainContext { loading.dismiss() createEmail(parser, data?.data) @@ -113,7 +113,8 @@ private const val ZIP_NAME = "debug" fun SettingsActivity.sendDebug(url: String, html: String?) { val downloader = OfflineWebsite( - url, FbCookie.webCookie ?: "", + url, + cookie = fbCookie.webCookie ?: "", baseUrl = FB_URL_BASE, html = html, baseDir = DebugActivity.baseDir(this) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt index 2bd9e6c1..854f315c 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt @@ -125,14 +125,14 @@ fun Activity.cookies(): ArrayList { * Note that most requests may need to first check if the url can be launched as an overlay * See [requestWebOverlay] to verify the launch */ -private inline fun Context.launchWebOverlayImpl(url: String) { +private inline fun Context.launchWebOverlayImpl(url: String, fbCookie: FbCookie) { val prefs = Prefs.get() val argUrl = url.formattedFbUrl L.v { "Launch received: $url\nLaunch web overlay: $argUrl" } if (argUrl.isFacebookUrl && argUrl.contains("/logout.php")) { L.d { "Logout php found" } ctxCoroutine.launch { - FbCookie.logout(this@launchWebOverlayImpl) + fbCookie.logout(this@launchWebOverlayImpl) } } else if (!(prefs.linksInDefaultApp && resolveActivityForUri(Uri.parse(argUrl)))) { startActivity(false, intentBuilder = { @@ -141,13 +141,13 @@ private inline fun Context.launchWebOverlay } } -fun Context.launchWebOverlay(url: String) = launchWebOverlayImpl(url) +fun Context.launchWebOverlay(url: String, fbCookie: FbCookie) = launchWebOverlayImpl(url, fbCookie) // TODO Currently, default is overlay. Switch this if default changes -fun Context.launchWebOverlayDesktop(url: String) = launchWebOverlay(url) +fun Context.launchWebOverlayDesktop(url: String, fbCookie: FbCookie) = launchWebOverlay(url, fbCookie) -fun Context.launchWebOverlayMobile(url: String) = - launchWebOverlayImpl(url) +fun Context.launchWebOverlayMobile(url: String, fbCookie: FbCookie) = + launchWebOverlayImpl(url, fbCookie) private fun Context.fadeBundle() = ActivityOptions.makeCustomAnimation( this, @@ -409,8 +409,6 @@ fun EmailBuilder.addFrostDetails() { addItem("Locale", Locale.getDefault().displayName) } -fun frostJsoup(url: String): Document = frostJsoup(FbCookie.webCookie, url) - fun frostJsoup(cookie: String?, url: String): Document = Jsoup.connect(url).run { if (cookie.isNullOrBlank()) this diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/WebContextMenu.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/WebContextMenu.kt index 61e2c11f..46c87c96 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/WebContextMenu.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/WebContextMenu.kt @@ -25,12 +25,13 @@ import com.afollestad.materialdialogs.callbacks.onDismiss import com.afollestad.materialdialogs.list.listItems import com.pitchedapps.frost.R import com.pitchedapps.frost.activities.MainActivity +import com.pitchedapps.frost.facebook.FbCookie import com.pitchedapps.frost.facebook.formattedFbUrl /** * Created by Allan Wang on 2017-07-07. */ -fun Context.showWebContextMenu(wc: WebContext) { +fun Context.showWebContextMenu(wc: WebContext, fbCookie: FbCookie) { if (wc.isEmpty) return var title = wc.url ?: string(R.string.menu) title = @@ -43,7 +44,7 @@ fun Context.showWebContextMenu(wc: WebContext) { materialDialog { title(text = title) listItems(items = menuItems.map { string(it.textId) }) { _, position, _ -> - menuItems[position].onClick(this@showWebContextMenu, wc) + menuItems[position].onClick(this@showWebContextMenu, wc, fbCookie) } onDismiss { // showing the dialog interrupts the touch down event, so we must ensure that the viewpager's swipe is enabled @@ -65,16 +66,16 @@ class WebContext(val unformattedUrl: String?, val text: String?) { enum class WebContextType( val textId: Int, val constraint: (wc: WebContext) -> Boolean, - val onClick: (c: Context, wc: WebContext) -> Unit + val onClick: (c: Context, wc: WebContext, fc: FbCookie) -> Unit ) { OPEN_LINK( R.string.open_link, { it.hasUrl }, - { c, wc -> c.launchWebOverlay(wc.url!!) }), - COPY_LINK(R.string.copy_link, { it.hasUrl }, { c, wc -> c.copyToClipboard(wc.url) }), - COPY_TEXT(R.string.copy_text, { it.hasText }, { c, wc -> c.copyToClipboard(wc.text) }), - SHARE_LINK(R.string.share_link, { it.hasUrl }, { c, wc -> c.shareText(wc.url) }), - DEBUG_LINK(R.string.debug_link, { it.hasUrl }, { c, wc -> + { c, wc , fc -> c.launchWebOverlay(wc.url!!, fc) }), + COPY_LINK(R.string.copy_link, { it.hasUrl }, { c, wc , fc-> c.copyToClipboard(wc.url) }), + COPY_TEXT(R.string.copy_text, { it.hasText }, { c, wc , fc-> c.copyToClipboard(wc.text) }), + SHARE_LINK(R.string.share_link, { it.hasUrl }, { c, wc, fc -> c.shareText(wc.url) }), + DEBUG_LINK(R.string.debug_link, { it.hasUrl }, { c, wc, fc -> c.materialDialog { title(R.string.debug_link) message(R.string.debug_link_desc) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt index 3b1fc3ca..b02612e3 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt @@ -31,6 +31,7 @@ import com.pitchedapps.frost.contracts.FrostContentParent import com.pitchedapps.frost.db.FrostDatabase import com.pitchedapps.frost.db.currentCookie import com.pitchedapps.frost.facebook.FB_HOME_URL +import com.pitchedapps.frost.facebook.FbCookie import com.pitchedapps.frost.facebook.USER_AGENT import com.pitchedapps.frost.fragments.WebFragment import com.pitchedapps.frost.utils.L @@ -41,11 +42,11 @@ import com.pitchedapps.frost.web.FrostChromeClient import com.pitchedapps.frost.web.FrostJSI import com.pitchedapps.frost.web.FrostWebViewClient import com.pitchedapps.frost.web.NestedWebView +import org.koin.core.KoinComponent +import org.koin.core.inject import kotlin.math.abs import kotlin.math.max import kotlin.math.min -import org.koin.core.KoinComponent -import org.koin.core.inject /** * Created by Allan Wang on 2017-05-29. @@ -59,6 +60,7 @@ class FrostWebView @JvmOverloads constructor( FrostContentCore, KoinComponent { + val fbCookie: FbCookie by inject() val prefs: Prefs by inject() override fun reload(animate: Boolean) { diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt index 449f0ab0..9fe4c4c9 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt @@ -35,13 +35,17 @@ import com.pitchedapps.frost.utils.showWebContextMenu import com.pitchedapps.frost.views.FrostWebView import kotlinx.coroutines.channels.SendChannel import kotlinx.coroutines.launch +import org.koin.android.ext.android.inject +import org.koin.core.KoinComponent +import org.koin.core.inject /** * Created by Allan Wang on 2017-06-01. */ class FrostJSI(val web: FrostWebView) { - private val prefs: Prefs = web.prefs + private val fbCookie: FbCookie get() = web.fbCookie + private val prefs: Prefs get() = web.prefs private val context: Context = web.context private val activity: MainActivity? = context as? MainActivity private val header: SendChannel? = activity?.headerBadgeChannel @@ -80,7 +84,7 @@ class FrostJSI(val web: FrostWebView) { @JavascriptInterface fun contextMenu(url: String?, text: String?) { // url will be formatted through webcontext - web.post { context.showWebContextMenu(WebContext(url.takeIf { it.isIndependent }, text)) } + web.post { context.showWebContextMenu(WebContext(url.takeIf { it.isIndependent }, text), fbCookie) } } /** @@ -114,7 +118,7 @@ class FrostJSI(val web: FrostWebView) { fun loadLogin() { L.d { "Sign up button found; load login" } context.ctxCoroutine.launch { - FbCookie.logout(context) + fbCookie.logout(context) } } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostUrlOverlayValidator.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostUrlOverlayValidator.kt index d33b51f6..c54f9822 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostUrlOverlayValidator.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostUrlOverlayValidator.kt @@ -68,7 +68,7 @@ fun FrostWebView.requestWebOverlay(url: String): Boolean { } if (url.isIndirectImageUrl) { L.d { "Found indirect fb image" } - context.launchImageActivity(url, cookie = FbCookie.webCookie) + context.launchImageActivity(url, cookie = fbCookie.webCookie) return true } if (!url.isIndependent) { @@ -81,19 +81,19 @@ fun FrostWebView.requestWebOverlay(url: String): Boolean { // already overlay; manage user agent if (userAgentString != USER_AGENT_DESKTOP_CONST && shouldUseDesktop) { L._i { "Switch to desktop agent overlay" } - context.launchWebOverlayDesktop(url) + context.launchWebOverlayDesktop(url, fbCookie) return true } if (userAgentString == USER_AGENT_DESKTOP_CONST && !shouldUseDesktop) { L._i { "Switch from desktop agent" } - context.launchWebOverlayMobile(url) + context.launchWebOverlayMobile(url, fbCookie) return true } L._i { "return false switch" } return false } L.v { "Request web overlay passed" } - context.launchWebOverlay(url) + context.launchWebOverlay(url, fbCookie) return true } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClients.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClients.kt index a931b2ed..a3c61323 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClients.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClients.kt @@ -67,6 +67,7 @@ open class BaseWebViewClient : WebViewClient() { */ open class FrostWebViewClient(val web: FrostWebView) : BaseWebViewClient() { + private val fbCookie: FbCookie get() = web.fbCookie private val prefs: Prefs get() = web.prefs private val refresh: SendChannel = web.parent.refreshChannel private val isMain = web.parent.baseEnum != null @@ -205,7 +206,7 @@ open class FrostWebViewClient(val web: FrostWebView) : BaseWebViewClient() { return launchRequest(request) } if (url.isIndirectImageUrl) { - return launchImage(url.formattedFbUrl, cookie = FbCookie.webCookie) + return launchImage(url.formattedFbUrl, cookie = fbCookie.webCookie) } if (url.isImageUrl) { return launchImage(url.formattedFbUrl) 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 0299ea77..37e06db9 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt @@ -54,6 +54,7 @@ class LoginWebView @JvmOverloads constructor( defStyleAttr: Int = 0 ) : WebView(context, attrs, defStyleAttr), KoinComponent { + private val fbCookie: FbCookie by inject() private val prefs: Prefs by inject() private val completable: CompletableDeferred = CompletableDeferred() private lateinit var progressCallback: (Int) -> Unit @@ -72,7 +73,7 @@ class LoginWebView @JvmOverloads constructor( this@LoginWebView.progressCallback = progressCallback L.d { "Begin loading login" } launchMain { - FbCookie.reset() + fbCookie.reset() setupWebview() loadUrl(FB_LOGIN_URL) } -- cgit v1.2.3