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.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import androidx.appcompat.view.menu.m;
import androidx.appcompat.widget.y;

/* JADX INFO: compiled from: StandardMenuPopup.java */
/* JADX INFO: loaded from: classes.dex */
final class r extends k implements PopupWindow.OnDismissListener, AdapterView.OnItemClickListener, m, View.OnKeyListener {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private static final int f450d = a.a.g.j;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private final Context f451e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private final g f452f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private final f f453g;
    private final boolean h;
    private final int i;
    private final int j;
    private final int k;
    final y l;
    private PopupWindow.OnDismissListener o;
    private View p;
    View q;
    private m.a r;
    ViewTreeObserver s;
    private boolean t;
    private boolean u;
    private int v;
    private boolean x;
    final ViewTreeObserver.OnGlobalLayoutListener m = new a();
    private final View.OnAttachStateChangeListener n = new b();
    private int w = 0;

    /* JADX INFO: compiled from: StandardMenuPopup.java */
    class a implements ViewTreeObserver.OnGlobalLayoutListener {
        a() {
        }

        @Override // android.view.ViewTreeObserver.OnGlobalLayoutListener
        public void onGlobalLayout() {
            if (!r.this.e() || r.this.l.q()) {
                return;
            }
            View view = r.this.q;
            if (view == null || !view.isShown()) {
                r.this.i();
            } else {
                r.this.l.h();
            }
        }
    }

    /* JADX INFO: compiled from: StandardMenuPopup.java */
    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 = r.this.s;
            if (viewTreeObserver != null) {
                if (!viewTreeObserver.isAlive()) {
                    r.this.s = view.getViewTreeObserver();
                }
                r rVar = r.this;
                rVar.s.removeGlobalOnLayoutListener(rVar.m);
            }
            view.removeOnAttachStateChangeListener(this);
        }
    }

    public r(Context context, g gVar, View view, int i, int i2, boolean z) {
        this.f451e = context;
        this.f452f = gVar;
        this.h = z;
        this.f453g = new f(gVar, LayoutInflater.from(context), z, f450d);
        this.j = i;
        this.k = i2;
        Resources resources = context.getResources();
        this.i = Math.max(resources.getDisplayMetrics().widthPixels / 2, resources.getDimensionPixelSize(a.a.d.f17d));
        this.p = view;
        this.l = new y(context, null, i, i2);
        gVar.b(this, context);
    }

    private boolean A() {
        View view;
        if (e()) {
            return true;
        }
        if (this.t || (view = this.p) == null) {
            return false;
        }
        this.q = view;
        this.l.C(this);
        this.l.D(this);
        this.l.B(true);
        View view2 = this.q;
        boolean z = this.s == null;
        ViewTreeObserver viewTreeObserver = view2.getViewTreeObserver();
        this.s = viewTreeObserver;
        if (z) {
            viewTreeObserver.addOnGlobalLayoutListener(this.m);
        }
        view2.addOnAttachStateChangeListener(this.n);
        this.l.t(view2);
        this.l.x(this.w);
        if (!this.u) {
            this.v = k.p(this.f453g, null, this.f451e, this.i);
            this.u = true;
        }
        this.l.w(this.v);
        this.l.A(2);
        this.l.y(o());
        this.l.h();
        ListView listViewK = this.l.k();
        listViewK.setOnKeyListener(this);
        if (this.x && this.f452f.u() != null) {
            FrameLayout frameLayout = (FrameLayout) LayoutInflater.from(this.f451e).inflate(a.a.g.i, (ViewGroup) listViewK, false);
            TextView textView = (TextView) frameLayout.findViewById(R.id.title);
            if (textView != null) {
                textView.setText(this.f452f.u());
            }
            frameLayout.setEnabled(false);
            listViewK.addHeaderView(frameLayout, null, false);
        }
        this.l.s(this.f453g);
        this.l.h();
        return true;
    }

    @Override // androidx.appcompat.view.menu.m
    public void a(g gVar, boolean z) {
        if (gVar != this.f452f) {
            return;
        }
        i();
        m.a aVar = this.r;
        if (aVar != null) {
            aVar.a(gVar, z);
        }
    }

    @Override // androidx.appcompat.view.menu.m
    public void b(boolean z) {
        this.u = false;
        f fVar = this.f453g;
        if (fVar != null) {
            fVar.notifyDataSetChanged();
        }
    }

    @Override // androidx.appcompat.view.menu.m
    public boolean c() {
        return false;
    }

    @Override // androidx.appcompat.view.menu.q
    public boolean e() {
        return !this.t && this.l.e();
    }

    @Override // androidx.appcompat.view.menu.m
    public void g(m.a aVar) {
        this.r = aVar;
    }

    @Override // androidx.appcompat.view.menu.q
    public void h() {
        if (!A()) {
            throw new IllegalStateException("StandardMenuPopup cannot be used without an anchor");
        }
    }

    @Override // androidx.appcompat.view.menu.q
    public void i() {
        if (e()) {
            this.l.i();
        }
    }

    @Override // androidx.appcompat.view.menu.q
    public ListView k() {
        return this.l.k();
    }

    @Override // androidx.appcompat.view.menu.m
    public boolean l(s sVar) {
        if (sVar.hasVisibleItems()) {
            l lVar = new l(this.f451e, sVar, this.q, this.h, this.j, this.k);
            lVar.j(this.r);
            lVar.g(k.y(sVar));
            lVar.i(this.o);
            this.o = null;
            this.f452f.d(false);
            int iL = this.l.l();
            int iN = this.l.n();
            if ((Gravity.getAbsoluteGravity(this.w, a.c.i.l.c(this.p)) & 7) == 5) {
                iL += this.p.getWidth();
            }
            if (lVar.n(iL, iN)) {
                m.a aVar = this.r;
                if (aVar == null) {
                    return true;
                }
                aVar.b(sVar);
                return true;
            }
        }
        return false;
    }

    @Override // androidx.appcompat.view.menu.k
    public void m(g gVar) {
    }

    @Override // android.widget.PopupWindow.OnDismissListener
    public void onDismiss() {
        this.t = true;
        this.f452f.close();
        ViewTreeObserver viewTreeObserver = this.s;
        if (viewTreeObserver != null) {
            if (!viewTreeObserver.isAlive()) {
                this.s = this.q.getViewTreeObserver();
            }
            this.s.removeGlobalOnLayoutListener(this.m);
            this.s = null;
        }
        this.q.removeOnAttachStateChangeListener(this.n);
        PopupWindow.OnDismissListener onDismissListener = this.o;
        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;
        }
        i();
        return true;
    }

    @Override // androidx.appcompat.view.menu.k
    public void q(View view) {
        this.p = view;
    }

    @Override // androidx.appcompat.view.menu.k
    public void s(boolean z) {
        this.f453g.f(z);
    }

    @Override // androidx.appcompat.view.menu.k
    public void t(int i) {
        this.w = i;
    }

    @Override // androidx.appcompat.view.menu.k
    public void u(int i) {
        this.l.z(i);
    }

    @Override // androidx.appcompat.view.menu.k
    public void v(PopupWindow.OnDismissListener onDismissListener) {
        this.o = onDismissListener;
    }

    @Override // androidx.appcompat.view.menu.k
    public void w(boolean z) {
        this.x = z;
    }

    @Override // androidx.appcompat.view.menu.k
    public void x(int i) {
        this.l.I(i);
    }
}
