Skip to content

Commit

Permalink
Fixed WebView dark theme not follow system theme correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
WSTxda committed Aug 26, 2024
1 parent 8e46142 commit f4cbcd5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion play-services-core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@

<activity
android:name="org.microg.gms.auth.login.LoginActivity"
android:configChanges="keyboardHidden|keyboard|screenSize"
android:configChanges="uiMode|screenSize|orientation|keyboardHidden|keyboard|screenSize"
android:exported="true"
android:process=":ui"
android:theme="@style/LoginTheme">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,19 +231,23 @@ private void init() {
}
}

private static boolean isSystemDarkTheme(Context context) {
int nightModeFlags = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
return nightModeFlags == Configuration.UI_MODE_NIGHT_YES;
}

private static WebView createWebView(Context context) {
Context themedContext = new ContextThemeWrapper(context, R.style.LoginTheme);
boolean systemIsDark = isSystemDarkTheme(context);

WebView webView = new WebView(themedContext);
WebView webView = new WebView(context);
webView.setVisibility(View.INVISIBLE);
webView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
webView.setBackgroundColor(Color.TRANSPARENT);

boolean appIsDark = (themedContext.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;

// Apply dark theme to WebView based on system state
if (Build.VERSION.SDK_INT >= 29) {
if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
WebSettingsCompat.setForceDark(webView.getSettings(), appIsDark ? WebSettingsCompat.FORCE_DARK_ON : WebSettingsCompat.FORCE_DARK_OFF);
WebSettingsCompat.setForceDark(webView.getSettings(), systemIsDark ? WebSettingsCompat.FORCE_DARK_ON : WebSettingsCompat.FORCE_DARK_OFF);
}
}

Expand Down

0 comments on commit f4cbcd5

Please sign in to comment.