package o;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.google.android.gms.common.R;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.qa0, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final class ViewOnKeyListenerC2248qa0 extends UO implements PopupWindow.OnDismissListener, View.OnKeyListener {
    public boolean A;
    public boolean B;
    public int C;
    public boolean E;
    public final Context m;
    public final CO n;

    /* renamed from: o, reason: collision with root package name */
    public final C3089zO f690o;
    public final boolean p;
    public final int q;
    public final int r;
    public final C0711aP s;
    public PopupWindow.OnDismissListener v;
    public View w;
    public View x;
    public InterfaceC0998dP y;
    public ViewTreeObserver z;
    public final ViewTreeObserverOnGlobalLayoutListenerC1647k8 t = new ViewTreeObserverOnGlobalLayoutListenerC1647k8(3, this);
    public final ViewOnAttachStateChangeListenerC2491t3 u = new ViewOnAttachStateChangeListenerC2491t3(5, this);
    public int D = 0;

    public ViewOnKeyListenerC2248qa0(Context context, CO co, View view, int i, boolean z) {
        this.m = context;
        this.n = co;
        this.p = z;
        this.f690o = new C3089zO(co, LayoutInflater.from(context), z, R.layout.abc_popup_menu_item_layout);
        this.r = i;
        Resources resources = context.getResources();
        this.q = Math.max(resources.getDisplayMetrics().widthPixels / 2, resources.getDimensionPixelSize(R.dimen.abc_config_prefDialogWidth));
        this.w = view;
        this.s = new C0711aP(context, null, i, 0);
        co.b(this, context);
    }

    @Override // o.InterfaceC1093eP
    public final void a(CO co, boolean z) {
        if (co != this.n) {
            return;
        }
        dismiss();
        InterfaceC0998dP interfaceC0998dP = this.y;
        if (interfaceC0998dP != null) {
            interfaceC0998dP.a(co, z);
        }
    }

    @Override // o.D80
    public final boolean b() {
        return !this.A && this.s.K.isShowing();
    }

    @Override // o.InterfaceC1093eP
    public final void c() {
        this.B = false;
        C3089zO c3089zO = this.f690o;
        if (c3089zO != null) {
            c3089zO.notifyDataSetChanged();
        }
    }

    @Override // o.D80
    public final void dismiss() {
        if (b()) {
            this.s.dismiss();
        }
    }

    @Override // o.D80
    public final void e() {
        View view;
        if (b()) {
            return;
        }
        if (this.A || (view = this.w) == null) {
            throw new IllegalStateException("StandardMenuPopup cannot be used without an anchor");
        }
        this.x = view;
        C0711aP c0711aP = this.s;
        C0979d8 c0979d8 = c0711aP.K;
        C0979d8 c0979d82 = c0711aP.K;
        c0979d8.setOnDismissListener(this);
        c0711aP.A = this;
        c0711aP.J = true;
        c0979d82.setFocusable(true);
        View view2 = this.x;
        boolean z = this.z == null;
        ViewTreeObserver viewTreeObserver = view2.getViewTreeObserver();
        this.z = viewTreeObserver;
        if (z) {
            viewTreeObserver.addOnGlobalLayoutListener(this.t);
        }
        view2.addOnAttachStateChangeListener(this.u);
        c0711aP.z = view2;
        c0711aP.w = this.D;
        boolean z2 = this.B;
        Context context = this.m;
        C3089zO c3089zO = this.f690o;
        if (!z2) {
            this.C = UO.m(c3089zO, context, this.q);
            this.B = true;
        }
        c0711aP.r(this.C);
        c0979d82.setInputMethodMode(2);
        Rect rect = this.l;
        c0711aP.I = rect != null ? new Rect(rect) : null;
        c0711aP.e();
        C0097Dt c0097Dt = c0711aP.n;
        c0097Dt.setOnKeyListener(this);
        if (this.E) {
            CO co = this.n;
            if (co.m != null) {
                FrameLayout frameLayout = (FrameLayout) LayoutInflater.from(context).inflate(R.layout.abc_popup_menu_header_item_layout, (ViewGroup) c0097Dt, false);
                TextView textView = (TextView) frameLayout.findViewById(android.R.id.title);
                if (textView != null) {
                    textView.setText(co.m);
                }
                frameLayout.setEnabled(false);
                c0097Dt.addHeaderView(frameLayout, null, false);
            }
        }
        c0711aP.q(c3089zO);
        c0711aP.e();
    }

    @Override // o.InterfaceC1093eP
    public final void f(InterfaceC0998dP interfaceC0998dP) {
        this.y = interfaceC0998dP;
    }

    @Override // o.D80
    public final C0097Dt h() {
        return this.s.n;
    }

    @Override // o.InterfaceC1093eP
    public final boolean i() {
        return false;
    }

    @Override // o.InterfaceC1093eP
    public final boolean j(SubMenuC2155pb0 subMenuC2155pb0) {
        boolean z;
        if (subMenuC2155pb0.hasVisibleItems()) {
            WO wo = new WO(this.m, subMenuC2155pb0, this.x, this.p, this.r, 0);
            InterfaceC0998dP interfaceC0998dP = this.y;
            wo.h = interfaceC0998dP;
            UO uo = wo.i;
            if (uo != null) {
                uo.f(interfaceC0998dP);
            }
            int size = subMenuC2155pb0.f.size();
            int i = 0;
            while (true) {
                if (i >= size) {
                    z = false;
                    break;
                }
                MenuItem item = subMenuC2155pb0.getItem(i);
                if (item.isVisible() && item.getIcon() != null) {
                    z = true;
                    break;
                }
                i++;
            }
            wo.g = z;
            UO uo2 = wo.i;
            if (uo2 != null) {
                uo2.o(z);
            }
            wo.j = this.v;
            this.v = null;
            this.n.c(false);
            C0711aP c0711aP = this.s;
            int width = c0711aP.q;
            int iO = c0711aP.o();
            if ((Gravity.getAbsoluteGravity(this.D, this.w.getLayoutDirection()) & 7) == 5) {
                width += this.w.getWidth();
            }
            if (!wo.b()) {
                if (wo.e != null) {
                    wo.d(width, iO, true, true);
                }
            }
            InterfaceC0998dP interfaceC0998dP2 = this.y;
            if (interfaceC0998dP2 != null) {
                interfaceC0998dP2.k(subMenuC2155pb0);
            }
            return true;
        }
        return false;
    }

    @Override // o.UO
    public final void n(View view) {
        this.w = view;
    }

    @Override // o.UO
    public final void o(boolean z) {
        this.f690o.c = z;
    }

    @Override // android.widget.PopupWindow.OnDismissListener
    public final void onDismiss() {
        this.A = true;
        this.n.c(true);
        ViewTreeObserver viewTreeObserver = this.z;
        if (viewTreeObserver != null) {
            if (!viewTreeObserver.isAlive()) {
                this.z = this.x.getViewTreeObserver();
            }
            this.z.removeGlobalOnLayoutListener(this.t);
            this.z = null;
        }
        this.x.removeOnAttachStateChangeListener(this.u);
        PopupWindow.OnDismissListener onDismissListener = this.v;
        if (onDismissListener != null) {
            onDismissListener.onDismiss();
        }
    }

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

    @Override // o.UO
    public final void p(int i) {
        this.D = i;
    }

    @Override // o.UO
    public final void q(int i) {
        this.s.q = i;
    }

    @Override // o.UO
    public final void r(PopupWindow.OnDismissListener onDismissListener) {
        this.v = onDismissListener;
    }

    @Override // o.UO
    public final void s(boolean z) {
        this.E = z;
    }

    @Override // o.UO
    public final void t(int i) {
        this.s.k(i);
    }

    @Override // o.UO
    public final void l(CO co) {
    }
}
