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.o;
import androidx.appcompat.view.menu.p;
import androidx.appcompat.widget.ActionMenuView;
import c.f.l.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 {
    private View A;
    e B;
    a C;
    RunnableC0006c D;
    private b E;
    final f F;
    int G;
    d m;
    private Drawable n;
    private boolean o;
    private boolean p;
    private boolean q;
    private int r;
    private int s;
    private int t;
    private boolean u;
    private boolean v;
    private boolean w;
    private boolean x;
    private int y;
    private final SparseBooleanArray z;

    /* JADX INFO: compiled from: ActionMenuPresenter.java */
    private class a extends androidx.appcompat.view.menu.n {
        public a(Context context, androidx.appcompat.view.menu.u uVar, View view) {
            super(context, uVar, view, false, c.a.a.actionOverflowMenuStyle);
            if (!((androidx.appcompat.view.menu.j) uVar.getItem()).l()) {
                View view2 = c.this.m;
                f(view2 == null ? (View) ((androidx.appcompat.view.menu.b) c.this).l : view2);
            }
            j(c.this.F);
        }

        @Override // androidx.appcompat.view.menu.n
        protected void e() {
            c cVar = c.this;
            cVar.C = null;
            cVar.G = 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.s a() {
            a aVar = c.this.C;
            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: b, reason: collision with root package name */
        private e f374b;

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

        @Override // java.lang.Runnable
        public void run() {
            if (((androidx.appcompat.view.menu.b) c.this).f197g != null) {
                ((androidx.appcompat.view.menu.b) c.this).f197g.d();
            }
            View view = (View) ((androidx.appcompat.view.menu.b) c.this).l;
            if (view != null && view.getWindowToken() != null && this.f374b.m()) {
                c.this.B = this.f374b;
            }
            c.this.D = null;
        }
    }

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

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

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

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

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

        public d(Context context) {
            super(context, null, c.a.a.actionOverflowButtonStyle);
            setClickable(true);
            setFocusable(true);
            setVisibility(0);
            setEnabled(true);
            v0.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.J();
            return true;
        }

        @Override // android.widget.ImageView
        protected boolean setFrame(int i2, int i3, int i4, int i5) {
            boolean frame = super.setFrame(i2, i3, i4, i5);
            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.n {
        public e(Context context, androidx.appcompat.view.menu.h hVar, View view, boolean z) {
            super(context, hVar, view, z, c.a.a.actionOverflowMenuStyle);
            h(8388613);
            j(c.this.F);
        }

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

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

        @Override // androidx.appcompat.view.menu.o.a
        public void a(androidx.appcompat.view.menu.h hVar, boolean z) {
            if (hVar instanceof androidx.appcompat.view.menu.u) {
                hVar.D().e(false);
            }
            o.a aVarM = c.this.m();
            if (aVarM != null) {
                aVarM.a(hVar, z);
            }
        }

        @Override // androidx.appcompat.view.menu.o.a
        public boolean b(androidx.appcompat.view.menu.h hVar) {
            if (hVar == null) {
                return false;
            }
            c.this.G = ((androidx.appcompat.view.menu.u) hVar).getItem().getItemId();
            o.a aVarM = c.this.m();
            if (aVarM != null) {
                return aVarM.b(hVar);
            }
            return false;
        }
    }

    public c(Context context) {
        super(context, c.a.g.abc_action_menu_layout, c.a.g.abc_action_menu_item_layout);
        this.z = new SparseBooleanArray();
        this.F = new f();
    }

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

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

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

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

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

    public void E(Configuration configuration) {
        if (!this.u) {
            this.t = c.a.m.a.b(this.f196f).d();
        }
        androidx.appcompat.view.menu.h hVar = this.f197g;
        if (hVar != null) {
            hVar.K(true);
        }
    }

    public void F(boolean z) {
        this.x = z;
    }

    public void G(ActionMenuView actionMenuView) {
        this.l = actionMenuView;
        actionMenuView.b(this.f197g);
    }

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

    public void I(boolean z) {
        this.p = z;
        this.q = true;
    }

    public boolean J() {
        androidx.appcompat.view.menu.h hVar;
        if (!this.p || D() || (hVar = this.f197g) == null || this.l == null || this.D != null || hVar.z().isEmpty()) {
            return false;
        }
        RunnableC0006c runnableC0006c = new RunnableC0006c(new e(this.f196f, this.f197g, this.m, true));
        this.D = runnableC0006c;
        ((View) this.l).post(runnableC0006c);
        super.e(null);
        return true;
    }

    @Override // androidx.appcompat.view.menu.b, androidx.appcompat.view.menu.o
    public void a(androidx.appcompat.view.menu.h hVar, boolean z) {
        x();
        super.a(hVar, z);
    }

    @Override // androidx.appcompat.view.menu.b, androidx.appcompat.view.menu.o
    public void c(Context context, androidx.appcompat.view.menu.h hVar) {
        super.c(context, hVar);
        Resources resources = context.getResources();
        c.a.m.a aVarB = c.a.m.a.b(context);
        if (!this.q) {
            this.p = aVarB.h();
        }
        if (!this.w) {
            this.r = aVarB.c();
        }
        if (!this.u) {
            this.t = aVarB.d();
        }
        int measuredWidth = this.r;
        if (this.p) {
            if (this.m == null) {
                d dVar = new d(this.f195b);
                this.m = dVar;
                if (this.o) {
                    dVar.setImageDrawable(this.n);
                    this.n = null;
                    this.o = false;
                }
                int iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, 0);
                this.m.measure(iMakeMeasureSpec, iMakeMeasureSpec);
            }
            measuredWidth -= this.m.getMeasuredWidth();
        } else {
            this.m = null;
        }
        this.s = measuredWidth;
        this.y = (int) (resources.getDisplayMetrics().density * 56.0f);
        this.A = null;
    }

    @Override // androidx.appcompat.view.menu.b
    public void d(androidx.appcompat.view.menu.j jVar, p.a aVar) {
        aVar.e(jVar, 0);
        ActionMenuItemView actionMenuItemView = (ActionMenuItemView) aVar;
        actionMenuItemView.setItemInvoker((ActionMenuView) this.l);
        if (this.E == null) {
            this.E = new b();
        }
        actionMenuItemView.setPopupCallback(this.E);
    }

    @Override // androidx.appcompat.view.menu.b, androidx.appcompat.view.menu.o
    public boolean e(androidx.appcompat.view.menu.u uVar) {
        boolean z = false;
        if (!uVar.hasVisibleItems()) {
            return false;
        }
        androidx.appcompat.view.menu.u uVar2 = uVar;
        while (uVar2.e0() != this.f197g) {
            uVar2 = (androidx.appcompat.view.menu.u) uVar2.e0();
        }
        View viewY = y(uVar2.getItem());
        if (viewY == null) {
            return false;
        }
        uVar.getItem().getItemId();
        int size = uVar.size();
        int i2 = 0;
        while (true) {
            if (i2 >= size) {
                break;
            }
            MenuItem item = uVar.getItem(i2);
            if (item.isVisible() && item.getIcon() != null) {
                z = true;
                break;
            }
            i2++;
        }
        a aVar = new a(this.f196f, uVar, viewY);
        this.C = aVar;
        aVar.g(z);
        this.C.k();
        super.e(uVar);
        return true;
    }

    @Override // androidx.appcompat.view.menu.b, androidx.appcompat.view.menu.o
    public void f(boolean z) {
        super.f(z);
        ((View) this.l).requestLayout();
        androidx.appcompat.view.menu.h hVar = this.f197g;
        boolean z2 = false;
        if (hVar != null) {
            ArrayList<androidx.appcompat.view.menu.j> arrayListS = hVar.s();
            int size = arrayListS.size();
            for (int i2 = 0; i2 < size; i2++) {
                c.f.l.b bVarB = arrayListS.get(i2).b();
                if (bVarB != null) {
                    bVarB.i(this);
                }
            }
        }
        androidx.appcompat.view.menu.h hVar2 = this.f197g;
        ArrayList<androidx.appcompat.view.menu.j> arrayListZ = hVar2 != null ? hVar2.z() : null;
        if (this.p && arrayListZ != null) {
            int size2 = arrayListZ.size();
            if (size2 == 1) {
                z2 = !arrayListZ.get(0).isActionViewExpanded();
            } else if (size2 > 0) {
                z2 = true;
            }
        }
        if (z2) {
            if (this.m == null) {
                this.m = new d(this.f195b);
            }
            ViewGroup viewGroup = (ViewGroup) this.m.getParent();
            if (viewGroup != this.l) {
                if (viewGroup != null) {
                    viewGroup.removeView(this.m);
                }
                ActionMenuView actionMenuView = (ActionMenuView) this.l;
                actionMenuView.addView(this.m, actionMenuView.F());
            }
        } else {
            d dVar = this.m;
            if (dVar != null) {
                Object parent = dVar.getParent();
                Object obj = this.l;
                if (parent == obj) {
                    ((ViewGroup) obj).removeView(this.m);
                }
            }
        }
        ((ActionMenuView) this.l).setOverflowReserved(this.p);
    }

    /* 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.j] */
    /* 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.o
    public boolean g() {
        ArrayList<androidx.appcompat.view.menu.j> arrayListE;
        int size;
        int i2;
        int iL;
        ?? r0;
        int i3;
        c cVar = this;
        androidx.appcompat.view.menu.h hVar = cVar.f197g;
        ?? r2 = 0;
        if (hVar != null) {
            arrayListE = hVar.E();
            size = arrayListE.size();
        } else {
            arrayListE = null;
            size = 0;
        }
        int i4 = cVar.t;
        int i5 = cVar.s;
        int iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, 0);
        ViewGroup viewGroup = (ViewGroup) cVar.l;
        boolean z = false;
        int i6 = 0;
        int i7 = 0;
        for (int i8 = 0; i8 < size; i8++) {
            androidx.appcompat.view.menu.j jVar = arrayListE.get(i8);
            if (jVar.requiresActionButton()) {
                i6++;
            } else if (jVar.n()) {
                i7++;
            } else {
                z = true;
            }
            if (cVar.x && jVar.isActionViewExpanded()) {
                i4 = 0;
            }
        }
        if (cVar.p && (z || i7 + i6 > i4)) {
            i4--;
        }
        int i9 = i4 - i6;
        SparseBooleanArray sparseBooleanArray = cVar.z;
        sparseBooleanArray.clear();
        if (cVar.v) {
            int i10 = cVar.y;
            iL = i5 / i10;
            i2 = i10 + ((i5 % i10) / iL);
        } else {
            i2 = 0;
            iL = 0;
        }
        int i11 = 0;
        int i12 = 0;
        ?? r02 = cVar;
        while (i11 < size) {
            androidx.appcompat.view.menu.j jVar2 = arrayListE.get(i11);
            if (jVar2.requiresActionButton()) {
                View viewN = r02.n(jVar2, r02.A, viewGroup);
                if (r02.A == null) {
                    r02.A = viewN;
                }
                if (r02.v) {
                    iL -= ActionMenuView.L(viewN, i2, iL, iMakeMeasureSpec, r2);
                } else {
                    viewN.measure(iMakeMeasureSpec, iMakeMeasureSpec);
                }
                int measuredWidth = viewN.getMeasuredWidth();
                i5 -= measuredWidth;
                if (i12 == 0) {
                    i12 = measuredWidth;
                }
                int groupId = jVar2.getGroupId();
                if (groupId != 0) {
                    sparseBooleanArray.put(groupId, true);
                }
                jVar2.t(true);
                r0 = r2;
                i3 = size;
            } else if (jVar2.n()) {
                int groupId2 = jVar2.getGroupId();
                boolean z2 = sparseBooleanArray.get(groupId2);
                boolean z3 = (i9 > 0 || z2) && i5 > 0 && (!r02.v || iL > 0);
                boolean z4 = z3;
                if (z3) {
                    View viewN2 = r02.n(jVar2, r02.A, viewGroup);
                    i3 = size;
                    if (r02.A == null) {
                        r02.A = viewN2;
                    }
                    if (r02.v) {
                        int iL2 = ActionMenuView.L(viewN2, i2, iL, iMakeMeasureSpec, 0);
                        iL -= iL2;
                        if (iL2 == 0) {
                            z4 = false;
                        }
                    } else {
                        viewN2.measure(iMakeMeasureSpec, iMakeMeasureSpec);
                    }
                    int measuredWidth2 = viewN2.getMeasuredWidth();
                    i5 -= measuredWidth2;
                    if (i12 == 0) {
                        i12 = measuredWidth2;
                    }
                    z3 = z4 & (!r02.v ? i5 + i12 <= 0 : i5 < 0);
                } else {
                    i3 = size;
                }
                if (z3 && groupId2 != 0) {
                    sparseBooleanArray.put(groupId2, true);
                } else if (z2) {
                    sparseBooleanArray.put(groupId2, false);
                    for (int i13 = 0; i13 < i11; i13++) {
                        androidx.appcompat.view.menu.j jVar3 = arrayListE.get(i13);
                        if (jVar3.getGroupId() == groupId2) {
                            if (jVar3.l()) {
                                i9++;
                            }
                            jVar3.t(false);
                        }
                    }
                }
                if (z3) {
                    i9--;
                }
                jVar2.t(z3);
                r0 = 0;
            } else {
                r0 = r2;
                i3 = size;
                jVar2.t(r0);
            }
            i11++;
            r2 = r0;
            size = i3;
            r02 = this;
        }
        return true;
    }

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

    @Override // androidx.appcompat.view.menu.b
    public View n(androidx.appcompat.view.menu.j jVar, View view, ViewGroup viewGroup) {
        View actionView = jVar.getActionView();
        if (actionView == null || jVar.j()) {
            actionView = super.n(jVar, view, viewGroup);
        }
        actionView.setVisibility(jVar.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.p o(ViewGroup viewGroup) {
        androidx.appcompat.view.menu.p pVar = this.l;
        androidx.appcompat.view.menu.p pVarO = super.o(viewGroup);
        if (pVar != pVarO) {
            ((ActionMenuView) pVarO).setPresenter(this);
        }
        return pVarO;
    }

    @Override // androidx.appcompat.view.menu.b
    public boolean q(int i2, androidx.appcompat.view.menu.j jVar) {
        return jVar.l();
    }

    public boolean x() {
        return A() | B();
    }

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