package androidx.appcompat.view.menu; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Parcelable; import android.util.SparseArray; import android.view.ContextMenu; 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 androidx.core.content.ContextCompat; import androidx.core.internal.view.SupportMenu; import androidx.core.view.ActionProvider; import androidx.core.view.ViewConfigurationCompat; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; /* loaded from: classes.dex */ public class MenuBuilder implements SupportMenu { private static final int[] A = {1, 4, 5, 3, 2, 0}; private final Context a; private final Resources b; private boolean c; private boolean d; private Callback e; private ContextMenu.ContextMenuInfo m; CharSequence n; Drawable o; View p; private MenuItemImpl x; private boolean z; private int l = 0; private boolean q = false; private boolean r = false; private boolean s = false; private boolean t = false; private boolean u = false; private ArrayList v = new ArrayList<>(); private CopyOnWriteArrayList> w = new CopyOnWriteArrayList<>(); private boolean y = false; private ArrayList f = new ArrayList<>(); private ArrayList g = new ArrayList<>(); private boolean h = true; private ArrayList i = new ArrayList<>(); private ArrayList j = new ArrayList<>(); private boolean k = true; public interface Callback { void a(MenuBuilder menuBuilder); boolean a(MenuBuilder menuBuilder, MenuItem menuItem); } public interface ItemInvoker { boolean a(MenuItemImpl menuItemImpl); } public MenuBuilder(Context context) { this.a = context; this.b = context.getResources(); e(true); } private void d(boolean z) { if (this.w.isEmpty()) { return; } s(); Iterator> it = this.w.iterator(); while (it.hasNext()) { WeakReference next = it.next(); MenuPresenter menuPresenter = next.get(); if (menuPresenter == null) { this.w.remove(next); } else { menuPresenter.a(z); } } r(); } private void e(boolean z) { this.d = z && this.b.getConfiguration().keyboard != 1 && ViewConfigurationCompat.d(ViewConfiguration.get(this.a), this.a); } private static int f(int i) { int i2 = ((-65536) & i) >> 16; if (i2 >= 0) { int[] iArr = A; if (i2 < iArr.length) { return (i & 65535) | (iArr[i2] << 16); } } throw new IllegalArgumentException("order does not contain a valid category."); } public void a(MenuPresenter menuPresenter) { a(menuPresenter, this.a); } @Override // android.view.Menu public MenuItem add(CharSequence charSequence) { return a(0, 0, 0, charSequence); } @Override // android.view.Menu public int addIntentOptions(int i, int i2, int i3, ComponentName componentName, Intent[] intentArr, Intent intent, int i4, MenuItem[] menuItemArr) { int i5; PackageManager packageManager = this.a.getPackageManager(); List queryIntentActivityOptions = packageManager.queryIntentActivityOptions(componentName, intentArr, intent, 0); int size = queryIntentActivityOptions != null ? queryIntentActivityOptions.size() : 0; if ((i4 & 1) == 0) { removeGroup(i); } for (int i6 = 0; i6 < size; i6++) { ResolveInfo resolveInfo = queryIntentActivityOptions.get(i6); int i7 = resolveInfo.specificIndex; Intent intent2 = new Intent(i7 < 0 ? intent : intentArr[i7]); intent2.setComponent(new ComponentName(resolveInfo.activityInfo.applicationInfo.packageName, resolveInfo.activityInfo.name)); MenuItem intent3 = add(i, i2, i3, resolveInfo.loadLabel(packageManager)).setIcon(resolveInfo.loadIcon(packageManager)).setIntent(intent2); if (menuItemArr != null && (i5 = resolveInfo.specificIndex) >= 0) { menuItemArr[i5] = intent3; } } return size; } @Override // android.view.Menu public SubMenu addSubMenu(CharSequence charSequence) { return addSubMenu(0, 0, 0, charSequence); } public void b(MenuPresenter menuPresenter) { Iterator> it = this.w.iterator(); while (it.hasNext()) { WeakReference next = it.next(); MenuPresenter menuPresenter2 = next.get(); if (menuPresenter2 == null || menuPresenter2 == menuPresenter) { this.w.remove(next); } } } public MenuBuilder c(int i) { this.l = i; return this; } @Override // android.view.Menu public void clear() { MenuItemImpl menuItemImpl = this.x; if (menuItemImpl != null) { a(menuItemImpl); } this.f.clear(); b(true); } public void clearHeader() { this.o = null; this.n = null; this.p = null; b(false); } @Override // android.view.Menu public void close() { a(true); } protected String d() { return "android:menu:actionviewstates"; } @Override // android.view.Menu public MenuItem findItem(int i) { MenuItem findItem; int size = size(); for (int i2 = 0; i2 < size; i2++) { MenuItemImpl menuItemImpl = this.f.get(i2); if (menuItemImpl.getItemId() == i) { return menuItemImpl; } if (menuItemImpl.hasSubMenu() && (findItem = menuItemImpl.getSubMenu().findItem(i)) != null) { return findItem; } } return null; } public Drawable g() { return this.o; } @Override // android.view.Menu public MenuItem getItem(int i) { return this.f.get(i); } public CharSequence h() { return this.n; } @Override // android.view.Menu public boolean hasVisibleItems() { if (this.z) { return true; } int size = size(); for (int i = 0; i < size; i++) { if (this.f.get(i).isVisible()) { return true; } } return false; } public View i() { return this.p; } @Override // android.view.Menu public boolean isShortcutKey(int i, KeyEvent keyEvent) { return a(i, keyEvent) != null; } public ArrayList j() { b(); return this.j; } boolean k() { return this.t; } Resources l() { return this.b; } public MenuBuilder m() { return this; } public ArrayList n() { if (!this.h) { return this.g; } this.g.clear(); int size = this.f.size(); for (int i = 0; i < size; i++) { MenuItemImpl menuItemImpl = this.f.get(i); if (menuItemImpl.isVisible()) { this.g.add(menuItemImpl); } } this.h = false; this.k = true; return this.g; } public boolean o() { return this.y; } boolean p() { return this.c; } @Override // android.view.Menu public boolean performIdentifierAction(int i, int i2) { return a(findItem(i), i2); } @Override // android.view.Menu public boolean performShortcut(int i, KeyEvent keyEvent, int i2) { MenuItemImpl a = a(i, keyEvent); boolean a2 = a != null ? a(a, i2) : false; if ((i2 & 2) != 0) { a(true); } return a2; } public boolean q() { return this.d; } public void r() { this.q = false; if (this.r) { this.r = false; b(this.s); } } @Override // android.view.Menu public void removeGroup(int i) { int a = a(i); if (a >= 0) { int size = this.f.size() - a; int i2 = 0; while (true) { int i3 = i2 + 1; if (i2 >= size || this.f.get(a).getGroupId() != i) { break; } a(a, false); i2 = i3; } b(true); } } @Override // android.view.Menu public void removeItem(int i) { a(b(i), true); } public void s() { if (this.q) { return; } this.q = true; this.r = false; this.s = false; } @Override // android.view.Menu public void setGroupCheckable(int i, boolean z, boolean z2) { int size = this.f.size(); for (int i2 = 0; i2 < size; i2++) { MenuItemImpl menuItemImpl = this.f.get(i2); if (menuItemImpl.getGroupId() == i) { menuItemImpl.c(z2); menuItemImpl.setCheckable(z); } } } @Override // android.view.Menu public void setGroupDividerEnabled(boolean z) { this.y = z; } @Override // android.view.Menu public void setGroupEnabled(int i, boolean z) { int size = this.f.size(); for (int i2 = 0; i2 < size; i2++) { MenuItemImpl menuItemImpl = this.f.get(i2); if (menuItemImpl.getGroupId() == i) { menuItemImpl.setEnabled(z); } } } @Override // android.view.Menu public void setGroupVisible(int i, boolean z) { int size = this.f.size(); boolean z2 = false; for (int i2 = 0; i2 < size; i2++) { MenuItemImpl menuItemImpl = this.f.get(i2); if (menuItemImpl.getGroupId() == i && menuItemImpl.e(z)) { z2 = true; } } if (z2) { b(true); } } @Override // android.view.Menu public void setQwertyMode(boolean z) { this.c = z; b(false); } @Override // android.view.Menu public int size() { return this.f.size(); } public void a(MenuPresenter menuPresenter, Context context) { this.w.add(new WeakReference<>(menuPresenter)); menuPresenter.a(context, this); this.k = true; } @Override // android.view.Menu public MenuItem add(int i) { return a(0, 0, 0, this.b.getString(i)); } @Override // android.view.Menu public SubMenu addSubMenu(int i) { return addSubMenu(0, 0, 0, this.b.getString(i)); } void c(MenuItemImpl menuItemImpl) { this.k = true; b(true); } @Override // android.view.Menu public MenuItem add(int i, int i2, int i3, CharSequence charSequence) { return a(i, i2, i3, charSequence); } @Override // android.view.Menu public SubMenu addSubMenu(int i, int i2, int i3, CharSequence charSequence) { MenuItemImpl menuItemImpl = (MenuItemImpl) a(i, i2, i3, charSequence); SubMenuBuilder subMenuBuilder = new SubMenuBuilder(this.a, this, menuItemImpl); menuItemImpl.a(subMenuBuilder); return subMenuBuilder; } @Override // android.view.Menu public MenuItem add(int i, int i2, int i3, int i4) { return a(i, i2, i3, this.b.getString(i4)); } public void b(Bundle bundle) { int size = size(); SparseArray sparseArray = null; for (int i = 0; i < size; i++) { MenuItem item = getItem(i); 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()) { ((SubMenuBuilder) item.getSubMenu()).b(bundle); } } if (sparseArray != null) { bundle.putSparseParcelableArray(d(), sparseArray); } } public ArrayList c() { b(); return this.i; } public MenuItemImpl f() { return this.x; } private boolean a(SubMenuBuilder subMenuBuilder, MenuPresenter menuPresenter) { if (this.w.isEmpty()) { return false; } boolean a = menuPresenter != null ? menuPresenter.a(subMenuBuilder) : false; Iterator> it = this.w.iterator(); while (it.hasNext()) { WeakReference next = it.next(); MenuPresenter menuPresenter2 = next.get(); if (menuPresenter2 == null) { this.w.remove(next); } else if (!a) { a = menuPresenter2.a(subMenuBuilder); } } return a; } public Context e() { return this.a; } @Override // android.view.Menu public SubMenu addSubMenu(int i, int i2, int i3, int i4) { return addSubMenu(i, i2, i3, this.b.getString(i4)); } public void c(boolean z) { this.z = z; } protected MenuBuilder e(int i) { a(i, null, 0, null, null); return this; } void d(MenuItemImpl menuItemImpl) { this.h = true; b(true); } protected MenuBuilder d(int i) { a(0, null, i, null, null); return this; } public void a(Bundle bundle) { MenuItem findItem; if (bundle == null) { return; } SparseArray sparseParcelableArray = bundle.getSparseParcelableArray(d()); int size = size(); for (int i = 0; i < size; i++) { MenuItem item = getItem(i); View actionView = item.getActionView(); if (actionView != null && actionView.getId() != -1) { actionView.restoreHierarchyState(sparseParcelableArray); } if (item.hasSubMenu()) { ((SubMenuBuilder) item.getSubMenu()).a(bundle); } } int i2 = bundle.getInt("android:menu:expandedactionview"); if (i2 <= 0 || (findItem = findItem(i2)) == null) { return; } findItem.expandActionView(); } public int b(int i) { int size = size(); for (int i2 = 0; i2 < size; i2++) { if (this.f.get(i2).getItemId() == i) { return i2; } } return -1; } public void b(boolean z) { if (!this.q) { if (z) { this.h = true; this.k = true; } d(z); return; } this.r = true; if (z) { this.s = true; } } public void a(Callback callback) { this.e = callback; } protected MenuItem a(int i, int i2, int i3, CharSequence charSequence) { int f = f(i3); MenuItemImpl a = a(i, i2, i3, f, charSequence, this.l); ContextMenu.ContextMenuInfo contextMenuInfo = this.m; if (contextMenuInfo != null) { a.a(contextMenuInfo); } ArrayList arrayList = this.f; arrayList.add(a(arrayList, f), a); b(true); return a; } public void b() { ArrayList n = n(); if (this.k) { Iterator> it = this.w.iterator(); boolean z = false; while (it.hasNext()) { WeakReference next = it.next(); MenuPresenter menuPresenter = next.get(); if (menuPresenter == null) { this.w.remove(next); } else { z |= menuPresenter.b(); } } if (z) { this.i.clear(); this.j.clear(); int size = n.size(); for (int i = 0; i < size; i++) { MenuItemImpl menuItemImpl = n.get(i); if (menuItemImpl.h()) { this.i.add(menuItemImpl); } else { this.j.add(menuItemImpl); } } } else { this.i.clear(); this.j.clear(); this.j.addAll(n()); } this.k = false; } } private MenuItemImpl a(int i, int i2, int i3, int i4, CharSequence charSequence, int i5) { return new MenuItemImpl(this, i, i2, i3, i4, charSequence, i5); } private void a(int i, boolean z) { if (i < 0 || i >= this.f.size()) { return; } this.f.remove(i); if (z) { b(true); } } void a(MenuItem menuItem) { int groupId = menuItem.getGroupId(); int size = this.f.size(); s(); for (int i = 0; i < size; i++) { MenuItemImpl menuItemImpl = this.f.get(i); if (menuItemImpl.getGroupId() == groupId && menuItemImpl.i() && menuItemImpl.isCheckable()) { menuItemImpl.b(menuItemImpl == menuItem); } } r(); } public boolean b(MenuItemImpl menuItemImpl) { boolean z = false; if (this.w.isEmpty()) { return false; } s(); Iterator> it = this.w.iterator(); while (it.hasNext()) { WeakReference next = it.next(); MenuPresenter menuPresenter = next.get(); if (menuPresenter == null) { this.w.remove(next); } else { z = menuPresenter.b(this, menuItemImpl); if (z) { break; } } } r(); if (z) { this.x = menuItemImpl; } return z; } public int a(int i) { return a(i, 0); } public int a(int i, int i2) { int size = size(); if (i2 < 0) { i2 = 0; } while (i2 < size) { if (this.f.get(i2).getGroupId() == i) { return i2; } i2++; } return -1; } boolean a(MenuBuilder menuBuilder, MenuItem menuItem) { Callback callback = this.e; return callback != null && callback.a(menuBuilder, menuItem); } public void a() { Callback callback = this.e; if (callback != null) { callback.a(this); } } private static int a(ArrayList arrayList, int i) { for (int size = arrayList.size() - 1; size >= 0; size--) { if (arrayList.get(size).c() <= i) { return size + 1; } } return 0; } void a(List list, int i, KeyEvent keyEvent) { boolean p = p(); int modifiers = keyEvent.getModifiers(); KeyCharacterMap.KeyData keyData = new KeyCharacterMap.KeyData(); if (keyEvent.getKeyData(keyData) || i == 67) { int size = this.f.size(); for (int i2 = 0; i2 < size; i2++) { MenuItemImpl menuItemImpl = this.f.get(i2); if (menuItemImpl.hasSubMenu()) { ((MenuBuilder) menuItemImpl.getSubMenu()).a(list, i, keyEvent); } char alphabeticShortcut = p ? menuItemImpl.getAlphabeticShortcut() : menuItemImpl.getNumericShortcut(); if (((modifiers & 69647) == ((p ? menuItemImpl.getAlphabeticModifiers() : menuItemImpl.getNumericModifiers()) & 69647)) && alphabeticShortcut != 0) { char[] cArr = keyData.meta; if ((alphabeticShortcut == cArr[0] || alphabeticShortcut == cArr[2] || (p && alphabeticShortcut == '\b' && i == 67)) && menuItemImpl.isEnabled()) { list.add(menuItemImpl); } } } } } MenuItemImpl a(int i, KeyEvent keyEvent) { char numericShortcut; ArrayList arrayList = this.v; arrayList.clear(); a(arrayList, i, 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 p = p(); for (int i2 = 0; i2 < size; i2++) { MenuItemImpl menuItemImpl = arrayList.get(i2); if (p) { numericShortcut = menuItemImpl.getAlphabeticShortcut(); } else { numericShortcut = menuItemImpl.getNumericShortcut(); } if ((numericShortcut == keyData.meta[0] && (metaState & 2) == 0) || ((numericShortcut == keyData.meta[2] && (metaState & 2) != 0) || (p && numericShortcut == '\b' && i == 67))) { return menuItemImpl; } } return null; } public boolean a(MenuItem menuItem, int i) { return a(menuItem, (MenuPresenter) null, i); } public boolean a(MenuItem menuItem, MenuPresenter menuPresenter, int i) { MenuItemImpl menuItemImpl = (MenuItemImpl) menuItem; if (menuItemImpl == null || !menuItemImpl.isEnabled()) { return false; } boolean g = menuItemImpl.g(); ActionProvider a = menuItemImpl.a(); boolean z = a != null && a.a(); if (menuItemImpl.f()) { g |= menuItemImpl.expandActionView(); if (g) { a(true); } } else if (menuItemImpl.hasSubMenu() || z) { if ((i & 4) == 0) { a(false); } if (!menuItemImpl.hasSubMenu()) { menuItemImpl.a(new SubMenuBuilder(e(), this, menuItemImpl)); } SubMenuBuilder subMenuBuilder = (SubMenuBuilder) menuItemImpl.getSubMenu(); if (z) { a.a(subMenuBuilder); } g |= a(subMenuBuilder, menuPresenter); if (!g) { a(true); } } else if ((i & 1) == 0) { a(true); } return g; } public final void a(boolean z) { if (this.u) { return; } this.u = true; Iterator> it = this.w.iterator(); while (it.hasNext()) { WeakReference next = it.next(); MenuPresenter menuPresenter = next.get(); if (menuPresenter == null) { this.w.remove(next); } else { menuPresenter.a(this, z); } } this.u = false; } private void a(int i, CharSequence charSequence, int i2, Drawable drawable, View view) { Resources l = l(); if (view != null) { this.p = view; this.n = null; this.o = null; } else { if (i > 0) { this.n = l.getText(i); } else if (charSequence != null) { this.n = charSequence; } if (i2 > 0) { this.o = ContextCompat.c(e(), i2); } else if (drawable != null) { this.o = drawable; } this.p = null; } b(false); } protected MenuBuilder a(CharSequence charSequence) { a(0, charSequence, 0, null, null); return this; } protected MenuBuilder a(Drawable drawable) { a(0, null, 0, drawable, null); return this; } protected MenuBuilder a(View view) { a(0, null, 0, null, view); return this; } public boolean a(MenuItemImpl menuItemImpl) { boolean z = false; if (!this.w.isEmpty() && this.x == menuItemImpl) { s(); Iterator> it = this.w.iterator(); while (it.hasNext()) { WeakReference next = it.next(); MenuPresenter menuPresenter = next.get(); if (menuPresenter == null) { this.w.remove(next); } else { z = menuPresenter.a(this, menuItemImpl); if (z) { break; } } } r(); if (z) { this.x = null; } } return z; } }