package androidx.appcompat.widget;

import android.R;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.VisibleForTesting;
import androidx.core.view.x0;
import d.g;
import d.j;
import j.e;
import k.f;
import l.l3;
import l.m;
import l.n0;
import l.o0;
import l.p3;
import l.q0;
import l.r0;
import l.s0;
import l.y;

/* JADX INFO: loaded from: classes.dex */
public class AppCompatSpinner extends Spinner implements x0 {

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public static final int[] f681i = {R.attr.spinnerMode};

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

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

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public SpinnerAdapter f685d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final boolean f686e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final s0 f687f;

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

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

    public AppCompatSpinner(@NonNull Context context) {
        this(context, null);
    }

    public final int a(SpinnerAdapter spinnerAdapter, Drawable drawable) {
        int i10 = 0;
        if (spinnerAdapter == null) {
            return 0;
        }
        int iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(getMeasuredWidth(), 0);
        int iMakeMeasureSpec2 = View.MeasureSpec.makeMeasureSpec(getMeasuredHeight(), 0);
        int iMax = Math.max(0, getSelectedItemPosition());
        int iMin = Math.min(spinnerAdapter.getCount(), iMax + 15);
        View view = null;
        int iMax2 = 0;
        for (int iMax3 = Math.max(0, iMax - (15 - (iMin - iMax))); iMax3 < iMin; iMax3++) {
            int itemViewType = spinnerAdapter.getItemViewType(iMax3);
            if (itemViewType != i10) {
                view = null;
                i10 = itemViewType;
            }
            view = spinnerAdapter.getView(iMax3, view, this);
            if (view.getLayoutParams() == null) {
                view.setLayoutParams(new ViewGroup.LayoutParams(-2, -2));
            }
            view.measure(iMakeMeasureSpec, iMakeMeasureSpec2);
            iMax2 = Math.max(iMax2, view.getMeasuredWidth());
        }
        if (drawable == null) {
            return iMax2;
        }
        Rect rect = this.f689h;
        drawable.getPadding(rect);
        return iMax2 + rect.left + rect.right;
    }

    @Override // android.view.ViewGroup, android.view.View
    public final void drawableStateChanged() {
        super.drawableStateChanged();
        y yVar = this.f682a;
        if (yVar != null) {
            yVar.a();
        }
    }

    @Override // android.widget.Spinner
    public int getDropDownHorizontalOffset() {
        s0 s0Var = this.f687f;
        return s0Var != null ? s0Var.b() : super.getDropDownHorizontalOffset();
    }

    @Override // android.widget.Spinner
    public int getDropDownVerticalOffset() {
        s0 s0Var = this.f687f;
        return s0Var != null ? s0Var.l() : super.getDropDownVerticalOffset();
    }

    @Override // android.widget.Spinner
    public int getDropDownWidth() {
        return this.f687f != null ? this.f688g : super.getDropDownWidth();
    }

    @VisibleForTesting
    public final s0 getInternalPopup() {
        return this.f687f;
    }

    @Override // android.widget.Spinner
    public Drawable getPopupBackground() {
        s0 s0Var = this.f687f;
        return s0Var != null ? s0Var.getBackground() : super.getPopupBackground();
    }

    @Override // android.widget.Spinner
    public Context getPopupContext() {
        return this.f683b;
    }

    @Override // android.widget.Spinner
    public CharSequence getPrompt() {
        s0 s0Var = this.f687f;
        return s0Var != null ? s0Var.d() : super.getPrompt();
    }

    @Override // androidx.core.view.x0
    @Nullable
    @RestrictTo({RestrictTo.Scope.f557c})
    public ColorStateList getSupportBackgroundTintList() {
        y yVar = this.f682a;
        if (yVar != null) {
            return yVar.b();
        }
        return null;
    }

    @Override // androidx.core.view.x0
    @Nullable
    @RestrictTo({RestrictTo.Scope.f557c})
    public PorterDuff.Mode getSupportBackgroundTintMode() {
        y yVar = this.f682a;
        if (yVar != null) {
            return yVar.c();
        }
        return null;
    }

    @Override // android.widget.Spinner, android.widget.AdapterView, android.view.ViewGroup, android.view.View
    public final void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        s0 s0Var = this.f687f;
        if (s0Var == null || !s0Var.a()) {
            return;
        }
        s0Var.dismiss();
    }

    @Override // android.widget.Spinner, android.widget.AbsSpinner, android.view.View
    public final void onMeasure(int i10, int i11) {
        super.onMeasure(i10, i11);
        if (this.f687f == null || View.MeasureSpec.getMode(i10) != Integer.MIN_VALUE) {
            return;
        }
        setMeasuredDimension(Math.min(Math.max(getMeasuredWidth(), a(getAdapter(), getBackground())), View.MeasureSpec.getSize(i10)), getMeasuredHeight());
    }

    @Override // android.widget.Spinner, android.widget.AbsSpinner, android.view.View
    public final void onRestoreInstanceState(Parcelable parcelable) {
        ViewTreeObserver viewTreeObserver;
        r0 r0Var = (r0) parcelable;
        super.onRestoreInstanceState(r0Var.getSuperState());
        if (!r0Var.f29832a || (viewTreeObserver = getViewTreeObserver()) == null) {
            return;
        }
        viewTreeObserver.addOnGlobalLayoutListener(new f(this, 3));
    }

    @Override // android.widget.Spinner, android.widget.AbsSpinner, android.view.View
    public final Parcelable onSaveInstanceState() {
        r0 r0Var = new r0(super.onSaveInstanceState());
        s0 s0Var = this.f687f;
        r0Var.f29832a = s0Var != null && s0Var.a();
        return r0Var;
    }

    @Override // android.widget.Spinner, android.view.View
    public final boolean onTouchEvent(MotionEvent motionEvent) {
        m mVar = this.f684c;
        if (mVar == null || !mVar.onTouch(this, motionEvent)) {
            return super.onTouchEvent(motionEvent);
        }
        return true;
    }

    @Override // android.widget.Spinner, android.view.View
    public final boolean performClick() {
        s0 s0Var = this.f687f;
        if (s0Var == null) {
            return super.performClick();
        }
        if (s0Var.a()) {
            return true;
        }
        this.f687f.k(getTextDirection(), getTextAlignment());
        return true;
    }

    @Override // android.view.View
    public void setBackgroundDrawable(@Nullable Drawable drawable) {
        super.setBackgroundDrawable(drawable);
        y yVar = this.f682a;
        if (yVar != null) {
            yVar.d();
        }
    }

    @Override // android.view.View
    public void setBackgroundResource(@DrawableRes int i10) {
        super.setBackgroundResource(i10);
        y yVar = this.f682a;
        if (yVar != null) {
            yVar.e(i10);
        }
    }

    @Override // android.widget.Spinner
    public void setDropDownHorizontalOffset(int i10) {
        s0 s0Var = this.f687f;
        if (s0Var == null) {
            super.setDropDownHorizontalOffset(i10);
        } else {
            s0Var.j(i10);
            s0Var.c(i10);
        }
    }

    @Override // android.widget.Spinner
    public void setDropDownVerticalOffset(int i10) {
        s0 s0Var = this.f687f;
        if (s0Var != null) {
            s0Var.i(i10);
        } else {
            super.setDropDownVerticalOffset(i10);
        }
    }

    @Override // android.widget.Spinner
    public void setDropDownWidth(int i10) {
        if (this.f687f != null) {
            this.f688g = i10;
        } else {
            super.setDropDownWidth(i10);
        }
    }

    @Override // android.widget.Spinner
    public void setPopupBackgroundDrawable(Drawable drawable) {
        s0 s0Var = this.f687f;
        if (s0Var != null) {
            s0Var.setBackgroundDrawable(drawable);
        } else {
            super.setPopupBackgroundDrawable(drawable);
        }
    }

    @Override // android.widget.Spinner
    public void setPopupBackgroundResource(@DrawableRes int i10) {
        setPopupBackgroundDrawable(f.a.getDrawable(getPopupContext(), i10));
    }

    @Override // android.widget.Spinner
    public void setPrompt(CharSequence charSequence) {
        s0 s0Var = this.f687f;
        if (s0Var != null) {
            s0Var.h(charSequence);
        } else {
            super.setPrompt(charSequence);
        }
    }

    @Override // androidx.core.view.x0
    @RestrictTo({RestrictTo.Scope.f557c})
    public void setSupportBackgroundTintList(@Nullable ColorStateList colorStateList) {
        y yVar = this.f682a;
        if (yVar != null) {
            yVar.g(colorStateList);
        }
    }

    @Override // androidx.core.view.x0
    @RestrictTo({RestrictTo.Scope.f557c})
    public void setSupportBackgroundTintMode(@Nullable PorterDuff.Mode mode) {
        y yVar = this.f682a;
        if (yVar != null) {
            yVar.h(mode);
        }
    }

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

    @Override // android.widget.AdapterView
    public void setAdapter(SpinnerAdapter spinnerAdapter) {
        if (!this.f686e) {
            this.f685d = spinnerAdapter;
            return;
        }
        super.setAdapter(spinnerAdapter);
        s0 s0Var = this.f687f;
        if (s0Var != null) {
            Context context = this.f683b;
            if (context == null) {
                context = getContext();
            }
            s0Var.setAdapter(new o0(spinnerAdapter, context.getTheme()));
        }
    }

    public AppCompatSpinner(@NonNull Context context, @Nullable AttributeSet attributeSet, int i10) {
        this(context, attributeSet, i10, -1);
    }

    public AppCompatSpinner(@NonNull Context context, @Nullable AttributeSet attributeSet, int i10, int i11) {
        this(context, attributeSet, i10, i11, null);
    }

    /* JADX WARN: Removed duplicated region for block: B:29:0x006e  */
    /* JADX WARN: Removed duplicated region for block: B:32:0x00a9  */
    /* JADX WARN: Removed duplicated region for block: B:35:0x00c0  */
    /* JADX WARN: Removed duplicated region for block: B:38:0x00d9  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public AppCompatSpinner(@NonNull Context context, @Nullable AttributeSet attributeSet, int i10, int i11, Resources.Theme theme) throws Throwable {
        TypedArray typedArrayObtainStyledAttributes;
        CharSequence[] textArray;
        SpinnerAdapter spinnerAdapter;
        super(context, attributeSet, i10);
        this.f689h = new Rect();
        l3.a(getContext(), this);
        TypedArray typedArrayObtainStyledAttributes2 = context.obtainStyledAttributes(attributeSet, j.Spinner, i10, 0);
        p3 p3Var = new p3(context, typedArrayObtainStyledAttributes2);
        this.f682a = new y(this);
        if (theme != null) {
            this.f683b = new e(context, theme);
        } else {
            int resourceId = typedArrayObtainStyledAttributes2.getResourceId(j.Spinner_popupTheme, 0);
            if (resourceId != 0) {
                this.f683b = new e(context, resourceId);
            } else {
                this.f683b = context;
            }
        }
        TypedArray typedArray = null;
        if (i11 == -1) {
            try {
                typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, f681i, i10, 0);
            } catch (Exception unused) {
                typedArrayObtainStyledAttributes = null;
            } catch (Throwable th) {
                th = th;
            }
            try {
                if (typedArrayObtainStyledAttributes.hasValue(0)) {
                    i11 = typedArrayObtainStyledAttributes.getInt(0, 0);
                }
            } catch (Exception unused2) {
                if (typedArrayObtainStyledAttributes != null) {
                }
                int i12 = 1;
                TypedArray typedArray2 = p3Var.f29809b;
                if (i11 != 0) {
                }
                textArray = typedArray2.getTextArray(j.Spinner_android_entries);
                if (textArray != null) {
                }
                p3Var.e();
                this.f686e = true;
                spinnerAdapter = this.f685d;
                if (spinnerAdapter != null) {
                }
                this.f682a.loadFromAttributes(attributeSet, i10);
            } catch (Throwable th2) {
                th = th2;
                typedArray = typedArrayObtainStyledAttributes;
                if (typedArray != null) {
                    typedArray.recycle();
                }
                throw th;
            }
            typedArrayObtainStyledAttributes.recycle();
        }
        int i122 = 1;
        TypedArray typedArray22 = p3Var.f29809b;
        if (i11 != 0) {
            n0 n0Var = new n0(this);
            this.f687f = n0Var;
            n0Var.f29774c = typedArray22.getString(j.Spinner_android_prompt);
        } else if (i11 == 1) {
            q0 q0Var = new q0(this, this.f683b, attributeSet, i10);
            p3 p3VarD = p3.d(this.f683b, attributeSet, j.Spinner, i10);
            this.f688g = p3VarD.f29809b.getLayoutDimension(j.Spinner_android_dropDownWidth, -2);
            q0Var.setBackgroundDrawable(p3VarD.b(j.Spinner_android_popupBackground));
            q0Var.f29811h0 = typedArray22.getString(j.Spinner_android_prompt);
            p3VarD.e();
            this.f687f = q0Var;
            this.f684c = new m(this, this, q0Var, i122);
        }
        textArray = typedArray22.getTextArray(j.Spinner_android_entries);
        if (textArray != null) {
            ArrayAdapter arrayAdapter = new ArrayAdapter(context, R.layout.simple_spinner_item, textArray);
            arrayAdapter.setDropDownViewResource(g.support_simple_spinner_dropdown_item);
            setAdapter((SpinnerAdapter) arrayAdapter);
        }
        p3Var.e();
        this.f686e = true;
        spinnerAdapter = this.f685d;
        if (spinnerAdapter != null) {
            setAdapter(spinnerAdapter);
            this.f685d = null;
        }
        this.f682a.loadFromAttributes(attributeSet, i10);
    }
}
