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 g.e.j.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 {
    a A;
    RunnableC0003c B;
    private b C;
    final f D;
    int E;

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

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

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private boolean f148m;
    private boolean n;
    private boolean o;
    private int p;
    private int q;
    private int r;
    private boolean s;
    private boolean t;
    private boolean u;
    private boolean v;
    private int w;
    private final SparseBooleanArray x;
    private View y;
    e 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, g.a.a.actionOverflowMenuStyle);
            if (!((androidx.appcompat.view.menu.j) uVar.getItem()).l()) {
                View view2 = c.this.f146k;
                f(view2 == null ? (View) ((androidx.appcompat.view.menu.b) c.this).f67j : view2);
            }
            j(c.this.D);
        }

        @Override // androidx.appcompat.view.menu.n
        protected void e() {
            c cVar = c.this;
            cVar.A = null;
            cVar.E = 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.A;
            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 RunnableC0003c implements Runnable {
        private e b;

        public RunnableC0003c(e eVar) {
            this.b = eVar;
        }

        @Override // java.lang.Runnable
        public void run() {
            if (((androidx.appcompat.view.menu.b) c.this).e != null) {
                ((androidx.appcompat.view.menu.b) c.this).e.d();
            }
            View view = (View) ((androidx.appcompat.view.menu.b) c.this).f67j;
            if (view != null && view.getWindowToken() != null && this.b.m()) {
                c.this.z = this.b;
            }
            c.this.B = 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.z;
                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.B != null) {
                    return false;
                }
                cVar.A();
                return true;
            }
        }

        public d(Context context) {
            super(context, null, g.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 b() {
            return false;
        }

        @Override // androidx.appcompat.widget.ActionMenuView.a
        public boolean c() {
            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.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.n {
        public e(Context context, androidx.appcompat.view.menu.h hVar, View view, boolean z) {
            super(context, hVar, view, z, g.a.a.actionOverflowMenuStyle);
            h(8388613);
            j(c.this.D);
        }

        @Override // androidx.appcompat.view.menu.n
        protected void e() {
            if (((androidx.appcompat.view.menu.b) c.this).e != null) {
                ((androidx.appcompat.view.menu.b) c.this).e.close();
            }
            c.this.z = 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.E = ((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, g.a.g.abc_action_menu_layout, g.a.g.abc_action_menu_item_layout);
        this.x = new SparseBooleanArray();
        this.D = new f();
    }

    /* JADX WARN: Multi-variable type inference failed */
    private View y(MenuItem menuItem) {
        ViewGroup viewGroup = (ViewGroup) this.f67j;
        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;
        RunnableC0003c runnableC0003c = this.B;
        if (runnableC0003c != null && (obj = this.f67j) != null) {
            ((View) obj).removeCallbacks(runnableC0003c);
            this.B = null;
            return true;
        }
        e eVar = this.z;
        if (eVar == null) {
            return false;
        }
        eVar.b();
        return true;
    }

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

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

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

    public void E(Configuration configuration) {
        if (!this.s) {
            this.r = g.a.m.a.b(this.d).d();
        }
        androidx.appcompat.view.menu.h hVar = this.e;
        if (hVar != null) {
            hVar.K(true);
        }
    }

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

    public void G(ActionMenuView actionMenuView) {
        this.f67j = actionMenuView;
        actionMenuView.b(this.e);
    }

    public void H(Drawable drawable) {
        d dVar = this.f146k;
        if (dVar != null) {
            dVar.setImageDrawable(drawable);
        } else {
            this.f148m = true;
            this.f147l = drawable;
        }
    }

    public void I(boolean z) {
        this.n = z;
        this.o = true;
    }

    public boolean J() {
        androidx.appcompat.view.menu.h hVar;
        if (!this.n || D() || (hVar = this.e) == null || this.f67j == null || this.B != null || hVar.z().isEmpty()) {
            return false;
        }
        RunnableC0003c runnableC0003c = new RunnableC0003c(new e(this.d, this.e, this.f146k, true));
        this.B = runnableC0003c;
        ((View) this.f67j).post(runnableC0003c);
        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();
        g.a.m.a aVarB = g.a.m.a.b(context);
        if (!this.o) {
            this.n = aVarB.h();
        }
        if (!this.u) {
            this.p = aVarB.c();
        }
        if (!this.s) {
            this.r = aVarB.d();
        }
        int measuredWidth = this.p;
        if (this.n) {
            if (this.f146k == null) {
                d dVar = new d(this.b);
                this.f146k = dVar;
                if (this.f148m) {
                    dVar.setImageDrawable(this.f147l);
                    this.f147l = null;
                    this.f148m = false;
                }
                int iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, 0);
                this.f146k.measure(iMakeMeasureSpec, iMakeMeasureSpec);
            }
            measuredWidth -= this.f146k.getMeasuredWidth();
        } else {
            this.f146k = null;
        }
        this.q = measuredWidth;
        this.w = (int) (resources.getDisplayMetrics().density * 56.0f);
        this.y = null;
    }

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

    @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.e) {
            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.d, uVar, viewY);
        this.A = aVar;
        aVar.g(z);
        this.A.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.f67j).requestLayout();
        androidx.appcompat.view.menu.h hVar = this.e;
        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++) {
                g.e.j.b bVarB = arrayListS.get(i2).b();
                if (bVarB != null) {
                    bVarB.i(this);
                }
            }
        }
        androidx.appcompat.view.menu.h hVar2 = this.e;
        ArrayList<androidx.appcompat.view.menu.j> arrayListZ = hVar2 != null ? hVar2.z() : null;
        if (this.n && arrayListZ != null) {
            int size2 = arrayListZ.size();
            if (size2 == 1) {
                z2 = !arrayListZ.get(0).isActionViewExpanded();
            } else if (size2 > 0) {
                z2 = true;
            }
        }
        if (z2) {
            if (this.f146k == null) {
                this.f146k = new d(this.b);
            }
            ViewGroup viewGroup = (ViewGroup) this.f146k.getParent();
            if (viewGroup != this.f67j) {
                if (viewGroup != null) {
                    viewGroup.removeView(this.f146k);
                }
                ActionMenuView actionMenuView = (ActionMenuView) this.f67j;
                actionMenuView.addView(this.f146k, actionMenuView.D());
            }
        } else {
            d dVar = this.f146k;
            if (dVar != null) {
                Object parent = dVar.getParent();
                Object obj = this.f67j;
                if (parent == obj) {
                    ((ViewGroup) obj).removeView(this.f146k);
                }
            }
        }
        ((ActionMenuView) this.f67j).setOverflowReserved(this.n);
    }

    /* 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 iJ;
        ?? r0;
        int i3;
        c cVar = this;
        androidx.appcompat.view.menu.h hVar = cVar.e;
        ?? r2 = 0;
        if (hVar != null) {
            arrayListE = hVar.E();
            size = arrayListE.size();
        } else {
            arrayListE = null;
            size = 0;
        }
        int i4 = cVar.r;
        int i5 = cVar.q;
        int iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, 0);
        ViewGroup viewGroup = (ViewGroup) cVar.f67j;
        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.o()) {
                i6++;
            } else if (jVar.n()) {
                i7++;
            } else {
                z = true;
            }
            if (cVar.v && jVar.isActionViewExpanded()) {
                i4 = 0;
            }
        }
        if (cVar.n && (z || i7 + i6 > i4)) {
            i4--;
        }
        int i9 = i4 - i6;
        SparseBooleanArray sparseBooleanArray = cVar.x;
        sparseBooleanArray.clear();
        if (cVar.t) {
            int i10 = cVar.w;
            iJ = i5 / i10;
            i2 = i10 + ((i5 % i10) / iJ);
        } else {
            i2 = 0;
            iJ = 0;
        }
        int i11 = 0;
        int i12 = 0;
        ?? r02 = cVar;
        while (i11 < size) {
            androidx.appcompat.view.menu.j jVar2 = arrayListE.get(i11);
            if (jVar2.o()) {
                View viewN = r02.n(jVar2, r02.y, viewGroup);
                if (r02.y == null) {
                    r02.y = viewN;
                }
                if (r02.t) {
                    iJ -= ActionMenuView.J(viewN, i2, iJ, 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.u(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.t || iJ > 0);
                boolean z4 = z3;
                if (z3) {
                    View viewN2 = r02.n(jVar2, r02.y, viewGroup);
                    i3 = size;
                    if (r02.y == null) {
                        r02.y = viewN2;
                    }
                    if (r02.t) {
                        int iJ2 = ActionMenuView.J(viewN2, i2, iJ, iMakeMeasureSpec, 0);
                        iJ -= iJ2;
                        if (iJ2 == 0) {
                            z4 = false;
                        }
                    } else {
                        viewN2.measure(iMakeMeasureSpec, iMakeMeasureSpec);
                    }
                    int measuredWidth2 = viewN2.getMeasuredWidth();
                    i5 -= measuredWidth2;
                    if (i12 == 0) {
                        i12 = measuredWidth2;
                    }
                    z3 = z4 & (!r02.t ? 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.u(false);
                        }
                    }
                }
                if (z3) {
                    i9--;
                }
                jVar2.u(z3);
                r0 = 0;
            } else {
                r0 = r2;
                i3 = size;
                jVar2.u(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.f146k) {
            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.f67j;
        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.f146k;
        if (dVar != null) {
            return dVar.getDrawable();
        }
        if (this.f148m) {
            return this.f147l;
        }
        return null;
    }
}
