From 0ac9d2181536b79a6057c3b526f32a1cc8ac4704 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 13 Jun 2017 17:07:09 -0700 Subject: Created working workaround for menu item --- .../com/pitchedapps/frost/WebOverlayActivity.kt | 2 +- .../kotlin/com/pitchedapps/frost/facebook/FbTab.kt | 8 ++- .../com/pitchedapps/frost/fragments/WebFragment.kt | 15 +++-- .../com/pitchedapps/frost/injectors/JsAssets.kt | 2 +- .../kotlin/com/pitchedapps/frost/web/FrostJSI.kt | 24 +++++++- .../com/pitchedapps/frost/web/FrostWebView.kt | 2 +- .../pitchedapps/frost/web/FrostWebViewClient.kt | 71 ++++++++++++++-------- .../frost/web/FrostWebViewClientMenu.kt | 71 ++++++++++++++++++++++ .../com/pitchedapps/frost/web/FrostWebViewCore.kt | 15 +++-- 9 files changed, 163 insertions(+), 47 deletions(-) create mode 100644 app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClientMenu.kt (limited to 'app/src/main/kotlin') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/WebOverlayActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/WebOverlayActivity.kt index 70e86239..cc190004 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/WebOverlayActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/WebOverlayActivity.kt @@ -24,7 +24,7 @@ class WebOverlayActivity : AppCompatActivity() { supportActionBar?.setDisplayShowHomeEnabled(true) supportActionBar?.setDisplayHomeAsUpEnabled(true) toolbar.setNavigationOnClickListener { onBackPressed() } - frostWeb.web.baseUrl = url() + frostWeb.web.setupWebview(url()) frostWeb.web.loadBaseUrl() SwipeBackHelper.onCreate(this) SwipeBackHelper.getCurrentPage(this) 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 c3bad56c..4a9b3441 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbTab.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbTab.kt @@ -6,9 +6,11 @@ import com.mikepenz.google_material_typeface_library.GoogleMaterial import com.mikepenz.iconics.typeface.IIcon import com.mikepenz.material_design_iconic_typeface_library.MaterialDesignIconic import com.pitchedapps.frost.R -import com.pitchedapps.frost.injectors.JsAssets +import com.pitchedapps.frost.web.FrostWebViewClient +import com.pitchedapps.frost.web.FrostWebViewClientMenu +import io.reactivex.subjects.BehaviorSubject -enum class FbTab(@StringRes val titleId: Int, val icon: IIcon, relativeUrl: String, val javascript: JsAssets? = null) { +enum class FbTab(@StringRes val titleId: Int, val icon: IIcon, relativeUrl: String, val webClient: ((refreshObservable: BehaviorSubject) -> FrostWebViewClient)? = null) { FEED(R.string.feed, CommunityMaterial.Icon.cmd_newspaper, ""), FEED_MOST_RECENT(R.string.most_recent, GoogleMaterial.Icon.gmd_grade, "/?sk=h_chr"), FEED_TOP_STORIES(R.string.top_stories, GoogleMaterial.Icon.gmd_star, "/?sk=h_nor"), @@ -25,7 +27,7 @@ enum class FbTab(@StringRes val titleId: Int, val icon: IIcon, relativeUrl: Stri CHAT(R.string.chat, GoogleMaterial.Icon.gmd_chat, "buddylist"), PHOTOS(R.string.photos, GoogleMaterial.Icon.gmd_photo, "me/photos"), SETTINGS(R.string.settings, GoogleMaterial.Icon.gmd_settings, "settings"), - MENU(R.string.menu, GoogleMaterial.Icon.gmd_menu, "settings", JsAssets.MENU) + MENU(R.string.menu, GoogleMaterial.Icon.gmd_menu, "settings", { FrostWebViewClientMenu(it) }) ; val url = "$FB_URL_BASE$relativeUrl" 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 0f56592c..04b392ed 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt @@ -10,7 +10,6 @@ import android.view.ViewGroup import ca.allanwang.kau.utils.withBundle import com.pitchedapps.frost.MainActivity import com.pitchedapps.frost.facebook.FbTab -import com.pitchedapps.frost.injectors.JsAssets import com.pitchedapps.frost.web.FrostWebView import com.pitchedapps.frost.web.FrostWebViewCore import io.reactivex.android.schedulers.AndroidSchedulers @@ -25,21 +24,21 @@ class WebFragment : Fragment() { companion object { private const val ARG_URL = "arg_url" - private const val ARG_ID = "arg_id" + private const val ARG_URL_ENUM = "arg_url_enum" operator fun invoke(url: String) = WebFragment().withBundle { putString(ARG_URL, url) } operator fun invoke(data: FbTab) = WebFragment().withBundle { putString(ARG_URL, data.url) - if (data.javascript != null) putSerializable(ARG_ID, data.javascript) + putSerializable(ARG_URL_ENUM, data) } } // val refresh: SwipeRefreshLayout by lazy { frostWebView.refresh } val web: FrostWebViewCore by lazy { frostWebView.web } lateinit var url: String - var baseJavascript: JsAssets? = null + var urlEnum: FbTab? = null lateinit private var frostWebView: FrostWebView private var firstLoad = true private var refreshDisposable: Disposable? = null @@ -47,14 +46,13 @@ class WebFragment : Fragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) url = arguments.getString(ARG_URL) - baseJavascript = arguments.getSerializable(ARG_ID) as? JsAssets + urlEnum = arguments.getSerializable(ARG_URL_ENUM) as? FbTab } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { super.onCreateView(inflater, container, savedInstanceState) frostWebView = FrostWebView(context) - frostWebView.web.baseUrl = url - frostWebView.web.baseJavascript = baseJavascript + frostWebView.web.setupWebview(url, urlEnum) return frostWebView } @@ -106,7 +104,8 @@ class WebFragment : Fragment() { var pauseLoad: Boolean get() = web.settings.blockNetworkLoads set(value) { - web.settings.blockNetworkLoads = value + if (urlEnum != FbTab.MENU) + web.settings.blockNetworkLoads = value } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsAssets.kt b/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsAssets.kt index 2bf53d2e..e0a9dd3d 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsAssets.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsAssets.kt @@ -10,7 +10,7 @@ import com.pitchedapps.frost.utils.L * //TODO add folder mapping using Prefs */ enum class JsAssets { - MENU + MENU, MENU_CLICK ; var file = "${name.toLowerCase()}.min.js" 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,21 +6,31 @@ 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 get() = (context as? MainActivity)?.cookies() ?: arrayListOf() @JavascriptInterface 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()) @@ -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) : WebViewClient() { +open class FrostWebViewClient(val refreshObservable: Subject) : 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) : 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) : 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) : 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) : 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 // 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() refreshObservable = BehaviorSubject.create() titleObservable = BehaviorSubject.create() - 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?) { -- cgit v1.2.3