package androidx.appcompat.widget;

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.widget.TextView;
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 z {

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

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private static ConcurrentHashMap<String, Method> f226l = new ConcurrentHashMap<>();
    private int a = 0;
    private boolean b = false;
    private float c = -1.0f;
    private float d = -1.0f;
    private float e = -1.0f;
    private int[] f = new int[0];

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

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

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

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

    z(TextView textView) {
        this.f229i = textView;
        this.f230j = textView.getContext();
    }

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

    private void c() {
        this.a = 0;
        this.d = -1.0f;
        this.e = -1.0f;
        this.c = -1.0f;
        this.f = new int[0];
        this.b = false;
    }

    private StaticLayout d(CharSequence charSequence, Layout.Alignment alignment, int i2, int i3) {
        TextDirectionHeuristic textDirectionHeuristic = (TextDirectionHeuristic) m(this.f229i, "getTextDirectionHeuristic", TextDirectionHeuristics.FIRSTSTRONG_LTR);
        StaticLayout.Builder hyphenationFrequency = StaticLayout.Builder.obtain(charSequence, 0, charSequence.length(), this.f228h, i2).setAlignment(alignment).setLineSpacing(this.f229i.getLineSpacingExtra(), this.f229i.getLineSpacingMultiplier()).setIncludePad(this.f229i.getIncludeFontPadding()).setBreakStrategy(this.f229i.getBreakStrategy()).setHyphenationFrequency(this.f229i.getHyphenationFrequency());
        if (i3 == -1) {
            i3 = Integer.MAX_VALUE;
        }
        return hyphenationFrequency.setMaxLines(i3).setTextDirection(textDirectionHeuristic).build();
    }

    private StaticLayout e(CharSequence charSequence, Layout.Alignment alignment, int i2) {
        float fFloatValue;
        float fFloatValue2;
        boolean zBooleanValue;
        if (Build.VERSION.SDK_INT >= 16) {
            fFloatValue = this.f229i.getLineSpacingMultiplier();
            fFloatValue2 = this.f229i.getLineSpacingExtra();
            zBooleanValue = this.f229i.getIncludeFontPadding();
        } else {
            fFloatValue = ((Float) m(this.f229i, "getLineSpacingMultiplier", Float.valueOf(1.0f))).floatValue();
            fFloatValue2 = ((Float) m(this.f229i, "getLineSpacingExtra", Float.valueOf(0.0f))).floatValue();
            zBooleanValue = ((Boolean) m(this.f229i, "getIncludeFontPadding", Boolean.TRUE)).booleanValue();
        }
        return new StaticLayout(charSequence, this.f228h, i2, alignment, fFloatValue, fFloatValue2, zBooleanValue);
    }

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

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

    private <T> T m(Object obj, String str, T t) {
        try {
            return (T) l(str).invoke(obj, new Object[0]);
        } catch (Exception e) {
            Log.w("ACTVAutoSizeHelper", "Failed to invoke TextView#" + str + "() method", e);
            return t;
        }
    }

    private void s(float f) {
        if (f != this.f229i.getPaint().getTextSize()) {
            this.f229i.getPaint().setTextSize(f);
            boolean zIsInLayout = Build.VERSION.SDK_INT >= 18 ? this.f229i.isInLayout() : false;
            if (this.f229i.getLayout() != null) {
                this.b = false;
                try {
                    Method methodL = l("nullLayouts");
                    if (methodL != null) {
                        methodL.invoke(this.f229i, new Object[0]);
                    }
                } catch (Exception e) {
                    Log.w("ACTVAutoSizeHelper", "Failed to invoke TextView#nullLayouts() method", e);
                }
                if (zIsInLayout) {
                    this.f229i.forceLayout();
                } else {
                    this.f229i.requestLayout();
                }
                this.f229i.invalidate();
            }
        }
    }

    private boolean u() {
        if (y() && this.a == 1) {
            if (!this.f227g || this.f.length == 0) {
                float fRound = Math.round(this.d);
                int i2 = 1;
                while (Math.round(this.c + fRound) <= Math.round(this.e)) {
                    i2++;
                    fRound += this.c;
                }
                int[] iArr = new int[i2];
                float f = this.d;
                for (int i3 = 0; i3 < i2; i3++) {
                    iArr[i3] = Math.round(f);
                    f += this.c;
                }
                this.f = b(iArr);
            }
            this.b = true;
        } else {
            this.b = false;
        }
        return this.b;
    }

    private void v(TypedArray typedArray) {
        int length = typedArray.length();
        int[] iArr = new int[length];
        if (length > 0) {
            for (int i2 = 0; i2 < length; i2++) {
                iArr[i2] = typedArray.getDimensionPixelSize(i2, -1);
            }
            this.f = b(iArr);
            w();
        }
    }

    private boolean w() {
        boolean z = this.f.length > 0;
        this.f227g = z;
        if (z) {
            this.a = 1;
            int[] iArr = this.f;
            this.d = iArr[0];
            this.e = iArr[r0 - 1];
            this.c = -1.0f;
        }
        return this.f227g;
    }

    private boolean x(int i2, RectF rectF) {
        CharSequence transformation;
        CharSequence text = this.f229i.getText();
        TransformationMethod transformationMethod = this.f229i.getTransformationMethod();
        if (transformationMethod != null && (transformation = transformationMethod.getTransformation(text, this.f229i)) != null) {
            text = transformation;
        }
        int maxLines = Build.VERSION.SDK_INT >= 16 ? this.f229i.getMaxLines() : -1;
        TextPaint textPaint = this.f228h;
        if (textPaint == null) {
            this.f228h = new TextPaint();
        } else {
            textPaint.reset();
        }
        this.f228h.set(this.f229i.getPaint());
        this.f228h.setTextSize(i2);
        Layout.Alignment alignment = (Layout.Alignment) m(this.f229i, "getLayoutAlignment", Layout.Alignment.ALIGN_NORMAL);
        StaticLayout staticLayoutD = Build.VERSION.SDK_INT >= 23 ? d(text, alignment, Math.round(rectF.right), maxLines) : e(text, alignment, Math.round(rectF.right));
        return (maxLines == -1 || (staticLayoutD.getLineCount() <= maxLines && staticLayoutD.getLineEnd(staticLayoutD.getLineCount() - 1) == text.length())) && ((float) staticLayoutD.getHeight()) <= rectF.bottom;
    }

    private boolean y() {
        return !(this.f229i instanceof k);
    }

    private void z(float f, float f2, float f3) throws IllegalArgumentException {
        if (f <= 0.0f) {
            throw new IllegalArgumentException("Minimum auto-size text size (" + f + "px) is less or equal to (0px)");
        }
        if (f2 <= f) {
            throw new IllegalArgumentException("Maximum auto-size text size (" + f2 + "px) is less or equal to minimum auto-size text size (" + f + "px)");
        }
        if (f3 <= 0.0f) {
            throw new IllegalArgumentException("The auto-size step granularity (" + f3 + "px) is less or equal to (0px)");
        }
        this.a = 1;
        this.d = f;
        this.e = f2;
        this.c = f3;
        this.f227g = false;
    }

    void a() {
        if (n()) {
            if (this.b) {
                if (this.f229i.getMeasuredHeight() <= 0 || this.f229i.getMeasuredWidth() <= 0) {
                    return;
                }
                int measuredWidth = ((Boolean) m(this.f229i, "getHorizontallyScrolling", Boolean.FALSE)).booleanValue() ? 1048576 : (this.f229i.getMeasuredWidth() - this.f229i.getTotalPaddingLeft()) - this.f229i.getTotalPaddingRight();
                int height = (this.f229i.getHeight() - this.f229i.getCompoundPaddingBottom()) - this.f229i.getCompoundPaddingTop();
                if (measuredWidth <= 0 || height <= 0) {
                    return;
                }
                synchronized (f225k) {
                    f225k.setEmpty();
                    f225k.right = measuredWidth;
                    f225k.bottom = height;
                    float f = f(f225k);
                    if (f != this.f229i.getTextSize()) {
                        t(0, f);
                    }
                }
            }
            this.b = true;
        }
    }

    int g() {
        return Math.round(this.e);
    }

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

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

    int[] j() {
        return this.f;
    }

    int k() {
        return this.a;
    }

    boolean n() {
        return y() && this.a != 0;
    }

    void o(AttributeSet attributeSet, int i2) {
        int resourceId;
        TypedArray typedArrayObtainStyledAttributes = this.f230j.obtainStyledAttributes(attributeSet, g.a.j.AppCompatTextView, i2, 0);
        if (typedArrayObtainStyledAttributes.hasValue(g.a.j.AppCompatTextView_autoSizeTextType)) {
            this.a = typedArrayObtainStyledAttributes.getInt(g.a.j.AppCompatTextView_autoSizeTextType, 0);
        }
        float dimension = typedArrayObtainStyledAttributes.hasValue(g.a.j.AppCompatTextView_autoSizeStepGranularity) ? typedArrayObtainStyledAttributes.getDimension(g.a.j.AppCompatTextView_autoSizeStepGranularity, -1.0f) : -1.0f;
        float dimension2 = typedArrayObtainStyledAttributes.hasValue(g.a.j.AppCompatTextView_autoSizeMinTextSize) ? typedArrayObtainStyledAttributes.getDimension(g.a.j.AppCompatTextView_autoSizeMinTextSize, -1.0f) : -1.0f;
        float dimension3 = typedArrayObtainStyledAttributes.hasValue(g.a.j.AppCompatTextView_autoSizeMaxTextSize) ? typedArrayObtainStyledAttributes.getDimension(g.a.j.AppCompatTextView_autoSizeMaxTextSize, -1.0f) : -1.0f;
        if (typedArrayObtainStyledAttributes.hasValue(g.a.j.AppCompatTextView_autoSizePresetSizes) && (resourceId = typedArrayObtainStyledAttributes.getResourceId(g.a.j.AppCompatTextView_autoSizePresetSizes, 0)) > 0) {
            TypedArray typedArrayObtainTypedArray = typedArrayObtainStyledAttributes.getResources().obtainTypedArray(resourceId);
            v(typedArrayObtainTypedArray);
            typedArrayObtainTypedArray.recycle();
        }
        typedArrayObtainStyledAttributes.recycle();
        if (!y()) {
            this.a = 0;
            return;
        }
        if (this.a == 1) {
            if (!this.f227g) {
                DisplayMetrics displayMetrics = this.f230j.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;
                }
                z(dimension2, dimension3, dimension);
            }
            u();
        }
    }

    void p(int i2, int i3, int i4, int i5) throws IllegalArgumentException {
        if (y()) {
            DisplayMetrics displayMetrics = this.f230j.getResources().getDisplayMetrics();
            z(TypedValue.applyDimension(i5, i2, displayMetrics), TypedValue.applyDimension(i5, i3, displayMetrics), TypedValue.applyDimension(i5, i4, displayMetrics));
            if (u()) {
                a();
            }
        }
    }

    void q(int[] iArr, int i2) throws IllegalArgumentException {
        if (y()) {
            int length = iArr.length;
            if (length > 0) {
                int[] iArrCopyOf = new int[length];
                if (i2 == 0) {
                    iArrCopyOf = Arrays.copyOf(iArr, length);
                } else {
                    DisplayMetrics displayMetrics = this.f230j.getResources().getDisplayMetrics();
                    for (int i3 = 0; i3 < length; i3++) {
                        iArrCopyOf[i3] = Math.round(TypedValue.applyDimension(i2, iArr[i3], displayMetrics));
                    }
                }
                this.f = b(iArrCopyOf);
                if (!w()) {
                    throw new IllegalArgumentException("None of the preset sizes is valid: " + Arrays.toString(iArr));
                }
            } else {
                this.f227g = false;
            }
            if (u()) {
                a();
            }
        }
    }

    void r(int i2) {
        if (y()) {
            if (i2 == 0) {
                c();
                return;
            }
            if (i2 != 1) {
                throw new IllegalArgumentException("Unknown auto-size text type: " + i2);
            }
            DisplayMetrics displayMetrics = this.f230j.getResources().getDisplayMetrics();
            z(TypedValue.applyDimension(2, 12.0f, displayMetrics), TypedValue.applyDimension(2, 112.0f, displayMetrics), 1.0f);
            if (u()) {
                a();
            }
        }
    }

    void t(int i2, float f) {
        Context context = this.f230j;
        s(TypedValue.applyDimension(i2, f, (context == null ? Resources.getSystem() : context.getResources()).getDisplayMetrics()));
    }
}
