package c.a.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 com.seewo.vcommons.constants.Platform;
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 q {

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

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

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public boolean f5066j = false;
    public TextPaint k;
    public final TextView l;
    public final Context m;
    public final c n;

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

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

        @Override // c.a.g.q.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) q.e(textView, "getHorizontallyScrolling", Boolean.FALSE)).booleanValue();
        }
    }

    public q(TextView textView) {
        this.l = textView;
        this.m = textView.getContext();
        this.n = Build.VERSION.SDK_INT >= 29 ? new b() : new a();
    }

    public static Method d(String str) {
        try {
            Method declaredMethod = f5058b.get(str);
            if (declaredMethod == null && (declaredMethod = TextView.class.getDeclaredMethod(str, new Class[0])) != null) {
                declaredMethod.setAccessible(true);
                f5058b.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 t) {
        try {
            return (T) d(str).invoke(obj, new Object[0]);
        } catch (Exception e2) {
            Log.w("ACTVAutoSizeHelper", "Failed to invoke TextView#" + str + "() method", e2);
            return t;
        }
    }

    public void a() {
        if (i() && this.f5060d != 0) {
            if (this.f5061e) {
                if (this.l.getMeasuredHeight() <= 0 || this.l.getMeasuredWidth() <= 0) {
                    return;
                }
                int measuredWidth = this.n.b(this.l) ? 1048576 : (this.l.getMeasuredWidth() - this.l.getTotalPaddingLeft()) - this.l.getTotalPaddingRight();
                int height = (this.l.getHeight() - this.l.getCompoundPaddingBottom()) - this.l.getCompoundPaddingTop();
                if (measuredWidth <= 0 || height <= 0) {
                    return;
                }
                RectF rectF = f5057a;
                synchronized (rectF) {
                    rectF.setEmpty();
                    rectF.right = measuredWidth;
                    rectF.bottom = height;
                    float fC = c(rectF);
                    if (fC != this.l.getTextSize()) {
                        f(0, fC);
                    }
                }
            }
            this.f5061e = 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.f5065i.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.f5065i[i5];
            CharSequence text = this.l.getText();
            TransformationMethod transformationMethod = this.l.getTransformationMethod();
            if (transformationMethod != null && (transformation = transformationMethod.getTransformation(text, this.l)) != null) {
                text = transformation;
            }
            int maxLines = this.l.getMaxLines();
            TextPaint textPaint = this.k;
            if (textPaint == null) {
                this.k = new TextPaint();
            } else {
                textPaint.reset();
            }
            this.k.set(this.l.getPaint());
            this.k.setTextSize(i6);
            Layout.Alignment alignment = (Layout.Alignment) e(this.l, "getLayoutAlignment", Layout.Alignment.ALIGN_NORMAL);
            StaticLayout.Builder builderObtain = StaticLayout.Builder.obtain(text, 0, text.length(), this.k, Math.round(rectF.right));
            builderObtain.setAlignment(alignment).setLineSpacing(this.l.getLineSpacingExtra(), this.l.getLineSpacingMultiplier()).setIncludePad(this.l.getIncludeFontPadding()).setBreakStrategy(this.l.getBreakStrategy()).setHyphenationFrequency(this.l.getHyphenationFrequency()).setMaxLines(maxLines == -1 ? Platform.UNKNOWN : maxLines);
            try {
                this.n.a(builderObtain, this.l);
            } 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.f5065i[i4];
    }

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

    public final boolean g() {
        if (i() && this.f5060d == 1) {
            if (!this.f5066j || this.f5065i.length == 0) {
                int iFloor = ((int) Math.floor((this.f5064h - this.f5063g) / this.f5062f)) + 1;
                int[] iArr = new int[iFloor];
                for (int i2 = 0; i2 < iFloor; i2++) {
                    iArr[i2] = Math.round((i2 * this.f5062f) + this.f5063g);
                }
                this.f5065i = b(iArr);
            }
            this.f5061e = true;
        } else {
            this.f5061e = false;
        }
        return this.f5061e;
    }

    public final boolean h() {
        boolean z = this.f5065i.length > 0;
        this.f5066j = z;
        if (z) {
            this.f5060d = 1;
            this.f5063g = r0[0];
            this.f5064h = r0[r1 - 1];
            this.f5062f = -1.0f;
        }
        return z;
    }

    public final boolean i() {
        return !(this.l instanceof g);
    }

    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.f5060d = 1;
        this.f5063g = f2;
        this.f5064h = f3;
        this.f5062f = f4;
        this.f5066j = false;
    }
}
