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.FrameLayout; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.TextView; import androidx.appcompat.view.menu.j; import androidx.appcompat.widget.w1; import androidx.core.view.n0; /* loaded from: classes.dex */ final class l extends h implements PopupWindow.OnDismissListener, View.OnKeyListener { /* renamed from: v, reason: collision with root package name */ private static final int f677v = d.g.f8791m; /* renamed from: b, reason: collision with root package name */ private final Context f678b; /* renamed from: c, reason: collision with root package name */ private final e f679c; /* renamed from: d, reason: collision with root package name */ private final d f680d; /* renamed from: e, reason: collision with root package name */ private final boolean f681e; /* renamed from: f, reason: collision with root package name */ private final int f682f; /* renamed from: g, reason: collision with root package name */ private final int f683g; /* renamed from: h, reason: collision with root package name */ private final int f684h; /* renamed from: i, reason: collision with root package name */ final w1 f685i; /* renamed from: l, reason: collision with root package name */ private PopupWindow.OnDismissListener f688l; /* renamed from: m, reason: collision with root package name */ private View f689m; /* renamed from: n, reason: collision with root package name */ View f690n; /* renamed from: o, reason: collision with root package name */ private j.a f691o; /* renamed from: p, reason: collision with root package name */ ViewTreeObserver f692p; /* renamed from: q, reason: collision with root package name */ private boolean f693q; /* renamed from: r, reason: collision with root package name */ private boolean f694r; /* renamed from: s, reason: collision with root package name */ private int f695s; /* renamed from: u, reason: collision with root package name */ private boolean f697u; /* renamed from: j, reason: collision with root package name */ final ViewTreeObserver.OnGlobalLayoutListener f686j = new a(); /* renamed from: k, reason: collision with root package name */ private final View.OnAttachStateChangeListener f687k = new b(); /* renamed from: t, reason: collision with root package name */ private int f696t = 0; class a implements ViewTreeObserver.OnGlobalLayoutListener { a() { } @Override // android.view.ViewTreeObserver.OnGlobalLayoutListener public void onGlobalLayout() { if (!l.this.a() || l.this.f685i.x()) { return; } View view = l.this.f690n; if (view == null || !view.isShown()) { l.this.dismiss(); } else { l.this.f685i.h(); } } } class b implements View.OnAttachStateChangeListener { b() { } @Override // android.view.View.OnAttachStateChangeListener public void onViewAttachedToWindow(View view) { } @Override // android.view.View.OnAttachStateChangeListener public void onViewDetachedFromWindow(View view) { ViewTreeObserver viewTreeObserver = l.this.f692p; if (viewTreeObserver != null) { if (!viewTreeObserver.isAlive()) { l.this.f692p = view.getViewTreeObserver(); } l lVar = l.this; lVar.f692p.removeGlobalOnLayoutListener(lVar.f686j); } view.removeOnAttachStateChangeListener(this); } } public l(Context context, e eVar, View view, int i10, int i11, boolean z10) { this.f678b = context; this.f679c = eVar; this.f681e = z10; this.f680d = new d(eVar, LayoutInflater.from(context), z10, f677v); this.f683g = i10; this.f684h = i11; Resources resources = context.getResources(); this.f682f = Math.max(resources.getDisplayMetrics().widthPixels / 2, resources.getDimensionPixelSize(d.d.f8715b)); this.f689m = view; this.f685i = new w1(context, null, i10, i11); eVar.c(this, context); } private boolean z() { View view; if (a()) { return true; } if (this.f693q || (view = this.f689m) == null) { return false; } this.f690n = view; this.f685i.G(this); this.f685i.H(this); this.f685i.F(true); View view2 = this.f690n; boolean z10 = this.f692p == null; ViewTreeObserver viewTreeObserver = view2.getViewTreeObserver(); this.f692p = viewTreeObserver; if (z10) { viewTreeObserver.addOnGlobalLayoutListener(this.f686j); } view2.addOnAttachStateChangeListener(this.f687k); this.f685i.z(view2); this.f685i.C(this.f696t); if (!this.f694r) { this.f695s = h.o(this.f680d, null, this.f678b, this.f682f); this.f694r = true; } this.f685i.B(this.f695s); this.f685i.E(2); this.f685i.D(n()); this.f685i.h(); ListView j10 = this.f685i.j(); j10.setOnKeyListener(this); if (this.f697u && this.f679c.x() != null) { FrameLayout frameLayout = (FrameLayout) LayoutInflater.from(this.f678b).inflate(d.g.f8790l, (ViewGroup) j10, false); TextView textView = (TextView) frameLayout.findViewById(R.id.title); if (textView != null) { textView.setText(this.f679c.x()); } frameLayout.setEnabled(false); j10.addHeaderView(frameLayout, null, false); } this.f685i.p(this.f680d); this.f685i.h(); return true; } @Override // i.e public boolean a() { return !this.f693q && this.f685i.a(); } @Override // androidx.appcompat.view.menu.j public void b(e eVar, boolean z10) { if (eVar != this.f679c) { return; } dismiss(); j.a aVar = this.f691o; if (aVar != null) { aVar.b(eVar, z10); } } @Override // androidx.appcompat.view.menu.j public void c(boolean z10) { this.f694r = false; d dVar = this.f680d; if (dVar != null) { dVar.notifyDataSetChanged(); } } @Override // androidx.appcompat.view.menu.j public boolean d() { return false; } @Override // i.e public void dismiss() { if (a()) { this.f685i.dismiss(); } } @Override // androidx.appcompat.view.menu.j public void g(j.a aVar) { this.f691o = aVar; } @Override // i.e public void h() { if (!z()) { throw new IllegalStateException("StandardMenuPopup cannot be used without an anchor"); } } @Override // i.e public ListView j() { return this.f685i.j(); } @Override // androidx.appcompat.view.menu.j public boolean k(m mVar) { if (mVar.hasVisibleItems()) { i iVar = new i(this.f678b, mVar, this.f690n, this.f681e, this.f683g, this.f684h); iVar.j(this.f691o); iVar.g(h.x(mVar)); iVar.i(this.f688l); this.f688l = null; this.f679c.e(false); int b10 = this.f685i.b(); int o10 = this.f685i.o(); if ((Gravity.getAbsoluteGravity(this.f696t, n0.B(this.f689m)) & 7) == 5) { b10 += this.f689m.getWidth(); } if (iVar.n(b10, o10)) { j.a aVar = this.f691o; if (aVar == null) { return true; } aVar.c(mVar); return true; } } return false; } @Override // androidx.appcompat.view.menu.h public void l(e eVar) { } @Override // android.widget.PopupWindow.OnDismissListener public void onDismiss() { this.f693q = true; this.f679c.close(); ViewTreeObserver viewTreeObserver = this.f692p; if (viewTreeObserver != null) { if (!viewTreeObserver.isAlive()) { this.f692p = this.f690n.getViewTreeObserver(); } this.f692p.removeGlobalOnLayoutListener(this.f686j); this.f692p = null; } this.f690n.removeOnAttachStateChangeListener(this.f687k); PopupWindow.OnDismissListener onDismissListener = this.f688l; if (onDismissListener != null) { onDismissListener.onDismiss(); } } @Override // android.view.View.OnKeyListener public boolean onKey(View view, int i10, KeyEvent keyEvent) { if (keyEvent.getAction() != 1 || i10 != 82) { return false; } dismiss(); return true; } @Override // androidx.appcompat.view.menu.h public void p(View view) { this.f689m = view; } @Override // androidx.appcompat.view.menu.h public void r(boolean z10) { this.f680d.d(z10); } @Override // androidx.appcompat.view.menu.h public void s(int i10) { this.f696t = i10; } @Override // androidx.appcompat.view.menu.h public void t(int i10) { this.f685i.d(i10); } @Override // androidx.appcompat.view.menu.h public void u(PopupWindow.OnDismissListener onDismissListener) { this.f688l = onDismissListener; } @Override // androidx.appcompat.view.menu.h public void v(boolean z10) { this.f697u = z10; } @Override // androidx.appcompat.view.menu.h public void w(int i10) { this.f685i.l(i10); } }