aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClients2.kt
blob: 008b11976a910d1d736b6cec2762110bde207160 (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
192
193
194
195
196
/*
 * Copyright 2018 Allan Wang
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.pitchedapps.frost.web

import android.graphics.Bitmap
import android.graphics.Color
import android.webkit.WebResourceRequest
import android.webkit.WebView
import ca.allanwang.kau.utils.withAlpha
import com.pitchedapps.frost.enums.ThemeCategory
import com.pitchedapps.frost.injectors.JsActions
import com.pitchedapps.frost.injectors.JsAssets
import com.pitchedapps.frost.injectors.jsInject
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.isFacebookUrl
import com.pitchedapps.frost.utils.isMessengerUrl
import com.pitchedapps.frost.utils.launchImageActivity
import com.pitchedapps.frost.views.FrostWebView
import dagger.Binds
import dagger.Module
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
import javax.inject.Inject
import javax.inject.Qualifier

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

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class FrostWebClient

@EntryPoint
@InstallIn(FrostWebComponent::class)
interface FrostWebClientEntryPoint {

    @FrostWebScoped
    @FrostWebClient
    fun webClient(): FrostWebViewClient
}

@Module
@InstallIn(FrostWebComponent::class)
interface FrostWebViewClientModule {
    @Binds
    @FrostWebClient
    fun webClient(binds: FrostWebViewClient2): FrostWebViewClient
}

/**
 * The default webview client
 */
open class FrostWebViewClient2 @Inject constructor(
    web: FrostWebView,
    @FrostRefresh private val refreshEmit: FrostEmitter<Boolean>
) : FrostWebViewClient(web) {

    init {
        L.i { "Refresh web client 2" }
    }

    override fun doUpdateVisitedHistory(view: WebView, url: String?, isReload: Boolean) {
        super.doUpdateVisitedHistory(view, url, isReload)
        urlSupportsRefresh = urlSupportsRefresh(url)
        web.parent.swipeAllowedByPage = urlSupportsRefresh
        view.jsInject(
            JsAssets.AUTO_RESIZE_TEXTAREA.maybe(prefs.autoExpandTextBox),
            prefs = prefs
        )
        v { "History $url; refresh $urlSupportsRefresh" }
    }

    private fun urlSupportsRefresh(url: String?): Boolean {
        if (url == null) return false
        if (url.isMessengerUrl) return false
        if (!url.isFacebookUrl) return true
        if (url.contains("soft=composer")) return false
        if (url.contains("sharer.php") || url.contains("sharer-dialog.php")) return false
        return true
    }

    private fun WebView.facebookJsInject() {
        jsInject(*facebookJsInjectors.toTypedArray(), prefs = prefs)
    }

    private fun WebView.messengerJsInject() {
        jsInject(
            themeProvider.injector(ThemeCategory.MESSENGER),
            prefs = prefs
        )
    }

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

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

    override fun onPageCommitVisible(view: WebView, url: String?) {
        super.onPageCommitVisible(view, url)
        injectBackgroundColor()
        when {
            url.isFacebookUrl -> {
                v { "FB Page commit visible" }
                view.facebookJsInject()
            }
            url.isMessengerUrl -> {
                v { "Messenger Page commit visible" }
                view.messengerJsInject()
            }
            else -> {
                refreshEmit(false)
            }
        }
    }

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

    internal override fun injectAndFinish() {
        v { "page finished reveal" }
        refreshEmit(false)
        injectBackgroundColor()
        when {
            web.url.isFacebookUrl -> {
                web.jsInject(
                    JsActions.LOGIN_CHECK,
                    JsAssets.TEXTAREA_LISTENER,
                    JsAssets.HEADER_BADGES.maybe(isMain),
                    prefs = prefs
                )
                web.facebookJsInject()
            }
            web.url.isMessengerUrl -> {
                web.messengerJsInject()
            }
        }
    }

    /**
     * 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, cookie: String? = null): Boolean {
        v { "Launching image: $url" }
        web.context.launchImageActivity(url, text, cookie)
        if (web.canGoBack()) web.goBack()
        return true
    }
}

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