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 x = g.a.g.abc_popup_menu_item_layout;
    private final Context d;
    private final h e;
    private final g f;

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

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

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

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    final j0 f106k;
    private PopupWindow.OnDismissListener n;
    private View o;
    View p;
    private o.a q;
    ViewTreeObserver r;
    private boolean s;
    private boolean t;
    private int u;
    private boolean w;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    final ViewTreeObserver.OnGlobalLayoutListener f107l = new a();

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private final View.OnAttachStateChangeListener f108m = new b();
    private int v = 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.f106k.p()) {
                return;
            }
            View view = t.this.p;
            if (view == null || !view.isShown()) {
                t.this.dismiss();
            } else {
                t.this.f106k.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.r;
            if (viewTreeObserver != null) {
                if (!viewTreeObserver.isAlive()) {
                    t.this.r = view.getViewTreeObserver();
                }
                t tVar = t.this;
                tVar.r.removeGlobalOnLayoutListener(tVar.f107l);
            }
            view.removeOnAttachStateChangeListener(this);
        }
    }

    public t(Context context, h hVar, View view, int i2, int i3, boolean z) {
        this.d = context;
        this.e = hVar;
        this.f102g = z;
        this.f = new g(hVar, LayoutInflater.from(context), this.f102g, x);
        this.f104i = i2;
        this.f105j = i3;
        Resources resources = context.getResources();
        this.f103h = Math.max(resources.getDisplayMetrics().widthPixels / 2, resources.getDimensionPixelSize(g.a.d.abc_config_prefDialogWidth));
        this.o = view;
        this.f106k = new j0(this.d, null, this.f104i, this.f105j);
        hVar.c(this, context);
    }

    private boolean z() {
        View view;
        if (i()) {
            return true;
        }
        if (this.s || (view = this.o) == null) {
            return false;
        }
        this.p = view;
        this.f106k.B(this);
        this.f106k.C(this);
        this.f106k.A(true);
        View view2 = this.p;
        boolean z = this.r == null;
        ViewTreeObserver viewTreeObserver = view2.getViewTreeObserver();
        this.r = viewTreeObserver;
        if (z) {
            viewTreeObserver.addOnGlobalLayoutListener(this.f107l);
        }
        view2.addOnAttachStateChangeListener(this.f108m);
        this.f106k.s(view2);
        this.f106k.w(this.v);
        if (!this.t) {
            this.u = m.o(this.f, null, this.d, this.f103h);
            this.t = true;
        }
        this.f106k.v(this.u);
        this.f106k.z(2);
        this.f106k.x(n());
        this.f106k.b();
        ListView listViewD = this.f106k.d();
        listViewD.setOnKeyListener(this);
        if (this.w && this.e.x() != null) {
            FrameLayout frameLayout = (FrameLayout) LayoutInflater.from(this.d).inflate(g.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.e.x());
            }
            frameLayout.setEnabled(false);
            listViewD.addHeaderView(frameLayout, null, false);
        }
        this.f106k.r(this.f);
        this.f106k.b();
        return true;
    }

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

    @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.f106k.d();
    }

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

    @Override // androidx.appcompat.view.menu.o
    public boolean e(u uVar) {
        if (uVar.hasVisibleItems()) {
            n nVar = new n(this.d, uVar, this.p, this.f102g, this.f104i, this.f105j);
            nVar.j(this.q);
            nVar.g(m.x(uVar));
            nVar.i(this.n);
            this.n = null;
            this.e.e(false);
            int iK = this.f106k.k();
            int iM = this.f106k.m();
            if ((Gravity.getAbsoluteGravity(this.v, g.e.j.o.f(this.o)) & 7) == 5) {
                iK += this.o.getWidth();
            }
            if (nVar.n(iK, iM)) {
                o.a aVar = this.q;
                if (aVar == null) {
                    return true;
                }
                aVar.b(uVar);
                return true;
            }
        }
        return false;
    }

    @Override // androidx.appcompat.view.menu.o
    public void f(boolean z) {
        this.t = false;
        g gVar = this.f;
        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.s && this.f106k.i();
    }

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

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

    @Override // android.widget.PopupWindow.OnDismissListener
    public void onDismiss() {
        this.s = true;
        this.e.close();
        ViewTreeObserver viewTreeObserver = this.r;
        if (viewTreeObserver != null) {
            if (!viewTreeObserver.isAlive()) {
                this.r = this.p.getViewTreeObserver();
            }
            this.r.removeGlobalOnLayoutListener(this.f107l);
            this.r = null;
        }
        this.p.removeOnAttachStateChangeListener(this.f108m);
        PopupWindow.OnDismissListener onDismissListener = this.n;
        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.o = view;
    }

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

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

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

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

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

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