package androidx.appcompat.view.menu; import android.R; import android.content.Context; import android.content.res.Resources; import android.view.Gravity; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewTreeObserver; import android.widget.AdapterView; import android.widget.FrameLayout; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.TextView; import androidx.appcompat.R$dimen; import androidx.appcompat.R$layout; import androidx.appcompat.view.menu.MenuPresenter; import androidx.appcompat.widget.MenuPopupWindow; import androidx.core.view.ViewCompat; /* loaded from: classes.dex */ final class StandardMenuPopup extends MenuPopup implements PopupWindow.OnDismissListener, AdapterView.OnItemClickListener, MenuPresenter, View.OnKeyListener { private static final int v = R$layout.abc_popup_menu_item_layout; private final Context b; private final MenuBuilder c; private final MenuAdapter d; private final boolean e; private final int f; private final int g; private final int h; final MenuPopupWindow i; private PopupWindow.OnDismissListener l; private View m; View n; private MenuPresenter.Callback o; ViewTreeObserver p; private boolean q; private boolean r; private int s; private boolean u; final ViewTreeObserver.OnGlobalLayoutListener j = new ViewTreeObserver.OnGlobalLayoutListener() { // from class: androidx.appcompat.view.menu.StandardMenuPopup.1 @Override // android.view.ViewTreeObserver.OnGlobalLayoutListener public void onGlobalLayout() { if (!StandardMenuPopup.this.c() || StandardMenuPopup.this.i.k()) { return; } View view = StandardMenuPopup.this.n; if (view == null || !view.isShown()) { StandardMenuPopup.this.dismiss(); } else { StandardMenuPopup.this.i.a(); } } }; private final View.OnAttachStateChangeListener k = new View.OnAttachStateChangeListener() { // from class: androidx.appcompat.view.menu.StandardMenuPopup.2 @Override // android.view.View.OnAttachStateChangeListener public void onViewAttachedToWindow(View view) { } @Override // android.view.View.OnAttachStateChangeListener public void onViewDetachedFromWindow(View view) { ViewTreeObserver viewTreeObserver = StandardMenuPopup.this.p; if (viewTreeObserver != null) { if (!viewTreeObserver.isAlive()) { StandardMenuPopup.this.p = view.getViewTreeObserver(); } StandardMenuPopup standardMenuPopup = StandardMenuPopup.this; standardMenuPopup.p.removeGlobalOnLayoutListener(standardMenuPopup.j); } view.removeOnAttachStateChangeListener(this); } }; private int t = 0; public StandardMenuPopup(Context context, MenuBuilder menuBuilder, View view, int i, int i2, boolean z) { this.b = context; this.c = menuBuilder; this.e = z; this.d = new MenuAdapter(menuBuilder, LayoutInflater.from(context), this.e, v); this.g = i; this.h = i2; Resources resources = context.getResources(); this.f = Math.max(resources.getDisplayMetrics().widthPixels / 2, resources.getDimensionPixelSize(R$dimen.abc_config_prefDialogWidth)); this.m = view; this.i = new MenuPopupWindow(this.b, null, this.g, this.h); menuBuilder.a(this, context); } private boolean g() { View view; if (c()) { return true; } if (this.q || (view = this.m) == null) { return false; } this.n = view; this.i.a((PopupWindow.OnDismissListener) this); this.i.a((AdapterView.OnItemClickListener) this); this.i.a(true); View view2 = this.n; boolean z = this.p == null; this.p = view2.getViewTreeObserver(); if (z) { this.p.addOnGlobalLayoutListener(this.j); } view2.addOnAttachStateChangeListener(this.k); this.i.a(view2); this.i.c(this.t); if (!this.r) { this.s = MenuPopup.a(this.d, null, this.b, this.f); this.r = true; } this.i.b(this.s); this.i.e(2); this.i.a(f()); this.i.a(); ListView d = this.i.d(); d.setOnKeyListener(this); if (this.u && this.c.h() != null) { FrameLayout frameLayout = (FrameLayout) LayoutInflater.from(this.b).inflate(R$layout.abc_popup_menu_header_item_layout, (ViewGroup) d, false); TextView textView = (TextView) frameLayout.findViewById(R.id.title); if (textView != null) { textView.setText(this.c.h()); } frameLayout.setEnabled(false); d.addHeaderView(frameLayout, null, false); } this.i.a((ListAdapter) this.d); this.i.a(); return true; } @Override // androidx.appcompat.view.menu.MenuPopup public void a(int i) { this.t = i; } @Override // androidx.appcompat.view.menu.MenuPopup public void a(MenuBuilder menuBuilder) { } @Override // androidx.appcompat.view.menu.MenuPopup public void b(boolean z) { this.d.a(z); } @Override // androidx.appcompat.view.menu.MenuPresenter public boolean b() { return false; } @Override // androidx.appcompat.view.menu.ShowableListMenu public boolean c() { return !this.q && this.i.c(); } @Override // androidx.appcompat.view.menu.ShowableListMenu public ListView d() { return this.i.d(); } @Override // androidx.appcompat.view.menu.ShowableListMenu public void dismiss() { if (c()) { this.i.dismiss(); } } @Override // android.widget.PopupWindow.OnDismissListener public void onDismiss() { this.q = true; this.c.close(); ViewTreeObserver viewTreeObserver = this.p; if (viewTreeObserver != null) { if (!viewTreeObserver.isAlive()) { this.p = this.n.getViewTreeObserver(); } this.p.removeGlobalOnLayoutListener(this.j); this.p = null; } this.n.removeOnAttachStateChangeListener(this.k); PopupWindow.OnDismissListener onDismissListener = this.l; if (onDismissListener != null) { onDismissListener.onDismiss(); } } @Override // android.view.View.OnKeyListener public boolean onKey(View view, int i, KeyEvent keyEvent) { if (keyEvent.getAction() != 1 || i != 82) { return false; } dismiss(); return true; } @Override // androidx.appcompat.view.menu.ShowableListMenu public void a() { if (!g()) { throw new IllegalStateException("StandardMenuPopup cannot be used without an anchor"); } } @Override // androidx.appcompat.view.menu.MenuPopup public void b(int i) { this.i.d(i); } @Override // androidx.appcompat.view.menu.MenuPopup public void c(int i) { this.i.h(i); } @Override // androidx.appcompat.view.menu.MenuPopup public void c(boolean z) { this.u = z; } @Override // androidx.appcompat.view.menu.MenuPresenter public void a(boolean z) { this.r = false; MenuAdapter menuAdapter = this.d; if (menuAdapter != null) { menuAdapter.notifyDataSetChanged(); } } @Override // androidx.appcompat.view.menu.MenuPresenter public void a(MenuPresenter.Callback callback) { this.o = callback; } @Override // androidx.appcompat.view.menu.MenuPresenter public boolean a(SubMenuBuilder subMenuBuilder) { if (subMenuBuilder.hasVisibleItems()) { MenuPopupHelper menuPopupHelper = new MenuPopupHelper(this.b, subMenuBuilder, this.n, this.e, this.g, this.h); menuPopupHelper.a(this.o); menuPopupHelper.a(MenuPopup.b(subMenuBuilder)); menuPopupHelper.a(this.l); this.l = null; this.c.a(false); int g = this.i.g(); int h = this.i.h(); if ((Gravity.getAbsoluteGravity(this.t, ViewCompat.k(this.m)) & 7) == 5) { g += this.m.getWidth(); } if (menuPopupHelper.a(g, h)) { MenuPresenter.Callback callback = this.o; if (callback == null) { return true; } callback.a(subMenuBuilder); return true; } } return false; } @Override // androidx.appcompat.view.menu.MenuPresenter public void a(MenuBuilder menuBuilder, boolean z) { if (menuBuilder != this.c) { return; } dismiss(); MenuPresenter.Callback callback = this.o; if (callback != null) { callback.a(menuBuilder, z); } } @Override // androidx.appcompat.view.menu.MenuPopup public void a(View view) { this.m = view; } @Override // androidx.appcompat.view.menu.MenuPopup public void a(PopupWindow.OnDismissListener onDismissListener) { this.l = onDismissListener; } }