package androidx.appcompat.widget;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.StyleRes;
import androidx.appcompat.view.menu.ActionMenuItemView;
import eb.e;
import k.a0;
import k.d0;
import k.n;
import k.o;
import k.p;
import l.f4;
import l.j;
import l.q;
import l.r;
import l.s;
import l.z1;

/* JADX INFO: loaded from: classes.dex */
public class ActionMenuView extends LinearLayoutCompat implements o, d0 {
    public p S;
    public Context T;
    public int U;
    public boolean V;
    public l.p W;

    /* JADX INFO: renamed from: a0, reason: collision with root package name */
    public a0 f626a0;

    /* JADX INFO: renamed from: b0, reason: collision with root package name */
    public n f627b0;

    /* JADX INFO: renamed from: c0, reason: collision with root package name */
    public boolean f628c0;

    /* JADX INFO: renamed from: d0, reason: collision with root package name */
    public int f629d0;

    /* JADX INFO: renamed from: e0, reason: collision with root package name */
    public final int f630e0;

    /* JADX INFO: renamed from: f0, reason: collision with root package name */
    public final int f631f0;

    /* JADX INFO: renamed from: g0, reason: collision with root package name */
    public s f632g0;

    public ActionMenuView(@NonNull Context context) {
        this(context, null);
    }

    public static r l() {
        r rVar = new r(-2, -2);
        rVar.f29826a = false;
        ((LinearLayout.LayoutParams) rVar).gravity = 16;
        return rVar;
    }

    public static r m(ViewGroup.LayoutParams layoutParams) {
        r rVar;
        if (layoutParams == null) {
            return l();
        }
        if (layoutParams instanceof r) {
            r rVar2 = (r) layoutParams;
            rVar = new r(rVar2);
            rVar.f29826a = rVar2.f29826a;
        } else {
            rVar = new r(layoutParams);
        }
        if (((LinearLayout.LayoutParams) rVar).gravity <= 0) {
            ((LinearLayout.LayoutParams) rVar).gravity = 16;
        }
        return rVar;
    }

    @Override // k.o
    public final boolean a(k.r rVar) {
        return this.S.q(rVar, null, 0);
    }

    @Override // androidx.appcompat.widget.LinearLayoutCompat, android.view.ViewGroup
    public final boolean checkLayoutParams(ViewGroup.LayoutParams layoutParams) {
        return layoutParams instanceof r;
    }

    @Override // k.d0
    public final void d(p pVar) {
        this.S = pVar;
    }

    @Override // android.view.View
    public final boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
        return false;
    }

    @Override // androidx.appcompat.widget.LinearLayoutCompat, android.view.ViewGroup
    public final /* bridge */ /* synthetic */ ViewGroup.LayoutParams generateDefaultLayoutParams() {
        return l();
    }

    @Override // androidx.appcompat.widget.LinearLayoutCompat, android.view.ViewGroup
    public final /* bridge */ /* synthetic */ ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams layoutParams) {
        return m(layoutParams);
    }

    public Menu getMenu() {
        if (this.S == null) {
            Context context = getContext();
            p pVar = new p(context);
            this.S = pVar;
            pVar.f28884e = new xyz.stream.api.a0(this, 3);
            l.p pVar2 = new l.p(context);
            this.W = pVar2;
            pVar2.f29802m = true;
            pVar2.Q = true;
            a0 eVar = this.f626a0;
            if (eVar == null) {
                eVar = new e();
            }
            pVar2.f28814e = eVar;
            this.S.b(pVar2, this.T);
            l.p pVar3 = this.W;
            pVar3.f28817h = this;
            this.S = pVar3.f28812c;
        }
        return this.S;
    }

    @Nullable
    public Drawable getOverflowIcon() {
        getMenu();
        l.p pVar = this.W;
        l.n nVar = pVar.f29799j;
        if (nVar != null) {
            return nVar.getDrawable();
        }
        if (pVar.f29801l) {
            return pVar.f29800k;
        }
        return null;
    }

    public int getPopupTheme() {
        return this.U;
    }

    @RestrictTo({RestrictTo.Scope.f557c})
    public int getWindowAnimations() {
        return 0;
    }

    @Override // androidx.appcompat.widget.LinearLayoutCompat
    /* JADX INFO: renamed from: h */
    public final /* bridge */ /* synthetic */ z1 generateDefaultLayoutParams() {
        return l();
    }

    @Override // androidx.appcompat.widget.LinearLayoutCompat
    /* JADX INFO: renamed from: i */
    public final z1 generateLayoutParams(AttributeSet attributeSet) {
        return new r(getContext(), attributeSet);
    }

    @Override // androidx.appcompat.widget.LinearLayoutCompat
    /* JADX INFO: renamed from: j */
    public final /* bridge */ /* synthetic */ z1 generateLayoutParams(ViewGroup.LayoutParams layoutParams) {
        return m(layoutParams);
    }

    public final boolean n(int i10) {
        boolean zA = false;
        if (i10 == 0) {
            return false;
        }
        KeyEvent.Callback childAt = getChildAt(i10 - 1);
        KeyEvent.Callback childAt2 = getChildAt(i10);
        if (i10 < getChildCount() && (childAt instanceof q)) {
            zA = ((q) childAt).a();
        }
        return (i10 <= 0 || !(childAt2 instanceof q)) ? zA : zA | ((q) childAt2).b();
    }

    @Override // android.view.View
    public final void onConfigurationChanged(Configuration configuration) {
        super.onConfigurationChanged(configuration);
        l.p pVar = this.W;
        if (pVar != null) {
            pVar.b(false);
            if (this.W.g()) {
                this.W.e();
                this.W.i();
            }
        }
    }

    @Override // android.view.ViewGroup, android.view.View
    public final void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        l.p pVar = this.W;
        if (pVar != null) {
            pVar.e();
            j jVar = pVar.X;
            if (jVar == null || !jVar.b()) {
                return;
            }
            jVar.f28955j.dismiss();
        }
    }

    @Override // androidx.appcompat.widget.LinearLayoutCompat, android.view.ViewGroup, android.view.View
    public final void onLayout(boolean z10, int i10, int i11, int i12, int i13) {
        int width;
        int paddingLeft;
        if (!this.f628c0) {
            super.onLayout(z10, i10, i11, i12, i13);
            return;
        }
        int childCount = getChildCount();
        int i14 = (i13 - i11) / 2;
        int dividerWidth = getDividerWidth();
        int i15 = i12 - i10;
        int paddingRight = (i15 - getPaddingRight()) - getPaddingLeft();
        boolean z11 = f4.f29682a;
        boolean z12 = getLayoutDirection() == 1;
        int i16 = 0;
        int i17 = 0;
        for (int i18 = 0; i18 < childCount; i18++) {
            View childAt = getChildAt(i18);
            if (childAt.getVisibility() != 8) {
                r rVar = (r) childAt.getLayoutParams();
                if (rVar.f29826a) {
                    int measuredWidth = childAt.getMeasuredWidth();
                    if (n(i18)) {
                        measuredWidth += dividerWidth;
                    }
                    int measuredHeight = childAt.getMeasuredHeight();
                    if (z12) {
                        paddingLeft = getPaddingLeft() + ((LinearLayout.LayoutParams) rVar).leftMargin;
                        width = paddingLeft + measuredWidth;
                    } else {
                        width = (getWidth() - getPaddingRight()) - ((LinearLayout.LayoutParams) rVar).rightMargin;
                        paddingLeft = width - measuredWidth;
                    }
                    int i19 = i14 - (measuredHeight / 2);
                    childAt.layout(paddingLeft, i19, width, measuredHeight + i19);
                    paddingRight -= measuredWidth;
                    i16 = 1;
                } else {
                    paddingRight -= (childAt.getMeasuredWidth() + ((LinearLayout.LayoutParams) rVar).leftMargin) + ((LinearLayout.LayoutParams) rVar).rightMargin;
                    n(i18);
                    i17++;
                }
            }
        }
        if (childCount == 1 && i16 == 0) {
            View childAt2 = getChildAt(0);
            int measuredWidth2 = childAt2.getMeasuredWidth();
            int measuredHeight2 = childAt2.getMeasuredHeight();
            int i20 = (i15 / 2) - (measuredWidth2 / 2);
            int i21 = i14 - (measuredHeight2 / 2);
            childAt2.layout(i20, i21, measuredWidth2 + i20, measuredHeight2 + i21);
            return;
        }
        int i22 = i17 - (i16 ^ 1);
        int iMax = Math.max(0, i22 > 0 ? paddingRight / i22 : 0);
        if (z12) {
            int width2 = getWidth() - getPaddingRight();
            for (int i23 = 0; i23 < childCount; i23++) {
                View childAt3 = getChildAt(i23);
                r rVar2 = (r) childAt3.getLayoutParams();
                if (childAt3.getVisibility() != 8 && !rVar2.f29826a) {
                    int i24 = width2 - ((LinearLayout.LayoutParams) rVar2).rightMargin;
                    int measuredWidth3 = childAt3.getMeasuredWidth();
                    int measuredHeight3 = childAt3.getMeasuredHeight();
                    int i25 = i14 - (measuredHeight3 / 2);
                    childAt3.layout(i24 - measuredWidth3, i25, i24, measuredHeight3 + i25);
                    width2 = i24 - ((measuredWidth3 + ((LinearLayout.LayoutParams) rVar2).leftMargin) + iMax);
                }
            }
            return;
        }
        int paddingLeft2 = getPaddingLeft();
        for (int i26 = 0; i26 < childCount; i26++) {
            View childAt4 = getChildAt(i26);
            r rVar3 = (r) childAt4.getLayoutParams();
            if (childAt4.getVisibility() != 8 && !rVar3.f29826a) {
                int i27 = paddingLeft2 + ((LinearLayout.LayoutParams) rVar3).leftMargin;
                int measuredWidth4 = childAt4.getMeasuredWidth();
                int measuredHeight4 = childAt4.getMeasuredHeight();
                int i28 = i14 - (measuredHeight4 / 2);
                childAt4.layout(i27, i28, i27 + measuredWidth4, measuredHeight4 + i28);
                paddingLeft2 = measuredWidth4 + ((LinearLayout.LayoutParams) rVar3).rightMargin + iMax + i27;
            }
        }
    }

    /* JADX WARN: Type inference failed for: r1v20 */
    /* JADX WARN: Type inference failed for: r1v21, types: [boolean, int] */
    /* JADX WARN: Type inference failed for: r1v23 */
    /* JADX WARN: Type inference failed for: r1v26 */
    @Override // androidx.appcompat.widget.LinearLayoutCompat, android.view.View
    public final void onMeasure(int i10, int i11) {
        int i12;
        int i13;
        int i14;
        boolean z10;
        int i15;
        int i16;
        int i17;
        int i18;
        ?? r12;
        int i19;
        int i20;
        int i21;
        p pVar;
        boolean z11 = this.f628c0;
        boolean z12 = View.MeasureSpec.getMode(i10) == 1073741824;
        this.f628c0 = z12;
        if (z11 != z12) {
            this.f629d0 = 0;
        }
        int size = View.MeasureSpec.getSize(i10);
        if (this.f628c0 && (pVar = this.S) != null && size != this.f629d0) {
            this.f629d0 = size;
            pVar.p(true);
        }
        int childCount = getChildCount();
        if (!this.f628c0 || childCount <= 0) {
            for (int i22 = 0; i22 < childCount; i22++) {
                r rVar = (r) getChildAt(i22).getLayoutParams();
                ((LinearLayout.LayoutParams) rVar).rightMargin = 0;
                ((LinearLayout.LayoutParams) rVar).leftMargin = 0;
            }
            super.onMeasure(i10, i11);
            return;
        }
        int mode = View.MeasureSpec.getMode(i11);
        int size2 = View.MeasureSpec.getSize(i10);
        int size3 = View.MeasureSpec.getSize(i11);
        int paddingRight = getPaddingRight() + getPaddingLeft();
        int paddingBottom = getPaddingBottom() + getPaddingTop();
        int childMeasureSpec = ViewGroup.getChildMeasureSpec(i11, paddingBottom, -2);
        int i23 = size2 - paddingRight;
        int i24 = this.f630e0;
        int i25 = i23 / i24;
        int i26 = i23 % i24;
        if (i25 == 0) {
            setMeasuredDimension(i23, 0);
            return;
        }
        int i27 = (i26 / i25) + i24;
        int childCount2 = getChildCount();
        int i28 = 0;
        int iMax = 0;
        int i29 = 0;
        boolean z13 = false;
        int i30 = 0;
        int iMax2 = 0;
        long j10 = 0;
        while (true) {
            i12 = this.f631f0;
            if (i29 >= childCount2) {
                break;
            }
            View childAt = getChildAt(i29);
            int i31 = size3;
            if (childAt.getVisibility() == 8) {
                i19 = i23;
                i20 = paddingBottom;
            } else {
                boolean z14 = childAt instanceof ActionMenuItemView;
                int i32 = i28 + 1;
                if (z14) {
                    childAt.setPadding(i12, 0, i12, 0);
                }
                r rVar2 = (r) childAt.getLayoutParams();
                rVar2.f29831f = false;
                rVar2.f29828c = 0;
                rVar2.f29827b = 0;
                rVar2.f29829d = false;
                ((LinearLayout.LayoutParams) rVar2).leftMargin = 0;
                ((LinearLayout.LayoutParams) rVar2).rightMargin = 0;
                rVar2.f29830e = z14 && (TextUtils.isEmpty(((ActionMenuItemView) childAt).getText()) ^ true);
                int i33 = rVar2.f29826a ? 1 : i25;
                r rVar3 = (r) childAt.getLayoutParams();
                i19 = i23;
                i20 = paddingBottom;
                int iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.getSize(childMeasureSpec) - paddingBottom, View.MeasureSpec.getMode(childMeasureSpec));
                ActionMenuItemView actionMenuItemView = z14 ? (ActionMenuItemView) childAt : null;
                boolean z15 = actionMenuItemView != null && (TextUtils.isEmpty(actionMenuItemView.getText()) ^ true);
                if (i33 <= 0 || (z15 && i33 < 2)) {
                    i21 = 0;
                } else {
                    childAt.measure(View.MeasureSpec.makeMeasureSpec(i33 * i27, Integer.MIN_VALUE), iMakeMeasureSpec);
                    int measuredWidth = childAt.getMeasuredWidth();
                    i21 = measuredWidth / i27;
                    if (measuredWidth % i27 != 0) {
                        i21++;
                    }
                    if (z15 && i21 < 2) {
                        i21 = 2;
                    }
                }
                rVar3.f29829d = !rVar3.f29826a && z15;
                rVar3.f29827b = i21;
                childAt.measure(View.MeasureSpec.makeMeasureSpec(i21 * i27, 1073741824), iMakeMeasureSpec);
                iMax = Math.max(iMax, i21);
                if (rVar2.f29829d) {
                    i30++;
                }
                if (rVar2.f29826a) {
                    z13 = true;
                }
                i25 -= i21;
                iMax2 = Math.max(iMax2, childAt.getMeasuredHeight());
                if (i21 == 1) {
                    j10 |= (long) (1 << i29);
                }
                i28 = i32;
            }
            i29++;
            size3 = i31;
            paddingBottom = i20;
            i23 = i19;
        }
        int i34 = i23;
        int i35 = size3;
        int i36 = iMax2;
        boolean z16 = z13 && i28 == 2;
        boolean z17 = false;
        while (i30 > 0 && i25 > 0) {
            int i37 = Integer.MAX_VALUE;
            int i38 = 0;
            int i39 = 0;
            long j11 = 0;
            while (i39 < childCount2) {
                int i40 = i36;
                r rVar4 = (r) getChildAt(i39).getLayoutParams();
                boolean z18 = z17;
                if (rVar4.f29829d) {
                    int i41 = rVar4.f29827b;
                    if (i41 < i37) {
                        j11 = 1 << i39;
                        i37 = i41;
                        i38 = 1;
                    } else if (i41 == i37) {
                        i38++;
                        j11 |= 1 << i39;
                    }
                }
                i39++;
                z17 = z18;
                i36 = i40;
            }
            i14 = i36;
            z10 = z17;
            j10 |= j11;
            if (i38 > i25) {
                i13 = mode;
                break;
            }
            int i42 = i37 + 1;
            int i43 = 0;
            while (i43 < childCount2) {
                View childAt2 = getChildAt(i43);
                r rVar5 = (r) childAt2.getLayoutParams();
                int i44 = mode;
                int i45 = childMeasureSpec;
                int i46 = childCount2;
                long j12 = 1 << i43;
                if ((j11 & j12) != 0) {
                    if (z16 && rVar5.f29830e) {
                        r12 = 1;
                        r12 = 1;
                        if (i25 == 1) {
                            childAt2.setPadding(i12 + i27, 0, i12, 0);
                        }
                    } else {
                        r12 = 1;
                    }
                    rVar5.f29827b += r12;
                    rVar5.f29831f = r12;
                    i25--;
                } else if (rVar5.f29827b == i42) {
                    j10 |= j12;
                }
                i43++;
                childMeasureSpec = i45;
                mode = i44;
                childCount2 = i46;
            }
            i36 = i14;
            z17 = true;
        }
        i13 = mode;
        i14 = i36;
        z10 = z17;
        int i47 = childMeasureSpec;
        int i48 = childCount2;
        boolean z19 = !z13 && i28 == 1;
        if (i25 <= 0 || j10 == 0 || (i25 >= i28 - 1 && !z19 && iMax <= 1)) {
            i15 = i48;
        } else {
            float fBitCount = Long.bitCount(j10);
            if (!z19) {
                if ((j10 & 1) != 0 && !((r) getChildAt(0).getLayoutParams()).f29830e) {
                    fBitCount -= 0.5f;
                }
                int i49 = i48 - 1;
                if ((j10 & ((long) (1 << i49))) != 0 && !((r) getChildAt(i49).getLayoutParams()).f29830e) {
                    fBitCount -= 0.5f;
                }
            }
            int i50 = fBitCount > 0.0f ? (int) ((i25 * i27) / fBitCount) : 0;
            i15 = i48;
            for (int i51 = 0; i51 < i15; i51++) {
                if ((j10 & ((long) (1 << i51))) != 0) {
                    View childAt3 = getChildAt(i51);
                    r rVar6 = (r) childAt3.getLayoutParams();
                    if (childAt3 instanceof ActionMenuItemView) {
                        rVar6.f29828c = i50;
                        rVar6.f29831f = true;
                        if (i51 == 0 && !rVar6.f29830e) {
                            ((LinearLayout.LayoutParams) rVar6).leftMargin = (-i50) / 2;
                        }
                        z10 = true;
                    } else if (rVar6.f29826a) {
                        rVar6.f29828c = i50;
                        rVar6.f29831f = true;
                        ((LinearLayout.LayoutParams) rVar6).rightMargin = (-i50) / 2;
                        z10 = true;
                    } else {
                        if (i51 != 0) {
                            ((LinearLayout.LayoutParams) rVar6).leftMargin = i50 / 2;
                        }
                        if (i51 != i15 - 1) {
                            ((LinearLayout.LayoutParams) rVar6).rightMargin = i50 / 2;
                        }
                    }
                }
            }
        }
        if (z10) {
            int i52 = 0;
            while (i52 < i15) {
                View childAt4 = getChildAt(i52);
                r rVar7 = (r) childAt4.getLayoutParams();
                if (rVar7.f29831f) {
                    i18 = i47;
                    childAt4.measure(View.MeasureSpec.makeMeasureSpec((rVar7.f29827b * i27) + rVar7.f29828c, 1073741824), i18);
                } else {
                    i18 = i47;
                }
                i52++;
                i47 = i18;
            }
        }
        if (i13 != 1073741824) {
            i17 = i34;
            i16 = i14;
        } else {
            i16 = i35;
            i17 = i34;
        }
        setMeasuredDimension(i17, i16);
    }

    @RestrictTo({RestrictTo.Scope.f557c})
    public void setExpandedActionViewsExclusive(boolean z10) {
        this.W.U = z10;
    }

    @RestrictTo({RestrictTo.Scope.f557c})
    public void setMenuCallbacks(a0 a0Var, n nVar) {
        this.f626a0 = a0Var;
        this.f627b0 = nVar;
    }

    public void setOnMenuItemClickListener(s sVar) {
        this.f632g0 = sVar;
    }

    public void setOverflowIcon(@Nullable Drawable drawable) {
        getMenu();
        l.p pVar = this.W;
        l.n nVar = pVar.f29799j;
        if (nVar != null) {
            nVar.setImageDrawable(drawable);
        } else {
            pVar.f29801l = true;
            pVar.f29800k = drawable;
        }
    }

    @RestrictTo({RestrictTo.Scope.f557c})
    public void setOverflowReserved(boolean z10) {
        this.V = z10;
    }

    public void setPopupTheme(@StyleRes int i10) {
        if (this.U != i10) {
            this.U = i10;
            if (i10 == 0) {
                this.T = getContext();
            } else {
                this.T = new ContextThemeWrapper(getContext(), i10);
            }
        }
    }

    @RestrictTo({RestrictTo.Scope.f555a})
    public void setPresenter(l.p pVar) {
        this.W = pVar;
        pVar.f28817h = this;
        this.S = pVar.f28812c;
    }

    public ActionMenuView(@NonNull Context context, @Nullable AttributeSet attributeSet) {
        super(context, attributeSet);
        setBaselineAligned(false);
        float f10 = context.getResources().getDisplayMetrics().density;
        this.f630e0 = (int) (56.0f * f10);
        this.f631f0 = (int) (f10 * 4.0f);
        this.T = context;
        this.U = 0;
    }

    @Override // androidx.appcompat.widget.LinearLayoutCompat, android.view.ViewGroup
    public final ViewGroup.LayoutParams generateLayoutParams(AttributeSet attributeSet) {
        return new r(getContext(), attributeSet);
    }
}
