aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClients.kt
blob: f1b30795899571599ef01cc6deb0a2e211d43599 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.pitchedapps.frost.web

import android.graphics.Bitmap
import android.graphics.Color
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebView
import android.webkit.WebViewClient
import com.pitchedapps.frost.facebook.FB_URL_BASE
import com.pitchedapps.frost.facebook.FbItem
import com.pitchedapps.frost.injectors.*
import com.pitchedapps.frost.utils.*
import com.pitchedapps.frost.utils.iab.IS_FROST_PRO
import com.pitchedapps.frost.views.FrostWebView
import io.reactivex.subjects.Subject
import org.jetbrains.anko.withAlpha

/**
 * Created by Allan Wang on 2017-05-31.
 *
 * Collection of webview clients
 */

/**
 * The base of all webview clients
 * Used to ensure that resources are properly intercepted
 */
open class BaseWebViewClient : WebViewClient() {

    override fun shouldInterceptRequest(view: WebView, request: WebResourceRequest): WebResourceResponse?
            = view.shouldFrostInterceptRequest(request)

}

/**
 * The default webview client
 */
open class FrostWebViewClient(val web: FrostWebView) : BaseWebViewClient() {

    private val refresh: Subject<Boolean> = web.parent.refreshObservable
    private val isMain = web.parent.baseEnum != null

    protected inline fun v(crossinline message: () -> Any?) = L.v { "web client: ${message()}" }

    override fun onPageStarted(view: WebView, url: String?, favicon: Bitmap?) {
        super.onPageStarted(view, url, favicon)
        if (url == null) return
        v { "loading $url" }
        refresh.onNext(true)
    }

    private fun injectBackgroundColor() {
        web.setBackgroundColor(
                when {
                    isMain -> Color.TRANSPARENT
                    web.url.isFacebookUrl -> Prefs.bgColor.withAlpha(255)
                    else -> Color.WHITE
                }
        )
    }


    override fun onPageCommitVisible(view: WebView, url: String?) {
        super.onPageCommitVisible(view, url)
        injectBackgroundColor()
        if (url.isFacebookUrl)
            view.jsInject(
                    CssAssets.ROUND_ICONS.maybe(Prefs.showRoundedIcons),
                    CssHider.HEADER,
                    CssHider.CORE,
                    CssHider.COMPOSER.maybe(!Prefs.showComposer),
                    CssHider.PEOPLE_YOU_MAY_KNOW.maybe(!Prefs.showSuggestedFriends && IS_FROST_PRO),
                    CssHider.SUGGESTED_GROUPS.maybe(!Prefs.showSuggestedGroups && IS_FROST_PRO),
                    Prefs.themeInjector,
                    CssHider.NON_RECENT.maybe((web.url?.contains("?sk=h_chr") ?: false)
                            && Prefs.aggressiveRecents),
                    JsAssets.DOCUMENT_WATCHER)
        else
            refresh.onNext(false)
    }

    override fun onPageFinished(view: WebView, url: String?) {
        url ?: return
        v { "finished $url" }
        if (!url.isFacebookUrl) {
            refresh.onNext(false)
            return
        }
        onPageFinishedActions(url)
    }

    open internal fun onPageFinishedActions(url: String) {
        if (url.startsWith("${FbItem.MESSAGES.url}/read/") && Prefs.messageScrollToBottom)
            web.pageDown(true)
        injectAndFinish()
    }

    internal fun injectAndFinish() {
        v { "page finished reveal" }
        refresh.onNext(false)
        injectBackgroundColor()
        web.jsInject(
                JsActions.LOGIN_CHECK,
                JsAssets.CLICK_A,
                JsAssets.TEXTAREA_LISTENER,
                CssHider.ADS.maybe(!Prefs.showFacebookAds && IS_FROST_PRO),
                JsAssets.CONTEXT_A,
                JsAssets.MEDIA,
                JsAssets.HEADER_BADGES.maybe(web.parent.baseEnum != null)
        )
    }

    open fun handleHtml(html: String?) {
        L.d { "Handle Html" }
    }

    open fun emit(flag: Int) {
        L.d { "Emit $flag" }
    }

    /**
     * Helper to format the request and launch it
     * returns true to override the url
     * returns false if we are already in an overlaying activity
     */
    private fun launchRequest(request: WebResourceRequest): Boolean {
        v { "Launching url: ${request.url}" }
        return web.requestWebOverlay(request.url.toString())
    }

    private fun launchImage(url: String, text: String? = null): Boolean {
        v { "Launching image: $url" }
        web.context.launchImageActivity(url, text)
        if (web.canGoBack()) web.goBack()
        return true
    }

    override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
        v { "Url loading: ${request.url}" }
        val path = request.url?.path ?: return super.shouldOverrideUrlLoading(view, request)
        v { "Url path $path" }
        val url = request.url.toString()
        if (url.isExplicitIntent) {
            view.context.resolveActivityForUri(request.url)
            return true
        }
        if (path.startsWith("/composer/")) return launchRequest(request)
        if (url.isImageUrl)
            return launchImage(url)
        if (Prefs.linksInDefaultApp && view.context.resolveActivityForUri(request.url)) return true
        return super.shouldOverrideUrlLoading(view, request)
    }

}

private const val EMIT_THEME = 0b1
private const val EMIT_ID = 0b10
private const val EMIT_COMPLETE = EMIT_THEME or EMIT_ID
private const val EMIT_FINISH = 0

/**
 * Client variant for the menu view
 */
class FrostWebViewClientMenu(web: FrostWebView) : FrostWebViewClient(web) {

    private val String.shouldInjectMenu
        get() = when (removePrefix(FB_URL_BASE)) {
            "settings",
            "settings#",
            "settings#!/settings?soft=bookmarks" -> true
            else -> false
        }

    override fun onPageFinished(view: WebView, url: String?) {
        super.onPageFinished(view, url)
        if (url == null) return
        if (url.shouldInjectMenu) jsInject(JsAssets.MENU)
    }

    override fun emit(flag: Int) {
        super.emit(flag)
        when (flag) {
            EMIT_FINISH -> super.injectAndFinish()
        }
    }

    override fun onPageFinishedActions(url: String) {
        v { "Should inject ${url.shouldInjectMenu}" }
        if (!url.shouldInjectMenu) injectAndFinish()
    }
}