aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/web
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/web')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClients.kt12
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/web/FrostRequestInterceptor.kt29
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClients.kt72
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewCore.kt2
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt2
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/web/SearchWebView.kt13
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/web/WebStates.kt11
7 files changed, 87 insertions, 54 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClients.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClients.kt
index 6bc27256..61711092 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClients.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClients.kt
@@ -34,15 +34,9 @@ class FrostChromeClient(webCore: FrostWebViewCore) : WebChromeClient() {
val activityContract = (webCore.context as? ActivityWebContract)
val context = webCore.context!!
- companion object {
- val consoleBlacklist = setOf(
- "edge-chat"
- )
- }
-
override fun onConsoleMessage(consoleMessage: ConsoleMessage): Boolean {
if (consoleBlacklist.any { consoleMessage.message().contains(it) }) return true
- L.i("Chrome Console ${consoleMessage.lineNumber()}: ${consoleMessage.message()}")
+ L.d("Chrome Console ${consoleMessage.lineNumber()}: ${consoleMessage.message()}")
return true
}
@@ -63,10 +57,10 @@ class FrostChromeClient(webCore: FrostWebViewCore) : WebChromeClient() {
}
override fun onGeolocationPermissionsShowPrompt(origin: String, callback: GeolocationPermissions.Callback) {
- L.d("Requesting geolocation")
+ L.i("Requesting geolocation")
context.kauRequestPermissions(PERMISSION_ACCESS_FINE_LOCATION) {
granted, _ ->
- L.d("Geolocation response received; ${if (granted) "granted" else "denied"}")
+ L.i("Geolocation response received; ${if (granted) "granted" else "denied"}")
callback(origin, granted, true)
}
}
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostRequestInterceptor.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostRequestInterceptor.kt
index 3f2891d0..1a907f7f 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostRequestInterceptor.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostRequestInterceptor.kt
@@ -5,6 +5,7 @@ import android.webkit.WebResourceResponse
import android.webkit.WebView
import ca.allanwang.kau.utils.use
import com.pitchedapps.frost.utils.L
+import com.pitchedapps.frost.utils.Prefs
import okhttp3.HttpUrl
import java.io.ByteArrayInputStream
@@ -15,17 +16,17 @@ import java.io.ByteArrayInputStream
* Handler to decide when a request should be done by us
* This is the crux of Frost's optimizations for the web browser
*/
-val blankResource: WebResourceResponse by lazy { WebResourceResponse("text/plain", "utf-8", ByteArrayInputStream("".toByteArray())) }
+private val blankResource: WebResourceResponse by lazy { WebResourceResponse("text/plain", "utf-8", ByteArrayInputStream("".toByteArray())) }
//these hosts will redirect to a blank resource
-val blacklistHost: Set<String> by lazy {
+private val blacklistHost: Set<String> by lazy {
setOf(
"edge-chat.facebook.com"
)
}
//these hosts will return null and skip logging
-val whitelistHost: Set<String> by lazy {
+private val whitelistHost: Set<String> by lazy {
setOf(
"static.xx.fbcdn.net",
"m.facebook.com",
@@ -35,13 +36,13 @@ val whitelistHost: Set<String> by lazy {
//these hosts will skip ad inspection
//this list does not have to include anything from the two above
-val adWhitelistHost: Set<String> by lazy {
+private val adWhitelistHost: Set<String> by lazy {
setOf(
"scontent-sea1-1.xx.fbcdn.net"
)
}
-var adblock: Set<String>? = null
+private var adblock: Set<String>? = null
fun shouldFrostInterceptRequest(view: WebView, request: WebResourceRequest): WebResourceResponse? {
val httpUrl = HttpUrl.parse(request.url?.toString() ?: return null) ?: return null
@@ -53,7 +54,8 @@ fun shouldFrostInterceptRequest(view: WebView, request: WebResourceRequest): Web
if (adblock == null) adblock = view.context.assets.open("adblock.txt").bufferedReader().use { it.readLines().toSet() }
if (adblock?.any { url.contains(it) } ?: false) return blankResource
}
- L.v("Intercept Request ${host} ${url}")
+ if (!shouldLoadImages && !Prefs.loadMediaOnMeteredNetwork && request.isMedia) return blankResource
+ L.v("Intercept Request", "$host $url")
return null
}
@@ -64,16 +66,25 @@ fun WebResourceRequest.query(action: (url: String) -> Boolean): Boolean {
return action(url?.path ?: return false)
}
+val WebResourceRequest.isImage: Boolean
+ get() = query { it.contains(".jpg") || it.contains(".png") }
+
+val WebResourceRequest.isMedia: Boolean
+ get() = query { it.contains(".jpg") || it.contains(".png") || it.contains("video") }
+
/**
* Generic filter passthrough
* If Resource is already nonnull, pass it, otherwise check if filter is met and override the response accordingly
*/
-fun WebResourceResponse?.filter(request: WebResourceRequest, filter: (url: String) -> Boolean): WebResourceResponse?
- = this ?: if (request.query { filter(it) }) blankResource else null
+fun WebResourceResponse?.filter(request: WebResourceRequest, filter: (url: String) -> Boolean)
+ = filter(request.query { filter(it) })
+
+fun WebResourceResponse?.filter(filter: Boolean): WebResourceResponse?
+ = this ?: if (filter) blankResource else null
fun WebResourceResponse?.filterCss(request: WebResourceRequest): WebResourceResponse?
= filter(request) { it.endsWith(".css") }
fun WebResourceResponse?.filterImage(request: WebResourceRequest): WebResourceResponse?
- = filter(request) { it.contains(".jpg") || it.contains(".png") }
+ = filter(request.isImage)
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 f3068c43..3c08fda9 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClients.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClients.kt
@@ -2,6 +2,7 @@ package com.pitchedapps.frost.web
import android.content.Context
import android.graphics.Bitmap
+import android.graphics.Color
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebView
@@ -17,6 +18,7 @@ import com.pitchedapps.frost.injectors.*
import com.pitchedapps.frost.utils.*
import com.pitchedapps.frost.utils.iab.IS_FROST_PRO
import io.reactivex.subjects.Subject
+import org.jetbrains.anko.withAlpha
/**
* Created by Allan Wang on 2017-05-31.
@@ -41,18 +43,19 @@ open class BaseWebViewClient : WebViewClient() {
open class FrostWebViewClient(val webCore: FrostWebViewCore) : BaseWebViewClient() {
val refreshObservable: Subject<Boolean> = webCore.refreshObservable
+ val isMain = webCore.baseEnum != null
override fun onPageStarted(view: WebView, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
if (url == null) return
- L.i("FWV Loading $url")
-// L.v("Cookies ${CookieManager.getInstance().getCookie(url)}")
+ L.i("FWV Loading", url)
refreshObservable.onNext(true)
if (!url.contains(FACEBOOK_COM)) return
if (url.contains("logout.php")) FbCookie.logout(Prefs.userId, { launchLogin(view.context) })
else if (url.contains("login.php")) FbCookie.reset({ launchLogin(view.context) })
}
+
fun launchLogin(c: Context) {
if (c is MainActivity && c.cookies().isNotEmpty())
c.launchNewTask(SelectorActivity::class.java, c.cookies())
@@ -60,19 +63,28 @@ open class FrostWebViewClient(val webCore: FrostWebViewCore) : BaseWebViewClient
c.launchNewTask(LoginActivity::class.java)
}
+ fun injectBackgroundColor()
+ = webCore.setBackgroundColor(if (isMain) Color.TRANSPARENT else Prefs.bgColor.withAlpha(255))
+
+
+ override fun onPageCommitVisible(view: WebView, url: String?) {
+ super.onPageCommitVisible(view, url)
+ injectBackgroundColor()
+ view.jsInject(
+ CssAssets.ROUND_ICONS.maybe(Prefs.showRoundedIcons),
+ CssHider.HEADER,
+ CssHider.PEOPLE_YOU_MAY_KNOW.maybe(!Prefs.showSuggestedFriends && IS_FROST_PRO),
+ Prefs.themeInjector,
+ CssHider.NON_RECENT.maybe(webCore.url?.contains("?sk=h_chr") ?: false))
+ }
+
override fun onPageFinished(view: WebView, url: String?) {
- super.onPageFinished(view, url)
- if (url == null) return
- L.i("Page finished $url")
+ url ?: return
+ L.i("Page finished", url)
if (!url.contains(FACEBOOK_COM)) {
refreshObservable.onNext(false)
return
}
- view.jsInject(
- CssAssets.ROUND_ICONS.maybe(Prefs.showRoundedIcons),
- CssHider.PEOPLE_YOU_MAY_KNOW.maybe(!Prefs.showSuggestedFriends && IS_FROST_PRO),
- CssHider.ADS.maybe(!Prefs.showFacebookAds && IS_FROST_PRO)
- )
onPageFinishedActions(url)
}
@@ -82,19 +94,16 @@ open class FrostWebViewClient(val webCore: FrostWebViewCore) : BaseWebViewClient
internal fun injectAndFinish() {
L.d("Page finished reveal")
- webCore.jsInject(CssHider.HEADER,
- CssHider.NON_RECENT.maybe(webCore.url.contains("?sk=h_chr")),
- Prefs.themeInjector,
- callback = {
- refreshObservable.onNext(false)
- webCore.jsInject(
- JsActions.LOGIN_CHECK,
- JsAssets.CLICK_A.maybe(webCore.baseEnum != null && Prefs.overlayEnabled),
- JsAssets.TEXTAREA_LISTENER,
- JsAssets.CONTEXT_A,
- JsAssets.HEADER_BADGES.maybe(webCore.baseEnum != null)
- )
- })
+ refreshObservable.onNext(false)
+ injectBackgroundColor()
+ webCore.jsInject(
+ JsActions.LOGIN_CHECK,
+ JsAssets.CLICK_A.maybe(webCore.baseEnum != null && Prefs.overlayEnabled),
+ JsAssets.TEXTAREA_LISTENER,
+ CssHider.ADS.maybe(!Prefs.showFacebookAds && IS_FROST_PRO),
+ JsAssets.CONTEXT_A,
+ JsAssets.HEADER_BADGES.maybe(webCore.baseEnum != null)
+ )
}
open fun handleHtml(html: String?) {
@@ -111,26 +120,26 @@ open class FrostWebViewClient(val webCore: FrostWebViewCore) : BaseWebViewClient
* returns false if we are already in an overlaying activity
*/
private fun launchRequest(request: WebResourceRequest): Boolean {
- L.d("Launching Url", request.url.toString())
+ L.d("Launching Url", request.url?.toString() ?: "null")
if (webCore.context is WebOverlayActivity) return false
webCore.context.launchWebOverlay(request.url.toString())
return true
}
- private fun launchImage(request: WebResourceRequest, text: String? = null): Boolean {
- L.d("Launching Image", request.url.toString())
- webCore.context.launchImageActivity(request.url.toString(), text)
+ private fun launchImage(url: String, text: String? = null): Boolean {
+ L.d("Launching Image", url)
+ webCore.context.launchImageActivity(url, text)
if (webCore.canGoBack()) webCore.goBack()
return true
}
override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
- L.i("Url Loading ${request.url}")
- val path = request.url.path ?: return super.shouldOverrideUrlLoading(view, request)
- L.v("Url Loading Path $path")
+ L.i("Url Loading", request.url?.toString())
+ val path = request.url?.path ?: return super.shouldOverrideUrlLoading(view, request)
+ L.v("Url Loading Path", path)
if (path.startsWith("/composer/")) return launchRequest(request)
if (request.url.toString().contains("scontent-sea1-1.xx.fbcdn.net") && (path.endsWith(".jpg") || path.endsWith(".png")))
- return launchImage(request)
+ return launchImage(request.url.toString())
if (view.context.resolveActivityForUri(request.url)) return true
return super.shouldOverrideUrlLoading(view, request)
}
@@ -162,6 +171,7 @@ class FrostWebViewClientMenu(webCore: FrostWebViewCore) : FrostWebViewClient(web
}
override fun onPageFinishedActions(url: String) {
+ L.d("Should inject ${url.shouldInjectMenu}")
if (!url.shouldInjectMenu) injectAndFinish()
}
}
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 d8edc15c..390055cd 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewCore.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewCore.kt
@@ -76,7 +76,7 @@ class FrostWebViewCore @JvmOverloads constructor(
if (isVisible) fadeOut(duration = 200L)
} else if (loading) {
dispose?.dispose()
- if (animate && Prefs.animate) circularReveal(offset = 150L)
+ if (animate && Prefs.animate) circularReveal(offset = WEB_LOAD_DELAY)
else fadeIn(duration = 100L)
}
}
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 31be4450..aea25337 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt
@@ -60,7 +60,7 @@ class LoginWebView @JvmOverloads constructor(
view.jsInject(CssHider.HEADER.maybe(containsFacebook),
CssHider.CORE.maybe(containsFacebook),
Prefs.themeInjector.maybe(containsFacebook),
- callback = { if (!view.isVisible) view.fadeIn(offset = 150L) })
+ callback = { if (!view.isVisible) view.fadeIn(offset = WEB_LOAD_DELAY) })
}
fun checkForLogin(url: String?, onFound: (id: Long, cookie: String) -> Unit) {
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/SearchWebView.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/SearchWebView.kt
index fb4e5bf7..f63f4fdc 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/web/SearchWebView.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/web/SearchWebView.kt
@@ -56,13 +56,17 @@ class SearchWebView(context: Context, val contract: SearchContract) : WebView(co
addJavascriptInterface(SearchJSI(), "Frost")
searchSubject.debounce(300, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.newThread())
.map {
- Jsoup.parse(it).select("a:not([rel*='keywords(']):not([href=#])[rel]").map {
+ val doc = Jsoup.parse(it)
+ L.d(doc.getElementById("main-search_input")?.html())
+ val searchQuery = doc.getElementById("main-search-input")?.text() ?: "Null input"
+ L.d("Search query", searchQuery)
+ doc.select("a:not([rel*='keywords(']):not([href=#])[rel]").map {
element ->
//split text into separate items
- L.v("Search element ${element.attr("href")}")
+ L.v("Search element", element.attr("href"))
val texts = element.select("div").map { it.text() }.filter { !it.isNullOrBlank() }
val pair = Pair(texts, element.attr("href"))
- L.v("Search element potential $pair")
+ L.v("Search element potential", pair.toString())
pair
}.filter { it.first.isNotEmpty() }
}
@@ -124,6 +128,9 @@ class SearchWebView(context: Context, val contract: SearchContract) : WebView(co
searchSubject.onComplete()
contract.searchOverlayDispose()
}
+ 2 -> {
+ L.v("Search emission received")
+ }
}
}
}
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/WebStates.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/WebStates.kt
new file mode 100644
index 00000000..ad1fe467
--- /dev/null
+++ b/app/src/main/kotlin/com/pitchedapps/frost/web/WebStates.kt
@@ -0,0 +1,11 @@
+package com.pitchedapps.frost.web
+
+/**
+ * Created by Allan Wang on 2017-08-08.
+ *
+ * Global variables that are define states or constants for web contents
+ */
+const val WEB_LOAD_DELAY = 50L
+var shouldLoadImages = false
+
+val consoleBlacklist = setOf("edge-chat") \ No newline at end of file