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.view.menu.g;
import androidx.appcompat.view.menu.m;
import androidx.appcompat.widget.m0;

/* JADX INFO: loaded from: classes.dex */
public class ActionMenuView extends m0 implements g.b, androidx.appcompat.view.menu.n {
    private boolean A;
    private int B;
    private int C;
    private int D;
    e E;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    private androidx.appcompat.view.menu.g f647t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    private Context f648u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    private int f649v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    private boolean f650w;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    private androidx.appcompat.widget.c f651x;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    private m.a f652y;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    g.a f653z;

    public interface a {
        boolean a();

        boolean b();
    }

    private static class b implements m.a {
        b() {
        }

        @Override // androidx.appcompat.view.menu.m.a
        public void b(androidx.appcompat.view.menu.g gVar, boolean z2) {
        }

        @Override // androidx.appcompat.view.menu.m.a
        public boolean c(androidx.appcompat.view.menu.g gVar) {
            return false;
        }
    }

    public static class c extends m0.a {

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

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

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

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

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

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

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

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

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

        public c(int i3, int i4) {
            super(i3, i4);
            this.f654a = false;
        }
    }

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

        @Override // androidx.appcompat.view.menu.g.a
        public boolean a(androidx.appcompat.view.menu.g gVar, MenuItem menuItem) {
            e eVar = ActionMenuView.this.E;
            return eVar != null && eVar.onMenuItemClick(menuItem);
        }

        @Override // androidx.appcompat.view.menu.g.a
        public void b(androidx.appcompat.view.menu.g gVar) {
            g.a aVar = ActionMenuView.this.f653z;
            if (aVar != null) {
                aVar.b(gVar);
            }
        }
    }

    public interface e {
        boolean onMenuItemClick(MenuItem menuItem);
    }

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

    static int L(View view, int i3, int i4, int i5, int i6) {
        c cVar = (c) view.getLayoutParams();
        int iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.getSize(i5) - i6, View.MeasureSpec.getMode(i5));
        ActionMenuItemView actionMenuItemView = view instanceof ActionMenuItemView ? (ActionMenuItemView) view : null;
        boolean z2 = actionMenuItemView != null && actionMenuItemView.f();
        int i7 = 2;
        if (i4 <= 0 || (z2 && i4 < 2)) {
            i7 = 0;
        } else {
            view.measure(View.MeasureSpec.makeMeasureSpec(i4 * i3, Integer.MIN_VALUE), iMakeMeasureSpec);
            int measuredWidth = view.getMeasuredWidth();
            int i8 = measuredWidth / i3;
            if (measuredWidth % i3 != 0) {
                i8++;
            }
            if (!z2 || i8 >= 2) {
                i7 = i8;
            }
        }
        cVar.f657d = !cVar.f654a && z2;
        cVar.f655b = i7;
        view.measure(View.MeasureSpec.makeMeasureSpec(i3 * i7, 1073741824), iMakeMeasureSpec);
        return i7;
    }

    /* JADX WARN: Type inference failed for: r14v10 */
    /* JADX WARN: Type inference failed for: r14v11, types: [boolean, int] */
    /* JADX WARN: Type inference failed for: r14v14 */
    private void M(int i3, int i4) {
        int i5;
        int i6;
        boolean z2;
        int i7;
        int i8;
        boolean z3;
        boolean z4;
        int i9;
        ?? r14;
        int mode = View.MeasureSpec.getMode(i4);
        int size = View.MeasureSpec.getSize(i3);
        int size2 = View.MeasureSpec.getSize(i4);
        int paddingLeft = getPaddingLeft() + getPaddingRight();
        int paddingTop = getPaddingTop() + getPaddingBottom();
        int childMeasureSpec = ViewGroup.getChildMeasureSpec(i4, paddingTop, -2);
        int i10 = size - paddingLeft;
        int i11 = this.C;
        int i12 = i10 / i11;
        int i13 = i10 % i11;
        if (i12 == 0) {
            setMeasuredDimension(i10, 0);
            return;
        }
        int i14 = i11 + (i13 / i12);
        int childCount = getChildCount();
        int iMax = 0;
        int i15 = 0;
        boolean z5 = false;
        int i16 = 0;
        int iMax2 = 0;
        int i17 = 0;
        long j3 = 0;
        while (i15 < childCount) {
            View childAt = getChildAt(i15);
            int i18 = size2;
            if (childAt.getVisibility() != 8) {
                boolean z6 = childAt instanceof ActionMenuItemView;
                int i19 = i16 + 1;
                if (z6) {
                    int i20 = this.D;
                    i9 = i19;
                    r14 = 0;
                    childAt.setPadding(i20, 0, i20, 0);
                } else {
                    i9 = i19;
                    r14 = 0;
                }
                c cVar = (c) childAt.getLayoutParams();
                cVar.f659f = r14;
                cVar.f656c = r14;
                cVar.f655b = r14;
                cVar.f657d = r14;
                ((LinearLayout.LayoutParams) cVar).leftMargin = r14;
                ((LinearLayout.LayoutParams) cVar).rightMargin = r14;
                cVar.f658e = z6 && ((ActionMenuItemView) childAt).f();
                int iL = L(childAt, i14, cVar.f654a ? 1 : i12, childMeasureSpec, paddingTop);
                iMax2 = Math.max(iMax2, iL);
                if (cVar.f657d) {
                    i17++;
                }
                if (cVar.f654a) {
                    z5 = true;
                }
                i12 -= iL;
                iMax = Math.max(iMax, childAt.getMeasuredHeight());
                if (iL == 1) {
                    j3 |= (long) (1 << i15);
                    iMax = iMax;
                }
                i16 = i9;
            }
            i15++;
            size2 = i18;
        }
        int i21 = size2;
        boolean z7 = z5 && i16 == 2;
        boolean z8 = false;
        while (i17 > 0 && i12 > 0) {
            int i22 = Integer.MAX_VALUE;
            int i23 = 0;
            int i24 = 0;
            long j4 = 0;
            while (i24 < childCount) {
                boolean z9 = z8;
                c cVar2 = (c) getChildAt(i24).getLayoutParams();
                int i25 = iMax;
                if (cVar2.f657d) {
                    int i26 = cVar2.f655b;
                    if (i26 < i22) {
                        j4 = 1 << i24;
                        i22 = i26;
                        i23 = 1;
                    } else if (i26 == i22) {
                        i23++;
                        j4 |= 1 << i24;
                    }
                }
                i24++;
                iMax = i25;
                z8 = z9;
            }
            z2 = z8;
            i7 = iMax;
            j3 |= j4;
            if (i23 > i12) {
                i5 = mode;
                i6 = i10;
                break;
            }
            int i27 = i22 + 1;
            int i28 = 0;
            while (i28 < childCount) {
                View childAt2 = getChildAt(i28);
                c cVar3 = (c) childAt2.getLayoutParams();
                int i29 = i10;
                int i30 = mode;
                long j5 = 1 << i28;
                if ((j4 & j5) == 0) {
                    if (cVar3.f655b == i27) {
                        j3 |= j5;
                    }
                    z4 = z7;
                } else {
                    if (z7 && cVar3.f658e && i12 == 1) {
                        int i31 = this.D;
                        z4 = z7;
                        childAt2.setPadding(i31 + i14, 0, i31, 0);
                    } else {
                        z4 = z7;
                    }
                    cVar3.f655b++;
                    cVar3.f659f = true;
                    i12--;
                }
                i28++;
                mode = i30;
                i10 = i29;
                z7 = z4;
            }
            iMax = i7;
            z8 = true;
        }
        i5 = mode;
        i6 = i10;
        z2 = z8;
        i7 = iMax;
        boolean z10 = !z5 && i16 == 1;
        if (i12 <= 0 || j3 == 0 || (i12 >= i16 - 1 && !z10 && iMax2 <= 1)) {
            i8 = 0;
            z3 = z2;
        } else {
            float fBitCount = Long.bitCount(j3);
            if (z10) {
                i8 = 0;
            } else {
                i8 = 0;
                if ((j3 & 1) != 0 && !((c) getChildAt(0).getLayoutParams()).f658e) {
                    fBitCount -= 0.5f;
                }
                int i32 = childCount - 1;
                if ((j3 & ((long) (1 << i32))) != 0 && !((c) getChildAt(i32).getLayoutParams()).f658e) {
                    fBitCount -= 0.5f;
                }
            }
            int i33 = fBitCount > 0.0f ? (int) ((i12 * i14) / fBitCount) : i8;
            z3 = z2;
            for (int i34 = i8; i34 < childCount; i34++) {
                if ((j3 & ((long) (1 << i34))) != 0) {
                    View childAt3 = getChildAt(i34);
                    c cVar4 = (c) childAt3.getLayoutParams();
                    if (childAt3 instanceof ActionMenuItemView) {
                        cVar4.f656c = i33;
                        cVar4.f659f = true;
                        if (i34 == 0 && !cVar4.f658e) {
                            ((LinearLayout.LayoutParams) cVar4).leftMargin = (-i33) / 2;
                        }
                        z3 = true;
                    } else if (cVar4.f654a) {
                        cVar4.f656c = i33;
                        cVar4.f659f = true;
                        ((LinearLayout.LayoutParams) cVar4).rightMargin = (-i33) / 2;
                        z3 = true;
                    } else {
                        if (i34 != 0) {
                            ((LinearLayout.LayoutParams) cVar4).leftMargin = i33 / 2;
                        }
                        if (i34 != childCount - 1) {
                            ((LinearLayout.LayoutParams) cVar4).rightMargin = i33 / 2;
                        }
                    }
                }
            }
        }
        if (z3) {
            for (int i35 = i8; i35 < childCount; i35++) {
                View childAt4 = getChildAt(i35);
                c cVar5 = (c) childAt4.getLayoutParams();
                if (cVar5.f659f) {
                    childAt4.measure(View.MeasureSpec.makeMeasureSpec((cVar5.f655b * i14) + cVar5.f656c, 1073741824), childMeasureSpec);
                }
            }
        }
        setMeasuredDimension(i6, i5 != 1073741824 ? i7 : i21);
    }

    public void B() {
        androidx.appcompat.widget.c cVar = this.f651x;
        if (cVar != null) {
            cVar.y();
        }
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // androidx.appcompat.widget.m0
    /* JADX INFO: renamed from: C, reason: merged with bridge method [inline-methods] */
    public c generateDefaultLayoutParams() {
        c cVar = new c(-2, -2);
        ((LinearLayout.LayoutParams) cVar).gravity = 16;
        return cVar;
    }

    @Override // androidx.appcompat.widget.m0
    /* JADX INFO: renamed from: D, reason: merged with bridge method [inline-methods] */
    public c generateLayoutParams(AttributeSet attributeSet) {
        return new c(getContext(), attributeSet);
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // androidx.appcompat.widget.m0
    /* JADX INFO: renamed from: E, 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 c F() {
        c cVarGenerateDefaultLayoutParams = generateDefaultLayoutParams();
        cVarGenerateDefaultLayoutParams.f654a = true;
        return cVarGenerateDefaultLayoutParams;
    }

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

    public boolean H() {
        androidx.appcompat.widget.c cVar = this.f651x;
        return cVar != null && cVar.B();
    }

    public boolean I() {
        androidx.appcompat.widget.c cVar = this.f651x;
        return cVar != null && cVar.D();
    }

    public boolean J() {
        androidx.appcompat.widget.c cVar = this.f651x;
        return cVar != null && cVar.E();
    }

    public boolean K() {
        return this.f650w;
    }

    public androidx.appcompat.view.menu.g N() {
        return this.f647t;
    }

    public void O(m.a aVar, g.a aVar2) {
        this.f652y = aVar;
        this.f653z = aVar2;
    }

    public boolean P() {
        androidx.appcompat.widget.c cVar = this.f651x;
        return cVar != null && cVar.K();
    }

    @Override // androidx.appcompat.view.menu.g.b
    public boolean a(androidx.appcompat.view.menu.i iVar) {
        return this.f647t.L(iVar, 0);
    }

    @Override // androidx.appcompat.view.menu.n
    public void b(androidx.appcompat.view.menu.g gVar) {
        this.f647t = gVar;
    }

    @Override // androidx.appcompat.widget.m0, android.view.ViewGroup
    protected boolean checkLayoutParams(ViewGroup.LayoutParams layoutParams) {
        return layoutParams instanceof c;
    }

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

    public Menu getMenu() {
        if (this.f647t == null) {
            Context context = getContext();
            androidx.appcompat.view.menu.g gVar = new androidx.appcompat.view.menu.g(context);
            this.f647t = gVar;
            gVar.R(new d());
            androidx.appcompat.widget.c cVar = new androidx.appcompat.widget.c(context);
            this.f651x = cVar;
            cVar.J(true);
            androidx.appcompat.widget.c cVar2 = this.f651x;
            m.a bVar = this.f652y;
            if (bVar == null) {
                bVar = new b();
            }
            cVar2.h(bVar);
            this.f647t.c(this.f651x, this.f648u);
            this.f651x.H(this);
        }
        return this.f647t;
    }

    public Drawable getOverflowIcon() {
        getMenu();
        return this.f651x.A();
    }

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

    public int getWindowAnimations() {
        return 0;
    }

    @Override // android.view.View
    public void onConfigurationChanged(Configuration configuration) {
        super.onConfigurationChanged(configuration);
        androidx.appcompat.widget.c cVar = this.f651x;
        if (cVar != null) {
            cVar.j(false);
            if (this.f651x.E()) {
                this.f651x.B();
                this.f651x.K();
            }
        }
    }

    @Override // android.view.ViewGroup, android.view.View
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        B();
    }

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

    @Override // androidx.appcompat.widget.m0, android.view.View
    protected void onMeasure(int i3, int i4) {
        androidx.appcompat.view.menu.g gVar;
        boolean z2 = this.A;
        boolean z3 = View.MeasureSpec.getMode(i3) == 1073741824;
        this.A = z3;
        if (z2 != z3) {
            this.B = 0;
        }
        int size = View.MeasureSpec.getSize(i3);
        if (this.A && (gVar = this.f647t) != null && size != this.B) {
            this.B = size;
            gVar.K(true);
        }
        int childCount = getChildCount();
        if (this.A && childCount > 0) {
            M(i3, i4);
            return;
        }
        for (int i5 = 0; i5 < childCount; i5++) {
            c cVar = (c) getChildAt(i5).getLayoutParams();
            ((LinearLayout.LayoutParams) cVar).rightMargin = 0;
            ((LinearLayout.LayoutParams) cVar).leftMargin = 0;
        }
        super.onMeasure(i3, i4);
    }

    public void setExpandedActionViewsExclusive(boolean z2) {
        this.f651x.G(z2);
    }

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

    public void setOverflowIcon(Drawable drawable) {
        getMenu();
        this.f651x.I(drawable);
    }

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

    public void setPopupTheme(int i3) {
        if (this.f649v != i3) {
            this.f649v = i3;
            if (i3 == 0) {
                this.f648u = getContext();
            } else {
                this.f648u = new ContextThemeWrapper(getContext(), i3);
            }
        }
    }

    public void setPresenter(androidx.appcompat.widget.c cVar) {
        this.f651x = cVar;
        cVar.H(this);
    }

    public ActionMenuView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        setBaselineAligned(false);
        float f3 = context.getResources().getDisplayMetrics().density;
        this.C = (int) (56.0f * f3);
        this.D = (int) (f3 * 4.0f);
        this.f648u = context;
        this.f649v = 0;
    }
}
