jimu-decompiled/sources/androidx/appcompat/widget/AppCompatTextViewAutoSizeHelper.java
2025-05-13 19:24:51 +02:00

420 lines
15 KiB
Java

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 androidx.appcompat.R$styleable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
/* loaded from: classes.dex */
class AppCompatTextViewAutoSizeHelper {
private static final RectF k = new RectF();
private static ConcurrentHashMap<String, Method> l = 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];
private boolean g = false;
private TextPaint h;
private final TextView i;
private final Context j;
AppCompatTextViewAutoSizeHelper(TextView textView) {
this.i = textView;
this.j = this.i.getContext();
}
private void h() {
this.a = 0;
this.d = -1.0f;
this.e = -1.0f;
this.c = -1.0f;
this.f = new int[0];
this.b = false;
}
private boolean i() {
if (k() && this.a == 1) {
if (!this.g || this.f.length == 0) {
float round = Math.round(this.d);
int i = 1;
while (Math.round(this.c + round) <= Math.round(this.e)) {
i++;
round += this.c;
}
int[] iArr = new int[i];
float f = this.d;
for (int i2 = 0; i2 < i; i2++) {
iArr[i2] = Math.round(f);
f += this.c;
}
this.f = a(iArr);
}
this.b = true;
} else {
this.b = false;
}
return this.b;
}
private boolean j() {
this.g = this.f.length > 0;
if (this.g) {
this.a = 1;
int[] iArr = this.f;
this.d = iArr[0];
this.e = iArr[r0 - 1];
this.c = -1.0f;
}
return this.g;
}
private boolean k() {
return !(this.i instanceof AppCompatEditText);
}
void a(AttributeSet attributeSet, int i) {
int resourceId;
TypedArray obtainStyledAttributes = this.j.obtainStyledAttributes(attributeSet, R$styleable.AppCompatTextView, i, 0);
if (obtainStyledAttributes.hasValue(R$styleable.AppCompatTextView_autoSizeTextType)) {
this.a = obtainStyledAttributes.getInt(R$styleable.AppCompatTextView_autoSizeTextType, 0);
}
float dimension = obtainStyledAttributes.hasValue(R$styleable.AppCompatTextView_autoSizeStepGranularity) ? obtainStyledAttributes.getDimension(R$styleable.AppCompatTextView_autoSizeStepGranularity, -1.0f) : -1.0f;
float dimension2 = obtainStyledAttributes.hasValue(R$styleable.AppCompatTextView_autoSizeMinTextSize) ? obtainStyledAttributes.getDimension(R$styleable.AppCompatTextView_autoSizeMinTextSize, -1.0f) : -1.0f;
float dimension3 = obtainStyledAttributes.hasValue(R$styleable.AppCompatTextView_autoSizeMaxTextSize) ? obtainStyledAttributes.getDimension(R$styleable.AppCompatTextView_autoSizeMaxTextSize, -1.0f) : -1.0f;
if (obtainStyledAttributes.hasValue(R$styleable.AppCompatTextView_autoSizePresetSizes) && (resourceId = obtainStyledAttributes.getResourceId(R$styleable.AppCompatTextView_autoSizePresetSizes, 0)) > 0) {
TypedArray obtainTypedArray = obtainStyledAttributes.getResources().obtainTypedArray(resourceId);
a(obtainTypedArray);
obtainTypedArray.recycle();
}
obtainStyledAttributes.recycle();
if (!k()) {
this.a = 0;
return;
}
if (this.a == 1) {
if (!this.g) {
DisplayMetrics displayMetrics = this.j.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;
}
a(dimension2, dimension3, dimension);
}
i();
}
}
int b() {
return Math.round(this.e);
}
int c() {
return Math.round(this.d);
}
int d() {
return Math.round(this.c);
}
int[] e() {
return this.f;
}
int f() {
return this.a;
}
boolean g() {
return k() && this.a != 0;
}
void a(int i) {
if (k()) {
if (i == 0) {
h();
return;
}
if (i == 1) {
DisplayMetrics displayMetrics = this.j.getResources().getDisplayMetrics();
a(TypedValue.applyDimension(2, 12.0f, displayMetrics), TypedValue.applyDimension(2, 112.0f, displayMetrics), 1.0f);
if (i()) {
a();
return;
}
return;
}
throw new IllegalArgumentException("Unknown auto-size text type: " + i);
}
}
void a(int i, int i2, int i3, int i4) throws IllegalArgumentException {
if (k()) {
DisplayMetrics displayMetrics = this.j.getResources().getDisplayMetrics();
a(TypedValue.applyDimension(i4, i, displayMetrics), TypedValue.applyDimension(i4, i2, displayMetrics), TypedValue.applyDimension(i4, i3, displayMetrics));
if (i()) {
a();
}
}
}
void a(int[] iArr, int i) throws IllegalArgumentException {
if (k()) {
int length = iArr.length;
if (length > 0) {
int[] iArr2 = new int[length];
if (i == 0) {
iArr2 = Arrays.copyOf(iArr, length);
} else {
DisplayMetrics displayMetrics = this.j.getResources().getDisplayMetrics();
for (int i2 = 0; i2 < length; i2++) {
iArr2[i2] = Math.round(TypedValue.applyDimension(i, iArr[i2], displayMetrics));
}
}
this.f = a(iArr2);
if (!j()) {
throw new IllegalArgumentException("None of the preset sizes is valid: " + Arrays.toString(iArr));
}
} else {
this.g = false;
}
if (i()) {
a();
}
}
}
private void a(TypedArray typedArray) {
int length = typedArray.length();
int[] iArr = new int[length];
if (length > 0) {
for (int i = 0; i < length; i++) {
iArr[i] = typedArray.getDimensionPixelSize(i, -1);
}
this.f = a(iArr);
j();
}
}
private int[] a(int[] iArr) {
int length = iArr.length;
if (length == 0) {
return iArr;
}
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()) {
return iArr;
}
int size = arrayList.size();
int[] iArr2 = new int[size];
for (int i2 = 0; i2 < size; i2++) {
iArr2[i2] = ((Integer) arrayList.get(i2)).intValue();
}
return iArr2;
}
private void a(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) {
this.a = 1;
this.d = f;
this.e = f2;
this.c = f3;
this.g = false;
return;
}
throw new IllegalArgumentException("The auto-size step granularity (" + f3 + "px) is less or equal to (0px)");
}
void a() {
if (g()) {
if (this.b) {
if (this.i.getMeasuredHeight() <= 0 || this.i.getMeasuredWidth() <= 0) {
return;
}
int measuredWidth = ((Boolean) a((Object) this.i, "getHorizontallyScrolling", (String) false)).booleanValue() ? 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;
}
synchronized (k) {
k.setEmpty();
k.right = measuredWidth;
k.bottom = height;
float a = a(k);
if (a != this.i.getTextSize()) {
a(0, a);
}
}
}
this.b = true;
}
}
void a(int i, float f) {
Resources resources;
Context context = this.j;
if (context == null) {
resources = Resources.getSystem();
} else {
resources = context.getResources();
}
a(TypedValue.applyDimension(i, f, resources.getDisplayMetrics()));
}
private void a(float f) {
if (f != this.i.getPaint().getTextSize()) {
this.i.getPaint().setTextSize(f);
boolean isInLayout = Build.VERSION.SDK_INT >= 18 ? this.i.isInLayout() : false;
if (this.i.getLayout() != null) {
this.b = false;
try {
Method a = a("nullLayouts");
if (a != null) {
a.invoke(this.i, new Object[0]);
}
} catch (Exception e) {
Log.w("ACTVAutoSizeHelper", "Failed to invoke TextView#nullLayouts() method", e);
}
if (!isInLayout) {
this.i.requestLayout();
} else {
this.i.forceLayout();
}
this.i.invalidate();
}
}
}
private int a(RectF rectF) {
int length = this.f.length;
if (length != 0) {
int i = length - 1;
int i2 = 1;
int i3 = 0;
while (i2 <= i) {
int i4 = (i2 + i) / 2;
if (a(this.f[i4], rectF)) {
int i5 = i4 + 1;
i3 = i2;
i2 = i5;
} else {
i3 = i4 - 1;
i = i3;
}
}
return this.f[i3];
}
throw new IllegalStateException("No available text sizes to choose from.");
}
private boolean a(int i, RectF rectF) {
StaticLayout a;
CharSequence transformation;
CharSequence text = this.i.getText();
TransformationMethod transformationMethod = this.i.getTransformationMethod();
if (transformationMethod != null && (transformation = transformationMethod.getTransformation(text, this.i)) != null) {
text = transformation;
}
int maxLines = Build.VERSION.SDK_INT >= 16 ? this.i.getMaxLines() : -1;
TextPaint textPaint = this.h;
if (textPaint == null) {
this.h = new TextPaint();
} else {
textPaint.reset();
}
this.h.set(this.i.getPaint());
this.h.setTextSize(i);
Layout.Alignment alignment = (Layout.Alignment) a((Object) this.i, "getLayoutAlignment", (String) Layout.Alignment.ALIGN_NORMAL);
if (Build.VERSION.SDK_INT >= 23) {
a = a(text, alignment, Math.round(rectF.right), maxLines);
} else {
a = a(text, alignment, Math.round(rectF.right));
}
return (maxLines == -1 || (a.getLineCount() <= maxLines && a.getLineEnd(a.getLineCount() - 1) == text.length())) && ((float) a.getHeight()) <= rectF.bottom;
}
private StaticLayout a(CharSequence charSequence, Layout.Alignment alignment, int i, int i2) {
TextDirectionHeuristic textDirectionHeuristic = (TextDirectionHeuristic) a((Object) this.i, "getTextDirectionHeuristic", (String) TextDirectionHeuristics.FIRSTSTRONG_LTR);
StaticLayout.Builder hyphenationFrequency = StaticLayout.Builder.obtain(charSequence, 0, charSequence.length(), this.h, i).setAlignment(alignment).setLineSpacing(this.i.getLineSpacingExtra(), this.i.getLineSpacingMultiplier()).setIncludePad(this.i.getIncludeFontPadding()).setBreakStrategy(this.i.getBreakStrategy()).setHyphenationFrequency(this.i.getHyphenationFrequency());
if (i2 == -1) {
i2 = Integer.MAX_VALUE;
}
return hyphenationFrequency.setMaxLines(i2).setTextDirection(textDirectionHeuristic).build();
}
private StaticLayout a(CharSequence charSequence, Layout.Alignment alignment, int i) {
float floatValue;
float floatValue2;
boolean booleanValue;
if (Build.VERSION.SDK_INT >= 16) {
floatValue = this.i.getLineSpacingMultiplier();
floatValue2 = this.i.getLineSpacingExtra();
booleanValue = this.i.getIncludeFontPadding();
} else {
floatValue = ((Float) a((Object) this.i, "getLineSpacingMultiplier", (String) Float.valueOf(1.0f))).floatValue();
floatValue2 = ((Float) a((Object) this.i, "getLineSpacingExtra", (String) Float.valueOf(0.0f))).floatValue();
booleanValue = ((Boolean) a((Object) this.i, "getIncludeFontPadding", (String) true)).booleanValue();
}
return new StaticLayout(charSequence, this.h, i, alignment, floatValue, floatValue2, booleanValue);
}
private <T> T a(Object obj, String str, T t) {
try {
return (T) a(str).invoke(obj, new Object[0]);
} catch (Exception e) {
Log.w("ACTVAutoSizeHelper", "Failed to invoke TextView#" + str + "() method", e);
return t;
}
}
private Method a(String str) {
try {
Method method = l.get(str);
if (method == null && (method = TextView.class.getDeclaredMethod(str, new Class[0])) != null) {
method.setAccessible(true);
l.put(str, method);
}
return method;
} catch (Exception e) {
Log.w("ACTVAutoSizeHelper", "Failed to retrieve TextView#" + str + "() method", e);
return null;
}
}
}