package androidx.appcompat.widget;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewDebug;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
import android.widget.LinearLayout;
import androidx.appcompat.view.menu.ActionMenuItemView;
import androidx.appcompat.widget.Toolbar;
import d.b.c.p;
import d.b.f.i.g;
import d.b.f.i.i;
import d.b.f.i.m;
import d.b.f.i.n;
import d.b.g.c;
import d.b.g.d1;
import d.b.g.k0;
import io.netty.util.internal.shaded.org.jctools.util.Pow2;

/* JADX INFO: loaded from: classes.dex */
public class ActionMenuView extends k0 implements g.b, n {
    public g A;
    public Context B;
    public int C;
    public boolean D;
    public d.b.g.c E;
    public m.a F;
    public g.a G;
    public boolean H;
    public int I;
    public int J;
    public int K;
    public e L;

    public interface a {
        boolean a();

        boolean b();
    }

    public static class b implements m.a {
        @Override // d.b.f.i.m.a
        public void b(g gVar, boolean z2) {
        }

        @Override // d.b.f.i.m.a
        public boolean c(g gVar) {
            return false;
        }
    }

    public static class c extends k0.a {

        @ViewDebug.ExportedProperty
        public boolean a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        @ViewDebug.ExportedProperty
        public int f495b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        @ViewDebug.ExportedProperty
        public int f496c;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        @ViewDebug.ExportedProperty
        public boolean f497d;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        @ViewDebug.ExportedProperty
        public boolean f498e;

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

        public c(int i2, int i3) {
            super(i2, i3);
            this.a = false;
        }

        public c(Context context, AttributeSet attributeSet) {
            super(context, attributeSet);
        }

        public c(ViewGroup.LayoutParams layoutParams) {
            super(layoutParams);
        }

        public c(c cVar) {
            super(cVar);
            this.a = cVar.a;
        }
    }

    public class d implements g.a {
        public d() {
        }

        @Override // d.b.f.i.g.a
        public boolean a(g gVar, MenuItem menuItem) {
            e eVar = ActionMenuView.this.L;
            if (eVar == null) {
                return false;
            }
            Toolbar.f fVar = Toolbar.this.R;
            return fVar != null ? p.this.f1618c.onMenuItemSelected(0, menuItem) : false;
        }

        @Override // d.b.f.i.g.a
        public void b(g gVar) {
            g.a aVar = ActionMenuView.this.G;
            if (aVar != null) {
                aVar.b(gVar);
            }
        }
    }

    public interface e {
    }

    public ActionMenuView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        setBaselineAligned(false);
        float f2 = context.getResources().getDisplayMetrics().density;
        this.J = (int) (56.0f * f2);
        this.K = (int) (f2 * 4.0f);
        this.B = context;
        this.C = 0;
    }

    public static int t(View view, int i2, int i3, int i4, int i5) {
        c cVar = (c) view.getLayoutParams();
        int iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.getSize(i4) - i5, View.MeasureSpec.getMode(i4));
        ActionMenuItemView actionMenuItemView = view instanceof ActionMenuItemView ? (ActionMenuItemView) view : null;
        boolean z2 = false;
        boolean z3 = actionMenuItemView != null && actionMenuItemView.c();
        int i6 = 2;
        if (i3 <= 0 || (z3 && i3 < 2)) {
            i6 = 0;
        } else {
            view.measure(View.MeasureSpec.makeMeasureSpec(i3 * i2, Integer.MIN_VALUE), iMakeMeasureSpec);
            int measuredWidth = view.getMeasuredWidth();
            int i7 = measuredWidth / i2;
            if (measuredWidth % i2 != 0) {
                i7++;
            }
            if (!z3 || i7 >= 2) {
                i6 = i7;
            }
        }
        if (!cVar.a && z3) {
            z2 = true;
        }
        cVar.f497d = z2;
        cVar.f495b = i6;
        view.measure(View.MeasureSpec.makeMeasureSpec(i2 * i6, Pow2.MAX_POW2), iMakeMeasureSpec);
        return i6;
    }

    @Override // d.b.f.i.g.b
    public boolean a(i iVar) {
        return this.A.s(iVar, null, 0);
    }

    @Override // d.b.f.i.n
    public void b(g gVar) {
        this.A = gVar;
    }

    @Override // d.b.g.k0, android.view.ViewGroup
    public boolean checkLayoutParams(ViewGroup.LayoutParams layoutParams) {
        return layoutParams instanceof c;
    }

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

    @Override // d.b.g.k0, android.view.ViewGroup
    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attributeSet) {
        return new c(getContext(), attributeSet);
    }

    public Menu getMenu() {
        if (this.A == null) {
            Context context = getContext();
            g gVar = new g(context);
            this.A = gVar;
            gVar.f1791f = new d();
            d.b.g.c cVar = new d.b.g.c(context);
            this.E = cVar;
            cVar.f1919w = true;
            cVar.f1920x = true;
            m.a bVar = this.F;
            if (bVar == null) {
                bVar = new b();
            }
            cVar.f1744p = bVar;
            this.A.b(cVar, this.B);
            d.b.g.c cVar2 = this.E;
            cVar2.f1747s = this;
            this.A = cVar2.f1742n;
        }
        return this.A;
    }

    public Drawable getOverflowIcon() {
        getMenu();
        d.b.g.c cVar = this.E;
        c.d dVar = cVar.f1916t;
        if (dVar != null) {
            return dVar.getDrawable();
        }
        if (cVar.f1918v) {
            return cVar.f1917u;
        }
        return null;
    }

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

    public int getWindowAnimations() {
        return 0;
    }

    @Override // d.b.g.k0
    /* JADX INFO: renamed from: i */
    public k0.a generateLayoutParams(AttributeSet attributeSet) {
        return new c(getContext(), attributeSet);
    }

    @Override // android.view.View
    public void onConfigurationChanged(Configuration configuration) {
        super.onConfigurationChanged(configuration);
        d.b.g.c cVar = this.E;
        if (cVar != null) {
            cVar.c(false);
            if (this.E.m()) {
                this.E.j();
                this.E.n();
            }
        }
    }

    @Override // android.view.ViewGroup, android.view.View
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        d.b.g.c cVar = this.E;
        if (cVar != null) {
            cVar.a();
        }
    }

    @Override // d.b.g.k0, android.view.ViewGroup, android.view.View
    public void onLayout(boolean z2, int i2, int i3, int i4, int i5) {
        int width;
        int paddingLeft;
        if (!this.H) {
            super.onLayout(z2, i2, i3, i4, i5);
            return;
        }
        int childCount = getChildCount();
        int i6 = (i5 - i3) / 2;
        int dividerWidth = getDividerWidth();
        int i7 = i4 - i2;
        int paddingRight = (i7 - getPaddingRight()) - getPaddingLeft();
        boolean zB = d1.b(this);
        int i8 = 0;
        int i9 = 0;
        for (int i10 = 0; i10 < childCount; i10++) {
            View childAt = getChildAt(i10);
            if (childAt.getVisibility() != 8) {
                c cVar = (c) childAt.getLayoutParams();
                if (cVar.a) {
                    int measuredWidth = childAt.getMeasuredWidth();
                    if (s(i10)) {
                        measuredWidth += dividerWidth;
                    }
                    int measuredHeight = childAt.getMeasuredHeight();
                    if (zB) {
                        paddingLeft = getPaddingLeft() + ((LinearLayout.LayoutParams) cVar).leftMargin;
                        width = paddingLeft + measuredWidth;
                    } else {
                        width = (getWidth() - getPaddingRight()) - ((LinearLayout.LayoutParams) cVar).rightMargin;
                        paddingLeft = width - measuredWidth;
                    }
                    int i11 = i6 - (measuredHeight / 2);
                    childAt.layout(paddingLeft, i11, width, measuredHeight + i11);
                    paddingRight -= measuredWidth;
                    i8 = 1;
                } else {
                    paddingRight -= (childAt.getMeasuredWidth() + ((LinearLayout.LayoutParams) cVar).leftMargin) + ((LinearLayout.LayoutParams) cVar).rightMargin;
                    s(i10);
                    i9++;
                }
            }
        }
        if (childCount == 1 && i8 == 0) {
            View childAt2 = getChildAt(0);
            int measuredWidth2 = childAt2.getMeasuredWidth();
            int measuredHeight2 = childAt2.getMeasuredHeight();
            int i12 = (i7 / 2) - (measuredWidth2 / 2);
            int i13 = i6 - (measuredHeight2 / 2);
            childAt2.layout(i12, i13, measuredWidth2 + i12, measuredHeight2 + i13);
            return;
        }
        int i14 = i9 - (i8 ^ 1);
        int iMax = Math.max(0, i14 > 0 ? paddingRight / i14 : 0);
        if (zB) {
            int width2 = getWidth() - getPaddingRight();
            for (int i15 = 0; i15 < childCount; i15++) {
                View childAt3 = getChildAt(i15);
                c cVar2 = (c) childAt3.getLayoutParams();
                if (childAt3.getVisibility() != 8 && !cVar2.a) {
                    int i16 = width2 - ((LinearLayout.LayoutParams) cVar2).rightMargin;
                    int measuredWidth3 = childAt3.getMeasuredWidth();
                    int measuredHeight3 = childAt3.getMeasuredHeight();
                    int i17 = i6 - (measuredHeight3 / 2);
                    childAt3.layout(i16 - measuredWidth3, i17, i16, measuredHeight3 + i17);
                    width2 = i16 - ((measuredWidth3 + ((LinearLayout.LayoutParams) cVar2).leftMargin) + iMax);
                }
            }
            return;
        }
        int paddingLeft2 = getPaddingLeft();
        for (int i18 = 0; i18 < childCount; i18++) {
            View childAt4 = getChildAt(i18);
            c cVar3 = (c) childAt4.getLayoutParams();
            if (childAt4.getVisibility() != 8 && !cVar3.a) {
                int i19 = paddingLeft2 + ((LinearLayout.LayoutParams) cVar3).leftMargin;
                int measuredWidth4 = childAt4.getMeasuredWidth();
                int measuredHeight4 = childAt4.getMeasuredHeight();
                int i20 = i6 - (measuredHeight4 / 2);
                childAt4.layout(i19, i20, i19 + measuredWidth4, measuredHeight4 + i20);
                paddingLeft2 = measuredWidth4 + ((LinearLayout.LayoutParams) cVar3).rightMargin + iMax + i19;
            }
        }
    }

    /* JADX WARN: Type inference failed for: r3v33 */
    /* JADX WARN: Type inference failed for: r3v34, types: [boolean, int] */
    /* JADX WARN: Type inference failed for: r3v40 */
    @Override // d.b.g.k0, android.view.View
    public void onMeasure(int i2, int i3) {
        int i4;
        boolean z2;
        int i5;
        int i6;
        boolean z3;
        int i7;
        ?? r3;
        g gVar;
        boolean z4 = this.H;
        boolean z5 = View.MeasureSpec.getMode(i2) == 1073741824;
        this.H = z5;
        if (z4 != z5) {
            this.I = 0;
        }
        int size = View.MeasureSpec.getSize(i2);
        if (this.H && (gVar = this.A) != null && size != this.I) {
            this.I = size;
            gVar.q(true);
        }
        int childCount = getChildCount();
        if (!this.H || childCount <= 0) {
            for (int i8 = 0; i8 < childCount; i8++) {
                c cVar = (c) getChildAt(i8).getLayoutParams();
                ((LinearLayout.LayoutParams) cVar).rightMargin = 0;
                ((LinearLayout.LayoutParams) cVar).leftMargin = 0;
            }
            super.onMeasure(i2, i3);
            return;
        }
        int mode = View.MeasureSpec.getMode(i3);
        int size2 = View.MeasureSpec.getSize(i2);
        int size3 = View.MeasureSpec.getSize(i3);
        int paddingRight = getPaddingRight() + getPaddingLeft();
        int paddingBottom = getPaddingBottom() + getPaddingTop();
        int childMeasureSpec = ViewGroup.getChildMeasureSpec(i3, paddingBottom, -2);
        int i9 = size2 - paddingRight;
        int i10 = this.J;
        int i11 = i9 / i10;
        int i12 = i9 % i10;
        if (i11 == 0) {
            setMeasuredDimension(i9, 0);
            return;
        }
        int i13 = (i12 / i11) + i10;
        int childCount2 = getChildCount();
        int i14 = 0;
        int iMax = 0;
        int iMax2 = 0;
        int i15 = 0;
        int i16 = 0;
        boolean z6 = false;
        long j2 = 0;
        while (i16 < childCount2) {
            View childAt = getChildAt(i16);
            int i17 = size3;
            int i18 = i9;
            if (childAt.getVisibility() != 8) {
                boolean z7 = childAt instanceof ActionMenuItemView;
                int i19 = i14 + 1;
                if (z7) {
                    int i20 = this.K;
                    i7 = i19;
                    r3 = 0;
                    childAt.setPadding(i20, 0, i20, 0);
                } else {
                    i7 = i19;
                    r3 = 0;
                }
                c cVar2 = (c) childAt.getLayoutParams();
                cVar2.f499f = r3;
                cVar2.f496c = r3;
                cVar2.f495b = r3;
                cVar2.f497d = r3;
                ((LinearLayout.LayoutParams) cVar2).leftMargin = r3;
                ((LinearLayout.LayoutParams) cVar2).rightMargin = r3;
                cVar2.f498e = z7 && ((ActionMenuItemView) childAt).c();
                int iT = t(childAt, i13, cVar2.a ? 1 : i11, childMeasureSpec, paddingBottom);
                iMax2 = Math.max(iMax2, iT);
                if (cVar2.f497d) {
                    i15++;
                }
                if (cVar2.a) {
                    z6 = true;
                }
                i11 -= iT;
                iMax = Math.max(iMax, childAt.getMeasuredHeight());
                if (iT == 1) {
                    j2 |= (long) (1 << i16);
                }
                i14 = i7;
            }
            i16++;
            size3 = i17;
            i9 = i18;
        }
        int i21 = i9;
        int i22 = size3;
        boolean z8 = z6 && i14 == 2;
        boolean z9 = false;
        while (i15 > 0 && i11 > 0) {
            int i23 = Integer.MAX_VALUE;
            int i24 = 0;
            int i25 = 0;
            long j3 = 0;
            while (i24 < childCount2) {
                int i26 = iMax;
                c cVar3 = (c) getChildAt(i24).getLayoutParams();
                boolean z10 = z9;
                if (cVar3.f497d) {
                    int i27 = cVar3.f495b;
                    if (i27 < i23) {
                        j3 = 1 << i24;
                        i23 = i27;
                        i25 = 1;
                    } else if (i27 == i23) {
                        i25++;
                        j3 |= 1 << i24;
                    }
                }
                i24++;
                z9 = z10;
                iMax = i26;
            }
            i4 = iMax;
            z2 = z9;
            j2 |= j3;
            if (i25 > i11) {
                break;
            }
            int i28 = i23 + 1;
            int i29 = 0;
            while (i29 < childCount2) {
                View childAt2 = getChildAt(i29);
                c cVar4 = (c) childAt2.getLayoutParams();
                int i30 = i15;
                long j4 = 1 << i29;
                if ((j3 & j4) == 0) {
                    if (cVar4.f495b == i28) {
                        j2 |= j4;
                    }
                    z3 = z8;
                } else {
                    if (z8 && cVar4.f498e && i11 == 1) {
                        int i31 = this.K;
                        z3 = z8;
                        childAt2.setPadding(i31 + i13, 0, i31, 0);
                    } else {
                        z3 = z8;
                    }
                    cVar4.f495b++;
                    cVar4.f499f = true;
                    i11--;
                }
                i29++;
                i15 = i30;
                z8 = z3;
            }
            iMax = i4;
            z9 = true;
        }
        i4 = iMax;
        z2 = z9;
        boolean z11 = !z6 && i14 == 1;
        if (i11 > 0 && j2 != 0 && (i11 < i14 - 1 || z11 || iMax2 > 1)) {
            float fBitCount = Long.bitCount(j2);
            if (!z11) {
                if ((j2 & 1) != 0 && !((c) getChildAt(0).getLayoutParams()).f498e) {
                    fBitCount -= 0.5f;
                }
                int i32 = childCount2 - 1;
                if ((j2 & ((long) (1 << i32))) != 0 && !((c) getChildAt(i32).getLayoutParams()).f498e) {
                    fBitCount -= 0.5f;
                }
            }
            int i33 = fBitCount > 0.0f ? (int) ((i11 * i13) / fBitCount) : 0;
            for (int i34 = 0; i34 < childCount2; i34++) {
                if ((j2 & ((long) (1 << i34))) != 0) {
                    View childAt3 = getChildAt(i34);
                    c cVar5 = (c) childAt3.getLayoutParams();
                    if (childAt3 instanceof ActionMenuItemView) {
                        cVar5.f496c = i33;
                        cVar5.f499f = true;
                        if (i34 == 0 && !cVar5.f498e) {
                            ((LinearLayout.LayoutParams) cVar5).leftMargin = (-i33) / 2;
                        }
                        z2 = true;
                    } else if (cVar5.a) {
                        cVar5.f496c = i33;
                        cVar5.f499f = true;
                        ((LinearLayout.LayoutParams) cVar5).rightMargin = (-i33) / 2;
                        z2 = true;
                    } else {
                        if (i34 != 0) {
                            ((LinearLayout.LayoutParams) cVar5).leftMargin = i33 / 2;
                        }
                        if (i34 != childCount2 - 1) {
                            ((LinearLayout.LayoutParams) cVar5).rightMargin = i33 / 2;
                        }
                    }
                }
            }
        }
        if (z2) {
            for (int i35 = 0; i35 < childCount2; i35++) {
                View childAt4 = getChildAt(i35);
                c cVar6 = (c) childAt4.getLayoutParams();
                if (cVar6.f499f) {
                    childAt4.measure(View.MeasureSpec.makeMeasureSpec((cVar6.f495b * i13) + cVar6.f496c, Pow2.MAX_POW2), childMeasureSpec);
                }
            }
        }
        if (mode != 1073741824) {
            i6 = i21;
            i5 = i4;
        } else {
            i5 = i22;
            i6 = i21;
        }
        setMeasuredDimension(i6, i5);
    }

    @Override // d.b.g.k0
    /* JADX INFO: renamed from: q, reason: merged with bridge method [inline-methods] */
    public c generateDefaultLayoutParams() {
        c cVar = new c(-2, -2);
        ((LinearLayout.LayoutParams) cVar).gravity = 16;
        return cVar;
    }

    @Override // d.b.g.k0
    /* JADX INFO: renamed from: r, reason: merged with bridge method [inline-methods] */
    public c generateLayoutParams(ViewGroup.LayoutParams layoutParams) {
        if (layoutParams == null) {
            return generateDefaultLayoutParams();
        }
        c cVar = layoutParams instanceof c ? new c((c) layoutParams) : new c(layoutParams);
        if (((LinearLayout.LayoutParams) cVar).gravity <= 0) {
            ((LinearLayout.LayoutParams) cVar).gravity = 16;
        }
        return cVar;
    }

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

    public void setExpandedActionViewsExclusive(boolean z2) {
        this.E.B = z2;
    }

    public void setOnMenuItemClickListener(e eVar) {
        this.L = eVar;
    }

    public void setOverflowIcon(Drawable drawable) {
        getMenu();
        d.b.g.c cVar = this.E;
        c.d dVar = cVar.f1916t;
        if (dVar != null) {
            dVar.setImageDrawable(drawable);
        } else {
            cVar.f1918v = true;
            cVar.f1917u = drawable;
        }
    }

    public void setOverflowReserved(boolean z2) {
        this.D = z2;
    }

    public void setPopupTheme(int i2) {
        if (this.C != i2) {
            this.C = i2;
            if (i2 == 0) {
                this.B = getContext();
            } else {
                this.B = new ContextThemeWrapper(getContext(), i2);
            }
        }
    }

    public void setPresenter(d.b.g.c cVar) {
        this.E = cVar;
        cVar.f1747s = this;
        this.A = cVar.f1742n;
    }
}
