Skip to content

Commit 03a9e95

Browse files
authored
resources: smoother web viewing (fixes #13090) (#13023)
1 parent ad426b1 commit 03a9e95

2 files changed

Lines changed: 65 additions & 58 deletions

File tree

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ android {
1212
applicationId "org.ole.planet.myplanet"
1313
minSdk = 26
1414
targetSdk = 36
15-
versionCode = 5364
16-
versionName = "0.53.64"
15+
versionCode = 5365
16+
versionName = "0.53.65"
1717
ndkVersion = '26.3.11579264'
1818
vectorDrawables.useSupportLibrary = true
1919
}

app/src/main/java/org/ole/planet/myplanet/ui/viewer/WebViewActivity.kt

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -141,30 +141,35 @@ class WebViewActivity : AppCompatActivity() {
141141
}
142142
}
143143

144+
144145
private fun setWebClient() {
145-
val resourceId = intent.getStringExtra("RESOURCE_ID")
146-
var assetLoader: androidx.webkit.WebViewAssetLoader? = null
147-
if (resourceId != null) {
148-
val directory = File(getExternalFilesDir(null), "ole/$resourceId")
149-
val externalPathHandler = androidx.webkit.WebViewAssetLoader.PathHandler { path ->
150-
try {
151-
val file = File(directory, path)
152-
if (file.exists() && file.canonicalPath.startsWith(directory.canonicalPath)) {
153-
val mimeType = java.net.URLConnection.guessContentTypeFromName(file.name) ?: "application/octet-stream"
154-
return@PathHandler WebResourceResponse(mimeType, "utf-8", java.io.FileInputStream(file))
155-
}
156-
} catch (e: Exception) {
157-
e.printStackTrace()
146+
val assetLoader = setupAssetLoader()
147+
activityWebViewBinding.contentWebView.wv.webViewClient = createWebViewClient(assetLoader)
148+
}
149+
150+
private fun setupAssetLoader(): androidx.webkit.WebViewAssetLoader? {
151+
val resourceId = intent.getStringExtra("RESOURCE_ID") ?: return null
152+
val directory = File(getExternalFilesDir(null), "ole/$resourceId")
153+
val externalPathHandler = androidx.webkit.WebViewAssetLoader.PathHandler { path ->
154+
try {
155+
val file = File(directory, path)
156+
if (file.exists() && file.canonicalPath.startsWith(directory.canonicalPath)) {
157+
val mimeType = java.net.URLConnection.guessContentTypeFromName(file.name) ?: "application/octet-stream"
158+
return@PathHandler WebResourceResponse(mimeType, "utf-8", java.io.FileInputStream(file))
158159
}
159-
null
160+
} catch (e: Exception) {
161+
e.printStackTrace()
160162
}
161-
162-
assetLoader = androidx.webkit.WebViewAssetLoader.Builder()
163-
.addPathHandler("/assets/", externalPathHandler)
164-
.build()
163+
null
165164
}
166165

167-
activityWebViewBinding.contentWebView.wv.webViewClient = object : WebViewClient() {
166+
return androidx.webkit.WebViewAssetLoader.Builder()
167+
.addPathHandler("/assets/", externalPathHandler)
168+
.build()
169+
}
170+
171+
private fun createWebViewClient(assetLoader: androidx.webkit.WebViewAssetLoader?): WebViewClient {
172+
return object : WebViewClient() {
168173
override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
169174
super.onPageStarted(view, url, favicon)
170175

@@ -195,43 +200,7 @@ class WebViewActivity : AppCompatActivity() {
195200

196201
override fun onPageFinished(view: WebView, url: String) {
197202
super.onPageFinished(view, url)
198-
199-
val nightModeFlags = resources.configuration.uiMode and android.content.res.Configuration.UI_MODE_NIGHT_MASK
200-
if (nightModeFlags == android.content.res.Configuration.UI_MODE_NIGHT_YES) {
201-
view.evaluateJavascript(
202-
"""
203-
(function() {
204-
document.documentElement.setAttribute('dark', 'true');
205-
document.documentElement.style.backgroundColor = '#000';
206-
document.documentElement.style.color = '#FFF';
207-
const elements = document.querySelectorAll('*');
208-
elements.forEach(el => {
209-
if (window.getComputedStyle(el).color === 'rgb(0, 0, 0)') {
210-
el.style.color = '#FFF';
211-
}
212-
});
213-
})();
214-
""".trimIndent(),
215-
null
216-
)
217-
} else {
218-
view.evaluateJavascript(
219-
"""
220-
(function() {
221-
document.documentElement.removeAttribute('dark');
222-
document.documentElement.style.backgroundColor = '#FFF';
223-
document.documentElement.style.color = '#000';
224-
const elements = document.querySelectorAll('*');
225-
elements.forEach(el => {
226-
if (window.getComputedStyle(el).color === 'rgb(255, 255, 255)') {
227-
el.style.color = '#000';
228-
}
229-
});
230-
})();
231-
""".trimIndent(),
232-
null
233-
)
234-
}
203+
applyNightMode(view)
235204
}
236205

237206
override fun onReceivedError(view: WebView, request: WebResourceRequest, error: WebResourceError) {
@@ -250,6 +219,44 @@ class WebViewActivity : AppCompatActivity() {
250219
}
251220
}
252221

222+
private fun applyNightMode(view: WebView) {
223+
val nightModeFlags = resources.configuration.uiMode and android.content.res.Configuration.UI_MODE_NIGHT_MASK
224+
if (nightModeFlags == android.content.res.Configuration.UI_MODE_NIGHT_YES) {
225+
view.evaluateJavascript(
226+
"""
227+
(function() {
228+
document.documentElement.setAttribute('dark', 'true');
229+
document.documentElement.style.backgroundColor = '#000';
230+
document.documentElement.style.color = '#FFF';
231+
const elements = document.querySelectorAll('*');
232+
elements.forEach(el => {
233+
if (window.getComputedStyle(el).color === 'rgb(0, 0, 0)') {
234+
el.style.color = '#FFF';
235+
}
236+
});
237+
})();
238+
""".trimIndent(),
239+
null
240+
)
241+
} else {
242+
view.evaluateJavascript(
243+
"""
244+
(function() {
245+
document.documentElement.removeAttribute('dark');
246+
document.documentElement.style.backgroundColor = '#FFF';
247+
document.documentElement.style.color = '#000';
248+
const elements = document.querySelectorAll('*');
249+
elements.forEach(el => {
250+
if (window.getComputedStyle(el).color === 'rgb(255, 255, 255)') {
251+
el.style.color = '#000';
252+
}
253+
});
254+
})();
255+
""".trimIndent(),
256+
null
257+
)
258+
}
259+
}
253260
private fun clearCookie() {
254261
val cookieManager = CookieManager.getInstance()
255262
cookieManager.removeAllCookies(null)

0 commit comments

Comments
 (0)