diff options
author | Allan Wang <me@allanwang.ca> | 2017-06-13 17:07:09 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2017-06-13 17:07:09 -0700 |
commit | 0ac9d2181536b79a6057c3b526f32a1cc8ac4704 (patch) | |
tree | b561acf7fc94a79e8a48b548489e0f544d402b8c /app/src/main/kotlin/com/pitchedapps/frost/web | |
parent | e4377fed20ce1e3f4a0b236271eecdb1fe573b7a (diff) | |
download | frost-0ac9d2181536b79a6057c3b526f32a1cc8ac4704.tar.gz frost-0ac9d2181536b79a6057c3b526f32a1cc8ac4704.tar.bz2 frost-0ac9d2181536b79a6057c3b526f32a1cc8ac4704.zip |
Created working workaround for menu item
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/web')
5 files changed, 149 insertions, 34 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 index 66f638af..14ea4df8 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt @@ -6,15 +6,16 @@ import com.pitchedapps.frost.LoginActivity import com.pitchedapps.frost.MainActivity import com.pitchedapps.frost.SelectorActivity import com.pitchedapps.frost.dbflow.CookieModel +import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.cookies import com.pitchedapps.frost.utils.launchNewTask import com.pitchedapps.frost.utils.launchWebOverlay + /** * Created by Allan Wang on 2017-06-01. */ -class FrostJSI(val context: Context) { - +class FrostJSI(val context: Context, val webView: FrostWebViewCore) { val cookies: ArrayList<CookieModel> get() = (context as? MainActivity)?.cookies() ?: arrayListOf() @@ -22,6 +23,15 @@ class FrostJSI(val context: Context) { fun loadUrl(url: String) = context.launchWebOverlay(url) @JavascriptInterface + fun reloadBaseUrl() { + L.d("FrostJSI reload") + webView.post { + webView.stopLoading() + webView.loadBaseUrl() + } + } + + @JavascriptInterface fun loadLogin() { if (cookies.isNotEmpty()) context.launchNewTask(SelectorActivity::class.java, cookies) @@ -29,4 +39,14 @@ class FrostJSI(val context: Context) { context.launchNewTask(LoginActivity::class.java) } + @JavascriptInterface + fun emit(flag: Int) { + webView.post { webView.frostWebClient!!.emit(flag) } + } + + @JavascriptInterface + fun handleHtml(html: String) { + webView.post { webView.frostWebClient!!.handleHtml(html) } + } + }
\ 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 4554e0a4..167661a4 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebView.kt @@ -41,7 +41,7 @@ class FrostWebView @JvmOverloads constructor(context: Context, attrs: AttributeS }) } - //Some urls have javascript injections so make sure we load the base url + //Some urls have postJavascript injections so make sure we load the base url override fun onRefresh() { when (web.baseUrl) { FbTab.MENU.url -> web.loadBaseUrl() 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 28048a1d..2f24d055 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClient.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClient.kt @@ -4,6 +4,7 @@ import android.content.Context import android.graphics.Bitmap import android.view.KeyEvent import android.webkit.* +import ca.allanwang.kau.utils.isVisible import com.pitchedapps.frost.LoginActivity import com.pitchedapps.frost.MainActivity import com.pitchedapps.frost.SelectorActivity @@ -11,23 +12,25 @@ 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.injectors.JsAssets 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.fadeIn import com.pitchedapps.frost.views.fadeOut import io.reactivex.subjects.Subject /** * Created by Allan Wang on 2017-05-31. */ -class FrostWebViewClient(val refreshObservable: Subject<Boolean>) : WebViewClient() { +open class FrostWebViewClient(val refreshObservable: Subject<Boolean>) : WebViewClient() { 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)}") + L.v("Cookies ${CookieManager.getInstance().getCookie(url)}") refreshObservable.onNext(true) if (!url.contains(FACEBOOK_COM)) return if (url.contains("logout.php")) FbCookie.logout(Prefs.userId, { launchLogin(view.context) }) @@ -44,20 +47,42 @@ class FrostWebViewClient(val refreshObservable: Subject<Boolean>) : WebViewClien override fun onPageFinished(view: WebView, url: String) { super.onPageFinished(view, url) - refreshObservable.onNext(false) - if (!url.contains(FACEBOOK_COM)) return - L.i("Page finished $url") - with(view as FrostWebViewCore) { - if (url == view.baseUrl && view.baseJavascript != null) { - L.i("Base inject ${view.baseJavascript!!.name}") - view.baseJavascript!!.inject(view, { - L.i("Base injection done") - }) - } + if (!url.contains(FACEBOOK_COM)) { + refreshObservable.onNext(false) + if (!view.isVisible()) view.fadeIn(duration = 200L) + return } + L.i("Page finished $url") JsActions.LOGIN_CHECK.inject(view) + onPageFinishedReveal(view as FrostWebViewCore, url) + } + + open internal fun onPageFinishedReveal(view: FrostWebViewCore, url: String?) { + onPageFinishedReveal(view, true) + } + + internal fun onPageFinishedReveal(view: FrostWebViewCore, animate: Boolean) { + L.d("Page finished reveal") CssAssets.HEADER.inject(view, { - view.circularReveal(offset = 150L) + refreshObservable.onNext(false) + if (animate) view.circularReveal(offset = 150L) + else view.fadeIn(duration = 100L) + }) + } + + open fun handleHtml(html: String) { + L.d("Handle Html") + } + + open fun emit(flag: Int) { + L.d("Emit $flag") + } + + fun inject(jsAssets: JsAssets, view: WebView, callback: (String) -> Unit = {}) { + L.i("Post inject ${jsAssets.name}") + jsAssets.inject(view, { + L.i("Post injection done $it") + callback.invoke(it) }) } @@ -66,13 +91,13 @@ class FrostWebViewClient(val refreshObservable: Subject<Boolean>) : WebViewClien return super.shouldOverrideKeyEvent(view, event) } - override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean { - L.d("Url Loading ${request.url?.path}") + override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest?): Boolean { + 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) + override fun shouldInterceptRequest(view: WebView, request: WebResourceRequest?): WebResourceResponse? { + if (request == null || !(request.url.host?.contains(FACEBOOK_COM) ?: false)) return super.shouldInterceptRequest(view, request) L.v("Url intercept ${request.url.path}") return super.shouldInterceptRequest(view, request) } @@ -80,15 +105,11 @@ class FrostWebViewClient(val refreshObservable: Subject<Boolean>) : WebViewClien override fun onLoadResource(view: WebView, url: String) { if (!url.contains(FACEBOOK_COM)) return super.onLoadResource(view, url) L.v("Resource $url") - FrostWebOverlay.values.forEach { - if (url.contains(it.match)) - L.d("Resource Loaded $it") - } +// FrostWebOverlay.values.forEach { +// if (url.contains(it.match)) +// L.d("Resource Loaded $it") +// } super.onLoadResource(view, url) } - fun logout() { - - } - }
\ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClientMenu.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClientMenu.kt new file mode 100644 index 00000000..faf42fdd --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClientMenu.kt @@ -0,0 +1,71 @@ +package com.pitchedapps.frost.web + +import android.graphics.Bitmap +import android.webkit.WebView +import com.pitchedapps.frost.injectors.JsAssets +import com.pitchedapps.frost.utils.L +import io.reactivex.subjects.Subject + +/** + * Created by Allan Wang on 2017-05-31. + */ +class FrostWebViewClientMenu(refreshObservable: Subject<Boolean>) : FrostWebViewClient(refreshObservable) { + + var content: String? = null + var view: FrostWebViewCore? = null + var loadingFromBase: Boolean = false + + override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) { + super.onPageStarted(view, url, favicon) + if (content != null) { + when (url) { + "https://m.facebook.com/settings", + "https://m.facebook.com/settings#", + "https://m.facebook.com/settings#!/settings?soft=bookmarks" -> { + loadingFromBase = true + L.d("Load from stored $url") + view.stopLoading() + view.loadDataWithBaseURL("https://touch.facebook.com/notifications", content, "text/html", "utf-8", "https://google.ca/test") + } + } + } + } + + override fun onPageFinished(view: WebView, url: String) { + super.onPageFinished(view, url) + L.d("DON $url") + loadingFromBase = false + with(view as FrostWebViewCore) { + if (url == view.baseUrl) { + this@FrostWebViewClientMenu.view = view + inject(JsAssets.MENU, view, { + inject(JsAssets.MENU_CLICK, view) //menu injection must be after or we will have a loop from the click listener + }) + } else { + inject(JsAssets.MENU_CLICK, view) + } + } + } + + override fun emit(flag: Int) { + super.emit(flag) + if (view != null) super.onPageFinishedReveal(view!!, true) + view = null + } + + override fun onPageFinishedReveal(view: FrostWebViewCore, url: String?) { + when (url) { + "https://m.facebook.com/settings", + "https://m.facebook.com/settings#", + "https://m.facebook.com/settings#!/settings?soft=bookmarks" -> { + //do nothing; we will further inject before revealing + } + else -> super.onPageFinishedReveal(view, false) + } + } + + override fun handleHtml(html: String) { + super.handleHtml(html) + content = html //we will not save this locally in case things change + } +}
\ No newline at end of file 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 f0596f7c..8d057db1 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewCore.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewCore.kt @@ -11,8 +11,8 @@ import android.view.MotionEvent import android.view.View import android.view.animation.DecelerateInterpolator import android.webkit.WebView +import com.pitchedapps.frost.facebook.FbTab import com.pitchedapps.frost.facebook.USER_AGENT_BASIC -import com.pitchedapps.frost.injectors.JsAssets import io.reactivex.Scheduler import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.Disposable @@ -39,25 +39,28 @@ class FrostWebViewCore @JvmOverloads constructor( val titleObservable: BehaviorSubject<String> // Only emits on different non http titles var baseUrl: String? = null - var baseJavascript: JsAssets? = null + var baseEnum: FbTab? = null + internal var frostWebClient: FrostWebViewClient? = null init { isNestedScrollingEnabled = true progressObservable = BehaviorSubject.create<Int>() refreshObservable = BehaviorSubject.create<Boolean>() titleObservable = BehaviorSubject.create<String>() - setupWebview() } @SuppressLint("SetJavaScriptEnabled") - fun setupWebview() { + fun setupWebview(url: String, enum: FbTab? = null) { + baseUrl = url + baseEnum = enum settings.javaScriptEnabled = true settings.userAgentString = USER_AGENT_BASIC // settings.domStorageEnabled = true setLayerType(View.LAYER_TYPE_HARDWARE, null) - webViewClient = FrostWebViewClient(refreshObservable) + frostWebClient = baseEnum?.webClient?.invoke(refreshObservable) ?: FrostWebViewClient(refreshObservable) + webViewClient = frostWebClient webChromeClient = FrostChromeClient(progressObservable, titleObservable) - addJavascriptInterface(FrostJSI(context), "Frost") + addJavascriptInterface(FrostJSI(context, this), "Frost") } override fun loadUrl(url: String?) { |