package c.b.e.i;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
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 c.b.e.i.m;
import c.b.f.l0;
import c.b.f.s0;
import com.ifpdos.debugmenu.R;
import java.util.Objects;

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

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

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

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final boolean f762h;

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

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final int f765k;

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

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

        @Override // android.view.ViewTreeObserver.OnGlobalLayoutListener
        public void onGlobalLayout() {
            if (q.this.a()) {
                q qVar = q.this;
                if (qVar.f766l.D) {
                    return;
                }
                View view = qVar.q;
                if (view == null || !view.isShown()) {
                    q.this.dismiss();
                } else {
                    q.this.f766l.h();
                }
            }
        }
    }

    /* JADX INFO: compiled from: StandardMenuPopup.java */
    public class b implements View.OnAttachStateChangeListener {
        public b() {
        }

        @Override // android.view.View.OnAttachStateChangeListener
        public void onViewAttachedToWindow(View view) {
        }

        @Override // android.view.View.OnAttachStateChangeListener
        public void onViewDetachedFromWindow(View view) {
            ViewTreeObserver viewTreeObserver = q.this.s;
            if (viewTreeObserver != null) {
                if (!viewTreeObserver.isAlive()) {
                    q.this.s = view.getViewTreeObserver();
                }
                q qVar = q.this;
                qVar.s.removeGlobalOnLayoutListener(qVar.m);
            }
            view.removeOnAttachStateChangeListener(this);
        }
    }

    public q(Context context, g gVar, View view, int i2, int i3, boolean z) {
        this.f759e = context;
        this.f760f = gVar;
        this.f762h = z;
        this.f761g = new f(gVar, LayoutInflater.from(context), z, R.layout.abc_popup_menu_item_layout);
        this.f764j = i2;
        this.f765k = i3;
        Resources resources = context.getResources();
        this.f763i = Math.max(resources.getDisplayMetrics().widthPixels / 2, resources.getDimensionPixelSize(R.dimen.abc_config_prefDialogWidth));
        this.p = view;
        this.f766l = new s0(context, null, i2, i3);
        gVar.b(this, context);
    }

    @Override // c.b.e.i.p
    public boolean a() {
        return !this.t && this.f766l.a();
    }

    @Override // c.b.e.i.m
    public void b(g gVar, boolean z) {
        if (gVar != this.f760f) {
            return;
        }
        dismiss();
        m.a aVar = this.r;
        if (aVar != null) {
            aVar.b(gVar, z);
        }
    }

    @Override // c.b.e.i.m
    public void c(boolean z) {
        this.u = false;
        f fVar = this.f761g;
        if (fVar != null) {
            fVar.notifyDataSetChanged();
        }
    }

    @Override // c.b.e.i.m
    public boolean d() {
        return false;
    }

    @Override // c.b.e.i.p
    public void dismiss() {
        if (a()) {
            this.f766l.dismiss();
        }
    }

    @Override // c.b.e.i.m
    public void g(m.a aVar) {
        this.r = aVar;
    }

    @Override // c.b.e.i.p
    public void h() {
        View view;
        boolean z = true;
        if (!a()) {
            if (this.t || (view = this.p) == null) {
                z = false;
            } else {
                this.q = view;
                this.f766l.E.setOnDismissListener(this);
                s0 s0Var = this.f766l;
                s0Var.v = this;
                s0Var.s(true);
                View view2 = this.q;
                boolean z2 = this.s == null;
                ViewTreeObserver viewTreeObserver = view2.getViewTreeObserver();
                this.s = viewTreeObserver;
                if (z2) {
                    viewTreeObserver.addOnGlobalLayoutListener(this.m);
                }
                view2.addOnAttachStateChangeListener(this.n);
                s0 s0Var2 = this.f766l;
                s0Var2.u = view2;
                s0Var2.q = this.w;
                if (!this.u) {
                    this.v = k.m(this.f761g, null, this.f759e, this.f763i);
                    this.u = true;
                }
                this.f766l.r(this.v);
                this.f766l.E.setInputMethodMode(2);
                s0 s0Var3 = this.f766l;
                Rect rect = this.f745b;
                Objects.requireNonNull(s0Var3);
                s0Var3.C = rect != null ? new Rect(rect) : null;
                this.f766l.h();
                l0 l0Var = this.f766l.f937h;
                l0Var.setOnKeyListener(this);
                if (this.x && this.f760f.n != null) {
                    FrameLayout frameLayout = (FrameLayout) LayoutInflater.from(this.f759e).inflate(R.layout.abc_popup_menu_header_item_layout, (ViewGroup) l0Var, false);
                    TextView textView = (TextView) frameLayout.findViewById(android.R.id.title);
                    if (textView != null) {
                        textView.setText(this.f760f.n);
                    }
                    frameLayout.setEnabled(false);
                    l0Var.addHeaderView(frameLayout, null, false);
                }
                this.f766l.p(this.f761g);
                this.f766l.h();
            }
        }
        if (!z) {
            throw new IllegalStateException("StandardMenuPopup cannot be used without an anchor");
        }
    }

    @Override // c.b.e.i.p
    public ListView j() {
        return this.f766l.f937h;
    }

    /* JADX WARN: Removed duplicated region for block: B:23:0x0070  */
    @Override // c.b.e.i.m
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public boolean k(c.b.e.i.r r10) {
        /*
            r9 = this;
            boolean r0 = r10.hasVisibleItems()
            r1 = 0
            if (r0 == 0) goto L78
            c.b.e.i.l r0 = new c.b.e.i.l
            android.content.Context r3 = r9.f759e
            android.view.View r5 = r9.q
            boolean r6 = r9.f762h
            int r7 = r9.f764j
            int r8 = r9.f765k
            r2 = r0
            r4 = r10
            r2.<init>(r3, r4, r5, r6, r7, r8)
            c.b.e.i.m$a r2 = r9.r
            r0.d(r2)
            boolean r2 = c.b.e.i.k.u(r10)
            r0.f752h = r2
            c.b.e.i.k r3 = r0.f754j
            if (r3 == 0) goto L2a
            r3.o(r2)
        L2a:
            android.widget.PopupWindow$OnDismissListener r2 = r9.o
            r0.f755k = r2
            r2 = 0
            r9.o = r2
            c.b.e.i.g r2 = r9.f760f
            r2.c(r1)
            c.b.f.s0 r2 = r9.f766l
            int r3 = r2.f940k
            boolean r4 = r2.n
            if (r4 != 0) goto L40
            r2 = r1
            goto L42
        L40:
            int r2 = r2.f941l
        L42:
            int r4 = r9.w
            android.view.View r5 = r9.p
            java.util.WeakHashMap<android.view.View, c.f.j.i0> r6 = c.f.j.a0.a
            int r5 = c.f.j.a0.e.d(r5)
            int r4 = android.view.Gravity.getAbsoluteGravity(r4, r5)
            r4 = r4 & 7
            r5 = 5
            if (r4 != r5) goto L5c
            android.view.View r4 = r9.p
            int r4 = r4.getWidth()
            int r3 = r3 + r4
        L5c:
            boolean r4 = r0.b()
            r5 = 1
            if (r4 == 0) goto L64
            goto L6d
        L64:
            android.view.View r4 = r0.f750f
            if (r4 != 0) goto L6a
            r0 = r1
            goto L6e
        L6a:
            r0.e(r3, r2, r5, r5)
        L6d:
            r0 = r5
        L6e:
            if (r0 == 0) goto L78
            c.b.e.i.m$a r0 = r9.r
            if (r0 == 0) goto L77
            r0.c(r10)
        L77:
            return r5
        L78:
            return r1
        */
        throw new UnsupportedOperationException("Method not decompiled: c.b.e.i.q.k(c.b.e.i.r):boolean");
    }

    @Override // c.b.e.i.k
    public void l(g gVar) {
    }

    @Override // c.b.e.i.k
    public void n(View view) {
        this.p = view;
    }

    @Override // c.b.e.i.k
    public void o(boolean z) {
        this.f761g.f708f = z;
    }

    @Override // android.widget.PopupWindow.OnDismissListener
    public void onDismiss() {
        this.t = true;
        this.f760f.c(true);
        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 i2, KeyEvent keyEvent) {
        if (keyEvent.getAction() != 1 || i2 != 82) {
            return false;
        }
        dismiss();
        return true;
    }

    @Override // c.b.e.i.k
    public void p(int i2) {
        this.w = i2;
    }

    @Override // c.b.e.i.k
    public void q(int i2) {
        this.f766l.f940k = i2;
    }

    @Override // c.b.e.i.k
    public void r(PopupWindow.OnDismissListener onDismissListener) {
        this.o = onDismissListener;
    }

    @Override // c.b.e.i.k
    public void s(boolean z) {
        this.x = z;
    }

    @Override // c.b.e.i.k
    public void t(int i2) {
        s0 s0Var = this.f766l;
        s0Var.f941l = i2;
        s0Var.n = true;
    }
}
