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 com.google.android.gms.common.api.Api;

/* loaded from: classes.dex */
public class j0 implements o.e {
    private int A;
    private boolean B;
    private boolean C;
    int D;
    private View E;
    private int F;
    private DataSetObserver G;
    private View H;
    private Drawable I;
    private AdapterView.OnItemClickListener J;
    private AdapterView.OnItemSelectedListener K;
    final g L;
    private final f M;
    private final e N;
    private final c O;
    private Runnable P;
    final Handler Q;
    private final Rect R;
    private Rect S;
    private boolean T;
    PopupWindow U;

    /* renamed from: p, reason: collision with root package name */
    private Context f1096p;

    /* renamed from: q, reason: collision with root package name */
    private ListAdapter f1097q;

    /* renamed from: r, reason: collision with root package name */
    g0 f1098r;

    /* renamed from: s, reason: collision with root package name */
    private int f1099s;

    /* renamed from: t, reason: collision with root package name */
    private int f1100t;

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

    /* renamed from: v, reason: collision with root package name */
    private int f1102v;

    /* renamed from: w, reason: collision with root package name */
    private int f1103w;

    /* renamed from: x, reason: collision with root package name */
    private boolean f1104x;

    /* renamed from: y, reason: collision with root package name */
    private boolean f1105y;

    /* renamed from: z, reason: collision with root package name */
    private boolean f1106z;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public class a implements Runnable {
        a() {
        }

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

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public class b implements AdapterView.OnItemSelectedListener {
        b() {
        }

        @Override // android.widget.AdapterView.OnItemSelectedListener
        public void onItemSelected(AdapterView adapterView, View view, int i10, long j10) {
            g0 g0Var;
            if (i10 == -1 || (g0Var = j0.this.f1098r) == null) {
                return;
            }
            g0Var.setListSelectionHidden(false);
        }

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

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public class c implements Runnable {
        c() {
        }

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

    /* loaded from: classes.dex */
    private class d extends DataSetObserver {
        d() {
        }

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

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

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public class e implements AbsListView.OnScrollListener {
        e() {
        }

        @Override // android.widget.AbsListView.OnScrollListener
        public void onScroll(AbsListView absListView, int i10, int i11, int i12) {
        }

        @Override // android.widget.AbsListView.OnScrollListener
        public void onScrollStateChanged(AbsListView absListView, int i10) {
            if (i10 != 1 || j0.this.A() || j0.this.U.getContentView() == null) {
                return;
            }
            j0 j0Var = j0.this;
            j0Var.Q.removeCallbacks(j0Var.L);
            j0.this.L.run();
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public class f implements View.OnTouchListener {
        f() {
        }

        @Override // android.view.View.OnTouchListener
        public boolean onTouch(View view, MotionEvent motionEvent) {
            PopupWindow popupWindow;
            int action = motionEvent.getAction();
            int x10 = (int) motionEvent.getX();
            int y10 = (int) motionEvent.getY();
            if (action == 0 && (popupWindow = j0.this.U) != null && popupWindow.isShowing() && x10 >= 0 && x10 < j0.this.U.getWidth() && y10 >= 0 && y10 < j0.this.U.getHeight()) {
                j0 j0Var = j0.this;
                j0Var.Q.postDelayed(j0Var.L, 250L);
                return false;
            }
            if (action != 1) {
                return false;
            }
            j0 j0Var2 = j0.this;
            j0Var2.Q.removeCallbacks(j0Var2.L);
            return false;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public class g implements Runnable {
        g() {
        }

        @Override // java.lang.Runnable
        public void run() {
            g0 g0Var = j0.this.f1098r;
            if (g0Var == null || !androidx.core.view.s0.P(g0Var) || j0.this.f1098r.getCount() <= j0.this.f1098r.getChildCount()) {
                return;
            }
            int childCount = j0.this.f1098r.getChildCount();
            j0 j0Var = j0.this;
            if (childCount <= j0Var.D) {
                j0Var.U.setInputMethodMode(2);
                j0.this.a();
            }
        }
    }

    public j0(Context context) {
        this(context, null, h.a.D);
    }

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

    private void N(boolean z10) {
        this.U.setIsClippedToScreen(z10);
    }

    private int q() {
        int i10;
        int i11;
        int makeMeasureSpec;
        int i12;
        if (this.f1098r == null) {
            Context context = this.f1096p;
            this.P = new a();
            g0 s10 = s(context, !this.T);
            this.f1098r = s10;
            Drawable drawable = this.I;
            if (drawable != null) {
                s10.setSelector(drawable);
            }
            this.f1098r.setAdapter(this.f1097q);
            this.f1098r.setOnItemClickListener(this.J);
            this.f1098r.setFocusable(true);
            this.f1098r.setFocusableInTouchMode(true);
            this.f1098r.setOnItemSelectedListener(new b());
            this.f1098r.setOnScrollListener(this.N);
            AdapterView.OnItemSelectedListener onItemSelectedListener = this.K;
            if (onItemSelectedListener != null) {
                this.f1098r.setOnItemSelectedListener(onItemSelectedListener);
            }
            View view = this.f1098r;
            View view2 = this.E;
            if (view2 != null) {
                LinearLayout linearLayout = new LinearLayout(context);
                linearLayout.setOrientation(1);
                ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(-1, 0, 1.0f);
                int i13 = this.F;
                if (i13 == 0) {
                    linearLayout.addView(view2);
                    linearLayout.addView(view, layoutParams);
                } else if (i13 != 1) {
                    Log.e("ListPopupWindow", "Invalid hint position " + this.F);
                } else {
                    linearLayout.addView(view, layoutParams);
                    linearLayout.addView(view2);
                }
                int i14 = this.f1100t;
                if (i14 >= 0) {
                    i12 = Integer.MIN_VALUE;
                } else {
                    i14 = 0;
                    i12 = 0;
                }
                view2.measure(View.MeasureSpec.makeMeasureSpec(i14, i12), 0);
                LinearLayout.LayoutParams layoutParams2 = (LinearLayout.LayoutParams) view2.getLayoutParams();
                i10 = view2.getMeasuredHeight() + layoutParams2.topMargin + layoutParams2.bottomMargin;
                view = linearLayout;
            } else {
                i10 = 0;
            }
            this.U.setContentView(view);
        } else {
            View view3 = this.E;
            if (view3 != null) {
                LinearLayout.LayoutParams layoutParams3 = (LinearLayout.LayoutParams) view3.getLayoutParams();
                i10 = view3.getMeasuredHeight() + layoutParams3.topMargin + layoutParams3.bottomMargin;
            } else {
                i10 = 0;
            }
        }
        Drawable background = this.U.getBackground();
        if (background != null) {
            background.getPadding(this.R);
            Rect rect = this.R;
            int i15 = rect.top;
            i11 = rect.bottom + i15;
            if (!this.f1104x) {
                this.f1102v = -i15;
            }
        } else {
            this.R.setEmpty();
            i11 = 0;
        }
        int u10 = u(t(), this.f1102v, this.U.getInputMethodMode() == 2);
        if (this.B || this.f1099s == -1) {
            return u10 + i11;
        }
        int i16 = this.f1100t;
        if (i16 == -2) {
            int i17 = this.f1096p.getResources().getDisplayMetrics().widthPixels;
            Rect rect2 = this.R;
            makeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i17 - (rect2.left + rect2.right), Integer.MIN_VALUE);
        } else if (i16 != -1) {
            makeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i16, 1073741824);
        } else {
            int i18 = this.f1096p.getResources().getDisplayMetrics().widthPixels;
            Rect rect3 = this.R;
            makeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i18 - (rect3.left + rect3.right), 1073741824);
        }
        int d10 = this.f1098r.d(makeMeasureSpec, 0, -1, u10 - i10, -1);
        if (d10 > 0) {
            i10 += i11 + this.f1098r.getPaddingTop() + this.f1098r.getPaddingBottom();
        }
        return d10 + i10;
    }

    private int u(View view, int i10, boolean z10) {
        return this.U.getMaxAvailableHeight(view, i10, z10);
    }

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

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

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

    public void E(int i10) {
        this.U.setAnimationStyle(i10);
    }

    public void F(int i10) {
        Drawable background = this.U.getBackground();
        if (background == null) {
            Q(i10);
            return;
        }
        background.getPadding(this.R);
        Rect rect = this.R;
        this.f1100t = rect.left + rect.right + i10;
    }

    public void G(int i10) {
        this.A = i10;
    }

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

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

    public void J(boolean z10) {
        this.T = z10;
        this.U.setFocusable(z10);
    }

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

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

    public void M(boolean z10) {
        this.f1106z = true;
        this.f1105y = z10;
    }

    public void O(int i10) {
        this.F = i10;
    }

    public void P(int i10) {
        g0 g0Var = this.f1098r;
        if (!c() || g0Var == null) {
            return;
        }
        g0Var.setListSelectionHidden(false);
        g0Var.setSelection(i10);
        if (g0Var.getChoiceMode() != 0) {
            g0Var.setItemChecked(i10, true);
        }
    }

    public void Q(int i10) {
        this.f1100t = i10;
    }

    @Override // o.e
    public void a() {
        int q10 = q();
        boolean A = A();
        androidx.core.widget.i.b(this.U, this.f1103w);
        if (this.U.isShowing()) {
            if (androidx.core.view.s0.P(t())) {
                int i10 = this.f1100t;
                if (i10 == -1) {
                    i10 = -1;
                } else if (i10 == -2) {
                    i10 = t().getWidth();
                }
                int i11 = this.f1099s;
                if (i11 == -1) {
                    if (!A) {
                        q10 = -1;
                    }
                    if (A) {
                        this.U.setWidth(this.f1100t == -1 ? -1 : 0);
                        this.U.setHeight(0);
                    } else {
                        this.U.setWidth(this.f1100t == -1 ? -1 : 0);
                        this.U.setHeight(-1);
                    }
                } else if (i11 != -2) {
                    q10 = i11;
                }
                this.U.setOutsideTouchable((this.C || this.B) ? false : true);
                this.U.update(t(), this.f1101u, this.f1102v, i10 < 0 ? -1 : i10, q10 < 0 ? -1 : q10);
                return;
            }
            return;
        }
        int i12 = this.f1100t;
        if (i12 == -1) {
            i12 = -1;
        } else if (i12 == -2) {
            i12 = t().getWidth();
        }
        int i13 = this.f1099s;
        if (i13 == -1) {
            q10 = -1;
        } else if (i13 != -2) {
            q10 = i13;
        }
        this.U.setWidth(i12);
        this.U.setHeight(q10);
        N(true);
        this.U.setOutsideTouchable((this.C || this.B) ? false : true);
        this.U.setTouchInterceptor(this.M);
        if (this.f1106z) {
            androidx.core.widget.i.a(this.U, this.f1105y);
        }
        this.U.setEpicenterBounds(this.S);
        androidx.core.widget.i.c(this.U, t(), this.f1101u, this.f1102v, this.A);
        this.f1098r.setSelection(-1);
        if (!this.T || this.f1098r.isInTouchMode()) {
            r();
        }
        if (this.T) {
            return;
        }
        this.Q.post(this.O);
    }

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

    @Override // o.e
    public boolean c() {
        return this.U.isShowing();
    }

    public int d() {
        return this.f1101u;
    }

    @Override // o.e
    public void dismiss() {
        this.U.dismiss();
        C();
        this.U.setContentView(null);
        this.f1098r = null;
        this.Q.removeCallbacks(this.L);
    }

    public void f(int i10) {
        this.f1101u = i10;
    }

    public Drawable i() {
        return this.U.getBackground();
    }

    @Override // o.e
    public ListView k() {
        return this.f1098r;
    }

    public void l(int i10) {
        this.f1102v = i10;
        this.f1104x = true;
    }

    public int o() {
        if (this.f1104x) {
            return this.f1102v;
        }
        return 0;
    }

    public void p(ListAdapter listAdapter) {
        DataSetObserver dataSetObserver = this.G;
        if (dataSetObserver == null) {
            this.G = new d();
        } else {
            ListAdapter listAdapter2 = this.f1097q;
            if (listAdapter2 != null) {
                listAdapter2.unregisterDataSetObserver(dataSetObserver);
            }
        }
        this.f1097q = listAdapter;
        if (listAdapter != null) {
            listAdapter.registerDataSetObserver(this.G);
        }
        g0 g0Var = this.f1098r;
        if (g0Var != null) {
            g0Var.setAdapter(this.f1097q);
        }
    }

    public void r() {
        g0 g0Var = this.f1098r;
        if (g0Var != null) {
            g0Var.setListSelectionHidden(true);
            g0Var.requestLayout();
        }
    }

    g0 s(Context context, boolean z10) {
        return new g0(context, z10);
    }

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

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

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

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

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

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

    public j0(Context context, AttributeSet attributeSet, int i10) {
        this(context, attributeSet, i10, 0);
    }

    public j0(Context context, AttributeSet attributeSet, int i10, int i11) {
        this.f1099s = -2;
        this.f1100t = -2;
        this.f1103w = 1002;
        this.A = 0;
        this.B = false;
        this.C = false;
        this.D = Api.BaseClientBuilder.API_PRIORITY_OTHER;
        this.F = 0;
        this.L = new g();
        this.M = new f();
        this.N = new e();
        this.O = new c();
        this.R = new Rect();
        this.f1096p = context;
        this.Q = new Handler(context.getMainLooper());
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, h.j.f20378t1, i10, i11);
        this.f1101u = obtainStyledAttributes.getDimensionPixelOffset(h.j.f20383u1, 0);
        int dimensionPixelOffset = obtainStyledAttributes.getDimensionPixelOffset(h.j.f20388v1, 0);
        this.f1102v = dimensionPixelOffset;
        if (dimensionPixelOffset != 0) {
            this.f1104x = true;
        }
        obtainStyledAttributes.recycle();
        r rVar = new r(context, attributeSet, i10, i11);
        this.U = rVar;
        rVar.setInputMethodMode(1);
    }
}
