package o;

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.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.PopupWindow;
import com.google.android.gms.common.api.Api;
import java.lang.reflect.Method;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* loaded from: classes.dex */
public class UL implements D80 {
    public static final Method L;
    public static final Method M;
    public AdapterView.OnItemClickListener A;
    public AdapterView.OnItemSelectedListener B;
    public final Handler G;
    public Rect I;
    public boolean J;
    public final C0979d8 K;
    public final Context l;
    public ListAdapter m;
    public C0097Dt n;
    public int q;
    public int r;
    public boolean t;
    public boolean u;
    public boolean v;
    public RL y;
    public View z;

    /* renamed from: o, reason: collision with root package name */
    public final int f355o = -2;
    public int p = -2;
    public final int s = 1002;
    public int w = 0;
    public final int x = Api.BaseClientBuilder.API_PRIORITY_OTHER;
    public final QL C = new QL(this, 1);
    public final TL D = new TL(0, this);
    public final SL E = new SL(this);
    public final QL F = new QL(this, 0);
    public final Rect H = new Rect();

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

    public UL(Context context, AttributeSet attributeSet, int i, int i2) {
        int resourceId;
        this.l = context;
        this.G = new Handler(context.getMainLooper());
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, YZ.f425o, i, 0);
        this.q = typedArrayObtainStyledAttributes.getDimensionPixelOffset(0, 0);
        int dimensionPixelOffset = typedArrayObtainStyledAttributes.getDimensionPixelOffset(1, 0);
        this.r = dimensionPixelOffset;
        if (dimensionPixelOffset != 0) {
            this.t = true;
        }
        typedArrayObtainStyledAttributes.recycle();
        C0979d8 c0979d8 = new C0979d8(context, attributeSet, i, 0);
        TypedArray typedArrayObtainStyledAttributes2 = context.obtainStyledAttributes(attributeSet, YZ.s, i, 0);
        if (typedArrayObtainStyledAttributes2.hasValue(2)) {
            c0979d8.setOverlapAnchor(typedArrayObtainStyledAttributes2.getBoolean(2, false));
        }
        c0979d8.setBackgroundDrawable((!typedArrayObtainStyledAttributes2.hasValue(0) || (resourceId = typedArrayObtainStyledAttributes2.getResourceId(0, 0)) == 0) ? typedArrayObtainStyledAttributes2.getDrawable(0) : C80.x(context, resourceId));
        typedArrayObtainStyledAttributes2.recycle();
        this.K = c0979d8;
        c0979d8.setInputMethodMode(1);
    }

    public C0097Dt a(Context context, boolean z) {
        return new C0097Dt(context, z);
    }

    @Override // o.D80
    public final boolean b() {
        return this.K.isShowing();
    }

    public final int c() {
        return this.q;
    }

    @Override // o.D80
    public final void dismiss() {
        C0979d8 c0979d8 = this.K;
        c0979d8.dismiss();
        c0979d8.setContentView(null);
        this.n = null;
        this.G.removeCallbacks(this.C);
    }

    @Override // o.D80
    public final void e() {
        int i;
        int paddingBottom;
        C0097Dt c0097Dt;
        C0097Dt c0097Dt2 = this.n;
        Context context = this.l;
        C0979d8 c0979d8 = this.K;
        if (c0097Dt2 == null) {
            C0097Dt c0097DtA = a(context, !this.J);
            this.n = c0097DtA;
            c0097DtA.setAdapter(this.m);
            this.n.setOnItemClickListener(this.A);
            this.n.setFocusable(true);
            this.n.setFocusableInTouchMode(true);
            this.n.setOnItemSelectedListener(new NL(this));
            this.n.setOnScrollListener(this.E);
            AdapterView.OnItemSelectedListener onItemSelectedListener = this.B;
            if (onItemSelectedListener != null) {
                this.n.setOnItemSelectedListener(onItemSelectedListener);
            }
            c0979d8.setContentView(this.n);
        }
        Drawable background = c0979d8.getBackground();
        Rect rect = this.H;
        if (background != null) {
            background.getPadding(rect);
            int i2 = rect.top;
            i = rect.bottom + i2;
            if (!this.t) {
                this.r = -i2;
            }
        } else {
            rect.setEmpty();
            i = 0;
        }
        int iA = OL.a(c0979d8, this.z, this.r, c0979d8.getInputMethodMode() == 2);
        int i3 = this.f355o;
        if (i3 == -1) {
            paddingBottom = iA + i;
        } else {
            int i4 = this.p;
            int iA2 = this.n.a(i4 != -2 ? i4 != -1 ? View.MeasureSpec.makeMeasureSpec(i4, 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);
            paddingBottom = iA2 + (iA2 > 0 ? this.n.getPaddingBottom() + this.n.getPaddingTop() + i : 0);
        }
        boolean z = c0979d8.getInputMethodMode() == 2;
        c0979d8.setWindowLayoutType(this.s);
        if (c0979d8.isShowing()) {
            if (this.z.isAttachedToWindow()) {
                int width = this.p;
                if (width == -1) {
                    width = -1;
                } else if (width == -2) {
                    width = this.z.getWidth();
                }
                if (i3 == -1) {
                    i3 = z ? paddingBottom : -1;
                    if (z) {
                        c0979d8.setWidth(this.p == -1 ? -1 : 0);
                        c0979d8.setHeight(0);
                    } else {
                        c0979d8.setWidth(this.p == -1 ? -1 : 0);
                        c0979d8.setHeight(-1);
                    }
                } else if (i3 == -2) {
                    i3 = paddingBottom;
                }
                c0979d8.setOutsideTouchable(true);
                int i5 = width;
                View view = this.z;
                int i6 = this.q;
                int i7 = this.r;
                int i8 = i5 < 0 ? -1 : i5;
                if (i3 < 0) {
                    i3 = -1;
                }
                c0979d8.update(view, i6, i7, i8, i3);
                return;
            }
            return;
        }
        int width2 = this.p;
        if (width2 == -1) {
            width2 = -1;
        } else if (width2 == -2) {
            width2 = this.z.getWidth();
        }
        if (i3 == -1) {
            i3 = -1;
        } else if (i3 == -2) {
            i3 = paddingBottom;
        }
        c0979d8.setWidth(width2);
        c0979d8.setHeight(i3);
        if (Build.VERSION.SDK_INT <= 28) {
            Method method = L;
            if (method != null) {
                try {
                    method.invoke(c0979d8, Boolean.TRUE);
                } catch (Exception unused) {
                }
            }
        } else {
            PL.b(c0979d8, true);
        }
        c0979d8.setOutsideTouchable(true);
        c0979d8.setTouchInterceptor(this.D);
        if (this.v) {
            c0979d8.setOverlapAnchor(this.u);
        }
        if (Build.VERSION.SDK_INT <= 28) {
            Method method2 = M;
            if (method2 != null) {
                try {
                    method2.invoke(c0979d8, this.I);
                } catch (Exception unused2) {
                }
            }
        } else {
            PL.a(c0979d8, this.I);
        }
        c0979d8.showAsDropDown(this.z, this.q, this.r, this.w);
        this.n.setSelection(-1);
        if ((!this.J || this.n.isInTouchMode()) && (c0097Dt = this.n) != null) {
            c0097Dt.setListSelectionHidden(true);
            c0097Dt.requestLayout();
        }
        if (this.J) {
            return;
        }
        this.G.post(this.F);
    }

    public final Drawable g() {
        return this.K.getBackground();
    }

    @Override // o.D80
    public final C0097Dt h() {
        return this.n;
    }

    public final void j(Drawable drawable) {
        this.K.setBackgroundDrawable(drawable);
    }

    public final void k(int i) {
        this.r = i;
        this.t = true;
    }

    public final void m(int i) {
        this.q = i;
    }

    public final int o() {
        if (this.t) {
            return this.r;
        }
        return 0;
    }

    public void q(ListAdapter listAdapter) {
        RL rl = this.y;
        if (rl == null) {
            this.y = new RL(this);
        } else {
            ListAdapter listAdapter2 = this.m;
            if (listAdapter2 != null) {
                listAdapter2.unregisterDataSetObserver(rl);
            }
        }
        this.m = listAdapter;
        if (listAdapter != null) {
            listAdapter.registerDataSetObserver(this.y);
        }
        C0097Dt c0097Dt = this.n;
        if (c0097Dt != null) {
            c0097Dt.setAdapter(this.m);
        }
    }

    public final void r(int i) {
        Drawable background = this.K.getBackground();
        if (background == null) {
            this.p = i;
            return;
        }
        Rect rect = this.H;
        background.getPadding(rect);
        this.p = rect.left + rect.right + i;
    }
}
