Rabbit-R1/android (non root)/java/sources/androidx/appcompat/widget/AppCompatTextViewAutoSizeHelper.java
2024-05-21 17:08:36 -04:00

532 lines
24 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.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.view.View;
import android.widget.TextView;
import androidx.appcompat.R;
import androidx.core.view.ViewCompat;
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: Access modifiers changed from: package-private */
/* loaded from: classes.dex */
public class AppCompatTextViewAutoSizeHelper {
private static final int DEFAULT_AUTO_SIZE_GRANULARITY_IN_PX = 1;
private static final int DEFAULT_AUTO_SIZE_MAX_TEXT_SIZE_IN_SP = 112;
private static final int DEFAULT_AUTO_SIZE_MIN_TEXT_SIZE_IN_SP = 12;
private static final String TAG = "ACTVAutoSizeHelper";
static final float UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE = -1.0f;
private static final int VERY_WIDE = 1048576;
private final Context mContext;
private TextPaint mTempTextPaint;
private final TextView mTextView;
private static final RectF TEMP_RECTF = new RectF();
private static ConcurrentHashMap<String, Method> sTextViewMethodByNameCache = new ConcurrentHashMap<>();
private static ConcurrentHashMap<String, Field> sTextViewFieldByNameCache = new ConcurrentHashMap<>();
private int mAutoSizeTextType = 0;
private boolean mNeedsAutoSizeText = false;
private float mAutoSizeStepGranularityInPx = UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE;
private float mAutoSizeMinTextSizeInPx = UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE;
private float mAutoSizeMaxTextSizeInPx = UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE;
private int[] mAutoSizeTextSizesInPx = new int[0];
private boolean mHasPresetAutoSizeValues = false;
private final Impl mImpl = new Impl29();
private void clearAutoSizeConfiguration() {
this.mAutoSizeTextType = 0;
this.mAutoSizeMinTextSizeInPx = UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE;
this.mAutoSizeMaxTextSizeInPx = UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE;
this.mAutoSizeStepGranularityInPx = UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE;
this.mAutoSizeTextSizesInPx = new int[0];
this.mNeedsAutoSizeText = false;
}
/* JADX INFO: Access modifiers changed from: package-private */
public int[] getAutoSizeTextAvailableSizes() {
return this.mAutoSizeTextSizesInPx;
}
/* JADX INFO: Access modifiers changed from: package-private */
public int getAutoSizeTextType() {
return this.mAutoSizeTextType;
}
/* JADX INFO: Access modifiers changed from: private */
/* loaded from: classes.dex */
public static class Impl {
void computeAndSetTextDirection(StaticLayout.Builder builder, TextView textView) {
}
Impl() {
}
boolean isHorizontallyScrollable(TextView textView) {
return ((Boolean) AppCompatTextViewAutoSizeHelper.invokeAndReturnWithDefault(textView, "getHorizontallyScrolling", false)).booleanValue();
}
}
/* loaded from: classes.dex */
private static class Impl23 extends Impl {
Impl23() {
}
@Override // androidx.appcompat.widget.AppCompatTextViewAutoSizeHelper.Impl
void computeAndSetTextDirection(StaticLayout.Builder builder, TextView textView) {
builder.setTextDirection((TextDirectionHeuristic) AppCompatTextViewAutoSizeHelper.invokeAndReturnWithDefault(textView, "getTextDirectionHeuristic", TextDirectionHeuristics.FIRSTSTRONG_LTR));
}
}
/* loaded from: classes.dex */
private static class Impl29 extends Impl23 {
Impl29() {
}
@Override // androidx.appcompat.widget.AppCompatTextViewAutoSizeHelper.Impl
boolean isHorizontallyScrollable(TextView textView) {
return textView.isHorizontallyScrollable();
}
@Override // androidx.appcompat.widget.AppCompatTextViewAutoSizeHelper.Impl23, androidx.appcompat.widget.AppCompatTextViewAutoSizeHelper.Impl
void computeAndSetTextDirection(StaticLayout.Builder builder, TextView textView) {
builder.setTextDirection(textView.getTextDirectionHeuristic());
}
}
/* JADX INFO: Access modifiers changed from: package-private */
public AppCompatTextViewAutoSizeHelper(TextView textView) {
this.mTextView = textView;
this.mContext = textView.getContext();
}
/* JADX INFO: Access modifiers changed from: package-private */
public void loadFromAttributes(AttributeSet attributeSet, int i) {
int resourceId;
TypedArray obtainStyledAttributes = this.mContext.obtainStyledAttributes(attributeSet, R.styleable.AppCompatTextView, i, 0);
TextView textView = this.mTextView;
ViewCompat.saveAttributeDataForStyleable(textView, textView.getContext(), R.styleable.AppCompatTextView, attributeSet, obtainStyledAttributes, i, 0);
if (obtainStyledAttributes.hasValue(R.styleable.AppCompatTextView_autoSizeTextType)) {
this.mAutoSizeTextType = obtainStyledAttributes.getInt(R.styleable.AppCompatTextView_autoSizeTextType, 0);
}
float dimension = obtainStyledAttributes.hasValue(R.styleable.AppCompatTextView_autoSizeStepGranularity) ? obtainStyledAttributes.getDimension(R.styleable.AppCompatTextView_autoSizeStepGranularity, UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE) : -1.0f;
float dimension2 = obtainStyledAttributes.hasValue(R.styleable.AppCompatTextView_autoSizeMinTextSize) ? obtainStyledAttributes.getDimension(R.styleable.AppCompatTextView_autoSizeMinTextSize, UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE) : -1.0f;
float dimension3 = obtainStyledAttributes.hasValue(R.styleable.AppCompatTextView_autoSizeMaxTextSize) ? obtainStyledAttributes.getDimension(R.styleable.AppCompatTextView_autoSizeMaxTextSize, UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE) : -1.0f;
if (obtainStyledAttributes.hasValue(R.styleable.AppCompatTextView_autoSizePresetSizes) && (resourceId = obtainStyledAttributes.getResourceId(R.styleable.AppCompatTextView_autoSizePresetSizes, 0)) > 0) {
TypedArray obtainTypedArray = obtainStyledAttributes.getResources().obtainTypedArray(resourceId);
setupAutoSizeUniformPresetSizes(obtainTypedArray);
obtainTypedArray.recycle();
}
obtainStyledAttributes.recycle();
if (!supportsAutoSizeText()) {
this.mAutoSizeTextType = 0;
return;
}
if (this.mAutoSizeTextType == 1) {
if (!this.mHasPresetAutoSizeValues) {
DisplayMetrics displayMetrics = this.mContext.getResources().getDisplayMetrics();
if (dimension2 == UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE) {
dimension2 = TypedValue.applyDimension(2, 12.0f, displayMetrics);
}
if (dimension3 == UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE) {
dimension3 = TypedValue.applyDimension(2, 112.0f, displayMetrics);
}
if (dimension == UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE) {
dimension = 1.0f;
}
validateAndSetAutoSizeTextTypeUniformConfiguration(dimension2, dimension3, dimension);
}
setupAutoSizeText();
}
}
/* JADX INFO: Access modifiers changed from: package-private */
public void setAutoSizeTextTypeWithDefaults(int i) {
if (supportsAutoSizeText()) {
if (i == 0) {
clearAutoSizeConfiguration();
return;
}
if (i == 1) {
DisplayMetrics displayMetrics = this.mContext.getResources().getDisplayMetrics();
validateAndSetAutoSizeTextTypeUniformConfiguration(TypedValue.applyDimension(2, 12.0f, displayMetrics), TypedValue.applyDimension(2, 112.0f, displayMetrics), 1.0f);
if (setupAutoSizeText()) {
autoSizeText();
return;
}
return;
}
throw new IllegalArgumentException("Unknown auto-size text type: " + i);
}
}
/* JADX INFO: Access modifiers changed from: package-private */
public void setAutoSizeTextTypeUniformWithConfiguration(int i, int i2, int i3, int i4) throws IllegalArgumentException {
if (supportsAutoSizeText()) {
DisplayMetrics displayMetrics = this.mContext.getResources().getDisplayMetrics();
validateAndSetAutoSizeTextTypeUniformConfiguration(TypedValue.applyDimension(i4, i, displayMetrics), TypedValue.applyDimension(i4, i2, displayMetrics), TypedValue.applyDimension(i4, i3, displayMetrics));
if (setupAutoSizeText()) {
autoSizeText();
}
}
}
/* JADX INFO: Access modifiers changed from: package-private */
public void setAutoSizeTextTypeUniformWithPresetSizes(int[] iArr, int i) throws IllegalArgumentException {
if (supportsAutoSizeText()) {
int length = iArr.length;
if (length > 0) {
int[] iArr2 = new int[length];
if (i == 0) {
iArr2 = Arrays.copyOf(iArr, length);
} else {
DisplayMetrics displayMetrics = this.mContext.getResources().getDisplayMetrics();
for (int i2 = 0; i2 < length; i2++) {
iArr2[i2] = Math.round(TypedValue.applyDimension(i, iArr[i2], displayMetrics));
}
}
this.mAutoSizeTextSizesInPx = cleanupAutoSizePresetSizes(iArr2);
if (!setupAutoSizeUniformPresetSizesConfiguration()) {
throw new IllegalArgumentException("None of the preset sizes is valid: " + Arrays.toString(iArr));
}
} else {
this.mHasPresetAutoSizeValues = false;
}
if (setupAutoSizeText()) {
autoSizeText();
}
}
}
/* JADX INFO: Access modifiers changed from: package-private */
public int getAutoSizeStepGranularity() {
return Math.round(this.mAutoSizeStepGranularityInPx);
}
/* JADX INFO: Access modifiers changed from: package-private */
public int getAutoSizeMinTextSize() {
return Math.round(this.mAutoSizeMinTextSizeInPx);
}
/* JADX INFO: Access modifiers changed from: package-private */
public int getAutoSizeMaxTextSize() {
return Math.round(this.mAutoSizeMaxTextSizeInPx);
}
private void setupAutoSizeUniformPresetSizes(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.mAutoSizeTextSizesInPx = cleanupAutoSizePresetSizes(iArr);
setupAutoSizeUniformPresetSizesConfiguration();
}
}
private boolean setupAutoSizeUniformPresetSizesConfiguration() {
boolean z = this.mAutoSizeTextSizesInPx.length > 0;
this.mHasPresetAutoSizeValues = z;
if (z) {
this.mAutoSizeTextType = 1;
this.mAutoSizeMinTextSizeInPx = r0[0];
this.mAutoSizeMaxTextSizeInPx = r0[r1 - 1];
this.mAutoSizeStepGranularityInPx = UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE;
}
return z;
}
private int[] cleanupAutoSizePresetSizes(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 validateAndSetAutoSizeTextTypeUniformConfiguration(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) {
throw new IllegalArgumentException("The auto-size step granularity (" + f3 + "px) is less or equal to (0px)");
}
this.mAutoSizeTextType = 1;
this.mAutoSizeMinTextSizeInPx = f;
this.mAutoSizeMaxTextSizeInPx = f2;
this.mAutoSizeStepGranularityInPx = f3;
this.mHasPresetAutoSizeValues = false;
}
private boolean setupAutoSizeText() {
if (supportsAutoSizeText() && this.mAutoSizeTextType == 1) {
if (!this.mHasPresetAutoSizeValues || this.mAutoSizeTextSizesInPx.length == 0) {
int floor = ((int) Math.floor((this.mAutoSizeMaxTextSizeInPx - this.mAutoSizeMinTextSizeInPx) / this.mAutoSizeStepGranularityInPx)) + 1;
int[] iArr = new int[floor];
for (int i = 0; i < floor; i++) {
iArr[i] = Math.round(this.mAutoSizeMinTextSizeInPx + (i * this.mAutoSizeStepGranularityInPx));
}
this.mAutoSizeTextSizesInPx = cleanupAutoSizePresetSizes(iArr);
}
this.mNeedsAutoSizeText = true;
} else {
this.mNeedsAutoSizeText = false;
}
return this.mNeedsAutoSizeText;
}
/* JADX INFO: Access modifiers changed from: package-private */
public void autoSizeText() {
if (isAutoSizeEnabled()) {
if (this.mNeedsAutoSizeText) {
if (this.mTextView.getMeasuredHeight() <= 0 || this.mTextView.getMeasuredWidth() <= 0) {
return;
}
int measuredWidth = this.mImpl.isHorizontallyScrollable(this.mTextView) ? 1048576 : (this.mTextView.getMeasuredWidth() - this.mTextView.getTotalPaddingLeft()) - this.mTextView.getTotalPaddingRight();
int height = (this.mTextView.getHeight() - this.mTextView.getCompoundPaddingBottom()) - this.mTextView.getCompoundPaddingTop();
if (measuredWidth <= 0 || height <= 0) {
return;
}
RectF rectF = TEMP_RECTF;
synchronized (rectF) {
rectF.setEmpty();
rectF.right = measuredWidth;
rectF.bottom = height;
float findLargestTextSizeWhichFits = findLargestTextSizeWhichFits(rectF);
if (findLargestTextSizeWhichFits != this.mTextView.getTextSize()) {
setTextSizeInternal(0, findLargestTextSizeWhichFits);
}
}
}
this.mNeedsAutoSizeText = true;
}
}
/* JADX INFO: Access modifiers changed from: package-private */
public void setTextSizeInternal(int i, float f) {
Resources resources;
Context context = this.mContext;
if (context == null) {
resources = Resources.getSystem();
} else {
resources = context.getResources();
}
setRawTextSize(TypedValue.applyDimension(i, f, resources.getDisplayMetrics()));
}
private void setRawTextSize(float f) {
if (f != this.mTextView.getPaint().getTextSize()) {
this.mTextView.getPaint().setTextSize(f);
boolean isInLayout = Api18Impl.isInLayout(this.mTextView);
if (this.mTextView.getLayout() != null) {
this.mNeedsAutoSizeText = false;
try {
Method textViewMethod = getTextViewMethod("nullLayouts");
if (textViewMethod != null) {
textViewMethod.invoke(this.mTextView, new Object[0]);
}
} catch (Exception e) {
Log.w(TAG, "Failed to invoke TextView#nullLayouts() method", e);
}
if (!isInLayout) {
this.mTextView.requestLayout();
} else {
this.mTextView.forceLayout();
}
this.mTextView.invalidate();
}
}
}
private int findLargestTextSizeWhichFits(RectF rectF) {
int length = this.mAutoSizeTextSizesInPx.length;
if (length == 0) {
throw new IllegalStateException("No available text sizes to choose from.");
}
int i = 1;
int i2 = length - 1;
int i3 = 0;
while (i <= i2) {
int i4 = (i + i2) / 2;
if (suggestedSizeFitsInSpace(this.mAutoSizeTextSizesInPx[i4], rectF)) {
int i5 = i4 + 1;
i3 = i;
i = i5;
} else {
i3 = i4 - 1;
i2 = i3;
}
}
return this.mAutoSizeTextSizesInPx[i3];
}
void initTempTextPaint(int i) {
TextPaint textPaint = this.mTempTextPaint;
if (textPaint == null) {
this.mTempTextPaint = new TextPaint();
} else {
textPaint.reset();
}
this.mTempTextPaint.set(this.mTextView.getPaint());
this.mTempTextPaint.setTextSize(i);
}
StaticLayout createLayout(CharSequence charSequence, Layout.Alignment alignment, int i, int i2) {
return Api23Impl.createStaticLayoutForMeasuring(charSequence, alignment, i, i2, this.mTextView, this.mTempTextPaint, this.mImpl);
}
private boolean suggestedSizeFitsInSpace(int i, RectF rectF) {
CharSequence transformation;
CharSequence text = this.mTextView.getText();
TransformationMethod transformationMethod = this.mTextView.getTransformationMethod();
if (transformationMethod != null && (transformation = transformationMethod.getTransformation(text, this.mTextView)) != null) {
text = transformation;
}
int maxLines = Api16Impl.getMaxLines(this.mTextView);
initTempTextPaint(i);
StaticLayout createLayout = createLayout(text, (Layout.Alignment) invokeAndReturnWithDefault(this.mTextView, "getLayoutAlignment", Layout.Alignment.ALIGN_NORMAL), Math.round(rectF.right), maxLines);
return (maxLines == -1 || (createLayout.getLineCount() <= maxLines && createLayout.getLineEnd(createLayout.getLineCount() - 1) == text.length())) && ((float) createLayout.getHeight()) <= rectF.bottom;
}
private StaticLayout createStaticLayoutForMeasuringPre16(CharSequence charSequence, Layout.Alignment alignment, int i) {
return new StaticLayout(charSequence, this.mTempTextPaint, i, alignment, ((Float) accessAndReturnWithDefault(this.mTextView, "mSpacingMult", Float.valueOf(1.0f))).floatValue(), ((Float) accessAndReturnWithDefault(this.mTextView, "mSpacingAdd", Float.valueOf(0.0f))).floatValue(), ((Boolean) accessAndReturnWithDefault(this.mTextView, "mIncludePad", true)).booleanValue());
}
static <T> T invokeAndReturnWithDefault(Object obj, String str, T t) {
try {
return (T) getTextViewMethod(str).invoke(obj, new Object[0]);
} catch (Exception e) {
Log.w(TAG, "Failed to invoke TextView#" + str + "() method", e);
return t;
}
}
private static <T> T accessAndReturnWithDefault(Object obj, String str, T t) {
try {
Field textViewField = getTextViewField(str);
return textViewField == null ? t : (T) textViewField.get(obj);
} catch (IllegalAccessException e) {
Log.w(TAG, "Failed to access TextView#" + str + " member", e);
return t;
}
}
private static Method getTextViewMethod(String str) {
try {
Method method = sTextViewMethodByNameCache.get(str);
if (method == null && (method = TextView.class.getDeclaredMethod(str, new Class[0])) != null) {
method.setAccessible(true);
sTextViewMethodByNameCache.put(str, method);
}
return method;
} catch (Exception e) {
Log.w(TAG, "Failed to retrieve TextView#" + str + "() method", e);
return null;
}
}
private static Field getTextViewField(String str) {
try {
Field field = sTextViewFieldByNameCache.get(str);
if (field == null && (field = TextView.class.getDeclaredField(str)) != null) {
field.setAccessible(true);
sTextViewFieldByNameCache.put(str, field);
}
return field;
} catch (NoSuchFieldException e) {
Log.w(TAG, "Failed to access TextView#" + str + " member", e);
return null;
}
}
/* JADX INFO: Access modifiers changed from: package-private */
public boolean isAutoSizeEnabled() {
return supportsAutoSizeText() && this.mAutoSizeTextType != 0;
}
private boolean supportsAutoSizeText() {
return !(this.mTextView instanceof AppCompatEditText);
}
/* JADX INFO: Access modifiers changed from: private */
/* loaded from: classes.dex */
public static final class Api23Impl {
private Api23Impl() {
}
static StaticLayout createStaticLayoutForMeasuring(CharSequence charSequence, Layout.Alignment alignment, int i, int i2, TextView textView, TextPaint textPaint, Impl impl) {
StaticLayout.Builder obtain = StaticLayout.Builder.obtain(charSequence, 0, charSequence.length(), textPaint, i);
StaticLayout.Builder hyphenationFrequency = obtain.setAlignment(alignment).setLineSpacing(textView.getLineSpacingExtra(), textView.getLineSpacingMultiplier()).setIncludePad(textView.getIncludeFontPadding()).setBreakStrategy(textView.getBreakStrategy()).setHyphenationFrequency(textView.getHyphenationFrequency());
if (i2 == -1) {
i2 = Integer.MAX_VALUE;
}
hyphenationFrequency.setMaxLines(i2);
try {
impl.computeAndSetTextDirection(obtain, textView);
} catch (ClassCastException unused) {
Log.w(AppCompatTextViewAutoSizeHelper.TAG, "Failed to obtain TextDirectionHeuristic, auto size may be incorrect");
}
return obtain.build();
}
}
/* JADX INFO: Access modifiers changed from: private */
/* loaded from: classes.dex */
public static final class Api18Impl {
private Api18Impl() {
}
static boolean isInLayout(View view) {
return view.isInLayout();
}
}
/* JADX INFO: Access modifiers changed from: private */
/* loaded from: classes.dex */
public static final class Api16Impl {
private Api16Impl() {
}
static int getMaxLines(TextView textView) {
return textView.getMaxLines();
}
static StaticLayout createStaticLayoutForMeasuring(CharSequence charSequence, Layout.Alignment alignment, int i, TextView textView, TextPaint textPaint) {
return new StaticLayout(charSequence, textPaint, i, alignment, textView.getLineSpacingMultiplier(), textView.getLineSpacingExtra(), textView.getIncludeFontPadding());
}
}
}