package d.b.g;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
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.Log;
import android.util.TypedValue;
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: loaded from: classes.dex */
public class c0 {
    public static final RectF a = new RectF();

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

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

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

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

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

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

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

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

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

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

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

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

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

    public static class a extends c {
        @Override // d.b.g.c0.c
        public void a(StaticLayout.Builder builder, TextView textView) {
            builder.setTextDirection((TextDirectionHeuristic) c0.e(textView, "getTextDirectionHeuristic", TextDirectionHeuristics.FIRSTSTRONG_LTR));
        }
    }

    public static class b extends a {
        @Override // d.b.g.c0.a, d.b.g.c0.c
        public void a(StaticLayout.Builder builder, TextView textView) {
            builder.setTextDirection(textView.getTextDirectionHeuristic());
        }

        @Override // d.b.g.c0.c
        public boolean b(TextView textView) {
            return textView.isHorizontallyScrollable();
        }
    }

    public static class c {
        public void a(StaticLayout.Builder builder, TextView textView) {
            throw null;
        }

        public boolean b(TextView textView) {
            return ((Boolean) c0.e(textView, "getHorizontallyScrolling", Boolean.FALSE)).booleanValue();
        }
    }

    public c0(TextView textView) {
        this.f1940l = textView;
        this.f1941m = textView.getContext();
        this.f1942n = Build.VERSION.SDK_INT >= 29 ? new b() : new a();
    }

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

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

    public void a() {
        if (i() && this.f1932d != 0) {
            if (this.f1933e) {
                if (this.f1940l.getMeasuredHeight() <= 0 || this.f1940l.getMeasuredWidth() <= 0) {
                    return;
                }
                int measuredWidth = this.f1942n.b(this.f1940l) ? 1048576 : (this.f1940l.getMeasuredWidth() - this.f1940l.getTotalPaddingLeft()) - this.f1940l.getTotalPaddingRight();
                int height = (this.f1940l.getHeight() - this.f1940l.getCompoundPaddingBottom()) - this.f1940l.getCompoundPaddingTop();
                if (measuredWidth <= 0 || height <= 0) {
                    return;
                }
                RectF rectF = a;
                synchronized (rectF) {
                    rectF.setEmpty();
                    rectF.right = measuredWidth;
                    rectF.bottom = height;
                    float fC = c(rectF);
                    if (fC != this.f1940l.getTextSize()) {
                        f(0, fC);
                    }
                }
            }
            this.f1933e = true;
        }
    }

    public final 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;
    }

    public final int c(RectF rectF) {
        CharSequence transformation;
        int length = this.f1937i.length;
        if (length == 0) {
            throw new IllegalStateException("No available text sizes to choose from.");
        }
        int i2 = length - 1;
        int i3 = 1;
        int i4 = 0;
        while (i3 <= i2) {
            int i5 = (i3 + i2) / 2;
            int i6 = this.f1937i[i5];
            CharSequence text = this.f1940l.getText();
            TransformationMethod transformationMethod = this.f1940l.getTransformationMethod();
            if (transformationMethod != null && (transformation = transformationMethod.getTransformation(text, this.f1940l)) != null) {
                text = transformation;
            }
            int maxLines = this.f1940l.getMaxLines();
            TextPaint textPaint = this.f1939k;
            if (textPaint == null) {
                this.f1939k = new TextPaint();
            } else {
                textPaint.reset();
            }
            this.f1939k.set(this.f1940l.getPaint());
            this.f1939k.setTextSize(i6);
            Layout.Alignment alignment = (Layout.Alignment) e(this.f1940l, "getLayoutAlignment", Layout.Alignment.ALIGN_NORMAL);
            StaticLayout.Builder builderObtain = StaticLayout.Builder.obtain(text, 0, text.length(), this.f1939k, Math.round(rectF.right));
            builderObtain.setAlignment(alignment).setLineSpacing(this.f1940l.getLineSpacingExtra(), this.f1940l.getLineSpacingMultiplier()).setIncludePad(this.f1940l.getIncludeFontPadding()).setBreakStrategy(this.f1940l.getBreakStrategy()).setHyphenationFrequency(this.f1940l.getHyphenationFrequency()).setMaxLines(maxLines == -1 ? Integer.MAX_VALUE : maxLines);
            try {
                this.f1942n.a(builderObtain, this.f1940l);
            } catch (ClassCastException unused) {
                Log.w("ACTVAutoSizeHelper", "Failed to obtain TextDirectionHeuristic, auto size may be incorrect");
            }
            StaticLayout staticLayoutBuild = builderObtain.build();
            if ((maxLines == -1 || (staticLayoutBuild.getLineCount() <= maxLines && staticLayoutBuild.getLineEnd(staticLayoutBuild.getLineCount() - 1) == text.length())) && ((float) staticLayoutBuild.getHeight()) <= rectF.bottom) {
                int i7 = i5 + 1;
                i4 = i3;
                i3 = i7;
            } else {
                i4 = i5 - 1;
                i2 = i4;
            }
        }
        return this.f1937i[i4];
    }

    public void f(int i2, float f2) {
        Context context = this.f1941m;
        float fApplyDimension = TypedValue.applyDimension(i2, f2, (context == null ? Resources.getSystem() : context.getResources()).getDisplayMetrics());
        if (fApplyDimension != this.f1940l.getPaint().getTextSize()) {
            this.f1940l.getPaint().setTextSize(fApplyDimension);
            boolean zIsInLayout = this.f1940l.isInLayout();
            if (this.f1940l.getLayout() != null) {
                this.f1933e = false;
                try {
                    Method methodD = d("nullLayouts");
                    if (methodD != null) {
                        methodD.invoke(this.f1940l, new Object[0]);
                    }
                } catch (Exception e2) {
                    Log.w("ACTVAutoSizeHelper", "Failed to invoke TextView#nullLayouts() method", e2);
                }
                if (zIsInLayout) {
                    this.f1940l.forceLayout();
                } else {
                    this.f1940l.requestLayout();
                }
                this.f1940l.invalidate();
            }
        }
    }

    public final boolean g() {
        if (i() && this.f1932d == 1) {
            if (!this.f1938j || this.f1937i.length == 0) {
                int iFloor = ((int) Math.floor((this.f1936h - this.f1935g) / this.f1934f)) + 1;
                int[] iArr = new int[iFloor];
                for (int i2 = 0; i2 < iFloor; i2++) {
                    iArr[i2] = Math.round((i2 * this.f1934f) + this.f1935g);
                }
                this.f1937i = b(iArr);
            }
            this.f1933e = true;
        } else {
            this.f1933e = false;
        }
        return this.f1933e;
    }

    public final boolean h() {
        boolean z2 = this.f1937i.length > 0;
        this.f1938j = z2;
        if (z2) {
            this.f1932d = 1;
            this.f1935g = r0[0];
            this.f1936h = r0[r1 - 1];
            this.f1934f = -1.0f;
        }
        return z2;
    }

    public final boolean i() {
        return !(this.f1940l instanceof k);
    }

    public final void j(float f2, float f3, float f4) {
        if (f2 <= 0.0f) {
            throw new IllegalArgumentException("Minimum auto-size text size (" + f2 + "px) is less or equal to (0px)");
        }
        if (f3 <= f2) {
            throw new IllegalArgumentException("Maximum auto-size text size (" + f3 + "px) is less or equal to minimum auto-size text size (" + f2 + "px)");
        }
        if (f4 <= 0.0f) {
            throw new IllegalArgumentException("The auto-size step granularity (" + f4 + "px) is less or equal to (0px)");
        }
        this.f1932d = 1;
        this.f1935g = f2;
        this.f1936h = f3;
        this.f1934f = f4;
        this.f1938j = false;
    }
}
