package o;

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.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import androidx.appcompat.view.menu.ActionMenuItemView;
import androidx.appcompat.widget.ActionMenuView;
import com.google.android.gms.common.R;
import java.util.ArrayList;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.i1, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final class C1444i1 implements InterfaceC1093eP {
    public int A;
    public boolean B;
    public C1156f1 D;
    public C1156f1 E;
    public RunnableC0890cD F;
    public C1252g1 G;
    public final Context l;
    public Context m;
    public CO n;

    /* renamed from: o, reason: collision with root package name */
    public final LayoutInflater f573o;
    public InterfaceC0998dP p;
    public InterfaceC1477iP s;
    public C1348h1 t;
    public Drawable u;
    public boolean v;
    public boolean w;
    public boolean x;
    public int y;
    public int z;
    public final int q = R.layout.abc_action_menu_layout;
    public final int r = R.layout.abc_action_menu_item_layout;
    public final SparseBooleanArray C = new SparseBooleanArray();
    public final OD H = new OD(5, this);

    public C1444i1(Context context) {
        this.l = context;
        this.f573o = LayoutInflater.from(context);
    }

    @Override // o.InterfaceC1093eP
    public final void a(CO co, boolean z) {
        e();
        C1156f1 c1156f1 = this.E;
        if (c1156f1 != null && c1156f1.b()) {
            c1156f1.i.dismiss();
        }
        InterfaceC0998dP interfaceC0998dP = this.p;
        if (interfaceC0998dP != null) {
            interfaceC0998dP.a(co, z);
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r5v0, types: [android.view.View] */
    /* JADX WARN: Type inference failed for: r5v4, types: [o.hP] */
    /* JADX WARN: Type inference failed for: r5v7 */
    /* JADX WARN: Type inference failed for: r5v8 */
    public final View b(LO lo, View view, ViewGroup viewGroup) {
        View actionView = lo.getActionView();
        if (actionView == null || lo.e()) {
            ActionMenuItemView actionMenuItemView = view instanceof InterfaceC1381hP ? (InterfaceC1381hP) view : (InterfaceC1381hP) this.f573o.inflate(this.r, viewGroup, false);
            actionMenuItemView.a(lo);
            ActionMenuItemView actionMenuItemView2 = actionMenuItemView;
            actionMenuItemView2.setItemInvoker((ActionMenuView) this.s);
            if (this.G == null) {
                this.G = new C1252g1(this);
            }
            actionMenuItemView2.setPopupCallback(this.G);
            actionView = actionMenuItemView;
        }
        actionView.setVisibility(lo.C ? 8 : 0);
        ViewGroup.LayoutParams layoutParams = actionView.getLayoutParams();
        ((ActionMenuView) viewGroup).getClass();
        if (!(layoutParams instanceof C1633k1)) {
            actionView.setLayoutParams(ActionMenuView.k(layoutParams));
        }
        return actionView;
    }

    /* JADX WARN: Multi-variable type inference failed */
    @Override // o.InterfaceC1093eP
    public final void c() {
        int i;
        ViewGroup viewGroup = (ViewGroup) this.s;
        ArrayList arrayList = null;
        boolean z = false;
        if (viewGroup != null) {
            CO co = this.n;
            if (co != null) {
                co.i();
                ArrayList arrayListL = this.n.l();
                int size = arrayListL.size();
                i = 0;
                for (int i2 = 0; i2 < size; i2++) {
                    LO lo = (LO) arrayListL.get(i2);
                    if ((lo.x & 32) == 32) {
                        View childAt = viewGroup.getChildAt(i);
                        LO itemData = childAt instanceof InterfaceC1381hP ? ((InterfaceC1381hP) childAt).getItemData() : null;
                        View viewB = b(lo, childAt, viewGroup);
                        if (lo != itemData) {
                            viewB.setPressed(false);
                            viewB.jumpDrawablesToCurrentState();
                        }
                        if (viewB != childAt) {
                            ViewGroup viewGroup2 = (ViewGroup) viewB.getParent();
                            if (viewGroup2 != null) {
                                viewGroup2.removeView(viewB);
                            }
                            ((ViewGroup) this.s).addView(viewB, i);
                        }
                        i++;
                    }
                }
            } else {
                i = 0;
            }
            while (i < viewGroup.getChildCount()) {
                if (viewGroup.getChildAt(i) == this.t) {
                    i++;
                } else {
                    viewGroup.removeViewAt(i);
                }
            }
        }
        ((View) this.s).requestLayout();
        CO co2 = this.n;
        if (co2 != null) {
            co2.i();
            ArrayList arrayList2 = co2.i;
            int size2 = arrayList2.size();
            for (int i3 = 0; i3 < size2; i3++) {
                MO mo = ((LO) arrayList2.get(i3)).A;
            }
        }
        CO co3 = this.n;
        if (co3 != null) {
            co3.i();
            arrayList = co3.j;
        }
        if (this.w && arrayList != null) {
            int size3 = arrayList.size();
            if (size3 == 1) {
                z = !((LO) arrayList.get(0)).C;
            } else if (size3 > 0) {
                z = true;
            }
        }
        if (z) {
            if (this.t == null) {
                this.t = new C1348h1(this, this.l);
            }
            ViewGroup viewGroup3 = (ViewGroup) this.t.getParent();
            if (viewGroup3 != this.s) {
                if (viewGroup3 != null) {
                    viewGroup3.removeView(this.t);
                }
                ActionMenuView actionMenuView = (ActionMenuView) this.s;
                C1348h1 c1348h1 = this.t;
                actionMenuView.getClass();
                C1633k1 c1633k1J = ActionMenuView.j();
                c1633k1J.a = true;
                actionMenuView.addView(c1348h1, c1633k1J);
            }
        } else {
            C1348h1 c1348h12 = this.t;
            if (c1348h12 != null) {
                Object parent = c1348h12.getParent();
                Object obj = this.s;
                if (parent == obj) {
                    ((ViewGroup) obj).removeView(this.t);
                }
            }
        }
        ((ActionMenuView) this.s).setOverflowReserved(this.w);
    }

    @Override // o.InterfaceC1093eP
    public final boolean d(LO lo) {
        return false;
    }

    public final boolean e() {
        Object obj;
        RunnableC0890cD runnableC0890cD = this.F;
        if (runnableC0890cD != null && (obj = this.s) != null) {
            ((View) obj).removeCallbacks(runnableC0890cD);
            this.F = null;
            return true;
        }
        C1156f1 c1156f1 = this.D;
        if (c1156f1 == null) {
            return false;
        }
        if (c1156f1.b()) {
            c1156f1.i.dismiss();
        }
        return true;
    }

    @Override // o.InterfaceC1093eP
    public final void f(InterfaceC0998dP interfaceC0998dP) {
        throw null;
    }

    @Override // o.InterfaceC1093eP
    public final void g(Context context, CO co) {
        this.m = context;
        LayoutInflater.from(context);
        this.n = co;
        Resources resources = context.getResources();
        if (!this.x) {
            this.w = true;
        }
        int i = 2;
        this.y = context.getResources().getDisplayMetrics().widthPixels / 2;
        Configuration configuration = context.getResources().getConfiguration();
        int i2 = configuration.screenWidthDp;
        int i3 = configuration.screenHeightDp;
        if (configuration.smallestScreenWidthDp > 600 || i2 > 600 || ((i2 > 960 && i3 > 720) || (i2 > 720 && i3 > 960))) {
            i = 5;
        } else if (i2 >= 500 || ((i2 > 640 && i3 > 480) || (i2 > 480 && i3 > 640))) {
            i = 4;
        } else if (i2 >= 360) {
            i = 3;
        }
        this.A = i;
        int measuredWidth = this.y;
        if (this.w) {
            if (this.t == null) {
                C1348h1 c1348h1 = new C1348h1(this, this.l);
                this.t = c1348h1;
                if (this.v) {
                    c1348h1.setImageDrawable(this.u);
                    this.u = null;
                    this.v = false;
                }
                int iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, 0);
                this.t.measure(iMakeMeasureSpec, iMakeMeasureSpec);
            }
            measuredWidth -= this.t.getMeasuredWidth();
        } else {
            this.t = null;
        }
        this.z = measuredWidth;
        float f = resources.getDisplayMetrics().density;
    }

    public final boolean h() {
        C1156f1 c1156f1 = this.D;
        return c1156f1 != null && c1156f1.b();
    }

    @Override // o.InterfaceC1093eP
    public final boolean i() {
        int size;
        ArrayList arrayListL;
        int i;
        boolean z;
        C1444i1 c1444i1 = this;
        CO co = c1444i1.n;
        if (co != null) {
            arrayListL = co.l();
            size = arrayListL.size();
        } else {
            size = 0;
            arrayListL = null;
        }
        int i2 = c1444i1.A;
        int i3 = c1444i1.z;
        int iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, 0);
        ViewGroup viewGroup = (ViewGroup) c1444i1.s;
        int i4 = 0;
        boolean z2 = false;
        int i5 = 0;
        int i6 = 0;
        while (true) {
            i = 2;
            z = true;
            if (i4 >= size) {
                break;
            }
            LO lo = (LO) arrayListL.get(i4);
            int i7 = lo.y;
            if ((i7 & 2) == 2) {
                i5++;
            } else if ((i7 & 1) == 1) {
                i6++;
            } else {
                z2 = true;
            }
            if (c1444i1.B && lo.C) {
                i2 = 0;
            }
            i4++;
        }
        if (c1444i1.w && (z2 || i6 + i5 > i2)) {
            i2--;
        }
        int i8 = i2 - i5;
        SparseBooleanArray sparseBooleanArray = c1444i1.C;
        sparseBooleanArray.clear();
        int i9 = 0;
        int i10 = 0;
        while (i9 < size) {
            LO lo2 = (LO) arrayListL.get(i9);
            int i11 = lo2.y;
            boolean z3 = (i11 & 2) == i ? z : false;
            int i12 = lo2.b;
            if (z3) {
                View viewB = c1444i1.b(lo2, null, viewGroup);
                viewB.measure(iMakeMeasureSpec, iMakeMeasureSpec);
                int measuredWidth = viewB.getMeasuredWidth();
                i3 -= measuredWidth;
                if (i10 == 0) {
                    i10 = measuredWidth;
                }
                if (i12 != 0) {
                    sparseBooleanArray.put(i12, z);
                }
                lo2.f(z);
            } else if ((i11 & 1) == z) {
                boolean z4 = sparseBooleanArray.get(i12);
                boolean z5 = ((i8 > 0 || z4) && i3 > 0) ? z : false;
                if (z5) {
                    View viewB2 = c1444i1.b(lo2, null, viewGroup);
                    viewB2.measure(iMakeMeasureSpec, iMakeMeasureSpec);
                    int measuredWidth2 = viewB2.getMeasuredWidth();
                    i3 -= measuredWidth2;
                    if (i10 == 0) {
                        i10 = measuredWidth2;
                    }
                    z5 &= i3 + i10 > 0;
                }
                if (z5 && i12 != 0) {
                    sparseBooleanArray.put(i12, true);
                } else if (z4) {
                    sparseBooleanArray.put(i12, false);
                    for (int i13 = 0; i13 < i9; i13++) {
                        LO lo3 = (LO) arrayListL.get(i13);
                        if (lo3.b == i12) {
                            if ((lo3.x & 32) == 32) {
                                i8++;
                            }
                            lo3.f(false);
                        }
                    }
                }
                if (z5) {
                    i8--;
                }
                lo2.f(z5);
            } else {
                lo2.f(false);
                i9++;
                i = 2;
                c1444i1 = this;
                z = true;
            }
            i9++;
            i = 2;
            c1444i1 = this;
            z = true;
        }
        return z;
    }

    /* JADX WARN: Multi-variable type inference failed */
    @Override // o.InterfaceC1093eP
    public final boolean j(SubMenuC2155pb0 subMenuC2155pb0) {
        boolean z;
        if (subMenuC2155pb0.hasVisibleItems()) {
            SubMenuC2155pb0 subMenuC2155pb02 = subMenuC2155pb0;
            while (true) {
                CO co = subMenuC2155pb02.z;
                if (co == this.n) {
                    break;
                }
                subMenuC2155pb02 = (SubMenuC2155pb0) co;
            }
            LO lo = subMenuC2155pb02.A;
            ViewGroup viewGroup = (ViewGroup) this.s;
            View view = null;
            if (viewGroup != null) {
                int childCount = viewGroup.getChildCount();
                int i = 0;
                while (true) {
                    if (i >= childCount) {
                        break;
                    }
                    View childAt = viewGroup.getChildAt(i);
                    if ((childAt instanceof InterfaceC1381hP) && ((InterfaceC1381hP) childAt).getItemData() == lo) {
                        view = childAt;
                        break;
                    }
                    i++;
                }
            }
            if (view != null) {
                subMenuC2155pb0.A.getClass();
                int size = subMenuC2155pb0.f.size();
                int i2 = 0;
                while (true) {
                    if (i2 >= size) {
                        z = false;
                        break;
                    }
                    MenuItem item = subMenuC2155pb0.getItem(i2);
                    if (item.isVisible() && item.getIcon() != null) {
                        z = true;
                        break;
                    }
                    i2++;
                }
                C1156f1 c1156f1 = new C1156f1(this, this.m, subMenuC2155pb0, view);
                this.E = c1156f1;
                c1156f1.g = z;
                UO uo = c1156f1.i;
                if (uo != null) {
                    uo.o(z);
                }
                C1156f1 c1156f12 = this.E;
                if (!c1156f12.b()) {
                    if (c1156f12.e == null) {
                        throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor");
                    }
                    c1156f12.d(0, 0, false, false);
                }
                InterfaceC0998dP interfaceC0998dP = this.p;
                if (interfaceC0998dP != null) {
                    interfaceC0998dP.k(subMenuC2155pb0);
                }
                return true;
            }
        }
        return false;
    }

    @Override // o.InterfaceC1093eP
    public final boolean k(LO lo) {
        return false;
    }

    public final boolean l() {
        CO co;
        if (!this.w || h() || (co = this.n) == null || this.s == null || this.F != null) {
            return false;
        }
        co.i();
        if (co.j.isEmpty()) {
            return false;
        }
        RunnableC0890cD runnableC0890cD = new RunnableC0890cD(1, this, new C1156f1(this, this.m, this.n, this.t), false);
        this.F = runnableC0890cD;
        ((View) this.s).post(runnableC0890cD);
        return true;
    }
}
