feat(android): force dark mode (#2063)

Co-authored-by: 张 云 <zhangyun@sidechef.com>
Co-authored-by: Thibault Malbranche <thibault.malbranche@epitech.eu>
This commit is contained in:
Cloud Zhang
2021-08-09 19:57:33 +02:00
committed by GitHub
co-authored by 张 云 Thibault Malbranche
parent 2c541131db
commit 19980d888d
5 changed files with 58 additions and 0 deletions
@@ -46,6 +46,8 @@ import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;
import androidx.core.util.Pair;
import androidx.webkit.WebSettingsCompat;
import androidx.webkit.WebViewFeature;
import com.facebook.common.logging.FLog;
import com.facebook.react.modules.core.PermissionAwareActivity;
@@ -609,6 +611,27 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
((RNCWebView) view).setHasScrollEvent(hasScrollEvent);
}
@ReactProp(name = "forceDarkOn")
public void setForceDarkOn(WebView view, boolean enabled) {
// Only Android 10+ support dark mode
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
// Switch WebView dark mode
if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
int forceDarkMode = enabled ? WebSettingsCompat.FORCE_DARK_ON : WebSettingsCompat.FORCE_DARK_OFF;
WebSettingsCompat.setForceDark(view.getSettings(), forceDarkMode);
}
// Set how WebView content should be darkened.
// PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING: checks for the "color-scheme" <meta> tag.
// If present, it uses media queries. If absent, it applies user-agent (automatic)
// More information about Force Dark Strategy can be found here:
// https://developer.android.com/reference/androidx/webkit/WebSettingsCompat#setForceDarkStrategy(android.webkit.WebSettings)
if (enabled && WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK_STRATEGY)) {
WebSettingsCompat.setForceDarkStrategy(view.getSettings(), WebSettingsCompat.DARK_STRATEGY_PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING);
}
}
}
@Override
protected void addEventEmitters(ThemedReactContext reactContext, WebView view) {
// Do not register default touch emitter and let WebView implementation handle touches