package androidx.appcompat.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.database.DataSetObserver;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import java.lang.reflect.Method;

/* JADX INFO: compiled from: ListPopupWindow.java */
/* JADX INFO: loaded from: classes.dex */
public class n0 implements androidx.appcompat.view.menu.p {
    private static Method J;
    private static Method K;
    private static Method L;
    final i A;
    private final h B;
    private final g C;
    private final e D;
    final Handler E;
    private final Rect F;
    private Rect G;
    private boolean H;
    PopupWindow I;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private Context f1015e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private ListAdapter f1016f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    j0 f1017g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private int f1018h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private int f1019i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private int f1020j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private int f1021k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private int f1022l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private boolean f1023m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    private boolean f1024n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    private boolean f1025o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    private int f1026p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    private boolean f1027q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    private boolean f1028r;

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

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    private View f1030t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    private int f1031u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    private DataSetObserver f1032v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    private View f1033w;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    private Drawable f1034x;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    private AdapterView.OnItemClickListener f1035y;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    private AdapterView.OnItemSelectedListener f1036z;

    /* JADX INFO: compiled from: ListPopupWindow.java */
    class a implements Runnable {
        a() {
        }

        @Override // java.lang.Runnable
        public void run() {
            View viewT = n0.this.t();
            if (viewT == null || viewT.getWindowToken() == null) {
                return;
            }
            n0.this.a();
        }
    }

    /* JADX INFO: compiled from: ListPopupWindow.java */
    class b implements AdapterView.OnItemSelectedListener {
        b() {
        }

        @Override // android.widget.AdapterView.OnItemSelectedListener
        public void onItemSelected(AdapterView<?> adapterView, View view, int i3, long j3) {
            j0 j0Var;
            if (i3 == -1 || (j0Var = n0.this.f1017g) == null) {
                return;
            }
            j0Var.setListSelectionHidden(false);
        }

        @Override // android.widget.AdapterView.OnItemSelectedListener
        public void onNothingSelected(AdapterView<?> adapterView) {
        }
    }

    /* JADX INFO: compiled from: ListPopupWindow.java */
    static class c {
        static int a(PopupWindow popupWindow, View view, int i3, boolean z2) {
            return popupWindow.getMaxAvailableHeight(view, i3, z2);
        }
    }

    /* JADX INFO: compiled from: ListPopupWindow.java */
    static class d {
        static void a(PopupWindow popupWindow, Rect rect) {
            popupWindow.setEpicenterBounds(rect);
        }

        static void b(PopupWindow popupWindow, boolean z2) {
            popupWindow.setIsClippedToScreen(z2);
        }
    }

    /* JADX INFO: compiled from: ListPopupWindow.java */
    private class e implements Runnable {
        e() {
        }

        @Override // java.lang.Runnable
        public void run() {
            n0.this.r();
        }
    }

    /* JADX INFO: compiled from: ListPopupWindow.java */
    private class f extends DataSetObserver {
        f() {
        }

        @Override // android.database.DataSetObserver
        public void onChanged() {
            if (n0.this.c()) {
                n0.this.a();
            }
        }

        @Override // android.database.DataSetObserver
        public void onInvalidated() {
            n0.this.dismiss();
        }
    }

    /* JADX INFO: compiled from: ListPopupWindow.java */
    private class g implements AbsListView.OnScrollListener {
        g() {
        }

        @Override // android.widget.AbsListView.OnScrollListener
        public void onScroll(AbsListView absListView, int i3, int i4, int i5) {
        }

        @Override // android.widget.AbsListView.OnScrollListener
        public void onScrollStateChanged(AbsListView absListView, int i3) {
            if (i3 != 1 || n0.this.A() || n0.this.I.getContentView() == null) {
                return;
            }
            n0 n0Var = n0.this;
            n0Var.E.removeCallbacks(n0Var.A);
            n0.this.A.run();
        }
    }

    /* JADX INFO: compiled from: ListPopupWindow.java */
    private class h implements View.OnTouchListener {
        h() {
        }

        @Override // android.view.View.OnTouchListener
        public boolean onTouch(View view, MotionEvent motionEvent) {
            PopupWindow popupWindow;
            int action = motionEvent.getAction();
            int x2 = (int) motionEvent.getX();
            int y2 = (int) motionEvent.getY();
            if (action == 0 && (popupWindow = n0.this.I) != null && popupWindow.isShowing() && x2 >= 0 && x2 < n0.this.I.getWidth() && y2 >= 0 && y2 < n0.this.I.getHeight()) {
                n0 n0Var = n0.this;
                n0Var.E.postDelayed(n0Var.A, 250L);
                return false;
            }
            if (action != 1) {
                return false;
            }
            n0 n0Var2 = n0.this;
            n0Var2.E.removeCallbacks(n0Var2.A);
            return false;
        }
    }

    /* JADX INFO: compiled from: ListPopupWindow.java */
    private class i implements Runnable {
        i() {
        }

        @Override // java.lang.Runnable
        public void run() {
            j0 j0Var = n0.this.f1017g;
            if (j0Var == null || !androidx.core.view.y.U(j0Var) || n0.this.f1017g.getCount() <= n0.this.f1017g.getChildCount()) {
                return;
            }
            int childCount = n0.this.f1017g.getChildCount();
            n0 n0Var = n0.this;
            if (childCount <= n0Var.f1029s) {
                n0Var.I.setInputMethodMode(2);
                n0.this.a();
            }
        }
    }

    static {
        if (Build.VERSION.SDK_INT <= 28) {
            try {
                J = PopupWindow.class.getDeclaredMethod("setClipToScreenEnabled", Boolean.TYPE);
            } catch (NoSuchMethodException unused) {
                Log.i("ListPopupWindow", "Could not find method setClipToScreenEnabled() on PopupWindow. Oh well.");
            }
            try {
                L = PopupWindow.class.getDeclaredMethod("setEpicenterBounds", Rect.class);
            } catch (NoSuchMethodException unused2) {
                Log.i("ListPopupWindow", "Could not find method setEpicenterBounds(Rect) on PopupWindow. Oh well.");
            }
        }
        if (Build.VERSION.SDK_INT <= 23) {
            try {
                K = PopupWindow.class.getDeclaredMethod("getMaxAvailableHeight", View.class, Integer.TYPE, Boolean.TYPE);
            } catch (NoSuchMethodException unused3) {
                Log.i("ListPopupWindow", "Could not find method getMaxAvailableHeight(View, int, boolean) on PopupWindow. Oh well.");
            }
        }
    }

    public n0(Context context) {
        this(context, null, c.a.E);
    }

    private void C() {
        View view = this.f1030t;
        if (view != null) {
            ViewParent parent = view.getParent();
            if (parent instanceof ViewGroup) {
                ((ViewGroup) parent).removeView(this.f1030t);
            }
        }
    }

    private void N(boolean z2) {
        if (Build.VERSION.SDK_INT > 28) {
            d.b(this.I, z2);
            return;
        }
        Method method = J;
        if (method != null) {
            try {
                method.invoke(this.I, Boolean.valueOf(z2));
            } catch (Exception unused) {
                Log.i("ListPopupWindow", "Could not call setClipToScreenEnabled() on PopupWindow. Oh well.");
            }
        }
    }

    private int q() {
        int measuredHeight;
        int i3;
        int iMakeMeasureSpec;
        View view;
        int i4;
        if (this.f1017g == null) {
            Context context = this.f1015e;
            new a();
            j0 j0VarS = s(context, !this.H);
            this.f1017g = j0VarS;
            Drawable drawable = this.f1034x;
            if (drawable != null) {
                j0VarS.setSelector(drawable);
            }
            this.f1017g.setAdapter(this.f1016f);
            this.f1017g.setOnItemClickListener(this.f1035y);
            this.f1017g.setFocusable(true);
            this.f1017g.setFocusableInTouchMode(true);
            this.f1017g.setOnItemSelectedListener(new b());
            this.f1017g.setOnScrollListener(this.C);
            AdapterView.OnItemSelectedListener onItemSelectedListener = this.f1036z;
            if (onItemSelectedListener != null) {
                this.f1017g.setOnItemSelectedListener(onItemSelectedListener);
            }
            j0 j0Var = this.f1017g;
            View view2 = this.f1030t;
            if (view2 != null) {
                LinearLayout linearLayout = new LinearLayout(context);
                linearLayout.setOrientation(1);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(-1, 0, 1.0f);
                int i5 = this.f1031u;
                if (i5 == 0) {
                    linearLayout.addView(view2);
                    linearLayout.addView(j0Var, layoutParams);
                } else if (i5 != 1) {
                    Log.e("ListPopupWindow", "Invalid hint position " + this.f1031u);
                } else {
                    linearLayout.addView(j0Var, layoutParams);
                    linearLayout.addView(view2);
                }
                int i6 = this.f1019i;
                if (i6 >= 0) {
                    i4 = Integer.MIN_VALUE;
                } else {
                    i6 = 0;
                    i4 = 0;
                }
                view2.measure(View.MeasureSpec.makeMeasureSpec(i6, i4), 0);
                LinearLayout.LayoutParams layoutParams2 = (LinearLayout.LayoutParams) view2.getLayoutParams();
                measuredHeight = view2.getMeasuredHeight() + layoutParams2.topMargin + layoutParams2.bottomMargin;
                view = linearLayout;
            } else {
                measuredHeight = 0;
                view = j0Var;
            }
            this.I.setContentView(view);
        } else {
            View view3 = this.f1030t;
            if (view3 != null) {
                LinearLayout.LayoutParams layoutParams3 = (LinearLayout.LayoutParams) view3.getLayoutParams();
                measuredHeight = view3.getMeasuredHeight() + layoutParams3.topMargin + layoutParams3.bottomMargin;
            } else {
                measuredHeight = 0;
            }
        }
        Drawable background = this.I.getBackground();
        if (background != null) {
            background.getPadding(this.F);
            Rect rect = this.F;
            int i7 = rect.top;
            i3 = rect.bottom + i7;
            if (!this.f1023m) {
                this.f1021k = -i7;
            }
        } else {
            this.F.setEmpty();
            i3 = 0;
        }
        int iU = u(t(), this.f1021k, this.I.getInputMethodMode() == 2);
        if (this.f1027q || this.f1018h == -1) {
            return iU + i3;
        }
        int i8 = this.f1019i;
        if (i8 == -2) {
            int i9 = this.f1015e.getResources().getDisplayMetrics().widthPixels;
            Rect rect2 = this.F;
            iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i9 - (rect2.left + rect2.right), Integer.MIN_VALUE);
        } else if (i8 != -1) {
            iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i8, 1073741824);
        } else {
            int i10 = this.f1015e.getResources().getDisplayMetrics().widthPixels;
            Rect rect3 = this.F;
            iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i10 - (rect3.left + rect3.right), 1073741824);
        }
        int iD = this.f1017g.d(iMakeMeasureSpec, 0, -1, iU - measuredHeight, -1);
        if (iD > 0) {
            measuredHeight += i3 + this.f1017g.getPaddingTop() + this.f1017g.getPaddingBottom();
        }
        return iD + measuredHeight;
    }

    private int u(View view, int i3, boolean z2) {
        if (Build.VERSION.SDK_INT > 23) {
            return c.a(this.I, view, i3, z2);
        }
        Method method = K;
        if (method != null) {
            try {
                return ((Integer) method.invoke(this.I, view, Integer.valueOf(i3), Boolean.valueOf(z2))).intValue();
            } catch (Exception unused) {
                Log.i("ListPopupWindow", "Could not call getMaxAvailableHeightMethod(View, int, boolean) on PopupWindow. Using the public version.");
            }
        }
        return this.I.getMaxAvailableHeight(view, i3);
    }

    public boolean A() {
        return this.I.getInputMethodMode() == 2;
    }

    public boolean B() {
        return this.H;
    }

    public void D(View view) {
        this.f1033w = view;
    }

    public void E(int i3) {
        this.I.setAnimationStyle(i3);
    }

    public void F(int i3) {
        Drawable background = this.I.getBackground();
        if (background == null) {
            Q(i3);
            return;
        }
        background.getPadding(this.F);
        Rect rect = this.F;
        this.f1019i = rect.left + rect.right + i3;
    }

    public void G(int i3) {
        this.f1026p = i3;
    }

    public void H(Rect rect) {
        this.G = rect != null ? new Rect(rect) : null;
    }

    public void I(int i3) {
        this.I.setInputMethodMode(i3);
    }

    public void J(boolean z2) {
        this.H = z2;
        this.I.setFocusable(z2);
    }

    public void K(PopupWindow.OnDismissListener onDismissListener) {
        this.I.setOnDismissListener(onDismissListener);
    }

    public void L(AdapterView.OnItemClickListener onItemClickListener) {
        this.f1035y = onItemClickListener;
    }

    public void M(boolean z2) {
        this.f1025o = true;
        this.f1024n = z2;
    }

    public void O(int i3) {
        this.f1031u = i3;
    }

    public void P(int i3) {
        j0 j0Var = this.f1017g;
        if (!c() || j0Var == null) {
            return;
        }
        j0Var.setListSelectionHidden(false);
        j0Var.setSelection(i3);
        if (j0Var.getChoiceMode() != 0) {
            j0Var.setItemChecked(i3, true);
        }
    }

    public void Q(int i3) {
        this.f1019i = i3;
    }

    @Override // androidx.appcompat.view.menu.p
    public void a() {
        int iQ = q();
        boolean zA = A();
        androidx.core.widget.i.b(this.I, this.f1022l);
        if (this.I.isShowing()) {
            if (androidx.core.view.y.U(t())) {
                int width = this.f1019i;
                if (width == -1) {
                    width = -1;
                } else if (width == -2) {
                    width = t().getWidth();
                }
                int i3 = this.f1018h;
                if (i3 == -1) {
                    if (!zA) {
                        iQ = -1;
                    }
                    if (zA) {
                        this.I.setWidth(this.f1019i == -1 ? -1 : 0);
                        this.I.setHeight(0);
                    } else {
                        this.I.setWidth(this.f1019i == -1 ? -1 : 0);
                        this.I.setHeight(-1);
                    }
                } else if (i3 != -2) {
                    iQ = i3;
                }
                this.I.setOutsideTouchable((this.f1028r || this.f1027q) ? false : true);
                this.I.update(t(), this.f1020j, this.f1021k, width < 0 ? -1 : width, iQ < 0 ? -1 : iQ);
                return;
            }
            return;
        }
        int width2 = this.f1019i;
        if (width2 == -1) {
            width2 = -1;
        } else if (width2 == -2) {
            width2 = t().getWidth();
        }
        int i4 = this.f1018h;
        if (i4 == -1) {
            iQ = -1;
        } else if (i4 != -2) {
            iQ = i4;
        }
        this.I.setWidth(width2);
        this.I.setHeight(iQ);
        N(true);
        this.I.setOutsideTouchable((this.f1028r || this.f1027q) ? false : true);
        this.I.setTouchInterceptor(this.B);
        if (this.f1025o) {
            androidx.core.widget.i.a(this.I, this.f1024n);
        }
        if (Build.VERSION.SDK_INT <= 28) {
            Method method = L;
            if (method != null) {
                try {
                    method.invoke(this.I, this.G);
                } catch (Exception e3) {
                    Log.e("ListPopupWindow", "Could not invoke setEpicenterBounds on PopupWindow", e3);
                }
            }
        } else {
            d.a(this.I, this.G);
        }
        androidx.core.widget.i.c(this.I, t(), this.f1020j, this.f1021k, this.f1026p);
        this.f1017g.setSelection(-1);
        if (!this.H || this.f1017g.isInTouchMode()) {
            r();
        }
        if (this.H) {
            return;
        }
        this.E.post(this.D);
    }

    public void b(Drawable drawable) {
        this.I.setBackgroundDrawable(drawable);
    }

    @Override // androidx.appcompat.view.menu.p
    public boolean c() {
        return this.I.isShowing();
    }

    public void d(int i3) {
        this.f1020j = i3;
    }

    @Override // androidx.appcompat.view.menu.p
    public void dismiss() {
        this.I.dismiss();
        C();
        this.I.setContentView(null);
        this.f1017g = null;
        this.E.removeCallbacks(this.A);
    }

    public int e() {
        return this.f1020j;
    }

    public int h() {
        if (this.f1023m) {
            return this.f1021k;
        }
        return 0;
    }

    public Drawable j() {
        return this.I.getBackground();
    }

    @Override // androidx.appcompat.view.menu.p
    public ListView l() {
        return this.f1017g;
    }

    public void n(int i3) {
        this.f1021k = i3;
        this.f1023m = true;
    }

    public void o(ListAdapter listAdapter) {
        DataSetObserver dataSetObserver = this.f1032v;
        if (dataSetObserver == null) {
            this.f1032v = new f();
        } else {
            ListAdapter listAdapter2 = this.f1016f;
            if (listAdapter2 != null) {
                listAdapter2.unregisterDataSetObserver(dataSetObserver);
            }
        }
        this.f1016f = listAdapter;
        if (listAdapter != null) {
            listAdapter.registerDataSetObserver(this.f1032v);
        }
        j0 j0Var = this.f1017g;
        if (j0Var != null) {
            j0Var.setAdapter(this.f1016f);
        }
    }

    public void r() {
        j0 j0Var = this.f1017g;
        if (j0Var != null) {
            j0Var.setListSelectionHidden(true);
            j0Var.requestLayout();
        }
    }

    j0 s(Context context, boolean z2) {
        return new j0(context, z2);
    }

    public View t() {
        return this.f1033w;
    }

    public Object v() {
        if (c()) {
            return this.f1017g.getSelectedItem();
        }
        return null;
    }

    public long w() {
        if (c()) {
            return this.f1017g.getSelectedItemId();
        }
        return Long.MIN_VALUE;
    }

    public int x() {
        if (c()) {
            return this.f1017g.getSelectedItemPosition();
        }
        return -1;
    }

    public View y() {
        if (c()) {
            return this.f1017g.getSelectedView();
        }
        return null;
    }

    public int z() {
        return this.f1019i;
    }

    public n0(Context context, AttributeSet attributeSet, int i3) {
        this(context, attributeSet, i3, 0);
    }

    public n0(Context context, AttributeSet attributeSet, int i3, int i4) {
        this.f1018h = -2;
        this.f1019i = -2;
        this.f1022l = 1002;
        this.f1026p = 0;
        this.f1027q = false;
        this.f1028r = false;
        this.f1029s = Integer.MAX_VALUE;
        this.f1031u = 0;
        this.A = new i();
        this.B = new h();
        this.C = new g();
        this.D = new e();
        this.F = new Rect();
        this.f1015e = context;
        this.E = new Handler(context.getMainLooper());
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, c.j.f3314g1, i3, i4);
        this.f1020j = typedArrayObtainStyledAttributes.getDimensionPixelOffset(c.j.f3318h1, 0);
        int dimensionPixelOffset = typedArrayObtainStyledAttributes.getDimensionPixelOffset(c.j.f3322i1, 0);
        this.f1021k = dimensionPixelOffset;
        if (dimensionPixelOffset != 0) {
            this.f1023m = true;
        }
        typedArrayObtainStyledAttributes.recycle();
        t tVar = new t(context, attributeSet, i3, i4);
        this.I = tVar;
        tVar.setInputMethodMode(1);
    }
}
