Skip to content

Commit

Permalink
fix(android): modal host view crash
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli2018 authored and siguangli committed Oct 15, 2024
1 parent 9a19769 commit 53c8a09
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.tencent.mtt.hippy.views.common.HippyNestedScrollComponent.Priority;
import com.tencent.mtt.hippy.views.common.HippyNestedScrollHelper;
import com.tencent.mtt.hippy.views.custom.HippyCustomPropsController;
import com.tencent.mtt.hippy.views.modal.HippyModalHostView;
import com.tencent.mtt.hippy.views.view.HippyViewGroup;
import com.tencent.renderer.NativeRenderContext;
import com.tencent.renderer.Renderer;
Expand Down Expand Up @@ -543,8 +544,12 @@ private boolean checkOverflowVisible(@NonNull View view) {

protected void addView(ViewGroup parentView, View view, int index) {
int realIndex = index;
if (realIndex > parentView.getChildCount()) {
realIndex = parentView.getChildCount();
int childCount = parentView.getChildCount();
if (parentView instanceof HippyModalHostView) {
childCount = ((HippyModalHostView) parentView).getModalChildCount();
}
if (realIndex > childCount) {
realIndex = childCount;
}
try {
parentView.addView(view, realIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.content.DialogInterface;
import android.view.View;

import android.view.ViewGroup;
import androidx.annotation.NonNull;
import com.tencent.mtt.hippy.annotation.HippyController;
import com.tencent.mtt.hippy.annotation.HippyControllerProps;
Expand Down Expand Up @@ -101,4 +102,13 @@ public void onAfterUpdateProps(@NonNull HippyModalHostView v) {
v.showOrUpdate();
}

@Override
public int getChildCount(HippyModalHostView modalHostView) {
return modalHostView.getModalChildCount();
}

@Override
public View getChildAt(HippyModalHostView modalHostView, int i) {
return modalHostView.getModalChildAt(i);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,27 +126,30 @@ public void addView(View child, int index) {
mDialogRootView.addView(child, index);
}

@Override
public int getChildCount() {
return mDialogRootView.getChildCount();
}

@Override
public View getChildAt(int index) {
return mDialogRootView.getChildAt(index);
}

@Override
public void removeView(View child) {
mDialogRootView.removeView(child);
}

@Override
public void removeViewAt(int index) {
View child = getChildAt(index);
View child = getModalChildAt(index);
mDialogRootView.removeView(child);
}

// Do not directly override the getChildCount method of ModalHostView, as it may cause the crash:
// java.lang.IllegalArgumentException: parameter must be a descendant
// Because under Modal, there are actually no child views. When the system traverses internally, it returns the
// number of child views of mDialogRootView, which leads to inconsistent parent values in the internal
// verification process of the system.
public int getModalChildCount() {
return mDialogRootView.getChildCount();
}

public View getModalChildAt(int index) {
return mDialogRootView.getChildAt(index);
}

@Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
return false;
Expand Down Expand Up @@ -243,7 +246,7 @@ protected void setDialogBar(boolean isDarkIcon) {
lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
window.setAttributes(lp);
sysUI = sysUI | View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
} else if (mEnterImmersionStatusBar) {
sysUI = sysUI & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
sysUI = sysUI & ~View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
Expand Down

0 comments on commit 53c8a09

Please sign in to comment.