Skip to content

Commit 3e55f14

Browse files
ldjcmuafohrman
authored andcommitted
Fix status and nav bar color in catalog for edge to edge mode
PiperOrigin-RevId: 266467749
1 parent 6e9b3c6 commit 3e55f14

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

catalog/java/io/material/catalog/windowpreferences/WindowPreferencesManager.java

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static android.view.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
2222
import static android.view.View.SYSTEM_UI_FLAG_VISIBLE;
2323

24+
import android.annotation.TargetApi;
2425
import android.content.Context;
2526
import android.content.SharedPreferences;
2627
import android.graphics.Color;
@@ -38,6 +39,7 @@ public class WindowPreferencesManager {
3839

3940
private static final String PREFERENCES_NAME = "window_preferences";
4041
private static final String KEY_EDGE_TO_EDGE_ENABLED = "edge_to_edge_enabled";
42+
private static final int EDGE_TO_EDGE_BAR_ALPHA = 128;
4143

4244
@RequiresApi(VERSION_CODES.LOLLIPOP)
4345
private static final int EDGE_TO_EDGE_FLAGS =
@@ -66,14 +68,10 @@ public void applyEdgeToEdgePreference(Window window) {
6668
if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP) {
6769
return;
6870
}
69-
7071
boolean edgeToEdgeEnabled = isEdgeToEdgeEnabled();
71-
int statusBarColor = edgeToEdgeEnabled
72-
? TRANSPARENT
73-
: MaterialColors.getColor(context, android.R.attr.statusBarColor, Color.BLACK);
74-
int navbarColor = edgeToEdgeEnabled
75-
? TRANSPARENT
76-
: MaterialColors.getColor(context, android.R.attr.navigationBarColor, Color.BLACK);
72+
73+
int statusBarColor = getStatusBarColor(isEdgeToEdgeEnabled());
74+
int navbarColor = getNavBarColor(isEdgeToEdgeEnabled());
7775

7876
boolean lightBackground = isColorLight(
7977
MaterialColors.getColor(context, android.R.attr.colorBackground, Color.BLACK));
@@ -97,8 +95,36 @@ public void applyEdgeToEdgePreference(Window window) {
9795
decorView.setSystemUiVisibility(systemUiVisibility);
9896
}
9997

100-
private static boolean isColorLight(@ColorInt int color){
101-
return color != TRANSPARENT && ColorUtils.calculateLuminance(color) > 0.5;
98+
@SuppressWarnings("RestrictTo")
99+
@TargetApi(VERSION_CODES.LOLLIPOP)
100+
private int getStatusBarColor(boolean isEdgeToEdgeEnabled) {
101+
if (isEdgeToEdgeEnabled && VERSION.SDK_INT < VERSION_CODES.M) {
102+
int opaqueStatusBarColor =
103+
MaterialColors.getColor(context, android.R.attr.statusBarColor, Color.BLACK);
104+
return ColorUtils.setAlphaComponent(opaqueStatusBarColor, EDGE_TO_EDGE_BAR_ALPHA);
105+
}
106+
if (isEdgeToEdgeEnabled) {
107+
return TRANSPARENT;
108+
}
109+
return MaterialColors.getColor(context, android.R.attr.statusBarColor, Color.BLACK);
110+
}
111+
112+
@SuppressWarnings("RestrictTo")
113+
@TargetApi(VERSION_CODES.LOLLIPOP)
114+
private int getNavBarColor(boolean isEdgeToEdgeEnabled) {
115+
if (isEdgeToEdgeEnabled && VERSION.SDK_INT < VERSION_CODES.O_MR1) {
116+
int opaqueNavBarColor =
117+
MaterialColors.getColor(context, android.R.attr.navigationBarColor, Color.BLACK);
118+
return ColorUtils.setAlphaComponent(opaqueNavBarColor, EDGE_TO_EDGE_BAR_ALPHA);
119+
}
120+
if (isEdgeToEdgeEnabled) {
121+
return TRANSPARENT;
122+
}
123+
return MaterialColors.getColor(context, android.R.attr.navigationBarColor, Color.BLACK);
124+
}
125+
126+
private static boolean isColorLight(@ColorInt int color) {
127+
return color != TRANSPARENT && ColorUtils.calculateLuminance(color) > 0.5;
102128
}
103129

104130
private SharedPreferences getSharedPreferences() {

0 commit comments

Comments
 (0)