Skip to content

Commit 28dc750

Browse files
MGaetan89leticiarossi
authored andcommitted
[Badge] Remove BadgeUtils.USE_COMPAT_PARENT
Resolves #4170 GIT_ORIGIN_REV_ID=35f11c97b64f22b9c3994b41b35c955f7a4261ed PiperOrigin-RevId: 634877849
1 parent 182a507 commit 28dc750

File tree

5 files changed

+29
-187
lines changed

5 files changed

+29
-187
lines changed

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

Lines changed: 4 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import android.view.ViewGroup;
3737
import android.view.ViewParent;
3838
import android.widget.FrameLayout;
39-
import android.widget.FrameLayout.LayoutParams;
4039
import androidx.annotation.AttrRes;
4140
import androidx.annotation.ColorInt;
4241
import androidx.annotation.IntDef;
@@ -296,11 +295,6 @@ public void setVisible(boolean visible) {
296295
private void onVisibilityUpdated() {
297296
boolean visible = state.isVisible();
298297
setVisible(visible, /* restart= */ false);
299-
// When hiding a badge in pre-API 18, invalidate the custom parent in order to trigger a draw
300-
// pass to remove this badge from its foreground.
301-
if (BadgeUtils.USE_COMPAT_PARENT && getCustomBadgeParent() != null && !visible) {
302-
((ViewGroup) getCustomBadgeParent().getParent()).invalidate();
303-
}
304298
}
305299

306300
/**
@@ -386,11 +380,6 @@ public void updateBadgeCoordinates(
386380
* also updates this {@code BadgeDrawable BadgeDrawable's} bounds, because they are dependent on
387381
* the center coordinates.
388382
*
389-
* <p>For pre API-18, optionally wrap the anchor in a {@code FrameLayout} (if it's not done
390-
* already) that will be inserted into the anchor's view hierarchy and calculate the badge's
391-
* coordinates the parent {@code FrameLayout} because the {@code BadgeDrawable} will be set as the
392-
* parent's foreground.
393-
*
394383
* @param anchorView This badge's anchor.
395384
*/
396385
public void updateBadgeCoordinates(@NonNull View anchorView) {
@@ -402,85 +391,26 @@ public void updateBadgeCoordinates(@NonNull View anchorView) {
402391
* also updates this {@code BadgeDrawable BadgeDrawable's} bounds, because they are dependent on
403392
* the center coordinates.
404393
*
405-
* <p>For pre API-18, if no {@code customBadgeParent} is specified, optionally wrap the anchor in
406-
* a {@code FrameLayout} (if it's not done already) that will be inserted into the anchor's view
407-
* hierarchy and calculate the badge's coordinates the parent {@code FrameLayout} because the
408-
* {@code BadgeDrawable} will be set as the parent's foreground.
409-
*
410394
* @param anchorView This badge's anchor.
411395
* @param customBadgeParent An optional parent view that will set this {@code BadgeDrawable} as
412396
* its foreground.
413397
*/
414398
public void updateBadgeCoordinates(
415399
@NonNull View anchorView, @Nullable FrameLayout customBadgeParent) {
416400
this.anchorViewRef = new WeakReference<>(anchorView);
401+
this.customBadgeParentRef = new WeakReference<>(customBadgeParent);
417402

418-
if (BadgeUtils.USE_COMPAT_PARENT && customBadgeParent == null) {
419-
tryWrapAnchorInCompatParent(anchorView);
420-
} else {
421-
this.customBadgeParentRef = new WeakReference<>(customBadgeParent);
422-
}
423-
if (!BadgeUtils.USE_COMPAT_PARENT) {
424-
updateAnchorParentToNotClip(anchorView);
425-
}
403+
updateAnchorParentToNotClip(anchorView);
426404
updateCenterAndBounds();
427405
invalidateSelf();
428406
}
429407

430-
private boolean isAnchorViewWrappedInCompatParent() {
431-
View customBadgeAnchorParent = getCustomBadgeParent();
432-
return customBadgeAnchorParent != null
433-
&& customBadgeAnchorParent.getId() == R.id.mtrl_anchor_parent;
434-
}
435-
436408
/** Returns a {@link FrameLayout} that will set this {@code BadgeDrawable} as its foreground. */
437409
@Nullable
438410
public FrameLayout getCustomBadgeParent() {
439411
return customBadgeParentRef != null ? customBadgeParentRef.get() : null;
440412
}
441413

442-
/**
443-
* ViewOverlay is not supported below api 18, wrap the anchor view in a {@code FrameLayout} in
444-
* order to support scrolling.
445-
*/
446-
private void tryWrapAnchorInCompatParent(final View anchorView) {
447-
ViewGroup anchorViewParent = (ViewGroup) anchorView.getParent();
448-
if ((anchorViewParent != null && anchorViewParent.getId() == R.id.mtrl_anchor_parent)
449-
|| (customBadgeParentRef != null && customBadgeParentRef.get() == anchorViewParent)) {
450-
return;
451-
}
452-
// Must call this before wrapping the anchor in a FrameLayout.
453-
updateAnchorParentToNotClip(anchorView);
454-
455-
// Create FrameLayout and configure it to wrap the anchor.
456-
final FrameLayout frameLayout = new FrameLayout(anchorView.getContext());
457-
frameLayout.setId(R.id.mtrl_anchor_parent);
458-
frameLayout.setClipChildren(false);
459-
frameLayout.setClipToPadding(false);
460-
frameLayout.setLayoutParams(anchorView.getLayoutParams());
461-
frameLayout.setMinimumWidth(anchorView.getWidth());
462-
frameLayout.setMinimumHeight(anchorView.getHeight());
463-
464-
int anchorIndex = anchorViewParent.indexOfChild(anchorView);
465-
anchorViewParent.removeViewAt(anchorIndex);
466-
anchorView.setLayoutParams(
467-
new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
468-
469-
frameLayout.addView(anchorView);
470-
anchorViewParent.addView(frameLayout, anchorIndex);
471-
customBadgeParentRef = new WeakReference<>(frameLayout);
472-
473-
// Update the badge's coordinates after the FrameLayout has been added to the view hierarchy and
474-
// has a size.
475-
frameLayout.post(
476-
new Runnable() {
477-
@Override
478-
public void run() {
479-
updateBadgeCoordinates(anchorView, frameLayout);
480-
}
481-
});
482-
}
483-
484414
private static void updateAnchorParentToNotClip(View anchorView) {
485415
ViewGroup anchorViewParent = (ViewGroup) anchorView.getParent();
486416
anchorViewParent.setClipChildren(false);
@@ -1230,11 +1160,9 @@ private void updateCenterAndBounds() {
12301160
anchorView.getDrawingRect(anchorRect);
12311161

12321162
ViewGroup customBadgeParent = customBadgeParentRef != null ? customBadgeParentRef.get() : null;
1233-
if (customBadgeParent != null || BadgeUtils.USE_COMPAT_PARENT) {
1163+
if (customBadgeParent != null) {
12341164
// Calculates coordinates relative to the parent.
1235-
ViewGroup viewGroup =
1236-
customBadgeParent == null ? (ViewGroup) anchorView.getParent() : customBadgeParent;
1237-
viewGroup.offsetDescendantRectToMyCoords(anchorView, anchorRect);
1165+
customBadgeParent.offsetDescendantRectToMyCoords(anchorView, anchorRect);
12381166
}
12391167

12401168
calculateCenterAndBounds(anchorRect, anchorView);
@@ -1389,10 +1317,6 @@ private void autoAdjustWithinViewBounds(@NonNull View anchorView, @Nullable View
13891317
totalAnchorYOffset = anchorView.getY();
13901318
totalAnchorXOffset = anchorView.getX();
13911319
anchorParent = anchorView.getParent();
1392-
} else if (isAnchorViewWrappedInCompatParent()) {
1393-
totalAnchorYOffset = ((View) customAnchorParent).getY();
1394-
totalAnchorXOffset = ((View) customAnchorParent).getX();
1395-
anchorParent = customAnchorParent.getParent();
13961320
} else {
13971321
totalAnchorYOffset = 0;
13981322
totalAnchorXOffset = 0;
@@ -1446,8 +1370,6 @@ private void autoAdjustWithinGrandparentBounds(@NonNull View anchorView) {
14461370
ViewParent anchorParent = null;
14471371
if (customAnchor == null) {
14481372
anchorParent = anchorView.getParent();
1449-
} else if (isAnchorViewWrappedInCompatParent()) {
1450-
anchorParent = customAnchor.getParent();
14511373
} else {
14521374
anchorParent = customAnchor;
14531375
}

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

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@
4747
@ExperimentalBadgeUtils
4848
public class BadgeUtils {
4949

50-
public static final boolean USE_COMPAT_PARENT = VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN_MR2;
51-
5250
private static final String LOG_TAG = "BadgeUtils";
5351

5452
private BadgeUtils() {
@@ -80,9 +78,9 @@ public static void attachBadgeDrawable(
8078

8179
/**
8280
* Attaches a BadgeDrawable to its associated anchor and update the BadgeDrawable's coordinates
83-
* based on the anchor. For API 18+, the BadgeDrawable will be added as a view overlay. For
84-
* pre-API 18, the BadgeDrawable will be set as the foreground of a FrameLayout that is an
85-
* ancestor of the anchor.
81+
* based on the anchor. The BadgeDrawable will be added as a view overlay as default. If it has a
82+
* FrameLayout custom parent that is an ancestor of the anchor, then the BadgeDrawable will be set
83+
* as the foreground of that.
8684
*/
8785
public static void attachBadgeDrawable(
8886
@NonNull BadgeDrawable badgeDrawable,
@@ -93,11 +91,7 @@ public static void attachBadgeDrawable(
9391
if (badgeDrawable.getCustomBadgeParent() != null) {
9492
badgeDrawable.getCustomBadgeParent().setForeground(badgeDrawable);
9593
} else {
96-
if (USE_COMPAT_PARENT) {
97-
throw new IllegalArgumentException("Trying to reference null customBadgeParent");
98-
} else {
99-
anchor.getOverlay().add(badgeDrawable);
100-
}
94+
anchor.getOverlay().add(badgeDrawable);
10195
}
10296
}
10397

@@ -117,9 +111,9 @@ public static void attachBadgeDrawable(
117111
/**
118112
* Attaches a BadgeDrawable to its associated action menu item on a toolbar, update the
119113
* BadgeDrawable's coordinates based on this anchor and adjust the BadgeDrawable's offset so it is
120-
* not clipped off by the toolbar. For API 18+, the BadgeDrawable will be added as a view overlay.
121-
* For pre-API 18, the BadgeDrawable will be set as the foreground of a FrameLayout that is an
122-
* ancestor of the anchor.
114+
* not clipped off by the toolbar. The BadgeDrawable will be added as a view overlay as default.
115+
* If it has a FrameLayout custom parent that is an ancestor of the anchor, then the BadgeDrawable
116+
* will be set as the foreground of that.
123117
*
124118
* <p>Menu item views are reused by the menu, so any structural changes to the menu may require
125119
* detaching the BadgeDrawable and re-attaching it to the correct item.
@@ -173,26 +167,27 @@ public void onInitializeAccessibilityNodeInfo(
173167
}
174168

175169
/**
176-
* Detaches a BadgeDrawable from its associated anchor. For API 18+, the BadgeDrawable will be
177-
* removed from its anchor's ViewOverlay. For pre-API 18, the BadgeDrawable will be removed from
178-
* the foreground of a FrameLayout that is an ancestor of the anchor.
170+
* Detaches a BadgeDrawable from its associated anchor. The BadgeDrawable will be removed from its
171+
* anchor's ViewOverlay. If it has a FrameLayout custom parent that is an ancestor of the anchor,
172+
* then the BadgeDrawable will be removed from the parent's foreground instead.
179173
*/
180174
public static void detachBadgeDrawable(
181175
@Nullable BadgeDrawable badgeDrawable, @NonNull View anchor) {
182176
if (badgeDrawable == null) {
183177
return;
184178
}
185-
if (USE_COMPAT_PARENT || badgeDrawable.getCustomBadgeParent() != null) {
179+
if (badgeDrawable.getCustomBadgeParent() != null) {
186180
badgeDrawable.getCustomBadgeParent().setForeground(null);
187181
} else {
188182
anchor.getOverlay().remove(badgeDrawable);
189183
}
190184
}
191185

192186
/**
193-
* Detaches a BadgeDrawable from its associated action menu item on a toolbar, For API 18+, the
194-
* BadgeDrawable will be removed from its anchor's ViewOverlay. For pre-API 18, the BadgeDrawable
195-
* will be removed from the foreground of a FrameLayout that is an ancestor of the anchor.
187+
* Detaches a BadgeDrawable from its associated action menu item on a toolbar, The BadgeDrawable
188+
* will be removed from its anchor's ViewOverlay. If it has a FrameLayout custom parent that is an
189+
* ancestor of the anchor, then the BadgeDrawable will be removed from the parent's foreground
190+
* instead.
196191
*/
197192
public static void detachBadgeDrawable(
198193
@Nullable BadgeDrawable badgeDrawable, @NonNull Toolbar toolbar, @IdRes int menuItemId) {
@@ -243,8 +238,8 @@ static void removeToolbarOffset(BadgeDrawable badgeDrawable) {
243238
}
244239

245240
/**
246-
* Sets the bounds of a BadgeDrawable to match the bounds of its anchor (for API 18+) or its
247-
* anchor's FrameLayout ancestor (pre-API 18).
241+
* Sets the bounds of a BadgeDrawable to match the bounds of its anchor or its anchor's
242+
* FrameLayout ancestor if it has a custom parent set.
248243
*/
249244
public static void setBadgeDrawableBounds(
250245
@NonNull BadgeDrawable badgeDrawable,

lib/java/com/google/android/material/badge/res/values/ids.xml

Lines changed: 0 additions & 22 deletions
This file was deleted.

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -910,8 +910,7 @@ private void tryUpdateBadgeBounds(View anchorView) {
910910
if (!hasBadge()) {
911911
return;
912912
}
913-
BadgeUtils.setBadgeDrawableBounds(
914-
badgeDrawable, anchorView, getCustomParentForBadge(anchorView));
913+
BadgeUtils.setBadgeDrawableBounds(badgeDrawable, anchorView, null);
915914
}
916915

917916
private void tryAttachBadgeToAnchor(@Nullable View anchorView) {
@@ -923,8 +922,7 @@ private void tryAttachBadgeToAnchor(@Nullable View anchorView) {
923922
setClipChildren(false);
924923
setClipToPadding(false);
925924

926-
BadgeUtils.attachBadgeDrawable(
927-
badgeDrawable, anchorView, getCustomParentForBadge(anchorView));
925+
BadgeUtils.attachBadgeDrawable(badgeDrawable, anchorView);
928926
}
929927
}
930928

@@ -942,15 +940,6 @@ private void tryRemoveBadgeFromAnchor(@Nullable View anchorView) {
942940
badgeDrawable = null;
943941
}
944942

945-
@Nullable
946-
private FrameLayout getCustomParentForBadge(View anchorView) {
947-
if (anchorView == icon) {
948-
return BadgeUtils.USE_COMPAT_PARENT ? ((FrameLayout) icon.getParent()) : null;
949-
}
950-
// TODO(b/138148581): Support displaying a badge on label-only bottom navigation views.
951-
return null;
952-
}
953-
954943
private int getSuggestedIconWidth() {
955944
int badgeWidth =
956945
badgeDrawable == null

0 commit comments

Comments
 (0)