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.o;
import androidx.appcompat.widget.j0;

/* JADX INFO: compiled from: StandardMenuPopup.java */
/* JADX INFO: loaded from: classes.dex */
final class t extends m implements PopupWindow.OnDismissListener, AdapterView.OnItemClickListener, o, View.OnKeyListener {
    private static final int z = c.a.g.abc_popup_menu_item_layout;

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

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

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

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private final boolean f281i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private final int f282j;

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

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

        @Override // android.view.ViewTreeObserver.OnGlobalLayoutListener
        public void onGlobalLayout() {
            if (!t.this.i() || t.this.m.p()) {
                return;
            }
            View view = t.this.r;
            if (view == null || !view.isShown()) {
                t.this.dismiss();
            } else {
                t.this.m.b();
            }
        }
    }

    /* 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 = t.this.t;
            if (viewTreeObserver != null) {
                if (!viewTreeObserver.isAlive()) {
                    t.this.t = view.getViewTreeObserver();
                }
                t tVar = t.this;
                tVar.t.removeGlobalOnLayoutListener(tVar.n);
            }
            view.removeOnAttachStateChangeListener(this);
        }
    }

    public t(Context context, h hVar, View view, int i2, int i3, boolean z2) {
        this.f278f = context;
        this.f279g = hVar;
        this.f281i = z2;
        this.f280h = new g(hVar, LayoutInflater.from(context), this.f281i, z);
        this.f283k = i2;
        this.l = i3;
        Resources resources = context.getResources();
        this.f282j = Math.max(resources.getDisplayMetrics().widthPixels / 2, resources.getDimensionPixelSize(c.a.d.abc_config_prefDialogWidth));
        this.q = view;
        this.m = new j0(this.f278f, null, this.f283k, this.l);
        hVar.c(this, context);
    }

    private boolean z() {
        View view;
        if (i()) {
            return true;
        }
        if (this.u || (view = this.q) == null) {
            return false;
        }
        this.r = view;
        this.m.B(this);
        this.m.C(this);
        this.m.A(true);
        View view2 = this.r;
        boolean z2 = this.t == null;
        ViewTreeObserver viewTreeObserver = view2.getViewTreeObserver();
        this.t = viewTreeObserver;
        if (z2) {
            viewTreeObserver.addOnGlobalLayoutListener(this.n);
        }
        view2.addOnAttachStateChangeListener(this.o);
        this.m.s(view2);
        this.m.w(this.x);
        if (!this.v) {
            this.w = m.o(this.f280h, null, this.f278f, this.f282j);
            this.v = true;
        }
        this.m.v(this.w);
        this.m.z(2);
        this.m.x(n());
        this.m.b();
        ListView listViewD = this.m.d();
        listViewD.setOnKeyListener(this);
        if (this.y && this.f279g.x() != null) {
            FrameLayout frameLayout = (FrameLayout) LayoutInflater.from(this.f278f).inflate(c.a.g.abc_popup_menu_header_item_layout, (ViewGroup) listViewD, false);
            TextView textView = (TextView) frameLayout.findViewById(R.id.title);
            if (textView != null) {
                textView.setText(this.f279g.x());
            }
            frameLayout.setEnabled(false);
            listViewD.addHeaderView(frameLayout, null, false);
        }
        this.m.r(this.f280h);
        this.m.b();
        return true;
    }

    @Override // androidx.appcompat.view.menu.o
    public void a(h hVar, boolean z2) {
        if (hVar != this.f279g) {
            return;
        }
        dismiss();
        o.a aVar = this.s;
        if (aVar != null) {
            aVar.a(hVar, z2);
        }
    }

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

    @Override // androidx.appcompat.view.menu.s
    public ListView d() {
        return this.m.d();
    }

    @Override // androidx.appcompat.view.menu.s
    public void dismiss() {
        if (i()) {
            this.m.dismiss();
        }
    }

    @Override // androidx.appcompat.view.menu.o
    public boolean e(u uVar) {
        if (uVar.hasVisibleItems()) {
            n nVar = new n(this.f278f, uVar, this.r, this.f281i, this.f283k, this.l);
            nVar.j(this.s);
            nVar.g(m.x(uVar));
            nVar.i(this.p);
            this.p = null;
            this.f279g.e(false);
            int iK = this.m.k();
            int iM = this.m.m();
            if ((Gravity.getAbsoluteGravity(this.x, c.f.l.t.u(this.q)) & 7) == 5) {
                iK += this.q.getWidth();
            }
            if (nVar.n(iK, iM)) {
                o.a aVar = this.s;
                if (aVar == null) {
                    return true;
                }
                aVar.b(uVar);
                return true;
            }
        }
        return false;
    }

    @Override // androidx.appcompat.view.menu.o
    public void f(boolean z2) {
        this.v = false;
        g gVar = this.f280h;
        if (gVar != null) {
            gVar.notifyDataSetChanged();
        }
    }

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

    @Override // androidx.appcompat.view.menu.s
    public boolean i() {
        return !this.u && this.m.i();
    }

    @Override // androidx.appcompat.view.menu.o
    public void k(o.a aVar) {
        this.s = aVar;
    }

    @Override // androidx.appcompat.view.menu.m
    public void l(h hVar) {
    }

    @Override // android.widget.PopupWindow.OnDismissListener
    public void onDismiss() {
        this.u = true;
        this.f279g.close();
        ViewTreeObserver viewTreeObserver = this.t;
        if (viewTreeObserver != null) {
            if (!viewTreeObserver.isAlive()) {
                this.t = this.r.getViewTreeObserver();
            }
            this.t.removeGlobalOnLayoutListener(this.n);
            this.t = null;
        }
        this.r.removeOnAttachStateChangeListener(this.o);
        PopupWindow.OnDismissListener onDismissListener = this.p;
        if (onDismissListener != null) {
            onDismissListener.onDismiss();
        }
    }

    @Override // android.view.View.OnKeyListener
    public boolean onKey(View view, int i2, KeyEvent keyEvent) {
        if (keyEvent.getAction() != 1 || i2 != 82) {
            return false;
        }
        dismiss();
        return true;
    }

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

    @Override // androidx.appcompat.view.menu.m
    public void r(boolean z2) {
        this.f280h.d(z2);
    }

    @Override // androidx.appcompat.view.menu.m
    public void s(int i2) {
        this.x = i2;
    }

    @Override // androidx.appcompat.view.menu.m
    public void t(int i2) {
        this.m.y(i2);
    }

    @Override // androidx.appcompat.view.menu.m
    public void u(PopupWindow.OnDismissListener onDismissListener) {
        this.p = onDismissListener;
    }

    @Override // androidx.appcompat.view.menu.m
    public void v(boolean z2) {
        this.y = z2;
    }

    @Override // androidx.appcompat.view.menu.m
    public void w(int i2) {
        this.m.H(i2);
    }
}
