3636import android .view .ViewGroup ;
3737import android .view .ViewParent ;
3838import android .widget .FrameLayout ;
39- import android .widget .FrameLayout .LayoutParams ;
4039import androidx .annotation .AttrRes ;
4140import androidx .annotation .ColorInt ;
4241import 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 }
0 commit comments