From 067ea15188f20fa268255153e35c2df732fdffee Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 6 Jun 2017 19:29:25 -0700 Subject: Clean up injectors and events --- .../main/kotlin/com/pitchedapps/frost/FrostApp.kt | 6 +- .../kotlin/com/pitchedapps/frost/MainActivity.kt | 6 +- .../com/pitchedapps/frost/SelectorActivity.kt | 17 +++++- .../com/pitchedapps/frost/events/FbAccountEvent.kt | 65 ---------------------- .../com/pitchedapps/frost/events/WebLaunchEvent.kt | 11 ---- .../com/pitchedapps/frost/facebook/FbConst.kt | 1 + .../com/pitchedapps/frost/facebook/FbCookie.kt | 53 +++++++++++++----- .../kotlin/com/pitchedapps/frost/facebook/FbTab.kt | 2 +- .../com/pitchedapps/frost/fragments/WebFragment.kt | 22 +++++++- .../frost/glide/CircleTransformation.kt | 19 ------- .../com/pitchedapps/frost/injectors/CssAssets.kt | 6 +- .../com/pitchedapps/frost/injectors/JsInjector.kt | 52 +++-------------- .../com/pitchedapps/frost/utils/ContextUtils.kt | 3 +- .../com/pitchedapps/frost/views/AccountItem.kt | 17 +++--- .../com/pitchedapps/frost/web/FrostChromeClient.kt | 5 +- .../kotlin/com/pitchedapps/frost/web/FrostJSI.kt | 17 +++++- .../pitchedapps/frost/web/FrostWebViewClient.kt | 28 ++++++---- .../com/pitchedapps/frost/web/FrostWebViewCore.kt | 3 +- .../com/pitchedapps/frost/web/LoginWebView.kt | 33 +++++------ 19 files changed, 156 insertions(+), 210 deletions(-) delete mode 100644 app/src/main/kotlin/com/pitchedapps/frost/events/FbAccountEvent.kt delete mode 100644 app/src/main/kotlin/com/pitchedapps/frost/events/WebLaunchEvent.kt delete mode 100644 app/src/main/kotlin/com/pitchedapps/frost/glide/CircleTransformation.kt (limited to 'app') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/FrostApp.kt b/app/src/main/kotlin/com/pitchedapps/frost/FrostApp.kt index 1e9fe6d3..625b419b 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/FrostApp.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/FrostApp.kt @@ -33,11 +33,11 @@ class FrostApp : Application() { // fun refWatcher(c: Context) = (c.applicationContext as FrostApp).refWatcher // } - lateinit var refWatcher: RefWatcher +// lateinit var refWatcher: RefWatcher override fun onCreate() { - if (LeakCanary.isInAnalyzerProcess(this)) return - refWatcher = LeakCanary.install(this) +// if (LeakCanary.isInAnalyzerProcess(this)) return +// refWatcher = LeakCanary.install(this) if (BuildConfig.DEBUG) { Timber.plant(DebugTree()) // LeakCanary.enableDisplayLeakActivity(this) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt index 0e5c9915..649a8941 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt @@ -20,7 +20,6 @@ import co.zsmb.materialdrawerkt.draweritems.badgeable.primaryItem import co.zsmb.materialdrawerkt.draweritems.profile.profile import com.mikepenz.materialdrawer.AccountHeader import com.mikepenz.materialdrawer.Drawer -import com.pitchedapps.frost.dbflow.CookieModel import com.pitchedapps.frost.dbflow.loadFbTabs import com.pitchedapps.frost.dbflow.saveAsync import com.pitchedapps.frost.facebook.FbCookie.switchUser @@ -28,7 +27,6 @@ import com.pitchedapps.frost.facebook.FbTab import com.pitchedapps.frost.facebook.PROFILE_PICTURE_URL import com.pitchedapps.frost.fragments.WebFragment import com.pitchedapps.frost.utils.* -import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.Disposable import io.reactivex.subjects.PublishSubject @@ -42,7 +40,7 @@ class MainActivity : AppCompatActivity() { lateinit var drawer: Drawer lateinit var drawerHeader: AccountHeader var titleDisposable: Disposable? = null - var refreshObservable = PublishSubject.create() + var refreshObservable = PublishSubject.create()!! override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -115,7 +113,7 @@ class MainActivity : AppCompatActivity() { } onProfileChanged { _, profile, current -> if (current) launchWebOverlay(FbTab.PROFILE.url) - else switchUser(profile.name.text) + else switchUser(profile.identifier, { refreshObservable.onNext(true) }) false } } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/SelectorActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/SelectorActivity.kt index f293c3d3..12a165ad 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/SelectorActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/SelectorActivity.kt @@ -4,11 +4,16 @@ import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.support.v7.widget.GridLayoutManager import android.support.v7.widget.RecyclerView +import android.view.View import butterknife.ButterKnife +import com.mikepenz.fastadapter.FastAdapter import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter +import com.mikepenz.fastadapter.listeners.ClickEventHook +import com.pitchedapps.frost.facebook.FbCookie 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.AccountItem /** @@ -25,8 +30,16 @@ class SelectorActivity : AppCompatActivity() { ButterKnife.bind(this) recycler.layoutManager = GridLayoutManager(this, 2) recycler.adapter = adapter - L.d("Selector ${cookies()}") adapter.add(cookies().map { AccountItem(it) }) - adapter.add(AccountItem()) // add account + adapter.add(AccountItem(null)) // add account + adapter.withItemEvent(object : ClickEventHook() { + override fun onBind(viewHolder: RecyclerView.ViewHolder): View? + = if (viewHolder is AccountItem.ViewHolder) viewHolder.v else null + + override fun onClick(v: View, position: Int, fastAdapter: FastAdapter, item: AccountItem) { + if (item.cookie == null) this@SelectorActivity.launchNewTask(LoginActivity::class.java) + else FbCookie.switchUser(item.cookie, { launchNewTask(MainActivity::class.java, cookies()) }) + } + }) } } \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/events/FbAccountEvent.kt b/app/src/main/kotlin/com/pitchedapps/frost/events/FbAccountEvent.kt deleted file mode 100644 index 051407cf..00000000 --- a/app/src/main/kotlin/com/pitchedapps/frost/events/FbAccountEvent.kt +++ /dev/null @@ -1,65 +0,0 @@ -package com.pitchedapps.frost.events - -import com.mikepenz.materialdrawer.AccountHeader -import com.mikepenz.materialdrawer.model.ProfileDrawerItem -import com.pitchedapps.frost.dbflow.CookieModel -import com.pitchedapps.frost.facebook.PROFILE_PICTURE_URL -import com.pitchedapps.frost.utils.L -import com.pitchedapps.frost.web.FrostWebViewCore - -/** - * Created by Allan Wang on 2017-06-02. - * - * An emitter for whenever a change occurs relating to the active facebook account - * All subscribers will call one of the execute methods below so the logic is handled within this class - * [data] [CookieModel] content - * [sender] Webview position that sent the event; or -1 otherwise - * [flag] See companion object - */ -class FbAccountEvent(val data: CookieModel, val sender: Int, val flag: Int) { - - init { - L.d(toString()) - } - - companion object { - const val FLAG_LOGOUT = -2 - const val FLAG_RESET = -1 - const val FLAG_NEW = 0 - const val FLAG_SWITCH = 1 - const val FLAG_USER_NAME = 2 - } - - fun execute(webView: FrostWebViewCore) { -// if (sender != -1 && sender == webView.position) return -// when (flag) { -// FLAG_LOGOUT, FLAG_RESET, FLAG_NEW, FLAG_SWITCH -> webView.loadBaseUrl() -// } - } - - /** - * If new user id is found; create an account header and fetch the username - * If the username is found and the current account is nameless, set the name - * Ignore other flags - */ - fun execute(accountHeader: AccountHeader) { - when (flag) { - FLAG_NEW -> { - val profile = ProfileDrawerItem() - .withName(data.name) - .withIcon(PROFILE_PICTURE_URL(data.id)) - accountHeader.addProfile(profile, 0) - accountHeader.setActiveProfile(profile, true) -// if (data.name == null) -// UsernameFetcher.fetch(data, sender) - } - FLAG_USER_NAME -> { - if (accountHeader.activeProfile.name == null) - accountHeader.activeProfile.withName(data.name) - } - FLAG_LOGOUT -> { - - } - } - } -} \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/events/WebLaunchEvent.kt b/app/src/main/kotlin/com/pitchedapps/frost/events/WebLaunchEvent.kt deleted file mode 100644 index 5ba35237..00000000 --- a/app/src/main/kotlin/com/pitchedapps/frost/events/WebLaunchEvent.kt +++ /dev/null @@ -1,11 +0,0 @@ -package com.pitchedapps.frost.events - -import android.content.Context -import com.pitchedapps.frost.utils.launchWebOverlay - -/** - * Created by Allan Wang on 2017-06-01. - */ -class WebLaunchEvent(val url: String) { - fun execute(context: Context) = context.launchWebOverlay(url) -} \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbConst.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbConst.kt index 59d76954..0a7041de 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbConst.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbConst.kt @@ -3,6 +3,7 @@ package com.pitchedapps.frost.facebook /** * Created by Allan Wang on 2017-06-01. */ +const val HTTPS_FACEBOOK_COM = "https://facebook.com" 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 2146ae26..2d46f36a 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt @@ -7,25 +7,46 @@ import com.pitchedapps.frost.dbflow.removeCookie import com.pitchedapps.frost.dbflow.saveFbCookie import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.Prefs +import io.reactivex.Observable +import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.subjects.SingleSubject /** * Created by Allan Wang on 2017-05-30. */ object FbCookie { - var webCookie: String? + val webCookie: String? get() = CookieManager.getInstance().getCookie(FB_URL_BASE) - set(value) { - CookieManager.getInstance().setCookie(FB_URL_BASE, value) - CookieManager.getInstance().flush() + + fun setWebCookie(cookie: String?, callback: (() -> Unit)?) { + with(CookieManager.getInstance()) { + removeAllCookies({ + if (cookie == null) { + callback?.invoke() + return@removeAllCookies + } + L.d("Setting cookie to $cookie") + val cookies = cookie.split(";").map { Pair(it, SingleSubject.create()) } + cookies.forEach { (cookie, callback) -> setCookie(FB_URL_BASE, cookie, { callback.onSuccess(it) }) } + Observable.zip(cookies.map { (_, callback) -> callback.toObservable() }, {}).subscribeOn(AndroidSchedulers.mainThread()).subscribe({ + callback?.invoke() + L.d("Cookies set: $webCookie") + flush() + }) + }) } + } operator fun invoke() { L.d("User ${Prefs.userId}") + with(CookieManager.getInstance()) { + setAcceptCookie(true) + } val dbCookie = loadFbCookie(Prefs.userId)?.cookie if (dbCookie != null && webCookie == null) { L.d("DbCookie found & WebCookie is null; setting webcookie") - webCookie = dbCookie + setWebCookie(dbCookie, null) } } @@ -37,28 +58,30 @@ object FbCookie { saveFbCookie(cookie) } - fun reset() { + fun reset(callback: () -> Unit) { Prefs.userId = Prefs.userIdDefault with(CookieManager.getInstance()) { - removeAllCookies(null) - flush() + removeAllCookies({ + flush() + callback.invoke() + }) } } - fun switchUser(id: Long) = switchUser(loadFbCookie(id)) + fun switchUser(id: Long, callback: () -> Unit) = switchUser(loadFbCookie(id), callback) - fun switchUser(name: String) = switchUser(loadFbCookie(name)) + fun switchUser(name: String, callback: () -> Unit) = switchUser(loadFbCookie(name), callback) - fun switchUser(cookie: CookieModel?) { + fun switchUser(cookie: CookieModel?, callback: () -> Unit) { if (cookie == null) return + L.d("Switching user to $cookie") Prefs.userId = cookie.id - webCookie = cookie.cookie - //TODO add webview refresh event + setWebCookie(cookie.cookie, callback) } - fun logout(id:Long) { + fun logout(id: Long, callback: () -> Unit) { L.d("Logging out user $id") removeCookie(id) - reset() + reset(callback) } } \ 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 ba595276..17dd4862 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbTab.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbTab.kt @@ -14,7 +14,7 @@ enum class FbTab(@StringRes val titleId: Int, val icon: IIcon, relativeUrl: Stri 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"), + MESSAGES(R.string.messages, MaterialDesignIconic.Icon.gmi_comments, "messages"), 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"), diff --git a/app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt b/app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt index 0542722f..2df3ae6f 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt @@ -1,5 +1,6 @@ package com.pitchedapps.frost.fragments +import android.annotation.SuppressLint import android.content.Context import android.os.Bundle import android.support.v4.app.Fragment @@ -7,7 +8,6 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import com.pitchedapps.frost.MainActivity -import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.putString import com.pitchedapps.frost.web.FrostWebView import com.pitchedapps.frost.web.FrostWebViewCore @@ -75,9 +75,27 @@ class WebFragment : Fragment() { override fun onDetach() { refreshDisposable?.dispose() - L.d("F Detatch") super.onDetach() } + @SuppressLint("SetJavaScriptEnabled") + override fun onResume() { + super.onResume() + pauseLoad = false + firstLoad() + } + + override fun onPause() { + pauseLoad = true + super.onPause() + } + + var pauseLoad: Boolean + get() = web.settings.blockNetworkLoads + set(value) { + web.settings.blockNetworkLoads = value + } + + fun onBackPressed() = frostWebView.onBackPressed() } \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/glide/CircleTransformation.kt b/app/src/main/kotlin/com/pitchedapps/frost/glide/CircleTransformation.kt deleted file mode 100644 index c37d7487..00000000 --- a/app/src/main/kotlin/com/pitchedapps/frost/glide/CircleTransformation.kt +++ /dev/null @@ -1,19 +0,0 @@ -package com.pitchedapps.frost.glide - -import android.graphics.Bitmap -import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool -import com.bumptech.glide.load.resource.bitmap.BitmapTransformation -import java.security.MessageDigest - -/** - * Created by Allan Wang on 2017-06-06. - */ -class CircleTransformation: BitmapTransformation() { - override fun updateDiskCacheKey(messageDigest: MessageDigest?) { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. - } - - override fun transform(pool: BitmapPool, toTransform: Bitmap, outWidth: Int, outHeight: Int): Bitmap { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. - } -} \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssAssets.kt b/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssAssets.kt index 5433be60..336ffc71 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssAssets.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssAssets.kt @@ -22,7 +22,11 @@ enum class CssAssets { injector = JsBuilder().css(content).build() } injector!!.inject(webView, callback) - L.d("CSS ${injector!!.function}") + L.v("CSS ${injector!!.function}") + } + + fun reset() { + injector = null } } \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt b/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt index 4ab48bdb..556a5555 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt @@ -5,44 +5,21 @@ import android.webkit.WebView /** * Created by Allan Wang on 2017-05-31. */ -enum class JsActions(val function: String) { - HIDE("style.display='none'"), - REMOVE("remove()") -} - -class VariableGenerator { +enum class JsActions(body: String) { + /** + * Redirects to login activity if create account is found + * see [com.pitchedapps.frost.web.FrostJSI.loadLogin] + */ + LOGIN_CHECK("document.getElementById('signup-button')&&Android.loadLogin();"); - var count = 0 + val function = "!function(){$body}();" - val next: String - get() { - var key = count - count++ - if (key == 0) return "a" - val name = StringBuilder() - while (key > 0) { - name.append(((key % 26) + 'a'.toInt()).toChar()) - key /= 26 - } - return name.toString() - } - - fun reset() { - count = 0 - } + fun inject(webView: WebView, callback: ((String) -> Unit)? = null) = JsInjector(function).inject(webView, callback) } class JsBuilder { - - private val map: MutableMap> = mutableMapOf() - private val v = VariableGenerator() private val css: StringBuilder by lazy { StringBuilder() } - fun append(action: JsActions, vararg ids: String): JsBuilder { - ids.forEach { id -> map[id]?.add(action) ?: map.put(id, mutableSetOf(action)) } - return this - } - fun css(css: String): JsBuilder { this.css.append(css.trim()) return this @@ -51,21 +28,10 @@ class JsBuilder { fun build() = JsInjector(toString()) override fun toString(): String { - v.reset() val builder = StringBuilder().append("!function(){") - map.forEach { id, actions -> - if (actions.size == 1) { - builder.append("document.getElementById('$id').${actions.first().function};") - } else { - val name = v.next - builder.append("var $name=document.getElementById('$id');") - actions.forEach { a -> builder.append("$name.${a.function};") } - } - } if (css.isNotBlank()) { - val name = v.next val cssMin = css.replace(Regex("\\s+"), "") - builder.append("var $name=document.createElement('style');$name.innerHTML='$cssMin';document.head.appendChild($name);") + builder.append("var a=document.createElement('style');a.innerHTML='$cssMin';document.head.appendChild(a);") } return builder.append("}()").toString() } 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 b8d76f40..59600a4d 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt @@ -5,6 +5,7 @@ import android.content.Context import android.content.Intent import android.support.v4.app.ActivityOptionsCompat import android.support.v4.content.ContextCompat +import com.pitchedapps.frost.LoginActivity import com.pitchedapps.frost.R import com.pitchedapps.frost.WebOverlayActivity import com.pitchedapps.frost.dbflow.CookieModel @@ -18,7 +19,7 @@ private const val ARG_URL = "arg_url" fun Context.launchNewTask(clazz: Class, cookieList: ArrayList = arrayListOf(), clearStack: Boolean = true) { val intent = (Intent(this, clazz)) - if (clearStack) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) + if (clearStack && (clazz != LoginActivity::class.java)) 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) if (this is Activity) finish() diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/AccountItem.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/AccountItem.kt index e0e33e79..6c5d5b6c 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/AccountItem.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/AccountItem.kt @@ -1,6 +1,5 @@ package com.pitchedapps.frost.views -import android.graphics.Bitmap import android.graphics.drawable.Drawable import android.support.v7.widget.AppCompatTextView import android.support.v7.widget.RecyclerView @@ -9,24 +8,23 @@ import android.widget.ImageView import butterknife.ButterKnife import com.bumptech.glide.Glide import com.bumptech.glide.load.DataSource -import com.bumptech.glide.load.Transformation import com.bumptech.glide.load.engine.GlideException import com.bumptech.glide.load.resource.bitmap.CircleCrop import com.bumptech.glide.request.RequestListener import com.bumptech.glide.request.RequestOptions import com.bumptech.glide.request.target.Target import com.mikepenz.fastadapter.items.AbstractItem +import com.mikepenz.google_material_typeface_library.GoogleMaterial import com.pitchedapps.frost.R import com.pitchedapps.frost.dbflow.CookieModel import com.pitchedapps.frost.facebook.PROFILE_PICTURE_URL import com.pitchedapps.frost.utils.bindView +import com.pitchedapps.frost.utils.toDrawable /** * Created by Allan Wang on 2017-06-05. */ -class AccountItem(val id: Long, val name: String) : AbstractItem() { - constructor() : this(-1L, "") - constructor(cookie: CookieModel) : this(cookie.id, cookie.name ?: "") +class AccountItem(val cookie: CookieModel?) : AbstractItem() { override fun getType(): Int = R.id.item_account @@ -38,10 +36,10 @@ class AccountItem(val id: Long, val name: String) : AbstractItem { + Glide.with(itemView).load(PROFILE_PICTURE_URL(cookie.id)).apply(options).listener(object : RequestListener { override fun onResourceReady(resource: Drawable?, model: Any?, target: Target?, dataSource: DataSource?, isFirstResource: Boolean): Boolean { text.fadeIn() return false @@ -54,6 +52,7 @@ class AccountItem(val id: Long, val name: String) : AbstractItem, val titleObservable: BehaviorSubject) : WebChromeClient() { - override fun onConsoleMessage(consoleMessage: ConsoleMessage): Boolean { - L.d("Chrome Console ${consoleMessage.lineNumber()}: ${consoleMessage.message()}") + L.v("Chrome Console ${consoleMessage.lineNumber()}: ${consoleMessage.message()}") return super.onConsoleMessage(consoleMessage) } override fun onReceivedTitle(view: WebView, title: String) { super.onReceivedTitle(view, title) if (title.contains("http") || titleObservable.value == title) return -// L.v("Title $title") titleObservable.onNext(title) } @@ -30,5 +28,4 @@ class FrostChromeClient(val progressObservable: Subject, val titleObservabl progressObservable.onNext(newProgress) } - } \ No newline at end of file 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 a3233c4c..045c180f 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt @@ -1,13 +1,26 @@ package com.pitchedapps.frost.web +import android.content.Context import android.webkit.JavascriptInterface +import com.pitchedapps.frost.LoginActivity +import com.pitchedapps.frost.SelectorActivity +import com.pitchedapps.frost.dbflow.CookieModel +import com.pitchedapps.frost.utils.launchNewTask +import com.pitchedapps.frost.utils.launchWebOverlay /** * Created by Allan Wang on 2017-06-01. */ -class FrostJSI { +class FrostJSI(val context: Context, val cookies: ArrayList) { @JavascriptInterface - fun loadUrl(url: String){ + fun loadUrl(url: String) = context.launchWebOverlay(url) + @JavascriptInterface + fun loadLogin() { + if (cookies.isNotEmpty()) + context.launchNewTask(SelectorActivity::class.java, cookies) + else + context.launchNewTask(LoginActivity::class.java, clearStack = false) } + } \ No newline at end of file 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 fb5fd0e7..37d10015 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClient.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClient.kt @@ -1,17 +1,19 @@ package com.pitchedapps.frost.web +import android.content.Context import android.graphics.Bitmap import android.view.KeyEvent -import android.webkit.WebResourceRequest -import android.webkit.WebResourceResponse -import android.webkit.WebView -import android.webkit.WebViewClient +import android.webkit.* import com.pitchedapps.frost.LoginActivity +import com.pitchedapps.frost.MainActivity +import com.pitchedapps.frost.SelectorActivity import com.pitchedapps.frost.facebook.FACEBOOK_COM import com.pitchedapps.frost.facebook.FbCookie import com.pitchedapps.frost.injectors.CssAssets +import com.pitchedapps.frost.injectors.JsActions import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.Prefs +import com.pitchedapps.frost.utils.cookies import com.pitchedapps.frost.utils.launchNewTask import com.pitchedapps.frost.views.circularReveal import com.pitchedapps.frost.views.fadeOut @@ -34,22 +36,26 @@ class FrostWebViewClient(val refreshObservable: Subject) : WebViewClien override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) { super.onPageStarted(view, url, favicon) L.i("FWV Loading $url") + L.i("Cookies ${CookieManager.getInstance().getCookie(url)}") refreshObservable.onNext(true) if (!url.contains(FACEBOOK_COM)) return - if (url.contains("logout.php")) { - FbCookie.logout(Prefs.userId) - view.context.launchNewTask(LoginActivity::class.java) - } else if (url.contains("login.php")) { - FbCookie.reset() - view.context.launchNewTask(LoginActivity::class.java) - } + if (url.contains("logout.php")) FbCookie.logout(Prefs.userId, { launchLogin(view.context) }) + else if (url.contains("login.php")) FbCookie.reset({ launchLogin(view.context) }) view.fadeOut(duration = 200L) } + fun launchLogin(c: Context) { + if (c is MainActivity && c.cookies().isNotEmpty()) + c.launchNewTask(SelectorActivity::class.java, c.cookies()) + else + c.launchNewTask(LoginActivity::class.java, clearStack = false) + } + override fun onPageFinished(view: WebView, url: String) { super.onPageFinished(view, url) refreshObservable.onNext(false) if (!url.contains(FACEBOOK_COM)) return + JsActions.LOGIN_CHECK.inject(view) CssAssets.HEADER.inject(view, { view.circularReveal(offset = 150L) }) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewCore.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewCore.kt index 7e121b83..6477deda 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewCore.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewCore.kt @@ -11,6 +11,7 @@ import android.util.AttributeSet import android.view.MotionEvent import android.view.View import android.view.animation.DecelerateInterpolator +import android.webkit.CookieManager import android.webkit.WebView import com.pitchedapps.frost.utils.L import io.reactivex.Scheduler @@ -113,7 +114,7 @@ class FrostWebViewCore @JvmOverloads constructor( */ fun scrollOrRefresh() { L.d("Scroll or Refresh") - if (scrollY < 5) reload() + if (scrollY < 5) loadBaseUrl() else scrollToTop() } 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 ede07710..191d9350 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt @@ -39,24 +39,25 @@ class LoginWebView @JvmOverloads constructor( lateinit var progressObservable: Subject init { - FbCookie.reset() - cookieObservable.filter { (_, cookie) -> cookie?.contains(userMatcher) ?: false } - .subscribe { - (url, cookie) -> - L.d("Checking cookie for $url\n\t$cookie") - val id = userMatcher.find(cookie!!)?.groups?.get(1)?.value - if (id != null) { - try { - FbCookie.save(id.toLong()) - //TODO proceed to next view - cookieObservable.onComplete() - loginObservable.onSuccess(CookieModel(id.toLong(), "", cookie)) - } catch (e: NumberFormatException) { - //todo send report that id has changed + FbCookie.reset({ + cookieObservable.filter { (_, cookie) -> cookie?.contains(userMatcher) ?: false } + .subscribe { + (url, cookie) -> + L.d("Checking cookie for $url\n\t$cookie") + val id = userMatcher.find(cookie!!)?.groups?.get(1)?.value + if (id != null) { + try { + FbCookie.save(id.toLong()) + //TODO proceed to next view + cookieObservable.onComplete() + loginObservable.onSuccess(CookieModel(id.toLong(), "", cookie)) + } catch (e: NumberFormatException) { + //todo send report that id has changed + } } } - } - setupWebview() + setupWebview() + }) } @SuppressLint("SetJavaScriptEnabled") -- cgit v1.2.3