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.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 io.netty.util.internal.shaded.org.jctools.util.Pow2;
import java.lang.reflect.Method;

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private int f423k;
    private int l;
    private boolean m;
    private boolean n;
    private boolean o;
    private int p;
    private boolean q;
    private boolean r;
    int s;
    private View t;
    private int u;
    private DataSetObserver v;
    private View w;
    private Drawable x;
    private AdapterView.OnItemClickListener y;
    private AdapterView.OnItemSelectedListener z;

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

        @Override // android.widget.AdapterView.OnItemSelectedListener
        public void onItemSelected(AdapterView<?> adapterView, View view, int i2, long j2) {
            d0 d0Var;
            if (i2 == -1 || (d0Var = h0.this.f419g) == null) {
                return;
            }
            d0Var.setListSelectionHidden(false);
        }

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

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

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

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

        @Override // android.database.DataSetObserver
        public void onChanged() {
            if (h0.this.i()) {
                h0.this.b();
            }
        }

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

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

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

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

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

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

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

        @Override // java.lang.Runnable
        public void run() {
            d0 d0Var = h0.this.f419g;
            if (d0Var == null || !c.f.l.t.G(d0Var) || h0.this.f419g.getCount() <= h0.this.f419g.getChildCount()) {
                return;
            }
            int childCount = h0.this.f419g.getChildCount();
            h0 h0Var = h0.this;
            if (childCount <= h0Var.s) {
                h0Var.I.setInputMethodMode(2);
                h0.this.b();
            }
        }
    }

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

    public h0(Context context, AttributeSet attributeSet, int i2) {
        this(context, attributeSet, i2, 0);
    }

    private void E(boolean z) {
        Method method = J;
        if (method != null) {
            try {
                method.invoke(this.I, Boolean.valueOf(z));
            } catch (Exception unused) {
                Log.i("ListPopupWindow", "Could not call setClipToScreenEnabled() on PopupWindow. Oh well.");
            }
        }
    }

    private int e() {
        int measuredHeight;
        int i2;
        int iMakeMeasureSpec;
        View view;
        int i3;
        if (this.f419g == null) {
            Context context = this.f417b;
            d0 d0VarG = g(context, !this.H);
            this.f419g = d0VarG;
            Drawable drawable = this.x;
            if (drawable != null) {
                d0VarG.setSelector(drawable);
            }
            this.f419g.setAdapter(this.f418f);
            this.f419g.setOnItemClickListener(this.y);
            this.f419g.setFocusable(true);
            this.f419g.setFocusableInTouchMode(true);
            this.f419g.setOnItemSelectedListener(new a());
            this.f419g.setOnScrollListener(this.C);
            AdapterView.OnItemSelectedListener onItemSelectedListener = this.z;
            if (onItemSelectedListener != null) {
                this.f419g.setOnItemSelectedListener(onItemSelectedListener);
            }
            d0 d0Var = this.f419g;
            View view2 = this.t;
            if (view2 != null) {
                LinearLayout linearLayout = new LinearLayout(context);
                linearLayout.setOrientation(1);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(-1, 0, 1.0f);
                int i4 = this.u;
                if (i4 == 0) {
                    linearLayout.addView(view2);
                    linearLayout.addView(d0Var, layoutParams);
                } else if (i4 != 1) {
                    Log.e("ListPopupWindow", "Invalid hint position " + this.u);
                } else {
                    linearLayout.addView(d0Var, layoutParams);
                    linearLayout.addView(view2);
                }
                int i5 = this.f421i;
                if (i5 >= 0) {
                    i3 = Integer.MIN_VALUE;
                } else {
                    i5 = 0;
                    i3 = 0;
                }
                view2.measure(View.MeasureSpec.makeMeasureSpec(i5, i3), 0);
                LinearLayout.LayoutParams layoutParams2 = (LinearLayout.LayoutParams) view2.getLayoutParams();
                measuredHeight = view2.getMeasuredHeight() + layoutParams2.topMargin + layoutParams2.bottomMargin;
                view = linearLayout;
            } else {
                measuredHeight = 0;
                view = d0Var;
            }
            this.I.setContentView(view);
        } else {
            View view3 = this.t;
            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 i6 = rect.top;
            i2 = rect.bottom + i6;
            if (!this.m) {
                this.f423k = -i6;
            }
        } else {
            this.F.setEmpty();
            i2 = 0;
        }
        int iL = l(h(), this.f423k, this.I.getInputMethodMode() == 2);
        if (this.q || this.f420h == -1) {
            return iL + i2;
        }
        int i7 = this.f421i;
        if (i7 == -2) {
            int i8 = this.f417b.getResources().getDisplayMetrics().widthPixels;
            Rect rect2 = this.F;
            iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i8 - (rect2.left + rect2.right), Integer.MIN_VALUE);
        } else if (i7 != -1) {
            iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i7, Pow2.MAX_POW2);
        } else {
            int i9 = this.f417b.getResources().getDisplayMetrics().widthPixels;
            Rect rect3 = this.F;
            iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i9 - (rect3.left + rect3.right), Pow2.MAX_POW2);
        }
        int iD = this.f419g.d(iMakeMeasureSpec, 0, -1, iL - measuredHeight, -1);
        if (iD > 0) {
            measuredHeight += i2 + this.f419g.getPaddingTop() + this.f419g.getPaddingBottom();
        }
        return iD + measuredHeight;
    }

    private int l(View view, int i2, boolean z) {
        Method method = K;
        if (method != null) {
            try {
                return ((Integer) method.invoke(this.I, view, Integer.valueOf(i2), Boolean.valueOf(z))).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, i2);
    }

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

    public void A(boolean z) {
        this.H = z;
        this.I.setFocusable(z);
    }

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

    public void C(AdapterView.OnItemClickListener onItemClickListener) {
        this.y = onItemClickListener;
    }

    public void D(boolean z) {
        this.o = true;
        this.n = z;
    }

    public void F(int i2) {
        this.u = i2;
    }

    public void G(int i2) {
        d0 d0Var = this.f419g;
        if (!i() || d0Var == null) {
            return;
        }
        d0Var.setListSelectionHidden(false);
        d0Var.setSelection(i2);
        if (d0Var.getChoiceMode() != 0) {
            d0Var.setItemChecked(i2, true);
        }
    }

    public void H(int i2) {
        this.f423k = i2;
        this.m = true;
    }

    public void I(int i2) {
        this.f421i = i2;
    }

    @Override // androidx.appcompat.view.menu.s
    public void b() {
        int iE = e();
        boolean zO = o();
        androidx.core.widget.h.b(this.I, this.l);
        if (this.I.isShowing()) {
            if (c.f.l.t.G(h())) {
                int width = this.f421i;
                if (width == -1) {
                    width = -1;
                } else if (width == -2) {
                    width = h().getWidth();
                }
                int i2 = this.f420h;
                if (i2 == -1) {
                    if (!zO) {
                        iE = -1;
                    }
                    if (zO) {
                        this.I.setWidth(this.f421i == -1 ? -1 : 0);
                        this.I.setHeight(0);
                    } else {
                        this.I.setWidth(this.f421i == -1 ? -1 : 0);
                        this.I.setHeight(-1);
                    }
                } else if (i2 != -2) {
                    iE = i2;
                }
                this.I.setOutsideTouchable((this.r || this.q) ? false : true);
                this.I.update(h(), this.f422j, this.f423k, width < 0 ? -1 : width, iE < 0 ? -1 : iE);
                return;
            }
            return;
        }
        int width2 = this.f421i;
        if (width2 == -1) {
            width2 = -1;
        } else if (width2 == -2) {
            width2 = h().getWidth();
        }
        int i3 = this.f420h;
        if (i3 == -1) {
            iE = -1;
        } else if (i3 != -2) {
            iE = i3;
        }
        this.I.setWidth(width2);
        this.I.setHeight(iE);
        E(true);
        this.I.setOutsideTouchable((this.r || this.q) ? false : true);
        this.I.setTouchInterceptor(this.B);
        if (this.o) {
            androidx.core.widget.h.a(this.I, this.n);
        }
        Method method = L;
        if (method != null) {
            try {
                method.invoke(this.I, this.G);
            } catch (Exception e2) {
                Log.e("ListPopupWindow", "Could not invoke setEpicenterBounds on PopupWindow", e2);
            }
        }
        androidx.core.widget.h.c(this.I, h(), this.f422j, this.f423k, this.p);
        this.f419g.setSelection(-1);
        if (!this.H || this.f419g.isInTouchMode()) {
            f();
        }
        if (this.H) {
            return;
        }
        this.E.post(this.D);
    }

    @Override // androidx.appcompat.view.menu.s
    public ListView d() {
        return this.f419g;
    }

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

    public void f() {
        d0 d0Var = this.f419g;
        if (d0Var != null) {
            d0Var.setListSelectionHidden(true);
            d0Var.requestLayout();
        }
    }

    d0 g(Context context, boolean z) {
        return new d0(context, z);
    }

    public View h() {
        return this.w;
    }

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

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

    public int k() {
        return this.f422j;
    }

    public int m() {
        if (this.m) {
            return this.f423k;
        }
        return 0;
    }

    public int n() {
        return this.f421i;
    }

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

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

    public void r(ListAdapter listAdapter) {
        DataSetObserver dataSetObserver = this.v;
        if (dataSetObserver == null) {
            this.v = new c();
        } else {
            ListAdapter listAdapter2 = this.f418f;
            if (listAdapter2 != null) {
                listAdapter2.unregisterDataSetObserver(dataSetObserver);
            }
        }
        this.f418f = listAdapter;
        if (listAdapter != null) {
            listAdapter.registerDataSetObserver(this.v);
        }
        d0 d0Var = this.f419g;
        if (d0Var != null) {
            d0Var.setAdapter(this.f418f);
        }
    }

    public void s(View view) {
        this.w = view;
    }

    public void t(int i2) {
        this.I.setAnimationStyle(i2);
    }

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

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

    public void w(int i2) {
        this.p = i2;
    }

    public void x(Rect rect) {
        this.G = rect;
    }

    public void y(int i2) {
        this.f422j = i2;
    }

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

    public h0(Context context, AttributeSet attributeSet, int i2, int i3) {
        this.f420h = -2;
        this.f421i = -2;
        this.l = 1002;
        this.p = 0;
        this.q = false;
        this.r = false;
        this.s = Integer.MAX_VALUE;
        this.u = 0;
        this.A = new f();
        this.B = new e();
        this.C = new d();
        this.D = new b();
        this.F = new Rect();
        this.f417b = context;
        this.E = new Handler(context.getMainLooper());
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, c.a.j.ListPopupWindow, i2, i3);
        this.f422j = typedArrayObtainStyledAttributes.getDimensionPixelOffset(c.a.j.ListPopupWindow_android_dropDownHorizontalOffset, 0);
        int dimensionPixelOffset = typedArrayObtainStyledAttributes.getDimensionPixelOffset(c.a.j.ListPopupWindow_android_dropDownVerticalOffset, 0);
        this.f423k = dimensionPixelOffset;
        if (dimensionPixelOffset != 0) {
            this.m = true;
        }
        typedArrayObtainStyledAttributes.recycle();
        q qVar = new q(context, attributeSet, i2, i3);
        this.I = qVar;
        qVar.setInputMethodMode(1);
    }
}
