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.os.Build;
import android.os.Handler;
import android.util.Log;
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.HeaderViewListAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.martsnew.tv.R;
import h0.V;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
import l6.C1139a;
import n.AbstractC1221l0;
import n.AbstractC1223m0;
import n.C1227o0;
import n.C1229p0;
import n.C1239v;

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: M, reason: collision with root package name */
    public InterfaceC1153n f14445M;

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

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public final ArrayList f14454w = new ArrayList();

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public final ArrayList f14455x = new ArrayList();

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

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

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    public final C1139a f14435A = new C1139a(this);

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

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

    /* JADX INFO: renamed from: K, reason: collision with root package name */
    public boolean f14443K = false;

    public ViewOnKeyListenerC1144e(Context context, View view, int i, boolean z8) {
        this.f14449r = context;
        this.D = view;
        this.f14451t = i;
        this.f14452u = z8;
        Field field = J.f4065a;
        this.f14439F = view.getLayoutDirection() == 1 ? 0 : 1;
        Resources resources = context.getResources();
        this.f14450s = Math.max(resources.getDisplayMetrics().widthPixels / 2, resources.getDimensionPixelSize(R.dimen.abc_config_prefDialogWidth));
        this.f14453v = new Handler();
    }

    @Override // m.InterfaceC1154o
    public final void a(MenuC1147h menuC1147h, boolean z8) {
        ArrayList arrayList = this.f14455x;
        int size = arrayList.size();
        int i = 0;
        while (true) {
            if (i >= size) {
                i = -1;
                break;
            } else if (menuC1147h == ((C1143d) arrayList.get(i)).f14433b) {
                break;
            } else {
                i++;
            }
        }
        if (i < 0) {
            return;
        }
        int i5 = i + 1;
        if (i5 < arrayList.size()) {
            ((C1143d) arrayList.get(i5)).f14433b.c(false);
        }
        C1143d c1143d = (C1143d) arrayList.remove(i);
        CopyOnWriteArrayList<WeakReference> copyOnWriteArrayList = c1143d.f14433b.f14482s;
        for (WeakReference weakReference : copyOnWriteArrayList) {
            InterfaceC1154o interfaceC1154o = (InterfaceC1154o) weakReference.get();
            if (interfaceC1154o == null || interfaceC1154o == this) {
                copyOnWriteArrayList.remove(weakReference);
            }
        }
        boolean z9 = this.f14448P;
        C1229p0 c1229p0 = c1143d.f14432a;
        if (z9) {
            AbstractC1221l0.b(c1229p0.f14884L, null);
            c1229p0.f14884L.setAnimationStyle(0);
        }
        c1229p0.dismiss();
        int size2 = arrayList.size();
        if (size2 > 0) {
            this.f14439F = ((C1143d) arrayList.get(size2 - 1)).f14434c;
        } else {
            View view = this.D;
            Field field = J.f4065a;
            this.f14439F = view.getLayoutDirection() == 1 ? 0 : 1;
        }
        if (size2 != 0) {
            if (z8) {
                ((C1143d) arrayList.get(0)).f14433b.c(false);
                return;
            }
            return;
        }
        dismiss();
        InterfaceC1153n interfaceC1153n = this.f14445M;
        if (interfaceC1153n != null) {
            interfaceC1153n.a(menuC1147h, true);
        }
        ViewTreeObserver viewTreeObserver = this.f14446N;
        if (viewTreeObserver != null) {
            if (viewTreeObserver.isAlive()) {
                this.f14446N.removeGlobalOnLayoutListener(this.f14456y);
            }
            this.f14446N = null;
        }
        this.f14438E.removeOnAttachStateChangeListener(this.f14457z);
        this.f14447O.onDismiss();
    }

    @Override // m.InterfaceC1156q
    public final void c() {
        if (h()) {
            return;
        }
        ArrayList arrayList = this.f14454w;
        int size = arrayList.size();
        int i = 0;
        while (i < size) {
            Object obj = arrayList.get(i);
            i++;
            v((MenuC1147h) obj);
        }
        arrayList.clear();
        View view = this.D;
        this.f14438E = view;
        if (view != null) {
            boolean z8 = this.f14446N == null;
            ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
            this.f14446N = viewTreeObserver;
            if (z8) {
                viewTreeObserver.addOnGlobalLayoutListener(this.f14456y);
            }
            this.f14438E.addOnAttachStateChangeListener(this.f14457z);
        }
    }

    @Override // m.InterfaceC1156q
    public final void dismiss() {
        ArrayList arrayList = this.f14455x;
        int size = arrayList.size();
        if (size > 0) {
            C1143d[] c1143dArr = (C1143d[]) arrayList.toArray(new C1143d[size]);
            for (int i = size - 1; i >= 0; i--) {
                C1143d c1143d = c1143dArr[i];
                if (c1143d.f14432a.f14884L.isShowing()) {
                    c1143d.f14432a.dismiss();
                }
            }
        }
    }

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

    @Override // m.InterfaceC1154o
    public final boolean f(SubMenuC1158s subMenuC1158s) {
        ArrayList arrayList = this.f14455x;
        int size = arrayList.size();
        int i = 0;
        while (i < size) {
            Object obj = arrayList.get(i);
            i++;
            C1143d c1143d = (C1143d) obj;
            if (subMenuC1158s == c1143d.f14433b) {
                c1143d.f14432a.f14887s.requestFocus();
                return true;
            }
        }
        if (!subMenuC1158s.hasVisibleItems()) {
            return false;
        }
        l(subMenuC1158s);
        InterfaceC1153n interfaceC1153n = this.f14445M;
        if (interfaceC1153n != null) {
            interfaceC1153n.d(subMenuC1158s);
        }
        return true;
    }

    @Override // m.InterfaceC1154o
    public final void g() {
        ArrayList arrayList = this.f14455x;
        int size = arrayList.size();
        int i = 0;
        while (i < size) {
            Object obj = arrayList.get(i);
            i++;
            ListAdapter adapter = ((C1143d) obj).f14432a.f14887s.getAdapter();
            if (adapter instanceof HeaderViewListAdapter) {
                adapter = ((HeaderViewListAdapter) adapter).getWrappedAdapter();
            }
            ((C1145f) adapter).notifyDataSetChanged();
        }
    }

    @Override // m.InterfaceC1156q
    public final boolean h() {
        ArrayList arrayList = this.f14455x;
        return arrayList.size() > 0 && ((C1143d) arrayList.get(0)).f14432a.f14884L.isShowing();
    }

    @Override // m.InterfaceC1156q
    public final ListView i() {
        ArrayList arrayList = this.f14455x;
        if (arrayList.isEmpty()) {
            return null;
        }
        return ((C1143d) arrayList.get(arrayList.size() - 1)).f14432a.f14887s;
    }

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

    @Override // m.AbstractC1149j
    public final void l(MenuC1147h menuC1147h) {
        menuC1147h.b(this, this.f14449r);
        if (h()) {
            v(menuC1147h);
        } else {
            this.f14454w.add(menuC1147h);
        }
    }

    @Override // m.AbstractC1149j
    public final void n(View view) {
        if (this.D != view) {
            this.D = view;
            int i = this.f14436B;
            Field field = J.f4065a;
            this.f14437C = Gravity.getAbsoluteGravity(i, view.getLayoutDirection());
        }
    }

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

    @Override // android.widget.PopupWindow.OnDismissListener
    public final void onDismiss() {
        C1143d c1143d;
        ArrayList arrayList = this.f14455x;
        int size = arrayList.size();
        int i = 0;
        while (true) {
            if (i >= size) {
                c1143d = null;
                break;
            }
            c1143d = (C1143d) arrayList.get(i);
            if (!c1143d.f14432a.f14884L.isShowing()) {
                break;
            } else {
                i++;
            }
        }
        if (c1143d != null) {
            c1143d.f14433b.c(false);
        }
    }

    @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) {
        if (this.f14436B != i) {
            this.f14436B = i;
            View view = this.D;
            Field field = J.f4065a;
            this.f14437C = Gravity.getAbsoluteGravity(i, view.getLayoutDirection());
        }
    }

    @Override // m.AbstractC1149j
    public final void q(int i) {
        this.f14440G = true;
        this.f14441I = i;
    }

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

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

    @Override // m.AbstractC1149j
    public final void t(int i) {
        this.H = true;
        this.f14442J = i;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:64:0x0165  */
    /* JADX WARN: Removed duplicated region for block: B:70:0x0173  */
    /* JADX WARN: Removed duplicated region for block: B:71:0x0175  */
    /* JADX WARN: Removed duplicated region for block: B:74:0x017f  */
    /* JADX WARN: Removed duplicated region for block: B:75:0x0184  */
    /* JADX WARN: Removed duplicated region for block: B:81:0x01be  */
    /* JADX WARN: Removed duplicated region for block: B:84:0x01c8  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void v(MenuC1147h menuC1147h) {
        int i;
        char c3;
        View childAt;
        C1143d c1143d;
        int i5;
        int i8;
        int i9;
        MenuItem item;
        C1145f c1145f;
        int headersCount;
        int firstVisiblePosition;
        Context context = this.f14449r;
        LayoutInflater layoutInflaterFrom = LayoutInflater.from(context);
        C1145f c1145f2 = new C1145f(menuC1147h, layoutInflaterFrom, this.f14452u, R.layout.abc_cascading_menu_item_layout);
        int i10 = 1;
        if (!h() && this.f14443K) {
            c1145f2.f14460s = true;
        } else if (h()) {
            c1145f2.f14460s = AbstractC1149j.u(menuC1147h);
        }
        int iM = AbstractC1149j.m(c1145f2, context, this.f14450s);
        C1229p0 c1229p0 = new C1229p0(context, this.f14451t);
        C1239v c1239v = c1229p0.f14884L;
        c1229p0.f14919P = this.f14435A;
        c1229p0.f14877C = this;
        c1239v.setOnDismissListener(this);
        c1229p0.f14876B = this.D;
        c1229p0.f14894z = this.f14437C;
        c1229p0.f14883K = true;
        c1239v.setFocusable(true);
        c1239v.setInputMethodMode(2);
        c1229p0.a(c1145f2);
        Drawable background = c1239v.getBackground();
        if (background != null) {
            Rect rect = c1229p0.f14881I;
            background.getPadding(rect);
            c1229p0.f14888t = rect.left + rect.right + iM;
        } else {
            c1229p0.f14888t = iM;
        }
        c1229p0.f14894z = this.f14437C;
        ArrayList arrayList = this.f14455x;
        if (arrayList.size() > 0) {
            c1143d = (C1143d) arrayList.get(arrayList.size() - 1);
            MenuC1147h menuC1147h2 = c1143d.f14433b;
            int size = menuC1147h2.f14470f.size();
            int i11 = 0;
            while (true) {
                if (i11 >= size) {
                    item = null;
                    break;
                }
                item = menuC1147h2.getItem(i11);
                if (item.hasSubMenu() && menuC1147h == item.getSubMenu()) {
                    break;
                } else {
                    i11++;
                }
            }
            if (item == null) {
                i = 1;
                childAt = null;
                c3 = 0;
            } else {
                C1227o0 c1227o0 = c1143d.f14432a.f14887s;
                ListAdapter adapter = c1227o0.getAdapter();
                if (adapter instanceof HeaderViewListAdapter) {
                    HeaderViewListAdapter headerViewListAdapter = (HeaderViewListAdapter) adapter;
                    headersCount = headerViewListAdapter.getHeadersCount();
                    c1145f = (C1145f) headerViewListAdapter.getWrappedAdapter();
                } else {
                    c1145f = (C1145f) adapter;
                    headersCount = 0;
                }
                int count = c1145f.getCount();
                int i12 = 0;
                c3 = 0;
                while (true) {
                    i = i10;
                    if (i12 >= count) {
                        i12 = -1;
                        break;
                    } else {
                        if (item == c1145f.getItem(i12)) {
                            break;
                        }
                        i12++;
                        i10 = i;
                    }
                }
                childAt = (i12 != -1 && (firstVisiblePosition = (i12 + headersCount) - c1227o0.getFirstVisiblePosition()) >= 0 && firstVisiblePosition < c1227o0.getChildCount()) ? c1227o0.getChildAt(firstVisiblePosition) : null;
            }
        } else {
            i = 1;
            c3 = 0;
            childAt = null;
            c1143d = null;
        }
        if (childAt != null) {
            if (Build.VERSION.SDK_INT <= 28) {
                Method method = C1229p0.f14918Q;
                if (method != null) {
                    try {
                        Object[] objArr = new Object[i];
                        objArr[c3] = Boolean.FALSE;
                        method.invoke(c1239v, objArr);
                    } catch (Exception unused) {
                        Log.i("MenuPopupWindow", "Could not invoke setTouchModal() on PopupWindow. Oh well.");
                    }
                }
            } else {
                AbstractC1223m0.a(c1239v, c3);
            }
            AbstractC1221l0.a(c1239v, null);
            C1227o0 c1227o02 = ((C1143d) arrayList.get(arrayList.size() - 1)).f14432a.f14887s;
            int[] iArr = new int[2];
            c1227o02.getLocationOnScreen(iArr);
            Rect rect2 = new Rect();
            this.f14438E.getWindowVisibleDisplayFrame(rect2);
            if (this.f14439F == 1) {
                i5 = (c1227o02.getWidth() + iArr[0]) + iM > rect2.right ? 0 : 1;
                boolean z8 = i5 != 1;
                this.f14439F = i5;
                if (Build.VERSION.SDK_INT < 26) {
                    c1229p0.f14876B = childAt;
                    i9 = 0;
                    i8 = 0;
                } else {
                    int[] iArr2 = new int[2];
                    this.D.getLocationOnScreen(iArr2);
                    int[] iArr3 = new int[2];
                    childAt.getLocationOnScreen(iArr3);
                    if ((this.f14437C & 7) == 5) {
                        iArr2[0] = this.D.getWidth() + iArr2[0];
                        iArr3[0] = childAt.getWidth() + iArr3[0];
                    }
                    i8 = iArr3[0] - iArr2[0];
                    i9 = iArr3[1] - iArr2[1];
                }
                c1229p0.f14889u = (this.f14437C & 5) != 5 ? z8 ? i8 + iM : i8 - childAt.getWidth() : z8 ? i8 + childAt.getWidth() : i8 - iM;
                c1229p0.f14893y = true;
                c1229p0.f14892x = true;
                c1229p0.f14890v = i9;
                c1229p0.f14891w = true;
            } else {
                if (iArr[0] - iM >= 0) {
                    i5 = 0;
                }
                if (i5 != 1) {
                }
                this.f14439F = i5;
                if (Build.VERSION.SDK_INT < 26) {
                }
                c1229p0.f14889u = (this.f14437C & 5) != 5 ? z8 ? i8 + iM : i8 - childAt.getWidth() : z8 ? i8 + childAt.getWidth() : i8 - iM;
                c1229p0.f14893y = true;
                c1229p0.f14892x = true;
                c1229p0.f14890v = i9;
                c1229p0.f14891w = true;
            }
        } else {
            if (this.f14440G) {
                c1229p0.f14889u = this.f14441I;
            }
            if (this.H) {
                c1229p0.f14890v = this.f14442J;
                c1229p0.f14891w = true;
            }
            Rect rect3 = this.f14512q;
            c1229p0.f14882J = rect3 != null ? new Rect(rect3) : null;
        }
        arrayList.add(new C1143d(c1229p0, menuC1147h, this.f14439F));
        c1229p0.c();
        C1227o0 c1227o03 = c1229p0.f14887s;
        c1227o03.setOnKeyListener(this);
        if (c1143d == null && this.f14444L && menuC1147h.f14475l != null) {
            FrameLayout frameLayout = (FrameLayout) layoutInflaterFrom.inflate(R.layout.abc_popup_menu_header_item_layout, (ViewGroup) c1227o03, false);
            TextView textView = (TextView) frameLayout.findViewById(android.R.id.title);
            frameLayout.setEnabled(false);
            textView.setText(menuC1147h.f14475l);
            c1227o03.addHeaderView(frameLayout, null, false);
            c1229p0.c();
        }
    }
}
