package c.b.e.i;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.SparseArray;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ViewConfiguration;
import c.f.c.a;
import c.f.j.c0;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/* JADX INFO: compiled from: MenuBuilder.java */
/* JADX INFO: loaded from: classes.dex */
public class g implements c.f.e.a.a {
    public static final int[] a = {1, 4, 5, 3, 2, 0};

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Context f712b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final Resources f713c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public boolean f714d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public boolean f715e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public a f716f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public ArrayList<i> f717g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public ArrayList<i> f718h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public boolean f719i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public ArrayList<i> f720j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public ArrayList<i> f721k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public boolean f722l;
    public CharSequence n;
    public Drawable o;
    public View p;
    public i w;
    public boolean y;
    public int m = 0;
    public boolean q = false;
    public boolean r = false;
    public boolean s = false;
    public boolean t = false;
    public ArrayList<i> u = new ArrayList<>();
    public CopyOnWriteArrayList<WeakReference<m>> v = new CopyOnWriteArrayList<>();
    public boolean x = false;

    /* JADX INFO: compiled from: MenuBuilder.java */
    public interface a {
        boolean a(g gVar, MenuItem menuItem);

        void b(g gVar);
    }

    /* JADX INFO: compiled from: MenuBuilder.java */
    public interface b {
        boolean a(i iVar);
    }

    public g(Context context) {
        boolean zB;
        boolean z = false;
        this.f712b = context;
        Resources resources = context.getResources();
        this.f713c = resources;
        this.f717g = new ArrayList<>();
        this.f718h = new ArrayList<>();
        this.f719i = true;
        this.f720j = new ArrayList<>();
        this.f721k = new ArrayList<>();
        this.f722l = true;
        if (resources.getConfiguration().keyboard != 1) {
            ViewConfiguration viewConfiguration = ViewConfiguration.get(context);
            if (Build.VERSION.SDK_INT >= 28) {
                zB = c0.b(viewConfiguration);
            } else {
                Resources resources2 = context.getResources();
                int identifier = resources2.getIdentifier("config_showMenuShortcutsWhenKeyboardPresent", "bool", "android");
                zB = identifier != 0 && resources2.getBoolean(identifier);
            }
            if (zB) {
                z = true;
            }
        }
        this.f715e = z;
    }

    public MenuItem a(int i2, int i3, int i4, CharSequence charSequence) {
        int i5;
        int i6 = ((-65536) & i4) >> 16;
        if (i6 >= 0) {
            int[] iArr = a;
            if (i6 < iArr.length) {
                int i7 = (iArr[i6] << 16) | (65535 & i4);
                i iVar = new i(this, i2, i3, i4, i7, charSequence, this.m);
                ArrayList<i> arrayList = this.f717g;
                int size = arrayList.size();
                while (true) {
                    size--;
                    if (size < 0) {
                        i5 = 0;
                        break;
                    }
                    if (arrayList.get(size).f728d <= i7) {
                        i5 = size + 1;
                        break;
                    }
                }
                arrayList.add(i5, iVar);
                q(true);
                return iVar;
            }
        }
        throw new IllegalArgumentException("order does not contain a valid category.");
    }

    @Override // android.view.Menu
    public MenuItem add(CharSequence charSequence) {
        return a(0, 0, 0, charSequence);
    }

    @Override // android.view.Menu
    public int addIntentOptions(int i2, int i3, int i4, ComponentName componentName, Intent[] intentArr, Intent intent, int i5, MenuItem[] menuItemArr) {
        int i6;
        PackageManager packageManager = this.f712b.getPackageManager();
        List<ResolveInfo> listQueryIntentActivityOptions = packageManager.queryIntentActivityOptions(componentName, intentArr, intent, 0);
        int size = listQueryIntentActivityOptions != null ? listQueryIntentActivityOptions.size() : 0;
        if ((i5 & 1) == 0) {
            removeGroup(i2);
        }
        for (int i7 = 0; i7 < size; i7++) {
            ResolveInfo resolveInfo = listQueryIntentActivityOptions.get(i7);
            int i8 = resolveInfo.specificIndex;
            Intent intent2 = new Intent(i8 < 0 ? intent : intentArr[i8]);
            ActivityInfo activityInfo = resolveInfo.activityInfo;
            intent2.setComponent(new ComponentName(activityInfo.applicationInfo.packageName, activityInfo.name));
            i iVar = (i) a(i2, i3, i4, resolveInfo.loadLabel(packageManager));
            iVar.setIcon(resolveInfo.loadIcon(packageManager));
            iVar.setIntent(intent2);
            if (menuItemArr != null && (i6 = resolveInfo.specificIndex) >= 0) {
                menuItemArr[i6] = iVar;
            }
        }
        return size;
    }

    @Override // android.view.Menu
    public SubMenu addSubMenu(CharSequence charSequence) {
        return addSubMenu(0, 0, 0, charSequence);
    }

    public void b(m mVar, Context context) {
        this.v.add(new WeakReference<>(mVar));
        mVar.i(context, this);
        this.f722l = true;
    }

    public final void c(boolean z) {
        if (this.t) {
            return;
        }
        this.t = true;
        for (WeakReference<m> weakReference : this.v) {
            m mVar = weakReference.get();
            if (mVar == null) {
                this.v.remove(weakReference);
            } else {
                mVar.b(this, z);
            }
        }
        this.t = false;
    }

    @Override // android.view.Menu
    public void clear() {
        i iVar = this.w;
        if (iVar != null) {
            d(iVar);
        }
        this.f717g.clear();
        q(true);
    }

    public void clearHeader() {
        this.o = null;
        this.n = null;
        this.p = null;
        q(false);
    }

    @Override // android.view.Menu
    public void close() {
        c(true);
    }

    public boolean d(i iVar) {
        boolean zE = false;
        if (!this.v.isEmpty() && this.w == iVar) {
            z();
            for (WeakReference<m> weakReference : this.v) {
                m mVar = weakReference.get();
                if (mVar != null) {
                    zE = mVar.e(this, iVar);
                    if (zE) {
                        break;
                    }
                } else {
                    this.v.remove(weakReference);
                }
            }
            y();
            if (zE) {
                this.w = null;
            }
        }
        return zE;
    }

    public boolean e(g gVar, MenuItem menuItem) {
        a aVar = this.f716f;
        return aVar != null && aVar.a(gVar, menuItem);
    }

    public boolean f(i iVar) {
        boolean zF = false;
        if (this.v.isEmpty()) {
            return false;
        }
        z();
        for (WeakReference<m> weakReference : this.v) {
            m mVar = weakReference.get();
            if (mVar != null) {
                zF = mVar.f(this, iVar);
                if (zF) {
                    break;
                }
            } else {
                this.v.remove(weakReference);
            }
        }
        y();
        if (zF) {
            this.w = iVar;
        }
        return zF;
    }

    @Override // android.view.Menu
    public MenuItem findItem(int i2) {
        MenuItem menuItemFindItem;
        int size = size();
        for (int i3 = 0; i3 < size; i3++) {
            i iVar = this.f717g.get(i3);
            if (iVar.a == i2) {
                return iVar;
            }
            if (iVar.hasSubMenu() && (menuItemFindItem = iVar.o.findItem(i2)) != null) {
                return menuItemFindItem;
            }
        }
        return null;
    }

    public i g(int i2, KeyEvent keyEvent) {
        ArrayList<i> arrayList = this.u;
        arrayList.clear();
        h(arrayList, i2, keyEvent);
        if (arrayList.isEmpty()) {
            return null;
        }
        int metaState = keyEvent.getMetaState();
        KeyCharacterMap.KeyData keyData = new KeyCharacterMap.KeyData();
        keyEvent.getKeyData(keyData);
        int size = arrayList.size();
        if (size == 1) {
            return arrayList.get(0);
        }
        boolean zN = n();
        for (int i3 = 0; i3 < size; i3++) {
            i iVar = arrayList.get(i3);
            char c2 = zN ? iVar.f734j : iVar.f732h;
            char[] cArr = keyData.meta;
            if ((c2 == cArr[0] && (metaState & 2) == 0) || ((c2 == cArr[2] && (metaState & 2) != 0) || (zN && c2 == '\b' && i2 == 67))) {
                return iVar;
            }
        }
        return null;
    }

    @Override // android.view.Menu
    public MenuItem getItem(int i2) {
        return this.f717g.get(i2);
    }

    public void h(List<i> list, int i2, KeyEvent keyEvent) {
        boolean zN = n();
        int modifiers = keyEvent.getModifiers();
        KeyCharacterMap.KeyData keyData = new KeyCharacterMap.KeyData();
        if (keyEvent.getKeyData(keyData) || i2 == 67) {
            int size = this.f717g.size();
            for (int i3 = 0; i3 < size; i3++) {
                i iVar = this.f717g.get(i3);
                if (iVar.hasSubMenu()) {
                    iVar.o.h(list, i2, keyEvent);
                }
                char c2 = zN ? iVar.f734j : iVar.f732h;
                if (((modifiers & 69647) == ((zN ? iVar.f735k : iVar.f733i) & 69647)) && c2 != 0) {
                    char[] cArr = keyData.meta;
                    if ((c2 == cArr[0] || c2 == cArr[2] || (zN && c2 == '\b' && i2 == 67)) && iVar.isEnabled()) {
                        list.add(iVar);
                    }
                }
            }
        }
    }

    @Override // android.view.Menu
    public boolean hasVisibleItems() {
        if (this.y) {
            return true;
        }
        int size = size();
        for (int i2 = 0; i2 < size; i2++) {
            if (this.f717g.get(i2).isVisible()) {
                return true;
            }
        }
        return false;
    }

    public void i() {
        ArrayList<i> arrayListL = l();
        if (this.f722l) {
            boolean zD = false;
            for (WeakReference<m> weakReference : this.v) {
                m mVar = weakReference.get();
                if (mVar == null) {
                    this.v.remove(weakReference);
                } else {
                    zD |= mVar.d();
                }
            }
            if (zD) {
                this.f720j.clear();
                this.f721k.clear();
                int size = arrayListL.size();
                for (int i2 = 0; i2 < size; i2++) {
                    i iVar = arrayListL.get(i2);
                    if (iVar.g()) {
                        this.f720j.add(iVar);
                    } else {
                        this.f721k.add(iVar);
                    }
                }
            } else {
                this.f720j.clear();
                this.f721k.clear();
                this.f721k.addAll(l());
            }
            this.f722l = false;
        }
    }

    @Override // android.view.Menu
    public boolean isShortcutKey(int i2, KeyEvent keyEvent) {
        return g(i2, keyEvent) != null;
    }

    public String j() {
        return "android:menu:actionviewstates";
    }

    public g k() {
        return this;
    }

    public ArrayList<i> l() {
        if (!this.f719i) {
            return this.f718h;
        }
        this.f718h.clear();
        int size = this.f717g.size();
        for (int i2 = 0; i2 < size; i2++) {
            i iVar = this.f717g.get(i2);
            if (iVar.isVisible()) {
                this.f718h.add(iVar);
            }
        }
        this.f719i = false;
        this.f722l = true;
        return this.f718h;
    }

    public boolean m() {
        return this.x;
    }

    public boolean n() {
        return this.f714d;
    }

    public boolean o() {
        return this.f715e;
    }

    public void p() {
        this.f722l = true;
        q(true);
    }

    @Override // android.view.Menu
    public boolean performIdentifierAction(int i2, int i3) {
        return r(findItem(i2), i3);
    }

    @Override // android.view.Menu
    public boolean performShortcut(int i2, KeyEvent keyEvent, int i3) {
        i iVarG = g(i2, keyEvent);
        boolean zS = iVarG != null ? s(iVarG, null, i3) : false;
        if ((i3 & 2) != 0) {
            c(true);
        }
        return zS;
    }

    public void q(boolean z) {
        if (this.q) {
            this.r = true;
            if (z) {
                this.s = true;
                return;
            }
            return;
        }
        if (z) {
            this.f719i = true;
            this.f722l = true;
        }
        if (this.v.isEmpty()) {
            return;
        }
        z();
        for (WeakReference<m> weakReference : this.v) {
            m mVar = weakReference.get();
            if (mVar == null) {
                this.v.remove(weakReference);
            } else {
                mVar.c(z);
            }
        }
        y();
    }

    public boolean r(MenuItem menuItem, int i2) {
        return s(menuItem, null, i2);
    }

    @Override // android.view.Menu
    public void removeGroup(int i2) {
        int size = size();
        int i3 = 0;
        while (true) {
            if (i3 >= size) {
                i3 = -1;
                break;
            } else if (this.f717g.get(i3).f726b == i2) {
                break;
            } else {
                i3++;
            }
        }
        if (i3 >= 0) {
            int size2 = this.f717g.size() - i3;
            int i4 = 0;
            while (true) {
                int i5 = i4 + 1;
                if (i4 >= size2 || this.f717g.get(i3).f726b != i2) {
                    break;
                }
                t(i3, false);
                i4 = i5;
            }
            q(true);
        }
    }

    @Override // android.view.Menu
    public void removeItem(int i2) {
        int size = size();
        int i3 = 0;
        while (true) {
            if (i3 >= size) {
                i3 = -1;
                break;
            } else if (this.f717g.get(i3).a == i2) {
                break;
            } else {
                i3++;
            }
        }
        t(i3, true);
    }

    /* JADX WARN: Removed duplicated region for block: B:25:0x0040  */
    /* JADX WARN: Removed duplicated region for block: B:32:0x004f  */
    /* JADX WARN: Removed duplicated region for block: B:35:0x0056  */
    /* JADX WARN: Removed duplicated region for block: B:38:0x0062  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public boolean s(android.view.MenuItem r7, c.b.e.i.m r8, int r9) {
        /*
            Method dump skipped, instruction units count: 211
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: c.b.e.i.g.s(android.view.MenuItem, c.b.e.i.m, int):boolean");
    }

    @Override // android.view.Menu
    public void setGroupCheckable(int i2, boolean z, boolean z2) {
        int size = this.f717g.size();
        for (int i3 = 0; i3 < size; i3++) {
            i iVar = this.f717g.get(i3);
            if (iVar.f726b == i2) {
                iVar.x = (iVar.x & (-5)) | (z2 ? 4 : 0);
                iVar.setCheckable(z);
            }
        }
    }

    @Override // android.view.Menu
    public void setGroupDividerEnabled(boolean z) {
        this.x = z;
    }

    @Override // android.view.Menu
    public void setGroupEnabled(int i2, boolean z) {
        int size = this.f717g.size();
        for (int i3 = 0; i3 < size; i3++) {
            i iVar = this.f717g.get(i3);
            if (iVar.f726b == i2) {
                iVar.setEnabled(z);
            }
        }
    }

    @Override // android.view.Menu
    public void setGroupVisible(int i2, boolean z) {
        int size = this.f717g.size();
        boolean z2 = false;
        for (int i3 = 0; i3 < size; i3++) {
            i iVar = this.f717g.get(i3);
            if (iVar.f726b == i2 && iVar.l(z)) {
                z2 = true;
            }
        }
        if (z2) {
            q(true);
        }
    }

    @Override // android.view.Menu
    public void setQwertyMode(boolean z) {
        this.f714d = z;
        q(false);
    }

    @Override // android.view.Menu
    public int size() {
        return this.f717g.size();
    }

    public final void t(int i2, boolean z) {
        if (i2 < 0 || i2 >= this.f717g.size()) {
            return;
        }
        this.f717g.remove(i2);
        if (z) {
            q(true);
        }
    }

    public void u(m mVar) {
        for (WeakReference<m> weakReference : this.v) {
            m mVar2 = weakReference.get();
            if (mVar2 == null || mVar2 == mVar) {
                this.v.remove(weakReference);
            }
        }
    }

    public void v(Bundle bundle) {
        MenuItem menuItemFindItem;
        if (bundle == null) {
            return;
        }
        SparseArray<Parcelable> sparseParcelableArray = bundle.getSparseParcelableArray(j());
        int size = size();
        for (int i2 = 0; i2 < size; i2++) {
            MenuItem item = getItem(i2);
            View actionView = item.getActionView();
            if (actionView != null && actionView.getId() != -1) {
                actionView.restoreHierarchyState(sparseParcelableArray);
            }
            if (item.hasSubMenu()) {
                ((r) item.getSubMenu()).v(bundle);
            }
        }
        int i3 = bundle.getInt("android:menu:expandedactionview");
        if (i3 <= 0 || (menuItemFindItem = findItem(i3)) == null) {
            return;
        }
        menuItemFindItem.expandActionView();
    }

    public void w(Bundle bundle) {
        int size = size();
        SparseArray<? extends Parcelable> sparseArray = null;
        for (int i2 = 0; i2 < size; i2++) {
            MenuItem item = getItem(i2);
            View actionView = item.getActionView();
            if (actionView != null && actionView.getId() != -1) {
                if (sparseArray == null) {
                    sparseArray = new SparseArray<>();
                }
                actionView.saveHierarchyState(sparseArray);
                if (item.isActionViewExpanded()) {
                    bundle.putInt("android:menu:expandedactionview", item.getItemId());
                }
            }
            if (item.hasSubMenu()) {
                ((r) item.getSubMenu()).w(bundle);
            }
        }
        if (sparseArray != null) {
            bundle.putSparseParcelableArray(j(), sparseArray);
        }
    }

    public final void x(int i2, CharSequence charSequence, int i3, Drawable drawable, View view) {
        Resources resources = this.f713c;
        if (view != null) {
            this.p = view;
            this.n = null;
            this.o = null;
        } else {
            if (i2 > 0) {
                this.n = resources.getText(i2);
            } else if (charSequence != null) {
                this.n = charSequence;
            }
            if (i3 > 0) {
                Context context = this.f712b;
                Object obj = c.f.c.a.a;
                this.o = a.b.b(context, i3);
            } else if (drawable != null) {
                this.o = drawable;
            }
            this.p = null;
        }
        q(false);
    }

    public void y() {
        this.q = false;
        if (this.r) {
            this.r = false;
            q(this.s);
        }
    }

    public void z() {
        if (this.q) {
            return;
        }
        this.q = true;
        this.r = false;
        this.s = false;
    }

    @Override // android.view.Menu
    public MenuItem add(int i2) {
        return a(0, 0, 0, this.f713c.getString(i2));
    }

    @Override // android.view.Menu
    public SubMenu addSubMenu(int i2) {
        return addSubMenu(0, 0, 0, this.f713c.getString(i2));
    }

    @Override // android.view.Menu
    public MenuItem add(int i2, int i3, int i4, CharSequence charSequence) {
        return a(i2, i3, i4, charSequence);
    }

    @Override // android.view.Menu
    public SubMenu addSubMenu(int i2, int i3, int i4, CharSequence charSequence) {
        i iVar = (i) a(i2, i3, i4, charSequence);
        r rVar = new r(this.f712b, this, iVar);
        iVar.o = rVar;
        rVar.setHeaderTitle(iVar.f729e);
        return rVar;
    }

    @Override // android.view.Menu
    public MenuItem add(int i2, int i3, int i4, int i5) {
        return a(i2, i3, i4, this.f713c.getString(i5));
    }

    @Override // android.view.Menu
    public SubMenu addSubMenu(int i2, int i3, int i4, int i5) {
        return addSubMenu(i2, i3, i4, this.f713c.getString(i5));
    }
}
