package androidx.appcompat.widget;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.util.SparseBooleanArray;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import androidx.appcompat.view.menu.ActionMenuItemView;
import androidx.appcompat.view.menu.m;
import androidx.appcompat.view.menu.n;
import androidx.appcompat.widget.ActionMenuView;
import androidx.core.view.b;
import java.util.ArrayList;

/* JADX INFO: compiled from: ActionMenuPresenter.java */
/* JADX INFO: loaded from: classes.dex */
class c extends androidx.appcompat.view.menu.b implements b.a {
    e A;
    a B;
    RunnableC0006c C;
    private b D;
    final f E;
    int F;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    d f805m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    private Drawable f806n;

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

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

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

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

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    private final SparseBooleanArray f818z;

    /* JADX INFO: compiled from: ActionMenuPresenter.java */
    private class a extends androidx.appcompat.view.menu.l {
        public a(Context context, androidx.appcompat.view.menu.r rVar, View view) {
            super(context, rVar, view, false, c.a.f3161l);
            if (!((androidx.appcompat.view.menu.i) rVar.getItem()).l()) {
                View view2 = c.this.f805m;
                f(view2 == null ? (View) ((androidx.appcompat.view.menu.b) c.this).f441l : view2);
            }
            j(c.this.E);
        }

        @Override // androidx.appcompat.view.menu.l
        protected void e() {
            c cVar = c.this;
            cVar.B = null;
            cVar.F = 0;
            super.e();
        }
    }

    /* JADX INFO: compiled from: ActionMenuPresenter.java */
    private class b extends ActionMenuItemView.b {
        b() {
        }

        @Override // androidx.appcompat.view.menu.ActionMenuItemView.b
        public androidx.appcompat.view.menu.p a() {
            a aVar = c.this.B;
            if (aVar != null) {
                return aVar.c();
            }
            return null;
        }
    }

    /* JADX INFO: renamed from: androidx.appcompat.widget.c$c, reason: collision with other inner class name */
    /* JADX INFO: compiled from: ActionMenuPresenter.java */
    private class RunnableC0006c implements Runnable {

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        private e f821e;

        public RunnableC0006c(e eVar) {
            this.f821e = eVar;
        }

        @Override // java.lang.Runnable
        public void run() {
            if (((androidx.appcompat.view.menu.b) c.this).f436g != null) {
                ((androidx.appcompat.view.menu.b) c.this).f436g.d();
            }
            View view = (View) ((androidx.appcompat.view.menu.b) c.this).f441l;
            if (view != null && view.getWindowToken() != null && this.f821e.m()) {
                c.this.A = this.f821e;
            }
            c.this.C = null;
        }
    }

    /* JADX INFO: compiled from: ActionMenuPresenter.java */
    private class d extends r implements ActionMenuView.a {

        /* JADX INFO: compiled from: ActionMenuPresenter.java */
        class a extends l0 {
            a(View view, c cVar) {
                super(view);
            }

            @Override // androidx.appcompat.widget.l0
            public androidx.appcompat.view.menu.p b() {
                e eVar = c.this.A;
                if (eVar == null) {
                    return null;
                }
                return eVar.c();
            }

            @Override // androidx.appcompat.widget.l0
            public boolean c() {
                c.this.K();
                return true;
            }

            @Override // androidx.appcompat.widget.l0
            public boolean d() {
                c cVar = c.this;
                if (cVar.C != null) {
                    return false;
                }
                cVar.B();
                return true;
            }
        }

        public d(Context context) {
            super(context, null, c.a.f3160k);
            setClickable(true);
            setFocusable(true);
            setVisibility(0);
            setEnabled(true);
            d1.a(this, getContentDescription());
            setOnTouchListener(new a(this, c.this));
        }

        @Override // androidx.appcompat.widget.ActionMenuView.a
        public boolean a() {
            return false;
        }

        @Override // androidx.appcompat.widget.ActionMenuView.a
        public boolean b() {
            return false;
        }

        @Override // android.view.View
        public boolean performClick() {
            if (super.performClick()) {
                return true;
            }
            playSoundEffect(0);
            c.this.K();
            return true;
        }

        @Override // android.widget.ImageView
        protected boolean setFrame(int i3, int i4, int i5, int i6) {
            boolean frame = super.setFrame(i3, i4, i5, i6);
            Drawable drawable = getDrawable();
            Drawable background = getBackground();
            if (drawable != null && background != null) {
                int width = getWidth();
                int height = getHeight();
                int iMax = Math.max(width, height) / 2;
                int paddingLeft = (width + (getPaddingLeft() - getPaddingRight())) / 2;
                int paddingTop = (height + (getPaddingTop() - getPaddingBottom())) / 2;
                androidx.core.graphics.drawable.a.l(background, paddingLeft - iMax, paddingTop - iMax, paddingLeft + iMax, paddingTop + iMax);
            }
            return frame;
        }
    }

    /* JADX INFO: compiled from: ActionMenuPresenter.java */
    private class e extends androidx.appcompat.view.menu.l {
        public e(Context context, androidx.appcompat.view.menu.g gVar, View view, boolean z2) {
            super(context, gVar, view, z2, c.a.f3161l);
            h(8388613);
            j(c.this.E);
        }

        @Override // androidx.appcompat.view.menu.l
        protected void e() {
            if (((androidx.appcompat.view.menu.b) c.this).f436g != null) {
                ((androidx.appcompat.view.menu.b) c.this).f436g.close();
            }
            c.this.A = null;
            super.e();
        }
    }

    /* JADX INFO: compiled from: ActionMenuPresenter.java */
    private class f implements m.a {
        f() {
        }

        @Override // androidx.appcompat.view.menu.m.a
        public void b(androidx.appcompat.view.menu.g gVar, boolean z2) {
            if (gVar instanceof androidx.appcompat.view.menu.r) {
                gVar.D().e(false);
            }
            m.a aVarM = c.this.m();
            if (aVarM != null) {
                aVarM.b(gVar, z2);
            }
        }

        @Override // androidx.appcompat.view.menu.m.a
        public boolean c(androidx.appcompat.view.menu.g gVar) {
            if (gVar == ((androidx.appcompat.view.menu.b) c.this).f436g) {
                return false;
            }
            c.this.F = ((androidx.appcompat.view.menu.r) gVar).getItem().getItemId();
            m.a aVarM = c.this.m();
            if (aVarM != null) {
                return aVarM.c(gVar);
            }
            return false;
        }
    }

    public c(Context context) {
        super(context, c.g.f3254c, c.g.f3253b);
        this.f818z = new SparseBooleanArray();
        this.E = new f();
    }

    /* JADX WARN: Multi-variable type inference failed */
    private View z(MenuItem menuItem) {
        ViewGroup viewGroup = (ViewGroup) this.f441l;
        if (viewGroup == null) {
            return null;
        }
        int childCount = viewGroup.getChildCount();
        for (int i3 = 0; i3 < childCount; i3++) {
            View childAt = viewGroup.getChildAt(i3);
            if ((childAt instanceof n.a) && ((n.a) childAt).getItemData() == menuItem) {
                return childAt;
            }
        }
        return null;
    }

    public Drawable A() {
        d dVar = this.f805m;
        if (dVar != null) {
            return dVar.getDrawable();
        }
        if (this.f807o) {
            return this.f806n;
        }
        return null;
    }

    public boolean B() {
        Object obj;
        RunnableC0006c runnableC0006c = this.C;
        if (runnableC0006c != null && (obj = this.f441l) != null) {
            ((View) obj).removeCallbacks(runnableC0006c);
            this.C = null;
            return true;
        }
        e eVar = this.A;
        if (eVar == null) {
            return false;
        }
        eVar.b();
        return true;
    }

    public boolean C() {
        a aVar = this.B;
        if (aVar == null) {
            return false;
        }
        aVar.b();
        return true;
    }

    public boolean D() {
        return this.C != null || E();
    }

    public boolean E() {
        e eVar = this.A;
        return eVar != null && eVar.d();
    }

    public void F(Configuration configuration) {
        if (!this.f813u) {
            this.f812t = androidx.appcompat.view.a.b(this.f435f).d();
        }
        androidx.appcompat.view.menu.g gVar = this.f436g;
        if (gVar != null) {
            gVar.K(true);
        }
    }

    public void G(boolean z2) {
        this.f816x = z2;
    }

    public void H(ActionMenuView actionMenuView) {
        this.f441l = actionMenuView;
        actionMenuView.b(this.f436g);
    }

    public void I(Drawable drawable) {
        d dVar = this.f805m;
        if (dVar != null) {
            dVar.setImageDrawable(drawable);
        } else {
            this.f807o = true;
            this.f806n = drawable;
        }
    }

    public void J(boolean z2) {
        this.f808p = z2;
        this.f809q = true;
    }

    public boolean K() {
        androidx.appcompat.view.menu.g gVar;
        if (!this.f808p || E() || (gVar = this.f436g) == null || this.f441l == null || this.C != null || gVar.z().isEmpty()) {
            return false;
        }
        RunnableC0006c runnableC0006c = new RunnableC0006c(new e(this.f435f, this.f436g, this.f805m, true));
        this.C = runnableC0006c;
        ((View) this.f441l).post(runnableC0006c);
        return true;
    }

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

    @Override // androidx.appcompat.view.menu.b
    public void c(androidx.appcompat.view.menu.i iVar, n.a aVar) {
        aVar.e(iVar, 0);
        ActionMenuItemView actionMenuItemView = (ActionMenuItemView) aVar;
        actionMenuItemView.setItemInvoker((ActionMenuView) this.f441l);
        if (this.D == null) {
            this.D = new b();
        }
        actionMenuItemView.setPopupCallback(this.D);
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r0v1, types: [androidx.appcompat.widget.c] */
    /* JADX WARN: Type inference failed for: r0v10 */
    /* JADX WARN: Type inference failed for: r0v11 */
    /* JADX WARN: Type inference failed for: r0v2, types: [boolean] */
    /* JADX WARN: Type inference failed for: r0v7 */
    /* JADX WARN: Type inference failed for: r0v8 */
    /* JADX WARN: Type inference failed for: r0v9 */
    /* JADX WARN: Type inference failed for: r15v1, types: [androidx.appcompat.view.menu.i] */
    /* JADX WARN: Type inference failed for: r3v0 */
    /* JADX WARN: Type inference failed for: r3v1, types: [int] */
    /* JADX WARN: Type inference failed for: r3v12 */
    @Override // androidx.appcompat.view.menu.m
    public boolean d() {
        ArrayList<androidx.appcompat.view.menu.i> arrayListE;
        int size;
        int i3;
        int iL;
        ?? r02;
        int i4;
        c cVar = this;
        androidx.appcompat.view.menu.g gVar = cVar.f436g;
        View view = null;
        ?? r3 = 0;
        if (gVar != null) {
            arrayListE = gVar.E();
            size = arrayListE.size();
        } else {
            arrayListE = null;
            size = 0;
        }
        int i5 = cVar.f812t;
        int i6 = cVar.f811s;
        int iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, 0);
        ViewGroup viewGroup = (ViewGroup) cVar.f441l;
        boolean z2 = false;
        int i7 = 0;
        int i8 = 0;
        for (int i9 = 0; i9 < size; i9++) {
            androidx.appcompat.view.menu.i iVar = arrayListE.get(i9);
            if (iVar.requiresActionButton()) {
                i7++;
            } else if (iVar.n()) {
                i8++;
            } else {
                z2 = true;
            }
            if (cVar.f816x && iVar.isActionViewExpanded()) {
                i5 = 0;
            }
        }
        if (cVar.f808p && (z2 || i8 + i7 > i5)) {
            i5--;
        }
        int i10 = i5 - i7;
        SparseBooleanArray sparseBooleanArray = cVar.f818z;
        sparseBooleanArray.clear();
        if (cVar.f814v) {
            int i11 = cVar.f817y;
            iL = i6 / i11;
            i3 = i11 + ((i6 % i11) / iL);
        } else {
            i3 = 0;
            iL = 0;
        }
        int i12 = 0;
        int i13 = 0;
        ?? r03 = cVar;
        while (i12 < size) {
            androidx.appcompat.view.menu.i iVar2 = arrayListE.get(i12);
            if (iVar2.requiresActionButton()) {
                View viewN = r03.n(iVar2, view, viewGroup);
                if (r03.f814v) {
                    iL -= ActionMenuView.L(viewN, i3, iL, iMakeMeasureSpec, r3);
                } else {
                    viewN.measure(iMakeMeasureSpec, iMakeMeasureSpec);
                }
                int measuredWidth = viewN.getMeasuredWidth();
                i6 -= measuredWidth;
                if (i13 == 0) {
                    i13 = measuredWidth;
                }
                int groupId = iVar2.getGroupId();
                if (groupId != 0) {
                    sparseBooleanArray.put(groupId, true);
                }
                iVar2.t(true);
                r02 = r3;
                i4 = size;
            } else if (iVar2.n()) {
                int groupId2 = iVar2.getGroupId();
                boolean z3 = sparseBooleanArray.get(groupId2);
                boolean z4 = (i10 > 0 || z3) && i6 > 0 && (!r03.f814v || iL > 0);
                boolean z5 = z4;
                i4 = size;
                if (z4) {
                    View viewN2 = r03.n(iVar2, null, viewGroup);
                    if (r03.f814v) {
                        int iL2 = ActionMenuView.L(viewN2, i3, iL, iMakeMeasureSpec, 0);
                        iL -= iL2;
                        if (iL2 == 0) {
                            z5 = false;
                        }
                    } else {
                        viewN2.measure(iMakeMeasureSpec, iMakeMeasureSpec);
                    }
                    boolean z6 = z5;
                    int measuredWidth2 = viewN2.getMeasuredWidth();
                    i6 -= measuredWidth2;
                    if (i13 == 0) {
                        i13 = measuredWidth2;
                    }
                    z4 = z6 & (!r03.f814v ? i6 + i13 <= 0 : i6 < 0);
                }
                if (z4 && groupId2 != 0) {
                    sparseBooleanArray.put(groupId2, true);
                } else if (z3) {
                    sparseBooleanArray.put(groupId2, false);
                    for (int i14 = 0; i14 < i12; i14++) {
                        androidx.appcompat.view.menu.i iVar3 = arrayListE.get(i14);
                        if (iVar3.getGroupId() == groupId2) {
                            if (iVar3.l()) {
                                i10++;
                            }
                            iVar3.t(false);
                        }
                    }
                }
                if (z4) {
                    i10--;
                }
                iVar2.t(z4);
                r02 = 0;
            } else {
                r02 = r3;
                i4 = size;
                iVar2.t(r02);
            }
            i12++;
            r3 = r02;
            size = i4;
            view = null;
            r03 = this;
        }
        return true;
    }

    @Override // androidx.appcompat.view.menu.b, androidx.appcompat.view.menu.m
    public void e(Context context, androidx.appcompat.view.menu.g gVar) {
        super.e(context, gVar);
        Resources resources = context.getResources();
        androidx.appcompat.view.a aVarB = androidx.appcompat.view.a.b(context);
        if (!this.f809q) {
            this.f808p = aVarB.h();
        }
        if (!this.f815w) {
            this.f810r = aVarB.c();
        }
        if (!this.f813u) {
            this.f812t = aVarB.d();
        }
        int measuredWidth = this.f810r;
        if (this.f808p) {
            if (this.f805m == null) {
                d dVar = new d(this.f434e);
                this.f805m = dVar;
                if (this.f807o) {
                    dVar.setImageDrawable(this.f806n);
                    this.f806n = null;
                    this.f807o = false;
                }
                int iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, 0);
                this.f805m.measure(iMakeMeasureSpec, iMakeMeasureSpec);
            }
            measuredWidth -= this.f805m.getMeasuredWidth();
        } else {
            this.f805m = null;
        }
        this.f811s = measuredWidth;
        this.f817y = (int) (resources.getDisplayMetrics().density * 56.0f);
    }

    @Override // androidx.appcompat.view.menu.b, androidx.appcompat.view.menu.m
    public boolean i(androidx.appcompat.view.menu.r rVar) {
        boolean z2 = false;
        if (!rVar.hasVisibleItems()) {
            return false;
        }
        androidx.appcompat.view.menu.r rVar2 = rVar;
        while (rVar2.e0() != this.f436g) {
            rVar2 = (androidx.appcompat.view.menu.r) rVar2.e0();
        }
        View viewZ = z(rVar2.getItem());
        if (viewZ == null) {
            return false;
        }
        rVar.getItem().getItemId();
        int size = rVar.size();
        int i3 = 0;
        while (true) {
            if (i3 >= size) {
                break;
            }
            MenuItem item = rVar.getItem(i3);
            if (item.isVisible() && item.getIcon() != null) {
                z2 = true;
                break;
            }
            i3++;
        }
        a aVar = new a(this.f435f, rVar, viewZ);
        this.B = aVar;
        aVar.g(z2);
        this.B.k();
        super.i(rVar);
        return true;
    }

    @Override // androidx.appcompat.view.menu.b, androidx.appcompat.view.menu.m
    public void j(boolean z2) {
        super.j(z2);
        ((View) this.f441l).requestLayout();
        androidx.appcompat.view.menu.g gVar = this.f436g;
        boolean z3 = false;
        if (gVar != null) {
            ArrayList<androidx.appcompat.view.menu.i> arrayListS = gVar.s();
            int size = arrayListS.size();
            for (int i3 = 0; i3 < size; i3++) {
                androidx.core.view.b bVarB = arrayListS.get(i3).b();
                if (bVarB != null) {
                    bVarB.i(this);
                }
            }
        }
        androidx.appcompat.view.menu.g gVar2 = this.f436g;
        ArrayList<androidx.appcompat.view.menu.i> arrayListZ = gVar2 != null ? gVar2.z() : null;
        if (this.f808p && arrayListZ != null) {
            int size2 = arrayListZ.size();
            if (size2 == 1) {
                z3 = !arrayListZ.get(0).isActionViewExpanded();
            } else if (size2 > 0) {
                z3 = true;
            }
        }
        if (z3) {
            if (this.f805m == null) {
                this.f805m = new d(this.f434e);
            }
            ViewGroup viewGroup = (ViewGroup) this.f805m.getParent();
            if (viewGroup != this.f441l) {
                if (viewGroup != null) {
                    viewGroup.removeView(this.f805m);
                }
                ActionMenuView actionMenuView = (ActionMenuView) this.f441l;
                actionMenuView.addView(this.f805m, actionMenuView.F());
            }
        } else {
            d dVar = this.f805m;
            if (dVar != null) {
                Object parent = dVar.getParent();
                Object obj = this.f441l;
                if (parent == obj) {
                    ((ViewGroup) obj).removeView(this.f805m);
                }
            }
        }
        ((ActionMenuView) this.f441l).setOverflowReserved(this.f808p);
    }

    @Override // androidx.appcompat.view.menu.b
    public boolean l(ViewGroup viewGroup, int i3) {
        if (viewGroup.getChildAt(i3) == this.f805m) {
            return false;
        }
        return super.l(viewGroup, i3);
    }

    @Override // androidx.appcompat.view.menu.b
    public View n(androidx.appcompat.view.menu.i iVar, View view, ViewGroup viewGroup) {
        View actionView = iVar.getActionView();
        if (actionView == null || iVar.j()) {
            actionView = super.n(iVar, view, viewGroup);
        }
        actionView.setVisibility(iVar.isActionViewExpanded() ? 8 : 0);
        ActionMenuView actionMenuView = (ActionMenuView) viewGroup;
        ViewGroup.LayoutParams layoutParams = actionView.getLayoutParams();
        if (!actionMenuView.checkLayoutParams(layoutParams)) {
            actionView.setLayoutParams(actionMenuView.generateLayoutParams(layoutParams));
        }
        return actionView;
    }

    @Override // androidx.appcompat.view.menu.b
    public androidx.appcompat.view.menu.n o(ViewGroup viewGroup) {
        androidx.appcompat.view.menu.n nVar = this.f441l;
        androidx.appcompat.view.menu.n nVarO = super.o(viewGroup);
        if (nVar != nVarO) {
            ((ActionMenuView) nVarO).setPresenter(this);
        }
        return nVarO;
    }

    @Override // androidx.appcompat.view.menu.b
    public boolean q(int i3, androidx.appcompat.view.menu.i iVar) {
        return iVar.l();
    }

    public boolean y() {
        return C() | B();
    }
}
