package l;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import androidx.annotation.AttrRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import java.lang.reflect.Method;

/* JADX INFO: loaded from: classes.dex */
public class h2 implements k.f0 {

    /* JADX INFO: renamed from: f0, reason: collision with root package name */
    public static final Method f29696f0;

    /* JADX INFO: renamed from: g0, reason: collision with root package name */
    public static final Method f29697g0;
    public View Q;
    public int R;
    public e2 S;
    public View T;
    public AdapterView.OnItemClickListener U;
    public AdapterView.OnItemSelectedListener V;
    public final a2 W;
    public final g2 X;
    public final f2 Y;
    public final a2 Z;

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

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public ListAdapter f29700b;

    /* JADX INFO: renamed from: b0, reason: collision with root package name */
    public final Rect f29701b0;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public v1 f29702c;

    /* JADX INFO: renamed from: c0, reason: collision with root package name */
    public Rect f29703c0;

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

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

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

    /* JADX INFO: renamed from: e0, reason: collision with root package name */
    public final h0 f29707e0;

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

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

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

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

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

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

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

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

    static {
        if (Build.VERSION.SDK_INT <= 28) {
            try {
                f29696f0 = PopupWindow.class.getDeclaredMethod("setClipToScreenEnabled", Boolean.TYPE);
            } catch (NoSuchMethodException unused) {
            }
            try {
                f29697g0 = PopupWindow.class.getDeclaredMethod("setEpicenterBounds", Rect.class);
            } catch (NoSuchMethodException unused2) {
            }
        }
    }

    public h2(Context context) {
        this(context, null, d.a.listPopupWindowStyle);
    }

    @Override // k.f0
    public final boolean a() {
        return this.f29707e0.isShowing();
    }

    public final int b() {
        return this.f29708f;
    }

    public final void c(int i10) {
        this.f29708f = i10;
    }

    @Override // k.f0
    public final void dismiss() {
        h0 h0Var = this.f29707e0;
        h0Var.dismiss();
        View view = this.Q;
        if (view != null) {
            ViewParent parent = view.getParent();
            if (parent instanceof ViewGroup) {
                ((ViewGroup) parent).removeView(this.Q);
            }
        }
        h0Var.setContentView(null);
        this.f29702c = null;
        this.f29699a0.removeCallbacks(this.W);
    }

    /* JADX WARN: Type inference fix 'apply assigned field type' failed
    java.lang.UnsupportedOperationException: ArgType.getObject(), call class: class jadx.core.dex.instructions.args.ArgType$PrimitiveArg
    	at jadx.core.dex.instructions.args.ArgType.getObject(ArgType.java:593)
    	at jadx.core.dex.attributes.nodes.ClassTypeVarsAttr.getTypeVarsMapFor(ClassTypeVarsAttr.java:35)
    	at jadx.core.dex.nodes.utils.TypeUtils.replaceClassGenerics(TypeUtils.java:177)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.insertExplicitUseCast(FixTypesVisitor.java:397)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.tryFieldTypeWithNewCasts(FixTypesVisitor.java:359)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.applyFieldType(FixTypesVisitor.java:309)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.visit(FixTypesVisitor.java:94)
     */
    @Override // k.f0
    public final void e() {
        int measuredHeight;
        int i10;
        int i11;
        v1 v1Var;
        View view;
        int i12;
        v1 v1Var2 = this.f29702c;
        h0 h0Var = this.f29707e0;
        Context context = this.f29698a;
        if (v1Var2 == null) {
            v1 v1VarM = m(context, !this.f29705d0);
            this.f29702c = v1VarM;
            v1VarM.setAdapter(this.f29700b);
            this.f29702c.setOnItemClickListener(this.U);
            this.f29702c.setFocusable(true);
            this.f29702c.setFocusableInTouchMode(true);
            this.f29702c.setOnItemSelectedListener(new b2(this, i));
            this.f29702c.setOnScrollListener(this.Y);
            AdapterView.OnItemSelectedListener onItemSelectedListener = this.V;
            if (onItemSelectedListener != null) {
                this.f29702c.setOnItemSelectedListener(onItemSelectedListener);
            }
            v1 v1Var3 = this.f29702c;
            View view2 = this.Q;
            if (view2 != null) {
                LinearLayout linearLayout = new LinearLayout(context);
                linearLayout.setOrientation(1);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(-1, 0, 1.0f);
                int i13 = this.R;
                if (i13 == 0) {
                    linearLayout.addView(view2);
                    linearLayout.addView(v1Var3, layoutParams);
                } else if (i13 == 1) {
                    linearLayout.addView(v1Var3, layoutParams);
                    linearLayout.addView(view2);
                }
                int i14 = this.f29706e;
                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();
                measuredHeight = view2.getMeasuredHeight() + layoutParams2.topMargin + layoutParams2.bottomMargin;
                view = linearLayout;
            } else {
                measuredHeight = 0;
                view = v1Var3;
            }
            h0Var.setContentView(view);
        } else {
            View view3 = this.Q;
            if (view3 != null) {
                LinearLayout.LayoutParams layoutParams3 = (LinearLayout.LayoutParams) view3.getLayoutParams();
                measuredHeight = layoutParams3.bottomMargin + view3.getMeasuredHeight() + layoutParams3.topMargin;
            } else {
                measuredHeight = 0;
            }
        }
        Drawable background = h0Var.getBackground();
        Rect rect = this.f29701b0;
        if (background != null) {
            background.getPadding(rect);
            int i15 = rect.top;
            i10 = rect.bottom + i15;
            if (!this.f29711i) {
                this.f29709g = -i15;
            }
        } else {
            rect.setEmpty();
            i10 = 0;
        }
        int iA = c2.a(h0Var, getAnchorView(), this.f29709g, h0Var.getInputMethodMode() == 2);
        int i16 = this.f29704d;
        if (i16 == -1) {
            i11 = iA + i10;
        } else {
            int i17 = this.f29706e;
            int iA2 = this.f29702c.a(i17 != -2 ? i17 != -1 ? View.MeasureSpec.makeMeasureSpec(i17, 1073741824) : View.MeasureSpec.makeMeasureSpec(context.getResources().getDisplayMetrics().widthPixels - (rect.left + rect.right), 1073741824) : View.MeasureSpec.makeMeasureSpec(context.getResources().getDisplayMetrics().widthPixels - (rect.left + rect.right), Integer.MIN_VALUE), iA - measuredHeight);
            if (iA2 > 0) {
                measuredHeight += this.f29702c.getPaddingBottom() + this.f29702c.getPaddingTop() + i10;
            }
            i11 = iA2 + measuredHeight;
        }
        boolean z10 = this.f29707e0.getInputMethodMode() == 2;
        h0Var.setWindowLayoutType(this.f29710h);
        if (h0Var.isShowing()) {
            if (getAnchorView().isAttachedToWindow()) {
                int width = this.f29706e;
                if (width == -1) {
                    width = -1;
                } else if (width == -2) {
                    width = getAnchorView().getWidth();
                }
                if (i16 == -1) {
                    i16 = z10 ? i11 : -1;
                    if (z10) {
                        h0Var.setWidth(this.f29706e == -1 ? -1 : 0);
                        h0Var.setHeight(0);
                    } else {
                        h0Var.setWidth(this.f29706e == -1 ? -1 : 0);
                        h0Var.setHeight(-1);
                    }
                } else if (i16 == -2) {
                    i16 = i11;
                }
                h0Var.setOutsideTouchable(true);
                View anchorView = getAnchorView();
                int i18 = this.f29708f;
                int i19 = this.f29709g;
                if (width < 0) {
                    width = -1;
                }
                if (i16 < 0) {
                    i16 = -1;
                }
                h0Var.update(anchorView, i18, i19, width, i16);
                return;
            }
            return;
        }
        int width2 = this.f29706e;
        if (width2 == -1) {
            width2 = -1;
        } else if (width2 == -2) {
            width2 = getAnchorView().getWidth();
        }
        if (i16 == -1) {
            i16 = -1;
        } else if (i16 == -2) {
            i16 = i11;
        }
        h0Var.setWidth(width2);
        h0Var.setHeight(i16);
        if (Build.VERSION.SDK_INT <= 28) {
            Method method = f29696f0;
            if (method != null) {
                try {
                    method.invoke(h0Var, Boolean.TRUE);
                } catch (Exception unused) {
                }
            }
        } else {
            d2.b(h0Var, true);
        }
        h0Var.setOutsideTouchable(true);
        h0Var.setTouchInterceptor(this.X);
        if (this.f29713k) {
            h0Var.setOverlapAnchor(this.f29712j);
        }
        if (Build.VERSION.SDK_INT <= 28) {
            Method method2 = f29697g0;
            if (method2 != null) {
                try {
                    method2.invoke(h0Var, this.f29703c0);
                } catch (Exception unused2) {
                }
            }
        } else {
            d2.a(h0Var, this.f29703c0);
        }
        h0Var.showAsDropDown(getAnchorView(), this.f29708f, this.f29709g, this.f29714l);
        this.f29702c.setSelection(-1);
        if ((!this.f29705d0 || this.f29702c.isInTouchMode()) && (v1Var = this.f29702c) != null) {
            v1Var.setListSelectionHidden(true);
            v1Var.requestLayout();
        }
        if (this.f29705d0) {
            return;
        }
        this.f29699a0.post(this.Z);
    }

    @Nullable
    public View getAnchorView() {
        return this.T;
    }

    @Nullable
    public Drawable getBackground() {
        return this.f29707e0.getBackground();
    }

    @Nullable
    public Rect getEpicenterBounds() {
        if (this.f29703c0 != null) {
            return new Rect(this.f29703c0);
        }
        return null;
    }

    @Override // k.f0
    @Nullable
    public ListView getListView() {
        return this.f29702c;
    }

    @Nullable
    public Object getSelectedItem() {
        if (this.f29707e0.isShowing()) {
            return this.f29702c.getSelectedItem();
        }
        return null;
    }

    @Nullable
    public View getSelectedView() {
        if (this.f29707e0.isShowing()) {
            return this.f29702c.getSelectedView();
        }
        return null;
    }

    public final void i(int i10) {
        this.f29709g = i10;
        this.f29711i = true;
    }

    public final int l() {
        if (this.f29711i) {
            return this.f29709g;
        }
        return 0;
    }

    public v1 m(Context context, boolean z10) {
        return new v1(context, z10);
    }

    public final void n(int i10) {
        Drawable background = this.f29707e0.getBackground();
        if (background == null) {
            this.f29706e = i10;
            return;
        }
        Rect rect = this.f29701b0;
        background.getPadding(rect);
        this.f29706e = rect.left + rect.right + i10;
    }

    public void setAdapter(@Nullable ListAdapter listAdapter) {
        e2 e2Var = this.S;
        if (e2Var == null) {
            this.S = new e2(this, 0);
        } else {
            ListAdapter listAdapter2 = this.f29700b;
            if (listAdapter2 != null) {
                listAdapter2.unregisterDataSetObserver(e2Var);
            }
        }
        this.f29700b = listAdapter;
        if (listAdapter != null) {
            listAdapter.registerDataSetObserver(this.S);
        }
        v1 v1Var = this.f29702c;
        if (v1Var != null) {
            v1Var.setAdapter(this.f29700b);
        }
    }

    public void setAnchorView(@Nullable View view) {
        this.T = view;
    }

    public void setBackgroundDrawable(@Nullable Drawable drawable) {
        this.f29707e0.setBackgroundDrawable(drawable);
    }

    public void setEpicenterBounds(@Nullable Rect rect) {
        this.f29703c0 = rect != null ? new Rect(rect) : null;
    }

    public void setOnDismissListener(@Nullable PopupWindow.OnDismissListener onDismissListener) {
        this.f29707e0.setOnDismissListener(onDismissListener);
    }

    public void setOnItemClickListener(@Nullable AdapterView.OnItemClickListener onItemClickListener) {
        this.U = onItemClickListener;
    }

    public void setOnItemSelectedListener(@Nullable AdapterView.OnItemSelectedListener onItemSelectedListener) {
        this.V = onItemSelectedListener;
    }

    public void setPromptView(@Nullable View view) {
        View view2;
        boolean zIsShowing = this.f29707e0.isShowing();
        if (zIsShowing && (view2 = this.Q) != null) {
            ViewParent parent = view2.getParent();
            if (parent instanceof ViewGroup) {
                ((ViewGroup) parent).removeView(this.Q);
            }
        }
        this.Q = view;
        if (zIsShowing) {
            e();
        }
    }

    public h2(@NonNull Context context, @Nullable AttributeSet attributeSet) {
        this(context, attributeSet, d.a.listPopupWindowStyle);
    }

    public h2(@NonNull Context context, @Nullable AttributeSet attributeSet, @AttrRes int i10) {
        this(context, attributeSet, i10, 0);
    }

    public h2(@NonNull Context context, @Nullable AttributeSet attributeSet, @AttrRes int i10, @StyleRes int i11) {
        this.f29704d = -2;
        this.f29706e = -2;
        this.f29710h = 1002;
        this.f29714l = 0;
        this.f29715m = Integer.MAX_VALUE;
        this.R = 0;
        this.W = new a2(this, 2);
        this.X = new g2(this, 0);
        this.Y = new f2(this);
        this.Z = new a2(this, 1);
        this.f29701b0 = new Rect();
        this.f29698a = context;
        this.f29699a0 = new Handler(context.getMainLooper());
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, d.j.ListPopupWindow, i10, i11);
        this.f29708f = typedArrayObtainStyledAttributes.getDimensionPixelOffset(d.j.ListPopupWindow_android_dropDownHorizontalOffset, 0);
        int dimensionPixelOffset = typedArrayObtainStyledAttributes.getDimensionPixelOffset(d.j.ListPopupWindow_android_dropDownVerticalOffset, 0);
        this.f29709g = dimensionPixelOffset;
        if (dimensionPixelOffset != 0) {
            this.f29711i = true;
        }
        typedArrayObtainStyledAttributes.recycle();
        h0 h0Var = new h0(context, attributeSet, i10, i11);
        this.f29707e0 = h0Var;
        h0Var.setInputMethodMode(1);
    }
}
