diff --git a/app/src/main/java/io/homeassistant/companion/android/onboarding/authentication/AuthenticationFragment.kt b/app/src/main/java/io/homeassistant/companion/android/onboarding/authentication/AuthenticationFragment.kt index c458441e0..dc0902e5d 100644 --- a/app/src/main/java/io/homeassistant/companion/android/onboarding/authentication/AuthenticationFragment.kt +++ b/app/src/main/java/io/homeassistant/companion/android/onboarding/authentication/AuthenticationFragment.kt @@ -66,7 +66,7 @@ class AuthenticationFragment : Fragment() { MdcTheme { AndroidView({ WebView(requireContext()).apply { - themesManager.setThemeForWebView() + themesManager.setThemeForWebView(requireContext(), settings) settings.javaScriptEnabled = true settings.domStorageEnabled = true settings.userAgentString = settings.userAgentString + " ${HomeAssistantApis.USER_AGENT_STRING}" diff --git a/app/src/main/java/io/homeassistant/companion/android/themes/ThemesManager.kt b/app/src/main/java/io/homeassistant/companion/android/themes/ThemesManager.kt index 5ceb27adb..739d22a5b 100644 --- a/app/src/main/java/io/homeassistant/companion/android/themes/ThemesManager.kt +++ b/app/src/main/java/io/homeassistant/companion/android/themes/ThemesManager.kt @@ -1,7 +1,12 @@ package io.homeassistant.companion.android.themes +import android.content.Context +import android.content.res.Configuration import android.os.Build +import android.webkit.WebSettings import androidx.appcompat.app.AppCompatDelegate +import androidx.webkit.WebSettingsCompat +import androidx.webkit.WebViewFeature import io.homeassistant.companion.android.common.data.prefs.PrefsRepository import kotlinx.coroutines.runBlocking import javax.inject.Inject @@ -35,9 +40,40 @@ class ThemesManager @Inject constructor( } } - fun setThemeForWebView() { + @Suppress("DEPRECATION") + fun setThemeForWebView(context: Context, webSettings: WebSettings) { val theme = getCurrentTheme() setNightModeBasedOnTheme(theme) + + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU && + WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK) && + WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK_STRATEGY) + ) { + // While an up-to-date official WebView respects the app light/dark theme automatically, + // some users are running forks where this doesn't seem to work or are reportedly + // unable to update. These deprecated settings are set to preserve compatibility for + // those users. Issue: https://github.com/home-assistant/android/issues/2985 + WebSettingsCompat.setForceDarkStrategy( + webSettings, WebSettingsCompat.DARK_STRATEGY_WEB_THEME_DARKENING_ONLY + ) + when (theme) { + "dark" -> { + WebSettingsCompat.setForceDark(webSettings, WebSettingsCompat.FORCE_DARK_ON) + } + "android", "system" -> { + val nightModeFlags = + context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK + if (nightModeFlags == Configuration.UI_MODE_NIGHT_YES) { + WebSettingsCompat.setForceDark(webSettings, WebSettingsCompat.FORCE_DARK_ON) + } else { + WebSettingsCompat.setForceDark(webSettings, WebSettingsCompat.FORCE_DARK_OFF) + } + } + else -> { + WebSettingsCompat.setForceDark(webSettings, WebSettingsCompat.FORCE_DARK_OFF) + } + } + } } private fun setNightModeBasedOnTheme(theme: String?) { diff --git a/app/src/main/java/io/homeassistant/companion/android/webview/WebViewActivity.kt b/app/src/main/java/io/homeassistant/companion/android/webview/WebViewActivity.kt index e9976a455..05b1b0a93 100644 --- a/app/src/main/java/io/homeassistant/companion/android/webview/WebViewActivity.kt +++ b/app/src/main/java/io/homeassistant/companion/android/webview/WebViewActivity.kt @@ -637,7 +637,7 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi // This enables the ability to have the launch screen behind the WebView until the web frontend gets rendered binding.webview.setBackgroundColor(Color.TRANSPARENT) - themesManager.setThemeForWebView() + themesManager.setThemeForWebView(this, webView.settings) val cookieManager = CookieManager.getInstance() cookieManager.setAcceptCookie(true)