package androidx.appcompat.widget;

import android.annotation.SuppressLint;
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.TextDirectionHeuristic;
import android.text.TextDirectionHeuristics;
import android.text.TextPaint;
import android.text.method.TransformationMethod;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;

/* JADX INFO: compiled from: AppCompatTextViewAutoSizeHelper.java */
/* JADX INFO: loaded from: classes.dex */
class e0 {

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

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    @SuppressLint({"BanConcurrentHashMap"})
    private static ConcurrentHashMap<String, Method> f886m = new ConcurrentHashMap<>();

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    @SuppressLint({"BanConcurrentHashMap"})
    private static ConcurrentHashMap<String, Field> f887n = new ConcurrentHashMap<>();

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

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

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

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private final f f898k;

    /* JADX INFO: compiled from: AppCompatTextViewAutoSizeHelper.java */
    private static final class a {
        static StaticLayout a(CharSequence charSequence, Layout.Alignment alignment, int i3, TextView textView, TextPaint textPaint) {
            return new StaticLayout(charSequence, textPaint, i3, alignment, textView.getLineSpacingMultiplier(), textView.getLineSpacingExtra(), textView.getIncludeFontPadding());
        }

        static int b(TextView textView) {
            return textView.getMaxLines();
        }
    }

    /* JADX INFO: compiled from: AppCompatTextViewAutoSizeHelper.java */
    private static final class b {
        static boolean a(View view) {
            return view.isInLayout();
        }
    }

    /* JADX INFO: compiled from: AppCompatTextViewAutoSizeHelper.java */
    private static final class c {
        static StaticLayout a(CharSequence charSequence, Layout.Alignment alignment, int i3, int i4, TextView textView, TextPaint textPaint, f fVar) {
            StaticLayout.Builder builderObtain = StaticLayout.Builder.obtain(charSequence, 0, charSequence.length(), textPaint, i3);
            StaticLayout.Builder hyphenationFrequency = builderObtain.setAlignment(alignment).setLineSpacing(textView.getLineSpacingExtra(), textView.getLineSpacingMultiplier()).setIncludePad(textView.getIncludeFontPadding()).setBreakStrategy(textView.getBreakStrategy()).setHyphenationFrequency(textView.getHyphenationFrequency());
            if (i4 == -1) {
                i4 = Integer.MAX_VALUE;
            }
            hyphenationFrequency.setMaxLines(i4);
            try {
                fVar.a(builderObtain, textView);
            } catch (ClassCastException unused) {
                Log.w("ACTVAutoSizeHelper", "Failed to obtain TextDirectionHeuristic, auto size may be incorrect");
            }
            return builderObtain.build();
        }
    }

    /* JADX INFO: compiled from: AppCompatTextViewAutoSizeHelper.java */
    private static class d extends f {
        d() {
        }

        @Override // androidx.appcompat.widget.e0.f
        void a(StaticLayout.Builder builder, TextView textView) {
            builder.setTextDirection((TextDirectionHeuristic) e0.p(textView, "getTextDirectionHeuristic", TextDirectionHeuristics.FIRSTSTRONG_LTR));
        }
    }

    /* JADX INFO: compiled from: AppCompatTextViewAutoSizeHelper.java */
    private static class e extends d {
        e() {
        }

        @Override // androidx.appcompat.widget.e0.d, androidx.appcompat.widget.e0.f
        void a(StaticLayout.Builder builder, TextView textView) {
            builder.setTextDirection(textView.getTextDirectionHeuristic());
        }

        @Override // androidx.appcompat.widget.e0.f
        boolean b(TextView textView) {
            return textView.isHorizontallyScrollable();
        }
    }

    /* JADX INFO: compiled from: AppCompatTextViewAutoSizeHelper.java */
    private static class f {
        f() {
        }

        void a(StaticLayout.Builder builder, TextView textView) {
        }

        boolean b(TextView textView) {
            return ((Boolean) e0.p(textView, "getHorizontallyScrolling", Boolean.FALSE)).booleanValue();
        }
    }

    e0(TextView textView) {
        this.f896i = textView;
        this.f897j = textView.getContext();
        int i3 = Build.VERSION.SDK_INT;
        if (i3 >= 29) {
            this.f898k = new e();
        } else if (i3 >= 23) {
            this.f898k = new d();
        } else {
            this.f898k = new f();
        }
    }

    private boolean A(int i3, RectF rectF) {
        CharSequence transformation;
        CharSequence text = this.f896i.getText();
        TransformationMethod transformationMethod = this.f896i.getTransformationMethod();
        if (transformationMethod != null && (transformation = transformationMethod.getTransformation(text, this.f896i)) != null) {
            text = transformation;
        }
        int iB = Build.VERSION.SDK_INT >= 16 ? a.b(this.f896i) : -1;
        o(i3);
        StaticLayout staticLayoutE = e(text, (Layout.Alignment) p(this.f896i, "getLayoutAlignment", Layout.Alignment.ALIGN_NORMAL), Math.round(rectF.right), iB);
        return (iB == -1 || (staticLayoutE.getLineCount() <= iB && staticLayoutE.getLineEnd(staticLayoutE.getLineCount() - 1) == text.length())) && ((float) staticLayoutE.getHeight()) <= rectF.bottom;
    }

    private boolean B() {
        return !(this.f896i instanceof l);
    }

    private void C(float f3, float f4, float f5) throws IllegalArgumentException {
        if (f3 <= 0.0f) {
            throw new IllegalArgumentException("Minimum auto-size text size (" + f3 + "px) is less or equal to (0px)");
        }
        if (f4 <= f3) {
            throw new IllegalArgumentException("Maximum auto-size text size (" + f4 + "px) is less or equal to minimum auto-size text size (" + f3 + "px)");
        }
        if (f5 <= 0.0f) {
            throw new IllegalArgumentException("The auto-size step granularity (" + f5 + "px) is less or equal to (0px)");
        }
        this.f888a = 1;
        this.f891d = f3;
        this.f892e = f4;
        this.f890c = f5;
        this.f894g = false;
    }

    private static <T> T a(Object obj, String str, T t2) {
        try {
            Field fieldM = m(str);
            return fieldM == null ? t2 : (T) fieldM.get(obj);
        } catch (IllegalAccessException e3) {
            Log.w("ACTVAutoSizeHelper", "Failed to access TextView#" + str + " member", e3);
            return t2;
        }
    }

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

    private void d() {
        this.f888a = 0;
        this.f891d = -1.0f;
        this.f892e = -1.0f;
        this.f890c = -1.0f;
        this.f893f = new int[0];
        this.f889b = false;
    }

    private StaticLayout f(CharSequence charSequence, Layout.Alignment alignment, int i3) {
        return new StaticLayout(charSequence, this.f895h, i3, alignment, ((Float) a(this.f896i, "mSpacingMult", Float.valueOf(1.0f))).floatValue(), ((Float) a(this.f896i, "mSpacingAdd", Float.valueOf(0.0f))).floatValue(), ((Boolean) a(this.f896i, "mIncludePad", Boolean.TRUE)).booleanValue());
    }

    private int g(RectF rectF) {
        int i3;
        int length = this.f893f.length;
        if (length == 0) {
            throw new IllegalStateException("No available text sizes to choose from.");
        }
        int i4 = 0;
        int i5 = 1;
        int i6 = length - 1;
        while (true) {
            int i7 = i5;
            int i8 = i4;
            i4 = i7;
            while (i4 <= i6) {
                i3 = (i4 + i6) / 2;
                if (A(this.f893f[i3], rectF)) {
                    break;
                }
                i8 = i3 - 1;
                i6 = i8;
            }
            return this.f893f[i8];
            i5 = i3 + 1;
        }
    }

    private static Field m(String str) {
        try {
            Field declaredField = f887n.get(str);
            if (declaredField == null && (declaredField = TextView.class.getDeclaredField(str)) != null) {
                declaredField.setAccessible(true);
                f887n.put(str, declaredField);
            }
            return declaredField;
        } catch (NoSuchFieldException e3) {
            Log.w("ACTVAutoSizeHelper", "Failed to access TextView#" + str + " member", e3);
            return null;
        }
    }

    private static Method n(String str) {
        try {
            Method declaredMethod = f886m.get(str);
            if (declaredMethod == null && (declaredMethod = TextView.class.getDeclaredMethod(str, new Class[0])) != null) {
                declaredMethod.setAccessible(true);
                f886m.put(str, declaredMethod);
            }
            return declaredMethod;
        } catch (Exception e3) {
            Log.w("ACTVAutoSizeHelper", "Failed to retrieve TextView#" + str + "() method", e3);
            return null;
        }
    }

    static <T> T p(Object obj, String str, T t2) {
        try {
            return (T) n(str).invoke(obj, new Object[0]);
        } catch (Exception e3) {
            Log.w("ACTVAutoSizeHelper", "Failed to invoke TextView#" + str + "() method", e3);
            return t2;
        }
    }

    private void v(float f3) {
        if (f3 != this.f896i.getPaint().getTextSize()) {
            this.f896i.getPaint().setTextSize(f3);
            boolean zA = Build.VERSION.SDK_INT >= 18 ? b.a(this.f896i) : false;
            if (this.f896i.getLayout() != null) {
                this.f889b = false;
                try {
                    Method methodN = n("nullLayouts");
                    if (methodN != null) {
                        methodN.invoke(this.f896i, new Object[0]);
                    }
                } catch (Exception e3) {
                    Log.w("ACTVAutoSizeHelper", "Failed to invoke TextView#nullLayouts() method", e3);
                }
                if (zA) {
                    this.f896i.forceLayout();
                } else {
                    this.f896i.requestLayout();
                }
                this.f896i.invalidate();
            }
        }
    }

    private boolean x() {
        if (B() && this.f888a == 1) {
            if (!this.f894g || this.f893f.length == 0) {
                int iFloor = ((int) Math.floor((this.f892e - this.f891d) / this.f890c)) + 1;
                int[] iArr = new int[iFloor];
                for (int i3 = 0; i3 < iFloor; i3++) {
                    iArr[i3] = Math.round(this.f891d + (i3 * this.f890c));
                }
                this.f893f = c(iArr);
            }
            this.f889b = true;
        } else {
            this.f889b = false;
        }
        return this.f889b;
    }

    private void y(TypedArray typedArray) {
        int length = typedArray.length();
        int[] iArr = new int[length];
        if (length > 0) {
            for (int i3 = 0; i3 < length; i3++) {
                iArr[i3] = typedArray.getDimensionPixelSize(i3, -1);
            }
            this.f893f = c(iArr);
            z();
        }
    }

    private boolean z() {
        boolean z2 = this.f893f.length > 0;
        this.f894g = z2;
        if (z2) {
            this.f888a = 1;
            this.f891d = r0[0];
            this.f892e = r0[r1 - 1];
            this.f890c = -1.0f;
        }
        return z2;
    }

    void b() {
        if (q()) {
            if (this.f889b) {
                if (this.f896i.getMeasuredHeight() <= 0 || this.f896i.getMeasuredWidth() <= 0) {
                    return;
                }
                int measuredWidth = this.f898k.b(this.f896i) ? 1048576 : (this.f896i.getMeasuredWidth() - this.f896i.getTotalPaddingLeft()) - this.f896i.getTotalPaddingRight();
                int height = (this.f896i.getHeight() - this.f896i.getCompoundPaddingBottom()) - this.f896i.getCompoundPaddingTop();
                if (measuredWidth <= 0 || height <= 0) {
                    return;
                }
                RectF rectF = f885l;
                synchronized (rectF) {
                    rectF.setEmpty();
                    rectF.right = measuredWidth;
                    rectF.bottom = height;
                    float fG = g(rectF);
                    if (fG != this.f896i.getTextSize()) {
                        w(0, fG);
                    }
                }
            }
            this.f889b = true;
        }
    }

    StaticLayout e(CharSequence charSequence, Layout.Alignment alignment, int i3, int i4) {
        int i5 = Build.VERSION.SDK_INT;
        return i5 >= 23 ? c.a(charSequence, alignment, i3, i4, this.f896i, this.f895h, this.f898k) : i5 >= 16 ? a.a(charSequence, alignment, i3, this.f896i, this.f895h) : f(charSequence, alignment, i3);
    }

    int h() {
        return Math.round(this.f892e);
    }

    int i() {
        return Math.round(this.f891d);
    }

    int j() {
        return Math.round(this.f890c);
    }

    int[] k() {
        return this.f893f;
    }

    int l() {
        return this.f888a;
    }

    void o(int i3) {
        TextPaint textPaint = this.f895h;
        if (textPaint == null) {
            this.f895h = new TextPaint();
        } else {
            textPaint.reset();
        }
        this.f895h.set(this.f896i.getPaint());
        this.f895h.setTextSize(i3);
    }

    boolean q() {
        return B() && this.f888a != 0;
    }

    void r(AttributeSet attributeSet, int i3) {
        int resourceId;
        Context context = this.f897j;
        int[] iArr = c.j.f3301d0;
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, iArr, i3, 0);
        TextView textView = this.f896i;
        androidx.core.view.y.p0(textView, textView.getContext(), iArr, attributeSet, typedArrayObtainStyledAttributes, i3, 0);
        int i4 = c.j.f3321i0;
        if (typedArrayObtainStyledAttributes.hasValue(i4)) {
            this.f888a = typedArrayObtainStyledAttributes.getInt(i4, 0);
        }
        int i5 = c.j.f3317h0;
        float dimension = typedArrayObtainStyledAttributes.hasValue(i5) ? typedArrayObtainStyledAttributes.getDimension(i5, -1.0f) : -1.0f;
        int i6 = c.j.f3309f0;
        float dimension2 = typedArrayObtainStyledAttributes.hasValue(i6) ? typedArrayObtainStyledAttributes.getDimension(i6, -1.0f) : -1.0f;
        int i7 = c.j.f3305e0;
        float dimension3 = typedArrayObtainStyledAttributes.hasValue(i7) ? typedArrayObtainStyledAttributes.getDimension(i7, -1.0f) : -1.0f;
        int i8 = c.j.f3313g0;
        if (typedArrayObtainStyledAttributes.hasValue(i8) && (resourceId = typedArrayObtainStyledAttributes.getResourceId(i8, 0)) > 0) {
            TypedArray typedArrayObtainTypedArray = typedArrayObtainStyledAttributes.getResources().obtainTypedArray(resourceId);
            y(typedArrayObtainTypedArray);
            typedArrayObtainTypedArray.recycle();
        }
        typedArrayObtainStyledAttributes.recycle();
        if (!B()) {
            this.f888a = 0;
            return;
        }
        if (this.f888a == 1) {
            if (!this.f894g) {
                DisplayMetrics displayMetrics = this.f897j.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;
                }
                C(dimension2, dimension3, dimension);
            }
            x();
        }
    }

    void s(int i3, int i4, int i5, int i6) throws IllegalArgumentException {
        if (B()) {
            DisplayMetrics displayMetrics = this.f897j.getResources().getDisplayMetrics();
            C(TypedValue.applyDimension(i6, i3, displayMetrics), TypedValue.applyDimension(i6, i4, displayMetrics), TypedValue.applyDimension(i6, i5, displayMetrics));
            if (x()) {
                b();
            }
        }
    }

    void t(int[] iArr, int i3) throws IllegalArgumentException {
        if (B()) {
            int length = iArr.length;
            if (length > 0) {
                int[] iArrCopyOf = new int[length];
                if (i3 == 0) {
                    iArrCopyOf = Arrays.copyOf(iArr, length);
                } else {
                    DisplayMetrics displayMetrics = this.f897j.getResources().getDisplayMetrics();
                    for (int i4 = 0; i4 < length; i4++) {
                        iArrCopyOf[i4] = Math.round(TypedValue.applyDimension(i3, iArr[i4], displayMetrics));
                    }
                }
                this.f893f = c(iArrCopyOf);
                if (!z()) {
                    throw new IllegalArgumentException("None of the preset sizes is valid: " + Arrays.toString(iArr));
                }
            } else {
                this.f894g = false;
            }
            if (x()) {
                b();
            }
        }
    }

    void u(int i3) {
        if (B()) {
            if (i3 == 0) {
                d();
                return;
            }
            if (i3 != 1) {
                throw new IllegalArgumentException("Unknown auto-size text type: " + i3);
            }
            DisplayMetrics displayMetrics = this.f897j.getResources().getDisplayMetrics();
            C(TypedValue.applyDimension(2, 12.0f, displayMetrics), TypedValue.applyDimension(2, 112.0f, displayMetrics), 1.0f);
            if (x()) {
                b();
            }
        }
    }

    void w(int i3, float f3) {
        Context context = this.f897j;
        v(TypedValue.applyDimension(i3, f3, (context == null ? Resources.getSystem() : context.getResources()).getDisplayMetrics()));
    }
}
