Skip to content

Commit 122c296

Browse files
afohrmanraajkumars
authored andcommitted
[Predictive Back] Fixed possible NaN crashes in MaterialBottomContainerBackHelper, 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
1 parent 917da52 commit 122c296

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ public void updateBackProgress(float progress) {
7373

7474
float width = view.getWidth();
7575
float height = view.getHeight();
76+
if (width <= 0f || height <= 0f) {
77+
return;
78+
}
79+
7680
float maxScaleXDelta = maxScaleXDistance / width;
7781
float maxScaleYDelta = maxScaleYDistance / height;
7882
float scaleXDelta = AnimationUtils.lerp(0, maxScaleXDelta, progress);
@@ -89,7 +93,7 @@ public void updateBackProgress(float progress) {
8993
View childView = viewGroup.getChildAt(i);
9094
// Preserve the original aspect ratio and container alignment of the child content.
9195
childView.setPivotY(-childView.getTop());
92-
childView.setScaleY(scaleX / scaleY);
96+
childView.setScaleY(scaleY != 0f ? scaleX / scaleY : 1f);
9397
}
9498
}
9599
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ public void updateBackProgress(
115115
float progress, boolean leftSwipeEdge, float touchY, float collapsedCornerSize) {
116116
float width = view.getWidth();
117117
float height = view.getHeight();
118+
if (width <= 0f || height <= 0f) {
119+
return;
120+
}
121+
118122
float scale = lerp(1, MIN_SCALE, progress);
119123

120124
float availableHorizontalSpace = max(0, (width - MIN_SCALE * width) / 2 - minEdgeGap);

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ public void updateBackProgress(float progress, boolean leftSwipeEdge, @GravityIn
8787

8888
int width = view.getWidth();
8989
int height = view.getHeight();
90+
91+
if (width <= 0f || height <= 0f) {
92+
return;
93+
}
94+
9095
float maxScaleXDeltaShrink = maxScaleXDistanceShrink / width;
9196
float maxScaleXDeltaGrow = maxScaleXDistanceGrow / width;
9297
float maxScaleYDelta = maxScaleYDistance / height;
@@ -112,7 +117,7 @@ public void updateBackProgress(float progress, boolean leftSwipeEdge, @GravityIn
112117
: -childView.getLeft());
113118
childView.setPivotY(-childView.getTop());
114119
float childScaleX = swipeEdgeMatchesGravity ? 1 - scaleXDelta : 1f;
115-
float childScaleY = scaleX / scaleY * childScaleX;
120+
float childScaleY = scaleY != 0f ? scaleX / scaleY * childScaleX : 1f;
116121
childView.setScaleX(childScaleX);
117122
childView.setScaleY(childScaleY);
118123
}

0 commit comments

Comments
 (0)