Simplify themes (#2923)

* Merge Follow Home Assistant/Follow System Settings theme

 - As these two themes behave the same now, don't present two options but instead just one. The default setting has been renamed back to "Follow System Settings" to best match expectations. Internally the "android" setting still works, settings will show "Not set".

* Remove useless webview force dark

 - The WebView force dark settings are no longer required (and will no longer be supported when targeting Android 13), as the latest appcompat/webview/HA frontend already respect the app's theme setting and allow overriding it
This commit is contained in:
Joris Pelgröm 2022-10-01 22:11:31 +02:00 committed by GitHub
parent 60aadc906a
commit c2937e77aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 6 additions and 57 deletions

View file

@ -66,7 +66,7 @@ class AuthenticationFragment : Fragment() {
MdcTheme {
AndroidView({
WebView(requireContext()).apply {
themesManager.setThemeForWebView(requireContext(), settings)
themesManager.setThemeForWebView()
settings.javaScriptEnabled = true
settings.domStorageEnabled = true
settings.userAgentString = settings.userAgentString + " ${HomeAssistantApis.USER_AGENT_STRING}"

View file

@ -1,12 +1,7 @@
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
@ -40,52 +35,9 @@ class ThemesManager @Inject constructor(
}
}
fun setThemeForWebView(context: Context, webSettings: WebSettings) {
if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK_STRATEGY) &&
WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)
) {
val theme = getCurrentTheme()
setNightModeBasedOnTheme(theme)
WebSettingsCompat.setForceDarkStrategy(
webSettings,
WebSettingsCompat.DARK_STRATEGY_WEB_THEME_DARKENING_ONLY
)
when (theme) {
"newTheme" -> {
// Just a template for custom themes
// context.setTheme(android.R.style.newTheme);
}
"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
)
}
}
}
fun setThemeForWebView() {
val theme = getCurrentTheme()
setNightModeBasedOnTheme(theme)
}
private fun setNightModeBasedOnTheme(theme: String?) {

View file

@ -621,7 +621,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(this, webView.settings)
themesManager.setThemeForWebView()
val cookieManager = CookieManager.getInstance()
cookieManager.setAcceptCookie(true)

View file

@ -2,13 +2,11 @@
<resources>
<string-array name="pref_theme_option_labels">
<item>@string/themes_option_label_system</item>
<item>@string/themes_option_label_android</item>
<item>@string/themes_option_label_light</item>
<item>@string/themes_option_label_dark</item>
</string-array>
<string-array name="pref_theme_option_values">
<item>@string/themes_option_value_system</item>
<item>@string/themes_option_value_android</item>
<item>@string/themes_option_value_light</item>
<item>@string/themes_option_value_dark</item>
</string-array>

View file

@ -708,10 +708,9 @@
<string name="template_widget_default">Enter Template Here</string>
<string name="template_widget_desc">Render any template with HTML formatting</string>
<string name="template_widget">Template Widget</string>
<string name="themes_option_label_android">Follow System Settings</string>
<string name="themes_option_label_dark">Dark</string>
<string name="themes_option_label_light">Light</string>
<string name="themes_option_label_system">Follow Home Assistant</string>
<string name="themes_option_label_system">Follow System Settings</string>
<string name="themes_option_value_android">android</string>
<string name="themes_option_value_dark">dark</string>
<string name="themes_option_value_light">light</string>