Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash in getChildDrawingOrder #40859

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

import com.facebook.common.logging.FLog;
import com.facebook.react.common.ReactConstants;

/** Helper to handle implementing ViewGroups with custom drawing order based on z-index. */
public class ViewGroupDrawingOrderHelper {
private final ViewGroup mViewGroup;
Expand Down Expand Up @@ -65,6 +69,14 @@ public boolean shouldEnableCustomDrawingOrder() {
* ViewGroup#getChildDrawingOrder}.
*/
public int getChildDrawingOrder(int childCount, int index) {
if (mDrawingOrderIndices != null
&& (index >= mDrawingOrderIndices.length || mDrawingOrderIndices[index] >= childCount)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this ever expected to happen under normal conditions? Can we log a warning here that mDrawingOrderIndices is out-of-sync?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will not happen under normal circumstances, but it is not ruled out that some third-party components may add or remove subview in the working thread.

FLog.w(ReactConstants.TAG,
"getChildDrawingOrder index out of bounds! Please check any custom view manipulations you" +
" may have done. childCount = %d, index = %d", childCount, index);
update();
}

if (mDrawingOrderIndices == null) {
ArrayList<View> viewsToSort = new ArrayList<>();
for (int i = 0; i < childCount; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,10 +418,10 @@ private void updateSubviewClipStatus(Rect clippingRect, int idx, int clippedSoFa
if (!intersects && child.getParent() != null && !isAnimating) {
// We can try saving on invalidate call here as the view that we remove is out of visible area
// therefore invalidation is not necessary.
super.removeViewsInLayout(idx - clippedSoFar, 1);
removeViewsInLayout(idx - clippedSoFar, 1);
needUpdateClippingRecursive = true;
} else if (intersects && child.getParent() == null) {
super.addViewInLayout(child, idx - clippedSoFar, sDefaultLayoutParam, true);
addViewInLayout(child, idx - clippedSoFar, sDefaultLayoutParam, true);
invalidate();
needUpdateClippingRecursive = true;
} else if (intersects) {
Expand Down Expand Up @@ -499,23 +499,18 @@ private boolean customDrawOrderDisabled() {
return ViewUtil.getUIManagerType(getId()) == UIManagerType.FABRIC;
}

@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
// This will get called for every overload of addView so there is not need to override every
// method.
private void handleAddView(View view) {
UiThreadUtil.assertOnUiThread();

if (!customDrawOrderDisabled()) {
getDrawingOrderHelper().handleAddView(child);
getDrawingOrderHelper().handleAddView(view);
setChildrenDrawingOrderEnabled(getDrawingOrderHelper().shouldEnableCustomDrawingOrder());
} else {
setChildrenDrawingOrderEnabled(false);
}

super.addView(child, index, params);
}

@Override
public void removeView(View view) {
private void handleRemoveView(View view) {
UiThreadUtil.assertOnUiThread();

if (!customDrawOrderDisabled()) {
Expand All @@ -524,22 +519,60 @@ public void removeView(View view) {
} else {
setChildrenDrawingOrderEnabled(false);
}
}

private void handleRemoveViews(int start, int count) {
int endIndex = start + count;
for (int index = start; index < endIndex; index++) {
if (index < getChildCount()) {
handleRemoveView(getChildAt(index));
}
}
}

@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
// This will get called for every overload of addView so there is not need to override every
// method.
handleAddView(child);
super.addView(child, index, params);
}

@Override
protected boolean addViewInLayout(View child, int index, LayoutParams params,
boolean preventRequestLayout) {
handleAddView(child);
return super.addViewInLayout(child, index, params, preventRequestLayout);
}

@Override
public void removeView(View view) {
handleRemoveView(view);
super.removeView(view);
}

@Override
public void removeViewAt(int index) {
UiThreadUtil.assertOnUiThread();
handleRemoveView(getChildAt(index));
super.removeViewAt(index);
}

if (!customDrawOrderDisabled()) {
getDrawingOrderHelper().handleRemoveView(getChildAt(index));
setChildrenDrawingOrderEnabled(getDrawingOrderHelper().shouldEnableCustomDrawingOrder());
} else {
setChildrenDrawingOrderEnabled(false);
}
@Override
public void removeViewInLayout(View view) {
handleRemoveView(view);
super.removeViewInLayout(view);
}

super.removeViewAt(index);
@Override
public void removeViewsInLayout(int start, int count) {
handleRemoveViews(start, count);
super.removeViewsInLayout(start, count);
}

@Override
public void removeViews(int start, int count) {
handleRemoveViews(start, count);
super.removeViews(start, count);
}

@Override
Expand Down Expand Up @@ -663,7 +696,7 @@ public void run() {
clippedSoFar++;
}
}
super.removeViewsInLayout(index - clippedSoFar, 1);
removeViewsInLayout(index - clippedSoFar, 1);
}
removeFromArray(index);
}
Expand Down