Skip to content

Commit ddc56c8

Browse files
wcshileticiarossi
authored andcommitted
Add convenience method to check if VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN_MR2 in BadgeUtils.
PiperOrigin-RevId: 249855393
1 parent f259def commit ddc56c8

File tree

3 files changed

+15
-22
lines changed

3 files changed

+15
-22
lines changed

lib/java/com/google/android/material/badge/BadgeDrawable.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import android.graphics.PixelFormat;
3131
import android.graphics.Rect;
3232
import android.graphics.drawable.Drawable;
33-
import android.os.Build.VERSION;
34-
import android.os.Build.VERSION_CODES;
3533
import android.os.Parcel;
3634
import android.os.Parcelable;
3735
import androidx.annotation.AttrRes;
@@ -579,7 +577,7 @@ private void calculateBadgeCenterCoordinates(
579577
// Returns the visible bounds of the anchor view.
580578
anchorView.getDrawingRect(anchorRect);
581579
anchorRect.top += res.getDimensionPixelSize(R.dimen.mtrl_badge_vertical_offset);
582-
if (customBadgeParent != null || VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN_MR2) {
580+
if (customBadgeParent != null || BadgeUtils.USE_COMPAT_PARENT) {
583581
// Calculates coordinates relative to the parent.
584582
ViewGroup viewGroup =
585583
customBadgeParent == null ? (ViewGroup) anchorView.getParent() : customBadgeParent;

lib/java/com/google/android/material/badge/BadgeUtils.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
@RestrictTo(Scope.LIBRARY)
3939
public class BadgeUtils {
4040

41+
public static final boolean USE_COMPAT_PARENT = VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN_MR2;
42+
4143
private BadgeUtils() {
4244
// Private constructor to prevent unwanted construction.
4345
}
@@ -68,10 +70,10 @@ public static void updateBadgeBounds(
6870
* ancestor of the anchor.
6971
*/
7072
public static void attachBadgeDrawable(
71-
BadgeDrawable badgeDrawable, View anchor, FrameLayout preApi18BadgeParent) {
72-
setBadgeDrawableBounds(badgeDrawable, anchor, preApi18BadgeParent);
73-
if (VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN_MR2) {
74-
preApi18BadgeParent.setForeground(badgeDrawable);
73+
BadgeDrawable badgeDrawable, View anchor, FrameLayout compatBadgeParent) {
74+
setBadgeDrawableBounds(badgeDrawable, anchor, compatBadgeParent);
75+
if (USE_COMPAT_PARENT) {
76+
compatBadgeParent.setForeground(badgeDrawable);
7577
} else {
7678
anchor.getOverlay().add(badgeDrawable);
7779
}
@@ -84,12 +86,12 @@ public static void attachBadgeDrawable(
8486
* an ancestor of the anchor.
8587
*/
8688
public static void detachBadgeDrawable(
87-
@Nullable BadgeDrawable badgeDrawable, View anchor, FrameLayout preApi18BadgeParent) {
89+
@Nullable BadgeDrawable badgeDrawable, View anchor, FrameLayout compatBadgeParent) {
8890
if (badgeDrawable == null) {
8991
return;
9092
}
91-
if (VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN_MR2) {
92-
preApi18BadgeParent.setForeground(null);
93+
if (USE_COMPAT_PARENT) {
94+
compatBadgeParent.setForeground(null);
9395
} else {
9496
anchor.getOverlay().remove(badgeDrawable);
9597
}
@@ -100,15 +102,12 @@ public static void detachBadgeDrawable(
100102
* anchor's FrameLayout ancestor (pre-API 18).
101103
*/
102104
public static void setBadgeDrawableBounds(
103-
BadgeDrawable badgeDrawable, View anchor, FrameLayout preApi18BadgeParent) {
105+
BadgeDrawable badgeDrawable, View anchor, FrameLayout compatBadgeParent) {
104106
Rect badgeBounds = new Rect();
105-
if (VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN_MR2) {
106-
preApi18BadgeParent.getDrawingRect(badgeBounds);
107-
} else {
108-
anchor.getDrawingRect(badgeBounds);
109-
}
107+
View badgeParent = USE_COMPAT_PARENT ? compatBadgeParent : anchor;
108+
badgeParent.getDrawingRect(badgeBounds);
110109
badgeDrawable.setBounds(badgeBounds);
111-
badgeDrawable.updateBadgeCoordinates(anchor, preApi18BadgeParent);
110+
badgeDrawable.updateBadgeCoordinates(anchor, compatBadgeParent);
112111
}
113112

114113
/**

lib/java/com/google/android/material/bottomnavigation/BottomNavigationItemView.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import android.content.res.ColorStateList;
2525
import android.content.res.Resources;
2626
import android.graphics.drawable.Drawable;
27-
import android.os.Build.VERSION;
28-
import android.os.Build.VERSION_CODES;
2927
import androidx.annotation.NonNull;
3028
import androidx.annotation.Nullable;
3129
import androidx.annotation.RestrictTo;
@@ -453,9 +451,7 @@ private void tryRemoveBadgeFromAnchor(View anchorView) {
453451
@Nullable
454452
private FrameLayout getCustomParentForBadge(View anchorView) {
455453
if (anchorView == icon) {
456-
return (VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN_MR2)
457-
? ((FrameLayout) icon.getParent())
458-
: null;
454+
return BadgeUtils.USE_COMPAT_PARENT ? ((FrameLayout) icon.getParent()) : null;
459455
}
460456
// TODO: Support displaying a badge on label-only bottom navigation views.
461457
return null;

0 commit comments

Comments
 (0)