package l;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.RectF;
import android.os.Build;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.method.TransformationMethod;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.media3.exoplayer.source.ProgressiveMediaSource;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;

/* JADX INFO: loaded from: classes.dex */
public final class j1 {

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public static final RectF f29733l = new RectF();

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public static final ConcurrentHashMap f29734m = new ConcurrentHashMap();

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public int f29735a = 0;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public boolean f29736b = false;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public float f29737c = -1.0f;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public float f29738d = -1.0f;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public float f29739e = -1.0f;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public int[] f29740f = new int[0];

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public boolean f29741g = false;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public TextPaint f29742h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final TextView f29743i;

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final g1 f29745k;

    public j1(TextView textView) {
        this.f29743i = textView;
        this.f29744j = textView.getContext();
        if (Build.VERSION.SDK_INT >= 29) {
            this.f29745k = new h1();
        } else {
            this.f29745k = new g1();
        }
    }

    public static int[] b(int[] iArr) {
        int length = iArr.length;
        if (length == 0) {
            return iArr;
        }
        Arrays.sort(iArr);
        ArrayList arrayList = new ArrayList();
        for (int i10 : iArr) {
            if (i10 > 0 && Collections.binarySearch(arrayList, Integer.valueOf(i10)) < 0) {
                arrayList.add(Integer.valueOf(i10));
            }
        }
        if (length == arrayList.size()) {
            return iArr;
        }
        int size = arrayList.size();
        int[] iArr2 = new int[size];
        for (int i11 = 0; i11 < size; i11++) {
            iArr2[i11] = ((Integer) arrayList.get(i11)).intValue();
        }
        return iArr2;
    }

    public static Object d(Object obj, Object obj2, String str) {
        try {
            return getTextViewMethod(str).invoke(obj, new Object[0]);
        } catch (Exception unused) {
            return obj2;
        }
    }

    @Nullable
    private static Method getTextViewMethod(@NonNull String str) {
        try {
            ConcurrentHashMap concurrentHashMap = f29734m;
            Method declaredMethod = (Method) concurrentHashMap.get(str);
            if (declaredMethod == null && (declaredMethod = TextView.class.getDeclaredMethod(str, new Class[0])) != null) {
                declaredMethod.setAccessible(true);
                concurrentHashMap.put(str, declaredMethod);
            }
            return declaredMethod;
        } catch (Exception unused) {
            return null;
        }
    }

    public final void a() {
        if (e()) {
            if (this.f29736b) {
                if (this.f29743i.getMeasuredHeight() <= 0 || this.f29743i.getMeasuredWidth() <= 0) {
                    return;
                }
                int measuredWidth = this.f29745k.b(this.f29743i) ? ProgressiveMediaSource.DEFAULT_LOADING_CHECK_INTERVAL_BYTES : (this.f29743i.getMeasuredWidth() - this.f29743i.getTotalPaddingLeft()) - this.f29743i.getTotalPaddingRight();
                int height = (this.f29743i.getHeight() - this.f29743i.getCompoundPaddingBottom()) - this.f29743i.getCompoundPaddingTop();
                if (measuredWidth <= 0 || height <= 0) {
                    return;
                }
                RectF rectF = f29733l;
                synchronized (rectF) {
                    try {
                        rectF.setEmpty();
                        rectF.right = measuredWidth;
                        rectF.bottom = height;
                        float fC = c(rectF);
                        if (fC != this.f29743i.getTextSize()) {
                            f(fC, 0);
                        }
                    } finally {
                    }
                }
            }
            this.f29736b = true;
        }
    }

    public final int c(RectF rectF) {
        CharSequence transformation;
        int length = this.f29740f.length;
        if (length == 0) {
            throw new IllegalStateException("No available text sizes to choose from.");
        }
        int i10 = length - 1;
        int i11 = 0;
        int i12 = 1;
        while (i12 <= i10) {
            int i13 = (i12 + i10) / 2;
            int i14 = this.f29740f[i13];
            TextView textView = this.f29743i;
            CharSequence text = textView.getText();
            TransformationMethod transformationMethod = textView.getTransformationMethod();
            if (transformationMethod != null && (transformation = transformationMethod.getTransformation(text, textView)) != null) {
                text = transformation;
            }
            int maxLines = textView.getMaxLines();
            TextPaint textPaint = this.f29742h;
            if (textPaint == null) {
                this.f29742h = new TextPaint();
            } else {
                textPaint.reset();
            }
            this.f29742h.set(textView.getPaint());
            this.f29742h.setTextSize(i14);
            StaticLayout staticLayoutA = f1.a(text, (Layout.Alignment) d(textView, Layout.Alignment.ALIGN_NORMAL, "getLayoutAlignment"), Math.round(rectF.right), maxLines, this.f29743i, this.f29742h, this.f29745k);
            if ((maxLines == -1 || (staticLayoutA.getLineCount() <= maxLines && staticLayoutA.getLineEnd(staticLayoutA.getLineCount() - 1) == text.length())) && staticLayoutA.getHeight() <= rectF.bottom) {
                int i15 = i13 + 1;
                i11 = i12;
                i12 = i15;
            } else {
                i11 = i13 - 1;
                i10 = i11;
            }
        }
        return this.f29740f[i11];
    }

    public final boolean e() {
        return i() && this.f29735a != 0;
    }

    public final void f(float f10, int i10) {
        Context context = this.f29744j;
        float fApplyDimension = TypedValue.applyDimension(i10, f10, (context == null ? Resources.getSystem() : context.getResources()).getDisplayMetrics());
        TextView textView = this.f29743i;
        if (fApplyDimension != textView.getPaint().getTextSize()) {
            textView.getPaint().setTextSize(fApplyDimension);
            boolean zIsInLayout = textView.isInLayout();
            if (textView.getLayout() != null) {
                this.f29736b = false;
                try {
                    Method textViewMethod = getTextViewMethod("nullLayouts");
                    if (textViewMethod != null) {
                        textViewMethod.invoke(textView, new Object[0]);
                    }
                } catch (Exception unused) {
                }
                if (zIsInLayout) {
                    textView.forceLayout();
                } else {
                    textView.requestLayout();
                }
                textView.invalidate();
            }
        }
    }

    public final boolean g() {
        if (i() && this.f29735a == 1) {
            if (!this.f29741g || this.f29740f.length == 0) {
                int iFloor = ((int) Math.floor((this.f29739e - this.f29738d) / this.f29737c)) + 1;
                int[] iArr = new int[iFloor];
                for (int i10 = 0; i10 < iFloor; i10++) {
                    iArr[i10] = Math.round((i10 * this.f29737c) + this.f29738d);
                }
                this.f29740f = b(iArr);
            }
            this.f29736b = true;
        } else {
            this.f29736b = false;
        }
        return this.f29736b;
    }

    public final boolean h() {
        boolean z10 = this.f29740f.length > 0;
        this.f29741g = z10;
        if (z10) {
            this.f29735a = 1;
            this.f29738d = r0[0];
            this.f29739e = r0[r1 - 1];
            this.f29737c = -1.0f;
        }
        return z10;
    }

    public final boolean i() {
        return !(this.f29743i instanceof AppCompatEditText);
    }

    public final void j(float f10, float f11, float f12) {
        if (f10 <= 0.0f) {
            throw new IllegalArgumentException("Minimum auto-size text size (" + f10 + "px) is less or equal to (0px)");
        }
        if (f11 <= f10) {
            throw new IllegalArgumentException("Maximum auto-size text size (" + f11 + "px) is less or equal to minimum auto-size text size (" + f10 + "px)");
        }
        if (f12 <= 0.0f) {
            throw new IllegalArgumentException("The auto-size step granularity (" + f12 + "px) is less or equal to (0px)");
        }
        this.f29735a = 1;
        this.f29738d = f10;
        this.f29739e = f11;
        this.f29737c = f12;
        this.f29741g = false;
    }

    public void loadFromAttributes(@Nullable AttributeSet attributeSet, int i10) {
        int resourceId;
        int[] iArr = d.j.AppCompatTextView;
        Context context = this.f29744j;
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, iArr, i10, 0);
        TextView textView = this.f29743i;
        androidx.core.view.s1.saveAttributeDataForStyleable(textView, textView.getContext(), iArr, attributeSet, typedArrayObtainStyledAttributes, i10, 0);
        int i11 = d.j.AppCompatTextView_autoSizeTextType;
        if (typedArrayObtainStyledAttributes.hasValue(i11)) {
            this.f29735a = typedArrayObtainStyledAttributes.getInt(i11, 0);
        }
        int i12 = d.j.AppCompatTextView_autoSizeStepGranularity;
        float dimension = typedArrayObtainStyledAttributes.hasValue(i12) ? typedArrayObtainStyledAttributes.getDimension(i12, -1.0f) : -1.0f;
        int i13 = d.j.AppCompatTextView_autoSizeMinTextSize;
        float dimension2 = typedArrayObtainStyledAttributes.hasValue(i13) ? typedArrayObtainStyledAttributes.getDimension(i13, -1.0f) : -1.0f;
        int i14 = d.j.AppCompatTextView_autoSizeMaxTextSize;
        float dimension3 = typedArrayObtainStyledAttributes.hasValue(i14) ? typedArrayObtainStyledAttributes.getDimension(i14, -1.0f) : -1.0f;
        int i15 = d.j.AppCompatTextView_autoSizePresetSizes;
        if (typedArrayObtainStyledAttributes.hasValue(i15) && (resourceId = typedArrayObtainStyledAttributes.getResourceId(i15, 0)) > 0) {
            TypedArray typedArrayObtainTypedArray = typedArrayObtainStyledAttributes.getResources().obtainTypedArray(resourceId);
            int length = typedArrayObtainTypedArray.length();
            int[] iArr2 = new int[length];
            if (length > 0) {
                for (int i16 = 0; i16 < length; i16++) {
                    iArr2[i16] = typedArrayObtainTypedArray.getDimensionPixelSize(i16, -1);
                }
                this.f29740f = b(iArr2);
                h();
            }
            typedArrayObtainTypedArray.recycle();
        }
        typedArrayObtainStyledAttributes.recycle();
        if (!i()) {
            this.f29735a = 0;
            return;
        }
        if (this.f29735a == 1) {
            if (!this.f29741g) {
                DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
                if (dimension2 == -1.0f) {
                    dimension2 = TypedValue.applyDimension(2, 12.0f, displayMetrics);
                }
                if (dimension3 == -1.0f) {
                    dimension3 = TypedValue.applyDimension(2, 112.0f, displayMetrics);
                }
                if (dimension == -1.0f) {
                    dimension = 1.0f;
                }
                j(dimension2, dimension3, dimension);
            }
            g();
        }
    }
}
