Skip to content

Commit

Permalink
Aw: Support simplified dark mode for pre-Q platform
Browse files Browse the repository at this point in the history
WebView relies on android.R.attr.isLightTheme to detect if the
app in dark theme, it only works for Q+, this patch finds
isLightTheme resource by name for pre-Q platform, Android X uses
this attri for dark theme, it should work for most cases.

Bug: 1345249
Change-Id: I83fbd16bd9944e1fcd08473cb394fb9cd00e4a81
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3782978
Reviewed-by: Richard Coles <torne@chromium.org>
Commit-Queue: Michael Bai <michaelbai@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1027349}
  • Loading branch information
Michael Bai authored and Chromium LUCI CQ committed Jul 22, 2022
1 parent 4c00099 commit 6a8fc4e
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Build;

import androidx.annotation.VisibleForTesting;
import androidx.core.graphics.ColorUtils;
Expand Down Expand Up @@ -67,8 +68,16 @@ public static int getNightMode(Context context) {
public static int getLightTheme(Context context) {
if (sLightThemeForTesting != null) return sLightThemeForTesting;
int lightTheme = LightTheme.LIGHT_THEME_UNDEFINED;
TypedArray a =
context.getTheme().obtainStyledAttributes(new int[] {android.R.attr.isLightTheme});
int resId = android.R.attr.isLightTheme;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
// android.R.attr.isLightTheme is added in Q, for pre-Q platform, WebView
// checks if app has isLightTheme attr which could be added by Android X
// and wasn't stripped out.
resId = context.getApplicationContext().getResources().getIdentifier(
"isLightTheme", "attr", context.getApplicationContext().getPackageName());
if (resId == 0) return lightTheme;
}
TypedArray a = context.getTheme().obtainStyledAttributes(new int[] {resId});
if (a.hasValue(0)) {
lightTheme = a.getBoolean(0, true) ? LightTheme.LIGHT_THEME_TRUE
: LightTheme.LIGHT_THEME_FALSE;
Expand Down

0 comments on commit 6a8fc4e

Please sign in to comment.