package androidx.appcompat.view;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Xml;
import android.view.InflateException;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import androidx.appcompat.view.menu.j;
import androidx.appcompat.widget.a1;
import androidx.appcompat.widget.i0;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

/* JADX INFO: compiled from: SupportMenuInflater.java */
/* JADX INFO: loaded from: classes.dex */
public class g extends MenuInflater {

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    static final Class<?>[] f338e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    static final Class<?>[] f339f;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    final Object[] f340a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    final Object[] f341b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    Context f342c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private Object f343d;

    /* JADX INFO: compiled from: SupportMenuInflater.java */
    private static class a implements MenuItem.OnMenuItemClickListener {

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        private static final Class<?>[] f344c = {MenuItem.class};

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private Object f345a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private Method f346b;

        public a(Object obj, String str) {
            this.f345a = obj;
            Class<?> cls = obj.getClass();
            try {
                this.f346b = cls.getMethod(str, f344c);
            } catch (Exception e3) {
                InflateException inflateException = new InflateException("Couldn't resolve menu item onClick handler " + str + " in class " + cls.getName());
                inflateException.initCause(e3);
                throw inflateException;
            }
        }

        @Override // android.view.MenuItem.OnMenuItemClickListener
        public boolean onMenuItemClick(MenuItem menuItem) {
            try {
                if (this.f346b.getReturnType() == Boolean.TYPE) {
                    return ((Boolean) this.f346b.invoke(this.f345a, menuItem)).booleanValue();
                }
                this.f346b.invoke(this.f345a, menuItem);
                return true;
            } catch (Exception e3) {
                throw new RuntimeException(e3);
            }
        }
    }

    /* JADX INFO: compiled from: SupportMenuInflater.java */
    private class b {
        androidx.core.view.b A;
        private CharSequence B;
        private CharSequence C;
        private ColorStateList D = null;
        private PorterDuff.Mode E = null;

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private Menu f347a;

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

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

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

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

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

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

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

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

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

        /* JADX INFO: renamed from: k, reason: collision with root package name */
        private CharSequence f357k;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        /* JADX INFO: renamed from: z, reason: collision with root package name */
        private String f372z;

        public b(Menu menu) {
            this.f347a = menu;
            h();
        }

        private char c(String str) {
            if (str == null) {
                return (char) 0;
            }
            return str.charAt(0);
        }

        private <T> T e(String str, Class<?>[] clsArr, Object[] objArr) {
            try {
                Constructor<?> constructor = Class.forName(str, false, g.this.f342c.getClassLoader()).getConstructor(clsArr);
                constructor.setAccessible(true);
                return (T) constructor.newInstance(objArr);
            } catch (Exception e3) {
                Log.w("SupportMenuInflater", "Cannot instantiate class: " + str, e3);
                return null;
            }
        }

        private void i(MenuItem menuItem) {
            boolean z2 = false;
            menuItem.setChecked(this.f365s).setVisible(this.f366t).setEnabled(this.f367u).setCheckable(this.f364r >= 1).setTitleCondensed(this.f358l).setIcon(this.f359m);
            int i3 = this.f368v;
            if (i3 >= 0) {
                menuItem.setShowAsAction(i3);
            }
            if (this.f372z != null) {
                if (g.this.f342c.isRestricted()) {
                    throw new IllegalStateException("The android:onClick attribute cannot be used within a restricted context");
                }
                menuItem.setOnMenuItemClickListener(new a(g.this.b(), this.f372z));
            }
            if (this.f364r >= 2) {
                if (menuItem instanceof androidx.appcompat.view.menu.i) {
                    ((androidx.appcompat.view.menu.i) menuItem).s(true);
                } else if (menuItem instanceof j) {
                    ((j) menuItem).h(true);
                }
            }
            String str = this.f370x;
            if (str != null) {
                menuItem.setActionView((View) e(str, g.f338e, g.this.f340a));
                z2 = true;
            }
            int i4 = this.f369w;
            if (i4 > 0) {
                if (z2) {
                    Log.w("SupportMenuInflater", "Ignoring attribute 'itemActionViewLayout'. Action view already specified.");
                } else {
                    menuItem.setActionView(i4);
                }
            }
            androidx.core.view.b bVar = this.A;
            if (bVar != null) {
                androidx.core.view.j.a(menuItem, bVar);
            }
            androidx.core.view.j.c(menuItem, this.B);
            androidx.core.view.j.g(menuItem, this.C);
            androidx.core.view.j.b(menuItem, this.f360n, this.f361o);
            androidx.core.view.j.f(menuItem, this.f362p, this.f363q);
            PorterDuff.Mode mode = this.E;
            if (mode != null) {
                androidx.core.view.j.e(menuItem, mode);
            }
            ColorStateList colorStateList = this.D;
            if (colorStateList != null) {
                androidx.core.view.j.d(menuItem, colorStateList);
            }
        }

        public void a() {
            this.f354h = true;
            i(this.f347a.add(this.f348b, this.f355i, this.f356j, this.f357k));
        }

        public SubMenu b() {
            this.f354h = true;
            SubMenu subMenuAddSubMenu = this.f347a.addSubMenu(this.f348b, this.f355i, this.f356j, this.f357k);
            i(subMenuAddSubMenu.getItem());
            return subMenuAddSubMenu;
        }

        public boolean d() {
            return this.f354h;
        }

        public void f(AttributeSet attributeSet) {
            TypedArray typedArrayObtainStyledAttributes = g.this.f342c.obtainStyledAttributes(attributeSet, c.j.f3326j1);
            this.f348b = typedArrayObtainStyledAttributes.getResourceId(c.j.f3334l1, 0);
            this.f349c = typedArrayObtainStyledAttributes.getInt(c.j.f3340n1, 0);
            this.f350d = typedArrayObtainStyledAttributes.getInt(c.j.f3343o1, 0);
            this.f351e = typedArrayObtainStyledAttributes.getInt(c.j.f3346p1, 0);
            this.f352f = typedArrayObtainStyledAttributes.getBoolean(c.j.f3337m1, true);
            this.f353g = typedArrayObtainStyledAttributes.getBoolean(c.j.f3330k1, true);
            typedArrayObtainStyledAttributes.recycle();
        }

        public void g(AttributeSet attributeSet) {
            a1 a1VarU = a1.u(g.this.f342c, attributeSet, c.j.f3349q1);
            this.f355i = a1VarU.n(c.j.f3358t1, 0);
            this.f356j = (a1VarU.k(c.j.f3367w1, this.f349c) & (-65536)) | (a1VarU.k(c.j.f3370x1, this.f350d) & 65535);
            this.f357k = a1VarU.p(c.j.f3373y1);
            this.f358l = a1VarU.p(c.j.f3376z1);
            this.f359m = a1VarU.n(c.j.f3352r1, 0);
            this.f360n = c(a1VarU.o(c.j.A1));
            this.f361o = a1VarU.k(c.j.H1, 4096);
            this.f362p = c(a1VarU.o(c.j.B1));
            this.f363q = a1VarU.k(c.j.L1, 4096);
            int i3 = c.j.C1;
            if (a1VarU.s(i3)) {
                this.f364r = a1VarU.a(i3, false) ? 1 : 0;
            } else {
                this.f364r = this.f351e;
            }
            this.f365s = a1VarU.a(c.j.f3361u1, false);
            this.f366t = a1VarU.a(c.j.f3364v1, this.f352f);
            this.f367u = a1VarU.a(c.j.f3355s1, this.f353g);
            this.f368v = a1VarU.k(c.j.M1, -1);
            this.f372z = a1VarU.o(c.j.D1);
            this.f369w = a1VarU.n(c.j.E1, 0);
            this.f370x = a1VarU.o(c.j.G1);
            String strO = a1VarU.o(c.j.F1);
            this.f371y = strO;
            boolean z2 = strO != null;
            if (z2 && this.f369w == 0 && this.f370x == null) {
                this.A = (androidx.core.view.b) e(strO, g.f339f, g.this.f341b);
            } else {
                if (z2) {
                    Log.w("SupportMenuInflater", "Ignoring attribute 'actionProviderClass'. Action view already specified.");
                }
                this.A = null;
            }
            this.B = a1VarU.p(c.j.I1);
            this.C = a1VarU.p(c.j.N1);
            int i4 = c.j.K1;
            if (a1VarU.s(i4)) {
                this.E = i0.d(a1VarU.k(i4, -1), this.E);
            } else {
                this.E = null;
            }
            int i5 = c.j.J1;
            if (a1VarU.s(i5)) {
                this.D = a1VarU.c(i5);
            } else {
                this.D = null;
            }
            a1VarU.w();
            this.f354h = false;
        }

        public void h() {
            this.f348b = 0;
            this.f349c = 0;
            this.f350d = 0;
            this.f351e = 0;
            this.f352f = true;
            this.f353g = true;
        }
    }

    static {
        Class<?>[] clsArr = {Context.class};
        f338e = clsArr;
        f339f = clsArr;
    }

    public g(Context context) {
        super(context);
        this.f342c = context;
        Object[] objArr = {context};
        this.f340a = objArr;
        this.f341b = objArr;
    }

    private Object a(Object obj) {
        return (!(obj instanceof Activity) && (obj instanceof ContextWrapper)) ? a(((ContextWrapper) obj).getBaseContext()) : obj;
    }

    private void c(XmlPullParser xmlPullParser, AttributeSet attributeSet, Menu menu) throws XmlPullParserException, IOException {
        b bVar = new b(menu);
        int eventType = xmlPullParser.getEventType();
        while (true) {
            if (eventType == 2) {
                String name = xmlPullParser.getName();
                if (!name.equals("menu")) {
                    throw new RuntimeException("Expecting menu, got " + name);
                }
                eventType = xmlPullParser.next();
            } else {
                eventType = xmlPullParser.next();
                if (eventType == 1) {
                    break;
                }
            }
        }
        String str = null;
        boolean z2 = false;
        boolean z3 = false;
        while (!z2) {
            if (eventType == 1) {
                throw new RuntimeException("Unexpected end of document");
            }
            if (eventType != 2) {
                if (eventType == 3) {
                    String name2 = xmlPullParser.getName();
                    if (z3 && name2.equals(str)) {
                        str = null;
                        z3 = false;
                    } else if (name2.equals("group")) {
                        bVar.h();
                    } else if (name2.equals("item")) {
                        if (!bVar.d()) {
                            androidx.core.view.b bVar2 = bVar.A;
                            if (bVar2 == null || !bVar2.a()) {
                                bVar.a();
                            } else {
                                bVar.b();
                            }
                        }
                    } else if (name2.equals("menu")) {
                        z2 = true;
                    }
                }
            } else if (!z3) {
                String name3 = xmlPullParser.getName();
                if (name3.equals("group")) {
                    bVar.f(attributeSet);
                } else if (name3.equals("item")) {
                    bVar.g(attributeSet);
                } else if (name3.equals("menu")) {
                    c(xmlPullParser, attributeSet, bVar.b());
                } else {
                    str = name3;
                    z3 = true;
                }
            }
            eventType = xmlPullParser.next();
        }
    }

    Object b() {
        if (this.f343d == null) {
            this.f343d = a(this.f342c);
        }
        return this.f343d;
    }

    @Override // android.view.MenuInflater
    public void inflate(int i3, Menu menu) {
        if (!(menu instanceof s.a)) {
            super.inflate(i3, menu);
            return;
        }
        XmlResourceParser layout = null;
        try {
            try {
                try {
                    layout = this.f342c.getResources().getLayout(i3);
                    c(layout, Xml.asAttributeSet(layout), menu);
                } catch (XmlPullParserException e3) {
                    throw new InflateException("Error inflating menu XML", e3);
                }
            } catch (IOException e4) {
                throw new InflateException("Error inflating menu XML", e4);
            }
        } finally {
            if (layout != null) {
                layout.close();
            }
        }
    }
}
