Skip to content

Commit c2051db

Browse files
hunterstichdrchen
authored andcommitted
[Internal] Avoid calling deprecated edge-to-edge APIs on 35+
Resolves #4626 Resolves #4507 Resolves #4732 Resolves #4607 Resolves #4883 PiperOrigin-RevId: 800945380
1 parent c99cba6 commit c2051db

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

lib/java/com/google/android/material/bottomsheet/BottomSheetDialog.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ protected void onCreate(Bundle savedInstanceState) {
136136
Window window = getWindow();
137137
if (window != null) {
138138
// The status bar should always be transparent because of the window animation.
139-
window.setStatusBarColor(0);
139+
EdgeToEdgeUtils.setStatusBarColor(window, 0);
140140

141141
window.addFlags(LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
142142
if (VERSION.SDK_INT < VERSION_CODES.M) {
@@ -187,8 +187,8 @@ public void onAttachedToWindow() {
187187
Window window = getWindow();
188188
if (window != null) {
189189
// If the navigation bar is transparent at all the BottomSheet should be edge to edge.
190-
boolean drawEdgeToEdge =
191-
edgeToEdgeEnabled && Color.alpha(window.getNavigationBarColor()) < 255;
190+
boolean drawEdgeToEdge = edgeToEdgeEnabled
191+
&& Color.alpha(EdgeToEdgeUtils.getNavigationBarColor(window)) < 255;
192192
if (container != null) {
193193
container.setFitsSystemWindows(!drawEdgeToEdge);
194194
}

lib/java/com/google/android/material/internal/EdgeToEdgeUtils.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ public static void applyEdgeToEdge(
9494
int statusBarColor = getStatusBarColor(window.getContext(), edgeToEdgeEnabled);
9595
int navigationBarColor = getNavigationBarColor(window.getContext(), edgeToEdgeEnabled);
9696

97-
window.setStatusBarColor(statusBarColor);
98-
window.setNavigationBarColor(navigationBarColor);
97+
setStatusBarColor(window, statusBarColor);
98+
setNavigationBarColor(window, navigationBarColor);
9999

100100
setLightStatusBar(
101101
window,
@@ -146,6 +146,12 @@ private static int getStatusBarColor(Context context, boolean isEdgeToEdgeEnable
146146
return MaterialColors.getColor(context, android.R.attr.statusBarColor, Color.BLACK);
147147
}
148148

149+
public static void setStatusBarColor(@NonNull Window window, @ColorInt int color) {
150+
if (VERSION.SDK_INT < VERSION_CODES.VANILLA_ICE_CREAM) {
151+
window.setStatusBarColor(color);
152+
}
153+
}
154+
149155
private static int getNavigationBarColor(Context context, boolean isEdgeToEdgeEnabled) {
150156
// Light navigation bars are only supported on O_MR1+. So we need to use a translucent black
151157
// navigation bar instead to ensure the text/icon contrast of it.
@@ -160,6 +166,19 @@ private static int getNavigationBarColor(Context context, boolean isEdgeToEdgeEn
160166
return MaterialColors.getColor(context, android.R.attr.navigationBarColor, Color.BLACK);
161167
}
162168

169+
public static int getNavigationBarColor(@NonNull Window window) {
170+
if (VERSION.SDK_INT < VERSION_CODES.VANILLA_ICE_CREAM) {
171+
return window.getNavigationBarColor();
172+
}
173+
return Color.TRANSPARENT;
174+
}
175+
176+
public static void setNavigationBarColor(@NonNull Window window, @ColorInt int color) {
177+
if (VERSION.SDK_INT < VERSION_CODES.VANILLA_ICE_CREAM) {
178+
window.setNavigationBarColor(color);
179+
}
180+
}
181+
163182
private static boolean isUsingLightSystemBar(int systemBarColor, boolean isLightBackground) {
164183
return isColorLight(systemBarColor) || (systemBarColor == TRANSPARENT && isLightBackground);
165184
}

lib/java/com/google/android/material/navigation/NavigationView.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import com.google.android.material.animation.AnimationUtils;
7575
import com.google.android.material.drawable.DrawableUtils;
7676
import com.google.android.material.internal.ContextUtils;
77+
import com.google.android.material.internal.EdgeToEdgeUtils;
7778
import com.google.android.material.internal.NavigationMenu;
7879
import com.google.android.material.internal.NavigationMenuPresenter;
7980
import com.google.android.material.internal.ScrimInsetsFrameLayout;
@@ -1130,8 +1131,8 @@ public void onGlobalLayout() {
11301131
Rect displayBounds = WindowUtils.getCurrentWindowBounds(activity);
11311132

11321133
boolean isBehindSystemNav = displayBounds.height() - getHeight() == tmpLocation[1];
1133-
boolean hasNonZeroAlpha =
1134-
Color.alpha(activity.getWindow().getNavigationBarColor()) != 0;
1134+
boolean hasNonZeroAlpha = Color.alpha(
1135+
EdgeToEdgeUtils.getNavigationBarColor(activity.getWindow())) != 0;
11351136
setDrawBottomInsetForeground(
11361137
isBehindSystemNav && hasNonZeroAlpha && isBottomInsetScrimEnabled());
11371138

lib/java/com/google/android/material/sidesheet/SheetDialog.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import androidx.core.view.ViewCompat;
4444
import androidx.core.view.WindowCompat;
4545
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
46+
import com.google.android.material.internal.EdgeToEdgeUtils;
4647
import com.google.android.material.motion.MaterialBackOrchestrator;
4748
import com.google.android.material.sidesheet.Sheet.StableSheetState;
4849

@@ -101,7 +102,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
101102
Window window = getWindow();
102103
if (window != null) {
103104
// The status bar should always be transparent because of the window animation.
104-
window.setStatusBarColor(0);
105+
EdgeToEdgeUtils.setStatusBarColor(window, 0);
105106

106107
window.addFlags(LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
107108
if (VERSION.SDK_INT < VERSION_CODES.M) {

0 commit comments

Comments
 (0)