package androidx.appcompat.widget;

import a.c.i.b;
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 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;
    RunnableC0022c C;
    private b D;
    final f E;
    int F;
    d l;
    private Drawable m;
    private boolean n;
    private boolean o;
    private boolean p;
    private int q;
    private int r;
    private int s;
    private boolean t;
    private boolean u;
    private boolean v;
    private boolean w;
    private int x;
    private final SparseBooleanArray y;
    private View z;

    /* JADX INFO: compiled from: ActionMenuPresenter.java */
    private class a extends androidx.appcompat.view.menu.l {
        public a(Context context, androidx.appcompat.view.menu.s sVar, View view) {
            super(context, sVar, view, false, a.a.a.i);
            if (!((androidx.appcompat.view.menu.h) sVar.getItem()).l()) {
                View view2 = c.this.l;
                f(view2 == null ? (View) ((androidx.appcompat.view.menu.b) c.this).k : 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.q 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 RunnableC0022c implements Runnable {

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

        public RunnableC0022c(e eVar) {
            this.f559c = eVar;
        }

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

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

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        private final float[] f561e;

        /* JADX INFO: compiled from: ActionMenuPresenter.java */
        class a extends u {
            final /* synthetic */ c j;

            /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
            a(View view, c cVar) {
                super(view);
                this.j = cVar;
            }

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

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

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

        public d(Context context) {
            super(context, null, a.a.a.h);
            this.f561e = new float[2];
            setClickable(true);
            setFocusable(true);
            setVisibility(0);
            setEnabled(true);
            k0.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.G();
            return true;
        }

        @Override // android.widget.ImageView
        protected boolean setFrame(int i, int i2, int i3, int i4) {
            boolean frame = super.setFrame(i, i2, i3, i4);
            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.k(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 z) {
            super(context, gVar, view, z, a.a.a.i);
            h(8388613);
            j(c.this.E);
        }

        @Override // androidx.appcompat.view.menu.l
        protected void e() {
            if (((androidx.appcompat.view.menu.b) c.this).f392e != null) {
                ((androidx.appcompat.view.menu.b) c.this).f392e.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 a(androidx.appcompat.view.menu.g gVar, boolean z) {
            if (gVar instanceof androidx.appcompat.view.menu.s) {
                gVar.z().d(false);
            }
            m.a aVarM = c.this.m();
            if (aVarM != null) {
                aVarM.a(gVar, z);
            }
        }

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

    public c(Context context) {
        super(context, a.a.g.f37c, a.a.g.f36b);
        this.y = new SparseBooleanArray();
        this.E = new f();
    }

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

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

    public void B(Configuration configuration) {
        if (!this.t) {
            this.s = a.a.m.a.a(this.f391d).c();
        }
        androidx.appcompat.view.menu.g gVar = this.f392e;
        if (gVar != null) {
            gVar.G(true);
        }
    }

    public void C(boolean z) {
        this.w = z;
    }

    public void D(ActionMenuView actionMenuView) {
        this.k = actionMenuView;
        actionMenuView.E(this.f392e);
    }

    public void E(Drawable drawable) {
        d dVar = this.l;
        if (dVar != null) {
            dVar.setImageDrawable(drawable);
        } else {
            this.n = true;
            this.m = drawable;
        }
    }

    public void F(boolean z) {
        this.o = z;
        this.p = true;
    }

    public boolean G() {
        androidx.appcompat.view.menu.g gVar;
        if (!this.o || A() || (gVar = this.f392e) == null || this.k == null || this.C != null || gVar.v().isEmpty()) {
            return false;
        }
        RunnableC0022c runnableC0022c = new RunnableC0022c(new e(this.f391d, this.f392e, this.l, true));
        this.C = runnableC0022c;
        ((View) this.k).post(runnableC0022c);
        super.l(null);
        return true;
    }

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

    @Override // androidx.appcompat.view.menu.b, androidx.appcompat.view.menu.m
    public void b(boolean z) {
        super.b(z);
        ((View) this.k).requestLayout();
        androidx.appcompat.view.menu.g gVar = this.f392e;
        boolean z2 = false;
        if (gVar != null) {
            ArrayList<androidx.appcompat.view.menu.h> arrayListR = gVar.r();
            int size = arrayListR.size();
            for (int i = 0; i < size; i++) {
                a.c.i.b bVarB = arrayListR.get(i).b();
                if (bVarB != null) {
                    bVarB.i(this);
                }
            }
        }
        androidx.appcompat.view.menu.g gVar2 = this.f392e;
        ArrayList<androidx.appcompat.view.menu.h> arrayListV = gVar2 != null ? gVar2.v() : null;
        if (this.o && arrayListV != null) {
            int size2 = arrayListV.size();
            if (size2 == 1) {
                z2 = !arrayListV.get(0).isActionViewExpanded();
            } else if (size2 > 0) {
                z2 = true;
            }
        }
        if (z2) {
            if (this.l == null) {
                this.l = new d(this.f390c);
            }
            ViewGroup viewGroup = (ViewGroup) this.l.getParent();
            if (viewGroup != this.k) {
                if (viewGroup != null) {
                    viewGroup.removeView(this.l);
                }
                ActionMenuView actionMenuView = (ActionMenuView) this.k;
                actionMenuView.addView(this.l, actionMenuView.C());
            }
        } else {
            d dVar = this.l;
            if (dVar != null) {
                Object parent = dVar.getParent();
                Object obj = this.k;
                if (parent == obj) {
                    ((ViewGroup) obj).removeView(this.l);
                }
            }
        }
        ((ActionMenuView) this.k).setOverflowReserved(this.o);
    }

    /* 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: r14v1, types: [androidx.appcompat.view.menu.h] */
    /* JADX WARN: Type inference failed for: r2v0 */
    /* JADX WARN: Type inference failed for: r2v1, types: [int] */
    /* JADX WARN: Type inference failed for: r2v17 */
    @Override // androidx.appcompat.view.menu.m
    public boolean c() {
        ArrayList<androidx.appcompat.view.menu.h> arrayListA;
        int size;
        int i;
        int iG;
        ?? r0;
        int i2;
        c cVar = this;
        androidx.appcompat.view.menu.g gVar = cVar.f392e;
        ?? r2 = 0;
        if (gVar != null) {
            arrayListA = gVar.A();
            size = arrayListA.size();
        } else {
            arrayListA = null;
            size = 0;
        }
        int i3 = cVar.s;
        int i4 = cVar.r;
        int iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, 0);
        ViewGroup viewGroup = (ViewGroup) cVar.k;
        boolean z = false;
        int i5 = 0;
        int i6 = 0;
        for (int i7 = 0; i7 < size; i7++) {
            androidx.appcompat.view.menu.h hVar = arrayListA.get(i7);
            if (hVar.o()) {
                i5++;
            } else if (hVar.n()) {
                i6++;
            } else {
                z = true;
            }
            if (cVar.w && hVar.isActionViewExpanded()) {
                i3 = 0;
            }
        }
        if (cVar.o && (z || i6 + i5 > i3)) {
            i3--;
        }
        int i8 = i3 - i5;
        SparseBooleanArray sparseBooleanArray = cVar.y;
        sparseBooleanArray.clear();
        if (cVar.u) {
            int i9 = cVar.x;
            iG = i4 / i9;
            i = i9 + ((i4 % i9) / iG);
        } else {
            i = 0;
            iG = 0;
        }
        int i10 = 0;
        int i11 = 0;
        ?? r02 = cVar;
        while (i10 < size) {
            androidx.appcompat.view.menu.h hVar2 = arrayListA.get(i10);
            if (hVar2.o()) {
                View viewN = r02.n(hVar2, r02.z, viewGroup);
                if (r02.z == null) {
                    r02.z = viewN;
                }
                if (r02.u) {
                    iG -= ActionMenuView.G(viewN, i, iG, iMakeMeasureSpec, r2);
                } else {
                    viewN.measure(iMakeMeasureSpec, iMakeMeasureSpec);
                }
                int measuredWidth = viewN.getMeasuredWidth();
                i4 -= measuredWidth;
                if (i11 == 0) {
                    i11 = measuredWidth;
                }
                int groupId = hVar2.getGroupId();
                if (groupId != 0) {
                    sparseBooleanArray.put(groupId, true);
                }
                hVar2.u(true);
                r0 = r2;
                i2 = size;
            } else if (hVar2.n()) {
                int groupId2 = hVar2.getGroupId();
                boolean z2 = sparseBooleanArray.get(groupId2);
                boolean z3 = (i8 > 0 || z2) && i4 > 0 && (!r02.u || iG > 0);
                boolean z4 = z3;
                if (z3) {
                    View viewN2 = r02.n(hVar2, r02.z, viewGroup);
                    i2 = size;
                    if (r02.z == null) {
                        r02.z = viewN2;
                    }
                    if (r02.u) {
                        int iG2 = ActionMenuView.G(viewN2, i, iG, iMakeMeasureSpec, 0);
                        iG -= iG2;
                        if (iG2 == 0) {
                            z4 = false;
                        }
                    } else {
                        viewN2.measure(iMakeMeasureSpec, iMakeMeasureSpec);
                    }
                    int measuredWidth2 = viewN2.getMeasuredWidth();
                    i4 -= measuredWidth2;
                    if (i11 == 0) {
                        i11 = measuredWidth2;
                    }
                    z3 = z4 & (!r02.u ? i4 + i11 <= 0 : i4 < 0);
                } else {
                    i2 = size;
                }
                if (z3 && groupId2 != 0) {
                    sparseBooleanArray.put(groupId2, true);
                } else if (z2) {
                    sparseBooleanArray.put(groupId2, false);
                    for (int i12 = 0; i12 < i10; i12++) {
                        androidx.appcompat.view.menu.h hVar3 = arrayListA.get(i12);
                        if (hVar3.getGroupId() == groupId2) {
                            if (hVar3.l()) {
                                i8++;
                            }
                            hVar3.u(false);
                        }
                    }
                }
                if (z3) {
                    i8--;
                }
                hVar2.u(z3);
                r0 = 0;
            } else {
                r0 = r2;
                i2 = size;
                hVar2.u(r0);
            }
            i10++;
            r2 = r0;
            size = i2;
            r02 = this;
        }
        return true;
    }

    @Override // androidx.appcompat.view.menu.b
    public void h(androidx.appcompat.view.menu.h hVar, n.a aVar) {
        aVar.d(hVar, 0);
        ActionMenuItemView actionMenuItemView = (ActionMenuItemView) aVar;
        actionMenuItemView.setItemInvoker((ActionMenuView) this.k);
        if (this.D == null) {
            this.D = new b();
        }
        actionMenuItemView.setPopupCallback(this.D);
    }

    @Override // androidx.appcompat.view.menu.b, androidx.appcompat.view.menu.m
    public void j(Context context, androidx.appcompat.view.menu.g gVar) {
        super.j(context, gVar);
        Resources resources = context.getResources();
        a.a.m.a aVarA = a.a.m.a.a(context);
        if (!this.p) {
            this.o = aVarA.g();
        }
        if (!this.v) {
            this.q = aVarA.b();
        }
        if (!this.t) {
            this.s = aVarA.c();
        }
        int measuredWidth = this.q;
        if (this.o) {
            if (this.l == null) {
                d dVar = new d(this.f390c);
                this.l = dVar;
                if (this.n) {
                    dVar.setImageDrawable(this.m);
                    this.m = null;
                    this.n = false;
                }
                int iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, 0);
                this.l.measure(iMakeMeasureSpec, iMakeMeasureSpec);
            }
            measuredWidth -= this.l.getMeasuredWidth();
        } else {
            this.l = null;
        }
        this.r = measuredWidth;
        this.x = (int) (resources.getDisplayMetrics().density * 56.0f);
        this.z = null;
    }

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

    @Override // androidx.appcompat.view.menu.b, androidx.appcompat.view.menu.m
    public boolean l(androidx.appcompat.view.menu.s sVar) {
        boolean z = false;
        if (!sVar.hasVisibleItems()) {
            return false;
        }
        androidx.appcompat.view.menu.s sVar2 = sVar;
        while (sVar2.W() != this.f392e) {
            sVar2 = (androidx.appcompat.view.menu.s) sVar2.W();
        }
        View viewW = w(sVar2.getItem());
        if (viewW == null) {
            return false;
        }
        this.F = sVar.getItem().getItemId();
        int size = sVar.size();
        int i = 0;
        while (true) {
            if (i >= size) {
                break;
            }
            MenuItem item = sVar.getItem(i);
            if (item.isVisible() && item.getIcon() != null) {
                z = true;
                break;
            }
            i++;
        }
        a aVar = new a(this.f391d, sVar, viewW);
        this.B = aVar;
        aVar.g(z);
        this.B.k();
        super.l(sVar);
        return true;
    }

    @Override // androidx.appcompat.view.menu.b
    public View n(androidx.appcompat.view.menu.h hVar, View view, ViewGroup viewGroup) {
        View actionView = hVar.getActionView();
        if (actionView == null || hVar.j()) {
            actionView = super.n(hVar, view, viewGroup);
        }
        actionView.setVisibility(hVar.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 boolean o(int i, androidx.appcompat.view.menu.h hVar) {
        return hVar.l();
    }

    public boolean v() {
        return y() | z();
    }

    public Drawable x() {
        d dVar = this.l;
        if (dVar != null) {
            return dVar.getDrawable();
        }
        if (this.n) {
            return this.m;
        }
        return null;
    }

    public boolean y() {
        Object obj;
        RunnableC0022c runnableC0022c = this.C;
        if (runnableC0022c != null && (obj = this.k) != null) {
            ((View) obj).removeCallbacks(runnableC0022c);
            this.C = null;
            return true;
        }
        e eVar = this.A;
        if (eVar == null) {
            return false;
        }
        eVar.b();
        return true;
    }

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