From 0b432ae87f27f77faffda6e748df26c1ba77b0e0 Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Fri, 19 Mar 2021 19:24:40 -0400 Subject: [PATCH] Fix WebView's infinite expansion --- .../tachiyomi/ui/webview/BaseWebViewActivity.kt | 14 +++++++------- .../tachiyomi/ui/webview/WebViewActivity.kt | 15 +++++++++++++++ app/src/main/res/layout/webview_activity.xml | 6 ++++-- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/webview/BaseWebViewActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/webview/BaseWebViewActivity.kt index de9f2a8b55..b1dee50216 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/webview/BaseWebViewActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/webview/BaseWebViewActivity.kt @@ -15,6 +15,7 @@ import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.ui.base.activity.BaseActivity import eu.kanade.tachiyomi.util.system.ThemeUtil import eu.kanade.tachiyomi.util.system.getResourceColor +import eu.kanade.tachiyomi.util.system.isBottomTappable import eu.kanade.tachiyomi.util.system.isInNightMode import eu.kanade.tachiyomi.util.system.setDefaultSettings import eu.kanade.tachiyomi.util.view.invisible @@ -88,12 +89,13 @@ open class BaseWebViewActivity : BaseActivity() { else getResourceColor(android.R.attr.colorPrimary) } // if the android q+ device has gesture nav, transparent nav bar - else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && - (v.rootWindowInsets.systemWindowInsetBottom != v.rootWindowInsets.tappableElementInsets.bottom) - ) { + else if (v.rootWindowInsets.isBottomTappable()) { getColor(android.R.color.transparent) } else { - getResourceColor(android.R.attr.colorBackground) + ColorUtils.setAlphaComponent( + getResourceColor(R.attr.colorPrimaryVariant), + 179 + ) } v.setPadding( insets.systemWindowInsetLeft, @@ -123,9 +125,7 @@ open class BaseWebViewActivity : BaseActivity() { } val marginB = webview.marginBottom webview.setOnApplyWindowInsetsListener { v, insets -> - val bottomInset = - if (Build.VERSION.SDK_INT >= 29) insets.tappableElementInsets.bottom - else insets.systemWindowInsetBottom + val bottomInset = insets.systemWindowInsetBottom v.updateLayoutParams { bottomMargin = marginB + bottomInset } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt index 7297b9bc1a..4d695cd947 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt @@ -4,6 +4,7 @@ import android.content.Context import android.content.Intent import android.content.res.Configuration import android.graphics.Bitmap +import android.graphics.Color import android.os.Bundle import android.view.Menu import android.view.MenuItem @@ -11,6 +12,7 @@ import android.view.View import android.view.ViewGroup import android.webkit.WebView import android.widget.LinearLayout +import androidx.annotation.ColorInt import androidx.core.graphics.ColorUtils import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.source.SourceManager @@ -69,6 +71,9 @@ open class WebViewActivity : BaseWebViewActivity() { title = view?.title swipe_refresh.isEnabled = true swipe_refresh?.isRefreshing = false + val thing = view?.evaluateJavascript("getComputedStyle(document.querySelector('body')).backgroundColor") { + nested_view.setBackgroundColor(parseHTMLColor(it)) + } } override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) { @@ -87,6 +92,16 @@ open class WebViewActivity : BaseWebViewActivity() { } } + @ColorInt + fun parseHTMLColor(color: String): Int { + val trimmedColor = color.trim('"') + val rgb = Regex("""^rgb\((\d+),\s*(\d+),\s*(\d+)\)$""").find(trimmedColor) + val red = rgb?.groupValues?.getOrNull(1)?.toIntOrNull() ?: return getResourceColor(android.R.attr.colorBackground) + val green = rgb.groupValues.getOrNull(2)?.toIntOrNull() ?: return getResourceColor(android.R.attr.colorBackground) + val blue = rgb.groupValues.getOrNull(3)?.toIntOrNull() ?: return getResourceColor(android.R.attr.colorBackground) + return Color.rgb(red, green, blue) + } + override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig) invalidateOptionsMenu() diff --git a/app/src/main/res/layout/webview_activity.xml b/app/src/main/res/layout/webview_activity.xml index 19bb58ddc9..8ebe85f764 100644 --- a/app/src/main/res/layout/webview_activity.xml +++ b/app/src/main/res/layout/webview_activity.xml @@ -13,6 +13,7 @@ android:orientation="vertical" android:id="@+id/web_linear_layout" android:layout_width="match_parent" + android:fitsSystemWindows="true" android:layout_height="wrap_content"> + app:layout_scrollFlags="scroll|enterAlways" /> @@ -38,8 +39,9 @@ android:layout_height="match_parent"> + android:layout_height="wrap_content" >