Skip to content

Commit

Permalink
[Predictive Back] Fixed possible NaN crashes in MaterialBottomContain…
Browse files Browse the repository at this point in the history
…erBackHelper, MaterialMainContainerBackHelper and MaterialSideContainerBackHelper.

There were some cases where we were dividing by the view's width and height in the  updateBackProgress methods, and this would cause a division by 0 exception and crash in cases where either the width or height are 0. Added a bunch of safeguards for division cases where the denominator could be 0.

PiperOrigin-RevId: 541929872
  • Loading branch information
afohrman authored and raajkumars committed Jun 26, 2023
1 parent 917da52 commit 122c296
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
Expand Up @@ -73,6 +73,10 @@ public void updateBackProgress(float progress) {

float width = view.getWidth();
float height = view.getHeight();
if (width <= 0f || height <= 0f) {
return;
}

float maxScaleXDelta = maxScaleXDistance / width;
float maxScaleYDelta = maxScaleYDistance / height;
float scaleXDelta = AnimationUtils.lerp(0, maxScaleXDelta, progress);
Expand All @@ -89,7 +93,7 @@ public void updateBackProgress(float progress) {
View childView = viewGroup.getChildAt(i);
// Preserve the original aspect ratio and container alignment of the child content.
childView.setPivotY(-childView.getTop());
childView.setScaleY(scaleX / scaleY);
childView.setScaleY(scaleY != 0f ? scaleX / scaleY : 1f);
}
}
}
Expand Down
Expand Up @@ -115,6 +115,10 @@ public void updateBackProgress(
float progress, boolean leftSwipeEdge, float touchY, float collapsedCornerSize) {
float width = view.getWidth();
float height = view.getHeight();
if (width <= 0f || height <= 0f) {
return;
}

float scale = lerp(1, MIN_SCALE, progress);

float availableHorizontalSpace = max(0, (width - MIN_SCALE * width) / 2 - minEdgeGap);
Expand Down
Expand Up @@ -87,6 +87,11 @@ public void updateBackProgress(float progress, boolean leftSwipeEdge, @GravityIn

int width = view.getWidth();
int height = view.getHeight();

if (width <= 0f || height <= 0f) {
return;
}

float maxScaleXDeltaShrink = maxScaleXDistanceShrink / width;
float maxScaleXDeltaGrow = maxScaleXDistanceGrow / width;
float maxScaleYDelta = maxScaleYDistance / height;
Expand All @@ -112,7 +117,7 @@ public void updateBackProgress(float progress, boolean leftSwipeEdge, @GravityIn
: -childView.getLeft());
childView.setPivotY(-childView.getTop());
float childScaleX = swipeEdgeMatchesGravity ? 1 - scaleXDelta : 1f;
float childScaleY = scaleX / scaleY * childScaleX;
float childScaleY = scaleY != 0f ? scaleX / scaleY * childScaleX : 1f;
childView.setScaleX(childScaleX);
childView.setScaleY(childScaleY);
}
Expand Down

0 comments on commit 122c296

Please sign in to comment.