Skip to content

Commit f2e70d2

Browse files
afohrmanhunterstich
authored andcommitted
[Android U] Replaced BuildCompat#isAtLeastU() method calls with explicit SDK checks for UPSIDE_DOWN_CAKE.
Before 1.12.0-alpha01 which was released on 6/7, BuildCompat#isAtLeastU() only returns true if API level is greater than 33 and the version running is a pre-release. Two things happened on 6/7 -- the version became a "platform stable" version, and core library version 1.12.0-alpha01 was released, which changes BuildCompat#isAtLeastU() to return true if the version is 34 and stable. PiperOrigin-RevId: 540304068
1 parent a67a885 commit f2e70d2

File tree

9 files changed

+17
-19
lines changed

9 files changed

+17
-19
lines changed

catalog/java/io/material/catalog/navigationdrawer/CustomNavigationDrawerDemoActivity.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import android.animation.Animator.AnimatorListener;
2222
import android.animation.ValueAnimator.AnimatorUpdateListener;
23+
import android.os.Build.VERSION;
2324
import android.os.Build.VERSION_CODES;
2425
import android.os.Bundle;
2526
import androidx.appcompat.app.ActionBarDrawerToggle;
@@ -35,7 +36,6 @@
3536
import androidx.annotation.NonNull;
3637
import androidx.annotation.Nullable;
3738
import androidx.annotation.RequiresApi;
38-
import androidx.core.os.BuildCompat;
3939
import androidx.drawerlayout.widget.DrawerLayout;
4040
import androidx.drawerlayout.widget.DrawerLayout.LayoutParams;
4141
import androidx.drawerlayout.widget.DrawerLayout.SimpleDrawerListener;
@@ -89,7 +89,7 @@ public void onDrawerOpened(@NonNull View drawerView) {
8989
currentDrawerView = drawerView;
9090
sideContainerBackHelper = new MaterialSideContainerBackHelper(drawerView);
9191

92-
if (BuildCompat.isAtLeastU()) {
92+
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
9393
if (drawerOnBackAnimationCallback == null) {
9494
drawerOnBackAnimationCallback = createOnBackAnimationCallback();
9595
}
@@ -111,7 +111,7 @@ public void onDrawerClosed(@NonNull View drawerView) {
111111
currentDrawerView = null;
112112
sideContainerBackHelper = null;
113113

114-
if (BuildCompat.isAtLeastU()) {
114+
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
115115
if (drawerOnBackAnimationCallback != null) {
116116
getOnBackInvokedDispatcher()
117117
.unregisterOnBackInvokedCallback(drawerOnBackAnimationCallback);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
import androidx.coordinatorlayout.widget.CoordinatorLayout.LayoutParams;
6464
import androidx.core.graphics.Insets;
6565
import androidx.core.math.MathUtils;
66-
import androidx.core.os.BuildCompat;
6766
import androidx.core.view.ViewCompat;
6867
import androidx.core.view.WindowInsetsCompat;
6968
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
@@ -1610,7 +1609,7 @@ public void handleBackInvoked() {
16101609
return;
16111610
}
16121611
BackEventCompat backEvent = bottomContainerBackHelper.onHandleBackInvoked();
1613-
if (backEvent == null || !BuildCompat.isAtLeastU()) {
1612+
if (backEvent == null || VERSION.SDK_INT < VERSION_CODES.UPSIDE_DOWN_CAKE) {
16141613
// If using traditional button system nav or if pre-U, just hide or collapse the bottom sheet.
16151614
setState(hideable ? STATE_HIDDEN : STATE_COLLAPSED);
16161615
return;

lib/java/com/google/android/material/color/ColorContrast.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import android.app.UiModeManager;
2323
import android.app.UiModeManager.ContrastChangeListener;
2424
import android.content.Context;
25+
import android.os.Build.VERSION;
2526
import android.os.Build.VERSION_CODES;
2627
import android.os.Bundle;
2728
import android.view.ContextThemeWrapper;
@@ -30,7 +31,6 @@
3031
import androidx.annotation.Nullable;
3132
import androidx.annotation.RequiresApi;
3233
import androidx.core.content.ContextCompat;
33-
import androidx.core.os.BuildCompat;
3434
import java.util.LinkedHashSet;
3535
import java.util.Set;
3636

@@ -134,7 +134,7 @@ public static Context wrapContextIfAvailable(
134134
/** Returns {@code true} if contrast control is available on the current SDK level. */
135135
@ChecksSdkIntAtLeast(api = VERSION_CODES.UPSIDE_DOWN_CAKE)
136136
public static boolean isContrastAvailable() {
137-
return BuildCompat.isAtLeastU();
137+
return VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE;
138138
}
139139

140140
private static int getContrastThemeOverlayResourceId(

lib/java/com/google/android/material/color/ColorResourcesOverride.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import androidx.annotation.Nullable;
2626
import androidx.annotation.RestrictTo;
2727
import androidx.annotation.StyleRes;
28-
import androidx.core.os.BuildCompat;
2928
import java.util.Map;
3029

3130
/**
@@ -93,7 +92,7 @@ Context wrapContextIfPossible(
9392
static ColorResourcesOverride getInstance() {
9493
if (VERSION_CODES.R <= VERSION.SDK_INT && VERSION.SDK_INT <= VERSION_CODES.TIRAMISU) {
9594
return ResourcesLoaderColorResourcesOverride.getInstance();
96-
} else if (BuildCompat.isAtLeastU()) {
95+
} else if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
9796
// TODO(b/255833419): Replace with FabricatedOverlayColorResourcesOverride() when available
9897
// for U+.
9998
return ResourcesLoaderColorResourcesOverride.getInstance();

lib/java/com/google/android/material/color/DynamicColors.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ private static boolean shouldOverrideNeutralChroma(@NonNull Context context) {
499499
MaterialAttributes.resolveBoolean(
500500
context, R.attr.preUDynamicNeutralChromaUpdateEnabled, /* defaultValue= */ false);
501501
// Update neutral palette chroma from 4 to 6 for backward compatibility.
502-
return !BuildCompat.isAtLeastU()
502+
return VERSION.SDK_INT < VERSION_CODES.UPSIDE_DOWN_CAKE
503503
&& VERSION.SDK_INT >= VERSION_CODES.S
504504
&& shouldUpdateNeutralChroma;
505505
}
@@ -564,6 +564,8 @@ private interface DeviceSupportCondition {
564564

565565
private static float getSystemContrast(Context context) {
566566
UiModeManager uiModeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
567-
return (uiModeManager == null || !BuildCompat.isAtLeastU()) ? 0 : uiModeManager.getContrast();
567+
return (uiModeManager == null || VERSION.SDK_INT < VERSION_CODES.UPSIDE_DOWN_CAKE)
568+
? 0
569+
: uiModeManager.getContrast();
568570
}
569571
}

lib/java/com/google/android/material/motion/MaterialBackOrchestrator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import androidx.annotation.Nullable;
3131
import androidx.annotation.RequiresApi;
3232
import androidx.annotation.RestrictTo;
33-
import androidx.core.os.BuildCompat;
3433

3534
/**
3635
* Utility class for views that support back handling via {@link MaterialBackHandler} which helps
@@ -98,7 +97,7 @@ public void stopListeningForBackCallbacks() {
9897

9998
@Nullable
10099
private static BackCallbackDelegate createBackCallbackDelegate() {
101-
if (BuildCompat.isAtLeastU()) {
100+
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
102101
return new Api34BackCallbackDelegate();
103102
} else if (VERSION.SDK_INT >= VERSION_CODES.TIRAMISU) {
104103
return new Api33BackCallbackDelegate();

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
import androidx.annotation.StyleRes;
7070
import androidx.annotation.VisibleForTesting;
7171
import androidx.core.content.ContextCompat;
72-
import androidx.core.os.BuildCompat;
7372
import androidx.core.view.GravityCompat;
7473
import androidx.core.view.ViewCompat;
7574
import androidx.core.view.WindowInsetsCompat;
@@ -982,7 +981,7 @@ public void handleBackInvoked() {
982981
DrawerLayout drawerLayout = drawerLayoutPair.first;
983982

984983
BackEventCompat backEvent = sideContainerBackHelper.onHandleBackInvoked();
985-
if (backEvent == null || !BuildCompat.isAtLeastU()) {
984+
if (backEvent == null || VERSION.SDK_INT < VERSION_CODES.UPSIDE_DOWN_CAKE) {
986985
drawerLayout.closeDrawer(this);
987986
return;
988987
}

lib/java/com/google/android/material/search/SearchView.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
import androidx.annotation.VisibleForTesting;
6464
import androidx.coordinatorlayout.widget.CoordinatorLayout;
6565
import androidx.core.graphics.drawable.DrawableCompat;
66-
import androidx.core.os.BuildCompat;
6766
import androidx.core.view.ViewCompat;
6867
import androidx.core.view.WindowInsetsCompat;
6968
import androidx.core.widget.TextViewCompat;
@@ -548,7 +547,7 @@ public void setupWithSearchBar(@Nullable SearchBar searchBar) {
548547
searchViewAnimationHelper.setSearchBar(searchBar);
549548
if (searchBar != null) {
550549
searchBar.setOnClickListener(v -> show());
551-
if (BuildCompat.isAtLeastU()) {
550+
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
552551
try {
553552
searchBar.setHandwritingDelegatorCallback(this::show);
554553
editText.setIsHandwritingDelegate(true);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import android.content.Context;
2828
import android.content.res.ColorStateList;
2929
import android.content.res.TypedArray;
30+
import android.os.Build.VERSION;
31+
import android.os.Build.VERSION_CODES;
3032
import android.os.Parcel;
3133
import android.os.Parcelable;
3234
import android.util.AttributeSet;
@@ -50,7 +52,6 @@
5052
import androidx.coordinatorlayout.widget.CoordinatorLayout;
5153
import androidx.coordinatorlayout.widget.CoordinatorLayout.LayoutParams;
5254
import androidx.core.math.MathUtils;
53-
import androidx.core.os.BuildCompat;
5455
import androidx.core.view.GravityCompat;
5556
import androidx.core.view.ViewCompat;
5657
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
@@ -1020,7 +1021,7 @@ public void handleBackInvoked() {
10201021
return;
10211022
}
10221023
BackEventCompat backEvent = sideContainerBackHelper.onHandleBackInvoked();
1023-
if (backEvent == null || !BuildCompat.isAtLeastU()) {
1024+
if (backEvent == null || VERSION.SDK_INT < VERSION_CODES.UPSIDE_DOWN_CAKE) {
10241025
setState(STATE_HIDDEN);
10251026
return;
10261027
}

0 commit comments

Comments
 (0)