package com.google.android.material.internal;

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.TextUtils;
import java.lang.reflect.Constructor;

/* JADX INFO: compiled from: StaticLayoutBuilderCompat.java */
/* JADX INFO: loaded from: classes.dex */
final class j {

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    static final int f4093n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    private static boolean f4094o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    private static Constructor<StaticLayout> f4095p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    private static Object f4096q;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private CharSequence f4097a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final TextPaint f4098b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final int f4099c;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private int f4101e;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private boolean f4108l;

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private Layout.Alignment f4102f = Layout.Alignment.ALIGN_NORMAL;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private int f4103g = Integer.MAX_VALUE;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private float f4104h = 0.0f;

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private int f4106j = f4093n;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private boolean f4107k = true;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private TextUtils.TruncateAt f4109m = null;

    /* JADX INFO: compiled from: StaticLayoutBuilderCompat.java */
    static class a extends Exception {
        a(Throwable th) {
            super("Error thrown initializing StaticLayout " + th.getMessage(), th);
        }
    }

    static {
        f4093n = Build.VERSION.SDK_INT >= 23 ? 1 : 0;
    }

    private j(CharSequence charSequence, TextPaint textPaint, int i3) {
        this.f4097a = charSequence;
        this.f4098b = textPaint;
        this.f4099c = i3;
        this.f4101e = charSequence.length();
    }

    private void b() throws a {
        Class<?> cls;
        if (f4094o) {
            return;
        }
        try {
            boolean z2 = this.f4108l && Build.VERSION.SDK_INT >= 23;
            if (Build.VERSION.SDK_INT >= 18) {
                cls = TextDirectionHeuristic.class;
                f4096q = z2 ? TextDirectionHeuristics.RTL : TextDirectionHeuristics.LTR;
            } else {
                ClassLoader classLoader = j.class.getClassLoader();
                String str = this.f4108l ? "RTL" : "LTR";
                Class<?> clsLoadClass = classLoader.loadClass("android.text.TextDirectionHeuristic");
                Class<?> clsLoadClass2 = classLoader.loadClass("android.text.TextDirectionHeuristics");
                f4096q = clsLoadClass2.getField(str).get(clsLoadClass2);
                cls = clsLoadClass;
            }
            Class cls2 = Integer.TYPE;
            Class cls3 = Float.TYPE;
            Constructor<StaticLayout> declaredConstructor = StaticLayout.class.getDeclaredConstructor(CharSequence.class, cls2, cls2, TextPaint.class, cls2, Layout.Alignment.class, cls, cls3, cls3, Boolean.TYPE, TextUtils.TruncateAt.class, cls2, cls2);
            f4095p = declaredConstructor;
            declaredConstructor.setAccessible(true);
            f4094o = true;
        } catch (Exception e3) {
            throw new a(e3);
        }
    }

    public static j c(CharSequence charSequence, TextPaint textPaint, int i3) {
        return new j(charSequence, textPaint, i3);
    }

    public StaticLayout a() throws a {
        if (this.f4097a == null) {
            this.f4097a = "";
        }
        int iMax = Math.max(0, this.f4099c);
        CharSequence charSequenceEllipsize = this.f4097a;
        if (this.f4103g == 1) {
            charSequenceEllipsize = TextUtils.ellipsize(charSequenceEllipsize, this.f4098b, iMax, this.f4109m);
        }
        int iMin = Math.min(charSequenceEllipsize.length(), this.f4101e);
        this.f4101e = iMin;
        if (Build.VERSION.SDK_INT < 23) {
            b();
            try {
                return (StaticLayout) ((Constructor) androidx.core.util.h.f(f4095p)).newInstance(charSequenceEllipsize, Integer.valueOf(this.f4100d), Integer.valueOf(this.f4101e), this.f4098b, Integer.valueOf(iMax), this.f4102f, androidx.core.util.h.f(f4096q), Float.valueOf(1.0f), Float.valueOf(0.0f), Boolean.valueOf(this.f4107k), null, Integer.valueOf(iMax), Integer.valueOf(this.f4103g));
            } catch (Exception e3) {
                throw new a(e3);
            }
        }
        if (this.f4108l && this.f4103g == 1) {
            this.f4102f = Layout.Alignment.ALIGN_OPPOSITE;
        }
        StaticLayout.Builder builderObtain = StaticLayout.Builder.obtain(charSequenceEllipsize, this.f4100d, iMin, this.f4098b, iMax);
        builderObtain.setAlignment(this.f4102f);
        builderObtain.setIncludePad(this.f4107k);
        builderObtain.setTextDirection(this.f4108l ? TextDirectionHeuristics.RTL : TextDirectionHeuristics.LTR);
        TextUtils.TruncateAt truncateAt = this.f4109m;
        if (truncateAt != null) {
            builderObtain.setEllipsize(truncateAt);
        }
        builderObtain.setMaxLines(this.f4103g);
        float f3 = this.f4104h;
        if (f3 != 0.0f || this.f4105i != 1.0f) {
            builderObtain.setLineSpacing(f3, this.f4105i);
        }
        if (this.f4103g > 1) {
            builderObtain.setHyphenationFrequency(this.f4106j);
        }
        return builderObtain.build();
    }

    public j d(Layout.Alignment alignment) {
        this.f4102f = alignment;
        return this;
    }

    public j e(TextUtils.TruncateAt truncateAt) {
        this.f4109m = truncateAt;
        return this;
    }

    public j f(int i3) {
        this.f4106j = i3;
        return this;
    }

    public j g(boolean z2) {
        this.f4107k = z2;
        return this;
    }

    public j h(boolean z2) {
        this.f4108l = z2;
        return this;
    }

    public j i(float f3, float f4) {
        this.f4104h = f3;
        this.f4105i = f4;
        return this;
    }

    public j j(int i3) {
        this.f4103g = i3;
        return this;
    }
}
