diff options
author | Allan Wang <me@allanwang.ca> | 2017-06-03 00:22:10 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2017-06-03 00:22:10 -0700 |
commit | 5796566137995c8d244720f87ba85bce0e0d2f00 (patch) | |
tree | 1736e12182c7cf2769ad953029dc6a645b803802 /app/src/main/kotlin/com/pitchedapps/frost/web | |
parent | bc197ad5769ee792d930ac5b634e1e9000230689 (diff) | |
download | frost-5796566137995c8d244720f87ba85bce0e0d2f00.tar.gz frost-5796566137995c8d244720f87ba85bce0e0d2f00.tar.bz2 frost-5796566137995c8d244720f87ba85bce0e0d2f00.zip |
css updates and beginning login migration
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/web')
5 files changed, 135 insertions, 16 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt new file mode 100644 index 00000000..8be4e278 --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt @@ -0,0 +1,13 @@ +package com.pitchedapps.frost.web + +import android.webkit.JavascriptInterface +import com.pitchedapps.frost.events.WebLaunchEvent +import org.greenrobot.eventbus.EventBus + +/** + * Created by Allan Wang on 2017-06-01. + */ +class FrostJSI { + @JavascriptInterface + fun loadUrl(url: String) = EventBus.getDefault().post(WebLaunchEvent(url)) +}
\ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebView.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebView.kt index a9cf0559..087e9174 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebView.kt @@ -9,7 +9,6 @@ import android.widget.FrameLayout import android.widget.ProgressBar import butterknife.ButterKnife import com.pitchedapps.frost.R -import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.bindView import io.reactivex.android.schedulers.AndroidSchedulers @@ -22,11 +21,15 @@ class FrostWebView @JvmOverloads constructor(context: Context, attrs: AttributeS get() = web.baseUrl set(value) { web.baseUrl = value - if (value != null) web.loadUrl(value) } val refresh: SwipeRefreshLayout by bindView(R.id.swipe_refresh) val web: FrostWebViewCore by bindView(R.id.frost_webview_core) val progress: ProgressBar by bindView(R.id.progressBar) + var position: Int + get() = web.position + set(value) { + web.position = value + } init { inflate(getContext(), R.layout.swipe_webview, this) 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 143c9f5a..4dcf0d9e 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClient.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClient.kt @@ -9,12 +9,11 @@ import com.pitchedapps.frost.injectors.CssAssets import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.views.circularReveal import com.pitchedapps.frost.views.fadeOut -import io.reactivex.subjects.Subject /** * Created by Allan Wang on 2017-05-31. */ -class FrostWebViewClient : WebViewClient() { +class FrostWebViewClient(val position: () -> Int) : WebViewClient() { companion object { //Collections of jewels mapped with url match -> id @@ -27,17 +26,17 @@ class FrostWebViewClient : WebViewClient() { override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) { super.onPageStarted(view, url, favicon) - L.d("FWV Loading $url") + L.i("FWV Loading $url") if (!url.contains(FACEBOOK_COM)) return - if (url.contains("logout.php")) FbCookie.logout() + if (url.contains("logout.php")) FbCookie.logout(position.invoke()) view.fadeOut(duration = 200L) } override fun onPageFinished(view: WebView, url: String) { super.onPageFinished(view, url) if (!url.contains(FACEBOOK_COM)) return - FbCookie.checkUserId(url, CookieManager.getInstance().getCookie(url)) - CssAssets.BASE.inject(view, { + FbCookie.checkUserId(url, CookieManager.getInstance().getCookie(url), position.invoke()) + CssAssets.HEADER.inject(view, { view.circularReveal(offset = 150L) }) } @@ -48,23 +47,22 @@ class FrostWebViewClient : WebViewClient() { } override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean { - L.d("Hi") L.d("Url Loading ${request.url?.path}") return super.shouldOverrideUrlLoading(view, request) } override fun shouldInterceptRequest(view: WebView, request: WebResourceRequest): WebResourceResponse? { if (!request.url.host.contains(FACEBOOK_COM)) return super.shouldInterceptRequest(view, request) - L.d("Url intercept ${request.url.path}") + L.v("Url intercept ${request.url.path}") return super.shouldInterceptRequest(view, request) } override fun onLoadResource(view: WebView, url: String) { if (!url.contains(FACEBOOK_COM)) return super.onLoadResource(view, url) - L.d("Resource $url") + L.v("Resource $url") FrostWebOverlay.values.forEach { if (url.contains(it.match)) - L.d("Loaded $it") + L.d("Resource Loaded $it") } super.onLoadResource(view, url) } 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 03659908..b2260b6f 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewCore.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewCore.kt @@ -10,12 +10,13 @@ import android.util.AttributeSet import android.view.MotionEvent import android.view.View import android.webkit.WebView -import com.pitchedapps.frost.events.WebEvent +import com.pitchedapps.frost.events.FbAccountEvent import com.pitchedapps.frost.utils.ObservableContainer import io.reactivex.subjects.BehaviorSubject import io.reactivex.subjects.Subject import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.Subscribe +import org.greenrobot.eventbus.ThreadMode /** * Created by Allan Wang on 2017-05-29. @@ -35,6 +36,7 @@ class FrostWebViewCore @JvmOverloads constructor( private var nestedOffsetY: Int = 0 override val progressObservable: Subject<Int> //TODO see if we need this var baseUrl: String? = null + var position: Int = -1 init { isNestedScrollingEnabled = true @@ -47,7 +49,7 @@ class FrostWebViewCore @JvmOverloads constructor( settings.javaScriptEnabled = true settings.domStorageEnabled = true setLayerType(View.LAYER_TYPE_HARDWARE, null) - setWebViewClient(FrostWebViewClient()) + setWebViewClient(FrostWebViewClient({position})) setWebChromeClient(FrostChromeClient(progressObservable)) } @@ -56,6 +58,8 @@ class FrostWebViewCore @JvmOverloads constructor( super.loadUrl(url) } + fun loadBaseUrl() = loadUrl(baseUrl) + override fun onTouchEvent(ev: MotionEvent): Boolean { val event = MotionEvent.obtain(ev) val action = MotionEventCompat.getActionMasked(event) @@ -106,8 +110,8 @@ class FrostWebViewCore @JvmOverloads constructor( super.onDetachedFromWindow() } - @Subscribe - fun webEvent(event: WebEvent) = event.execute(this) + @Subscribe(threadMode = ThreadMode.MAIN) + fun webEvent(event: FbAccountEvent) = event.execute(this) // Nested Scroll implements override fun setNestedScrollingEnabled(enabled: Boolean) { diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt new file mode 100644 index 00000000..6e0cf201 --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt @@ -0,0 +1,101 @@ +package com.pitchedapps.frost.web + +import android.annotation.SuppressLint +import android.content.Context +import android.util.AttributeSet +import android.view.View +import android.webkit.* +import com.pitchedapps.frost.dbflow.CookieModel +import com.pitchedapps.frost.facebook.FACEBOOK_COM +import com.pitchedapps.frost.facebook.FbCookie +import com.pitchedapps.frost.injectors.CssAssets +import com.pitchedapps.frost.utils.L +import com.pitchedapps.frost.views.circularReveal +import com.pitchedapps.frost.views.snackbar +import io.reactivex.subjects.PublishSubject +import io.reactivex.subjects.SingleSubject +import io.reactivex.subjects.Subject + +/** + * Created by Allan Wang on 2017-05-29. + * + * Courtesy of takahirom + * + * https://github.com/takahirom/webview-in-coordinatorlayout/blob/master/app/src/main/java/com/github/takahirom/webview_in_coodinator_layout/NestedWebView.java + */ + + +class LoginWebView @JvmOverloads constructor( + context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 +) : WebView(context, attrs, defStyleAttr) { + + companion object { + const val LOGIN_URL = "https://touch.facebook.com/login" + private val userMatcher: Regex by lazy { Regex("c_user=([0-9]*);") } + } + + val cookieObservable = PublishSubject.create<Pair<String, String?>>() + lateinit var loginObservable: SingleSubject<CookieModel> + lateinit var progressObservable: Subject<Int> + + init { + 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(), -1) + //TODO proceed to next view + cookieObservable.onComplete() + } catch (e: NumberFormatException) { + //todo send report that id has changed + } + } + } + setupWebview() + } + + @SuppressLint("SetJavaScriptEnabled") + fun setupWebview() { + settings.javaScriptEnabled = true + setLayerType(View.LAYER_TYPE_HARDWARE, null) + setWebViewClient(LoginClient()) + setWebChromeClient(LoginChromeClient()) + } + + fun loadLogin() { + loadUrl(LOGIN_URL) + } + + + inner class LoginClient : WebViewClient() { + + override fun onPageFinished(view: WebView, url: String) { + super.onPageFinished(view, url) + if (!url.contains(FACEBOOK_COM)) { + view.snackbar("No longer under facebook; refreshing...") + loadLogin() + return + } + CssAssets.LOGIN.inject(view, { + if (view.visibility == View.INVISIBLE) + view.circularReveal(offset = 150L) + }) + cookieObservable.onNext(Pair(url, CookieManager.getInstance().getCookie(url))) + } + } + + inner class LoginChromeClient : WebChromeClient() { + override fun onConsoleMessage(consoleMessage: ConsoleMessage): Boolean { + L.d("Login Console ${consoleMessage.lineNumber()}: ${consoleMessage.message()}") + return super.onConsoleMessage(consoleMessage) + } + + override fun onProgressChanged(view: WebView, newProgress: Int) { + super.onProgressChanged(view, newProgress) + progressObservable.onNext(newProgress) + } + } +}
\ No newline at end of file |