This repository has been archived by the owner on Aug 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContentWebView.kt
189 lines (168 loc) · 5.59 KB
/
ContentWebView.kt
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
package org.schulcloud.mobile.views
import android.annotation.TargetApi
import android.content.Context
import android.graphics.Color
import android.net.Uri
import android.os.Build
import android.util.AttributeSet
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.annotation.AttrRes
import okhttp3.Request
import org.schulcloud.mobile.BuildConfig
import org.schulcloud.mobile.R
import org.schulcloud.mobile.utils.*
import java.io.IOException
import kotlin.properties.Delegates
open class ContentWebView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
@AttrRes defStyleAttr: Int = 0
) : WebView(context, attrs, defStyleAttr) {
companion object {
val TAG: String = ContentWebView::class.java.simpleName
// language=HTML
val CONTENT_TEXT_FORMAT = """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<base href="https://$HOST" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
max-width: 100%%;
word-wrap: break-word;
}
body {
margin: 0;
font-family: 'Roboto', sans-serif;
color: %2${"$"}s;
}
body a {
color: %3${"$"}s;
text-decoration: none;
}
body > :first-child {
margin-top: 0;
}
body > :nth-last-child(2) {
margin-bottom: 0;
}
table {
table-layout: fixed;
width: 100%%;
}
ul {
-webkit-padding-start: 25px;
}
</style>
</head>
<body>
%1${"$"}s
<script>
for (tag of document.body.getElementsByTagName('*')) {
tag.style.width = '';
tag.style.height = '';
}
</script>
</body>
</html>"""
}
var content by Delegates.observable<String?>(null) { _, _, _ ->
onContentUpdated()
}
var contentFallback by Delegates.observable<String?>(null) { _, _, _ ->
onContentUpdated()
}
init {
if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
WebView.setWebContentsDebuggingEnabled(true)
@Suppress("SetJavaScriptEnabled")
settings.javaScriptEnabled = true
webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
return openUrl(Uri.parse(url))
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
override fun shouldOverrideUrlLoading(
view: WebView,
request: WebResourceRequest
): Boolean {
return openUrl(request.url)
}
private fun openUrl(url: Uri): Boolean {
if (url.toString() == getUrl())
return false
context.openUrl(url)
return true
}
override fun shouldInterceptRequest(view: WebView, url: String): WebResourceResponse? {
return handleRequest(url)
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
override fun shouldInterceptRequest(
view: WebView,
request: WebResourceRequest
): WebResourceResponse? {
return handleRequest(request.url.toString())
}
private fun handleRequest(url: String): WebResourceResponse? {
return try {
val response = HTTP_CLIENT.newCall(Request.Builder().url(url).build()).execute()
WebResourceResponse(
response.header(HEADER_CONTENT_TYPE)?.substringBefore(';'),
response.header(HEADER_CONTENT_ENCODING, ENCODING_UTF_8),
response.body()?.byteStream())
} catch (e: IOException) {
null
} catch (e: IllegalStateException) {
null
} catch (e: IllegalArgumentException) {
null
}
}
}
}
override fun loadUrl(url: String?) {
clear()
super.loadUrl(url)
}
override fun loadUrl(url: String?, additionalHttpHeaders: MutableMap<String, String>?) {
clear()
super.loadUrl(url, additionalHttpHeaders)
}
override fun loadData(data: String?, mimeType: String?, encoding: String?) {
clear()
super.loadData(data, mimeType, encoding)
}
override fun loadDataWithBaseURL(
baseUrl: String?,
data: String?,
mimeType: String?,
encoding: String?,
historyUrl: String?
) {
clear()
super.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl)
}
private fun clear() {
setBackgroundColor(Color.TRANSPARENT)
// Clear content to fix size if new size is smaller than current one
super.loadDataWithBaseURL(null, null, MIME_TEXT_HTML, ENCODING_UTF_8, null)
}
fun setUrl(url: String?) {
loadUrl(url)
}
private fun onContentUpdated() {
val content = content.takeUnless { content.isNullOrBlank() } ?: contentFallback ?: ""
loadDataWithBaseURL(HOST,
CONTENT_TEXT_FORMAT.format(
content,
context.getColorFromAttr(R.attr.colorOnBackground).cssColor,
context.getColorFromAttr(R.attr.colorOnBackgroundPrimary).cssColor),
MIME_TEXT_HTML, ENCODING_UTF_8, null)
}
}