package m;

import M.J;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
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.FrameLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.martsnew.tv.R;
import h0.V;
import java.lang.reflect.Field;
import n.C1227o0;
import n.C1229p0;

/* JADX INFO: renamed from: m.r, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class ViewOnKeyListenerC1157r extends AbstractC1149j implements PopupWindow.OnDismissListener, View.OnKeyListener {

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    public C1150k f14524A;

    /* JADX INFO: renamed from: B, reason: collision with root package name */
    public View f14525B;

    /* JADX INFO: renamed from: C, reason: collision with root package name */
    public View f14526C;
    public InterfaceC1153n D;

    /* JADX INFO: renamed from: E, reason: collision with root package name */
    public ViewTreeObserver f14527E;

    /* JADX INFO: renamed from: F, reason: collision with root package name */
    public boolean f14528F;

    /* JADX INFO: renamed from: G, reason: collision with root package name */
    public boolean f14529G;
    public int H;

    /* JADX INFO: renamed from: J, reason: collision with root package name */
    public boolean f14531J;

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

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public final MenuC1147h f14533s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final C1145f f14534t;

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

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

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

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public final C1229p0 f14538x;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    public final ViewTreeObserverOnGlobalLayoutListenerC1142c f14539y = new ViewTreeObserverOnGlobalLayoutListenerC1142c(this, 1);

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public final V f14540z = new V(2, this);

    /* JADX INFO: renamed from: I, reason: collision with root package name */
    public int f14530I = 0;

    public ViewOnKeyListenerC1157r(int i, Context context, View view, MenuC1147h menuC1147h, boolean z8) {
        this.f14532r = context;
        this.f14533s = menuC1147h;
        this.f14535u = z8;
        this.f14534t = new C1145f(menuC1147h, LayoutInflater.from(context), z8, R.layout.abc_popup_menu_item_layout);
        this.f14537w = i;
        Resources resources = context.getResources();
        this.f14536v = Math.max(resources.getDisplayMetrics().widthPixels / 2, resources.getDimensionPixelSize(R.dimen.abc_config_prefDialogWidth));
        this.f14525B = view;
        this.f14538x = new C1229p0(context, i);
        menuC1147h.b(this, context);
    }

    @Override // m.InterfaceC1154o
    public final void a(MenuC1147h menuC1147h, boolean z8) {
        if (menuC1147h != this.f14533s) {
            return;
        }
        dismiss();
        InterfaceC1153n interfaceC1153n = this.D;
        if (interfaceC1153n != null) {
            interfaceC1153n.a(menuC1147h, z8);
        }
    }

    @Override // m.InterfaceC1156q
    public final void c() {
        View view;
        if (h()) {
            return;
        }
        if (this.f14528F || (view = this.f14525B) == null) {
            throw new IllegalStateException("StandardMenuPopup cannot be used without an anchor");
        }
        this.f14526C = view;
        C1229p0 c1229p0 = this.f14538x;
        c1229p0.f14884L.setOnDismissListener(this);
        c1229p0.f14877C = this;
        c1229p0.f14883K = true;
        c1229p0.f14884L.setFocusable(true);
        View view2 = this.f14526C;
        boolean z8 = this.f14527E == null;
        ViewTreeObserver viewTreeObserver = view2.getViewTreeObserver();
        this.f14527E = viewTreeObserver;
        if (z8) {
            viewTreeObserver.addOnGlobalLayoutListener(this.f14539y);
        }
        view2.addOnAttachStateChangeListener(this.f14540z);
        c1229p0.f14876B = view2;
        c1229p0.f14894z = this.f14530I;
        boolean z9 = this.f14529G;
        Context context = this.f14532r;
        C1145f c1145f = this.f14534t;
        if (!z9) {
            this.H = AbstractC1149j.m(c1145f, context, this.f14536v);
            this.f14529G = true;
        }
        int i = this.H;
        Drawable background = c1229p0.f14884L.getBackground();
        if (background != null) {
            Rect rect = c1229p0.f14881I;
            background.getPadding(rect);
            c1229p0.f14888t = rect.left + rect.right + i;
        } else {
            c1229p0.f14888t = i;
        }
        c1229p0.f14884L.setInputMethodMode(2);
        Rect rect2 = this.f14512q;
        c1229p0.f14882J = rect2 != null ? new Rect(rect2) : null;
        c1229p0.c();
        C1227o0 c1227o0 = c1229p0.f14887s;
        c1227o0.setOnKeyListener(this);
        if (this.f14531J) {
            MenuC1147h menuC1147h = this.f14533s;
            if (menuC1147h.f14475l != null) {
                FrameLayout frameLayout = (FrameLayout) LayoutInflater.from(context).inflate(R.layout.abc_popup_menu_header_item_layout, (ViewGroup) c1227o0, false);
                TextView textView = (TextView) frameLayout.findViewById(android.R.id.title);
                if (textView != null) {
                    textView.setText(menuC1147h.f14475l);
                }
                frameLayout.setEnabled(false);
                c1227o0.addHeaderView(frameLayout, null, false);
            }
        }
        c1229p0.a(c1145f);
        c1229p0.c();
    }

    @Override // m.InterfaceC1156q
    public final void dismiss() {
        if (h()) {
            this.f14538x.dismiss();
        }
    }

    @Override // m.InterfaceC1154o
    public final boolean e() {
        return false;
    }

    @Override // m.InterfaceC1154o
    public final boolean f(SubMenuC1158s subMenuC1158s) {
        if (subMenuC1158s.hasVisibleItems()) {
            C1152m c1152m = new C1152m(this.f14537w, this.f14532r, this.f14526C, subMenuC1158s, this.f14535u);
            InterfaceC1153n interfaceC1153n = this.D;
            c1152m.f14521h = interfaceC1153n;
            AbstractC1149j abstractC1149j = c1152m.i;
            if (abstractC1149j != null) {
                abstractC1149j.j(interfaceC1153n);
            }
            boolean zU = AbstractC1149j.u(subMenuC1158s);
            c1152m.f14520g = zU;
            AbstractC1149j abstractC1149j2 = c1152m.i;
            if (abstractC1149j2 != null) {
                abstractC1149j2.o(zU);
            }
            c1152m.f14522j = this.f14524A;
            this.f14524A = null;
            this.f14533s.c(false);
            C1229p0 c1229p0 = this.f14538x;
            int width = c1229p0.f14889u;
            int i = !c1229p0.f14891w ? 0 : c1229p0.f14890v;
            int i5 = this.f14530I;
            View view = this.f14525B;
            Field field = J.f4065a;
            if ((Gravity.getAbsoluteGravity(i5, view.getLayoutDirection()) & 7) == 5) {
                width += this.f14525B.getWidth();
            }
            if (!c1152m.b()) {
                if (c1152m.f14518e != null) {
                    c1152m.d(width, i, true, true);
                }
            }
            InterfaceC1153n interfaceC1153n2 = this.D;
            if (interfaceC1153n2 != null) {
                interfaceC1153n2.d(subMenuC1158s);
            }
            return true;
        }
        return false;
    }

    @Override // m.InterfaceC1154o
    public final void g() {
        this.f14529G = false;
        C1145f c1145f = this.f14534t;
        if (c1145f != null) {
            c1145f.notifyDataSetChanged();
        }
    }

    @Override // m.InterfaceC1156q
    public final boolean h() {
        return !this.f14528F && this.f14538x.f14884L.isShowing();
    }

    @Override // m.InterfaceC1156q
    public final ListView i() {
        return this.f14538x.f14887s;
    }

    @Override // m.InterfaceC1154o
    public final void j(InterfaceC1153n interfaceC1153n) {
        this.D = interfaceC1153n;
    }

    @Override // m.AbstractC1149j
    public final void n(View view) {
        this.f14525B = view;
    }

    @Override // m.AbstractC1149j
    public final void o(boolean z8) {
        this.f14534t.f14460s = z8;
    }

    @Override // android.widget.PopupWindow.OnDismissListener
    public final void onDismiss() {
        this.f14528F = true;
        this.f14533s.c(true);
        ViewTreeObserver viewTreeObserver = this.f14527E;
        if (viewTreeObserver != null) {
            if (!viewTreeObserver.isAlive()) {
                this.f14527E = this.f14526C.getViewTreeObserver();
            }
            this.f14527E.removeGlobalOnLayoutListener(this.f14539y);
            this.f14527E = null;
        }
        this.f14526C.removeOnAttachStateChangeListener(this.f14540z);
        C1150k c1150k = this.f14524A;
        if (c1150k != null) {
            c1150k.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 // m.AbstractC1149j
    public final void p(int i) {
        this.f14530I = i;
    }

    @Override // m.AbstractC1149j
    public final void q(int i) {
        this.f14538x.f14889u = i;
    }

    @Override // m.AbstractC1149j
    public final void r(PopupWindow.OnDismissListener onDismissListener) {
        this.f14524A = (C1150k) onDismissListener;
    }

    @Override // m.AbstractC1149j
    public final void s(boolean z8) {
        this.f14531J = z8;
    }

    @Override // m.AbstractC1149j
    public final void t(int i) {
        C1229p0 c1229p0 = this.f14538x;
        c1229p0.f14890v = i;
        c1229p0.f14891w = true;
    }

    @Override // m.AbstractC1149j
    public final void l(MenuC1147h menuC1147h) {
    }
}
