package android.support.v7.view.menu;

import android.R;
import android.content.Context;
import android.content.res.Resources;
import android.support.v7.view.menu.o;
import android.support.v7.widget.j0;
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;

/* 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 w = a.b.d.a.g.abc_popup_menu_item_layout;

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

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

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

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

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

        @Override // android.view.ViewTreeObserver.OnGlobalLayoutListener
        public void onGlobalLayout() {
            if (!t.this.h() || t.this.j.p()) {
                return;
            }
            View view = t.this.o;
            if (view == null || !view.isShown()) {
                t.this.dismiss();
            } else {
                t.this.j.c();
            }
        }
    }

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

    public t(Context context, h hVar, View view, int i, int i2, boolean z) {
        this.f915c = context;
        this.f916d = hVar;
        this.f918f = z;
        this.f917e = new g(hVar, LayoutInflater.from(context), this.f918f, w);
        this.h = i;
        this.i = i2;
        Resources resources = context.getResources();
        this.g = Math.max(resources.getDisplayMetrics().widthPixels / 2, resources.getDimensionPixelSize(a.b.d.a.d.abc_config_prefDialogWidth));
        this.n = view;
        this.j = new j0(this.f915c, null, this.h, this.i);
        hVar.c(this, context);
    }

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

    @Override // android.support.v7.view.menu.o
    public void a(h hVar, boolean z) {
        if (hVar != this.f916d) {
            return;
        }
        dismiss();
        o.a aVar = this.p;
        if (aVar != null) {
            aVar.a(hVar, z);
        }
    }

    @Override // android.support.v7.view.menu.o
    public boolean b(u uVar) {
        if (uVar.hasVisibleItems()) {
            n nVar = new n(this.f915c, uVar, this.o, this.f918f, this.h, this.i);
            nVar.j(this.p);
            nVar.g(m.x(uVar));
            nVar.i(this.m);
            this.m = null;
            this.f916d.e(false);
            int iK = this.j.k();
            int iM = this.j.m();
            if ((Gravity.getAbsoluteGravity(this.u, a.b.c.g.p.f(this.n)) & 7) == 5) {
                iK += this.n.getWidth();
            }
            if (nVar.n(iK, iM)) {
                o.a aVar = this.p;
                if (aVar == null) {
                    return true;
                }
                aVar.b(uVar);
                return true;
            }
        }
        return false;
    }

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

    @Override // android.support.v7.view.menu.o
    public boolean d() {
        return false;
    }

    @Override // android.support.v7.view.menu.s
    public void dismiss() {
        if (h()) {
            this.j.dismiss();
        }
    }

    @Override // android.support.v7.view.menu.o
    public void f(o.a aVar) {
        this.p = aVar;
    }

    @Override // android.support.v7.view.menu.s
    public boolean h() {
        return !this.r && this.j.h();
    }

    @Override // android.support.v7.view.menu.s
    public ListView i() {
        return this.j.i();
    }

    @Override // android.support.v7.view.menu.o
    public void k(boolean z) {
        this.s = false;
        g gVar = this.f917e;
        if (gVar != null) {
            gVar.notifyDataSetChanged();
        }
    }

    @Override // android.support.v7.view.menu.m
    public void l(h hVar) {
    }

    @Override // android.widget.PopupWindow.OnDismissListener
    public void onDismiss() {
        this.r = true;
        this.f916d.close();
        ViewTreeObserver viewTreeObserver = this.q;
        if (viewTreeObserver != null) {
            if (!viewTreeObserver.isAlive()) {
                this.q = this.o.getViewTreeObserver();
            }
            this.q.removeGlobalOnLayoutListener(this.k);
            this.q = null;
        }
        this.o.removeOnAttachStateChangeListener(this.l);
        PopupWindow.OnDismissListener onDismissListener = this.m;
        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 // android.support.v7.view.menu.m
    public void p(View view) {
        this.n = view;
    }

    @Override // android.support.v7.view.menu.m
    public void r(boolean z) {
        this.f917e.d(z);
    }

    @Override // android.support.v7.view.menu.m
    public void s(int i) {
        this.u = i;
    }

    @Override // android.support.v7.view.menu.m
    public void t(int i) {
        this.j.y(i);
    }

    @Override // android.support.v7.view.menu.m
    public void u(PopupWindow.OnDismissListener onDismissListener) {
        this.m = onDismissListener;
    }

    @Override // android.support.v7.view.menu.m
    public void v(boolean z) {
        this.v = z;
    }

    @Override // android.support.v7.view.menu.m
    public void w(int i) {
        this.j.H(i);
    }
}
