package n;

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.TextPaint;
import android.text.method.TransformationMethod;
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: renamed from: n.O, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C1197O {

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

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

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

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

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

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

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

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

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

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

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

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

    static {
        new ConcurrentHashMap();
    }

    public C1197O(TextView textView) {
        this.i = textView;
        this.f14757j = textView.getContext();
        if (Build.VERSION.SDK_INT >= 29) {
            this.f14758k = new C1195M();
        } else {
            this.f14758k = new C1194L();
        }
    }

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

    public static Method d(String str) {
        try {
            ConcurrentHashMap concurrentHashMap = f14748m;
            Method declaredMethod = (Method) concurrentHashMap.get(str);
            if (declaredMethod != null || (declaredMethod = TextView.class.getDeclaredMethod(str, null)) == null) {
                return declaredMethod;
            }
            declaredMethod.setAccessible(true);
            concurrentHashMap.put(str, declaredMethod);
            return declaredMethod;
        } catch (Exception e8) {
            Log.w("ACTVAutoSizeHelper", "Failed to retrieve TextView#" + str + "() method", e8);
            return null;
        }
    }

    public static Object e(TextView textView, String str, Object obj) {
        try {
            return d(str).invoke(textView, null);
        } catch (Exception e8) {
            Log.w("ACTVAutoSizeHelper", "Failed to invoke TextView#" + str + "() method", e8);
            return obj;
        }
    }

    public final void a() {
        if (f()) {
            if (this.f14750b) {
                if (this.i.getMeasuredHeight() <= 0 || this.i.getMeasuredWidth() <= 0) {
                    return;
                }
                int measuredWidth = this.f14758k.b(this.i) ? 1048576 : (this.i.getMeasuredWidth() - this.i.getTotalPaddingLeft()) - this.i.getTotalPaddingRight();
                int height = (this.i.getHeight() - this.i.getCompoundPaddingBottom()) - this.i.getCompoundPaddingTop();
                if (measuredWidth <= 0 || height <= 0) {
                    return;
                }
                RectF rectF = f14747l;
                synchronized (rectF) {
                    try {
                        rectF.setEmpty();
                        rectF.right = measuredWidth;
                        rectF.bottom = height;
                        float fC = c(rectF);
                        if (fC != this.i.getTextSize()) {
                            g(0, fC);
                        }
                    } finally {
                    }
                }
            }
            this.f14750b = true;
        }
    }

    public final int c(RectF rectF) {
        CharSequence transformation;
        int length = this.f14754f.length;
        if (length == 0) {
            throw new IllegalStateException("No available text sizes to choose from.");
        }
        int i = length - 1;
        int i5 = 0;
        int i8 = 1;
        while (i8 <= i) {
            int i9 = (i8 + i) / 2;
            int i10 = this.f14754f[i9];
            TextView textView = this.i;
            CharSequence text = textView.getText();
            TransformationMethod transformationMethod = textView.getTransformationMethod();
            if (transformationMethod != null && (transformation = transformationMethod.getTransformation(text, textView)) != null) {
                text = transformation;
            }
            int iB = AbstractC1191I.b(textView);
            TextPaint textPaint = this.f14756h;
            if (textPaint == null) {
                this.f14756h = new TextPaint();
            } else {
                textPaint.reset();
            }
            this.f14756h.set(textView.getPaint());
            this.f14756h.setTextSize(i10);
            StaticLayout staticLayoutA = AbstractC1193K.a(text, (Layout.Alignment) e(textView, "getLayoutAlignment", Layout.Alignment.ALIGN_NORMAL), Math.round(rectF.right), iB, textView, this.f14756h, this.f14758k);
            if ((iB == -1 || (staticLayoutA.getLineCount() <= iB && staticLayoutA.getLineEnd(staticLayoutA.getLineCount() - 1) == text.length())) && staticLayoutA.getHeight() <= rectF.bottom) {
                int i11 = i9 + 1;
                i5 = i8;
                i8 = i11;
            } else {
                i5 = i9 - 1;
                i = i5;
            }
        }
        return this.f14754f[i5];
    }

    public final boolean f() {
        return j() && this.f14749a != 0;
    }

    public final void g(int i, float f3) {
        Context context = this.f14757j;
        float fApplyDimension = TypedValue.applyDimension(i, f3, (context == null ? Resources.getSystem() : context.getResources()).getDisplayMetrics());
        TextView textView = this.i;
        if (fApplyDimension != textView.getPaint().getTextSize()) {
            textView.getPaint().setTextSize(fApplyDimension);
            boolean zA = AbstractC1192J.a(textView);
            if (textView.getLayout() != null) {
                this.f14750b = false;
                try {
                    Method methodD = d("nullLayouts");
                    if (methodD != null) {
                        methodD.invoke(textView, null);
                    }
                } catch (Exception e8) {
                    Log.w("ACTVAutoSizeHelper", "Failed to invoke TextView#nullLayouts() method", e8);
                }
                if (zA) {
                    textView.forceLayout();
                } else {
                    textView.requestLayout();
                }
                textView.invalidate();
            }
        }
    }

    public final boolean h() {
        if (j() && this.f14749a == 1) {
            if (!this.f14755g || this.f14754f.length == 0) {
                int iFloor = ((int) Math.floor((this.f14753e - this.f14752d) / this.f14751c)) + 1;
                int[] iArr = new int[iFloor];
                for (int i = 0; i < iFloor; i++) {
                    iArr[i] = Math.round((i * this.f14751c) + this.f14752d);
                }
                this.f14754f = b(iArr);
            }
            this.f14750b = true;
        } else {
            this.f14750b = false;
        }
        return this.f14750b;
    }

    public final boolean i() {
        boolean z8 = this.f14754f.length > 0;
        this.f14755g = z8;
        if (z8) {
            this.f14749a = 1;
            this.f14752d = r0[0];
            this.f14753e = r0[r1 - 1];
            this.f14751c = -1.0f;
        }
        return z8;
    }

    public final boolean j() {
        return !(this.i instanceof r);
    }

    public final void k(float f3, float f7, float f8) {
        if (f3 <= 0.0f) {
            throw new IllegalArgumentException("Minimum auto-size text size (" + f3 + "px) is less or equal to (0px)");
        }
        if (f7 <= f3) {
            throw new IllegalArgumentException("Maximum auto-size text size (" + f7 + "px) is less or equal to minimum auto-size text size (" + f3 + "px)");
        }
        if (f8 <= 0.0f) {
            throw new IllegalArgumentException("The auto-size step granularity (" + f8 + "px) is less or equal to (0px)");
        }
        this.f14749a = 1;
        this.f14752d = f3;
        this.f14753e = f7;
        this.f14751c = f8;
        this.f14755g = false;
    }
}
