159 lines
6.9 KiB
Java
159 lines
6.9 KiB
Java
package androidx.core.content.res;
|
|
|
|
import android.content.res.ColorStateList;
|
|
import android.content.res.Resources;
|
|
import android.content.res.TypedArray;
|
|
import android.graphics.Color;
|
|
import android.util.AttributeSet;
|
|
import android.util.Log;
|
|
import android.util.StateSet;
|
|
import android.util.TypedValue;
|
|
import android.util.Xml;
|
|
import androidx.annotation.ColorInt;
|
|
import androidx.annotation.ColorRes;
|
|
import androidx.annotation.FloatRange;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.annotation.RestrictTo;
|
|
import androidx.annotation.XmlRes;
|
|
import androidx.core.R;
|
|
import androidx.core.view.ViewCompat;
|
|
import java.io.IOException;
|
|
import org.xmlpull.v1.XmlPullParser;
|
|
import org.xmlpull.v1.XmlPullParserException;
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP_PREFIX})
|
|
public final class ColorStateListInflaterCompat {
|
|
private static final ThreadLocal<TypedValue> sTempTypedValue = new ThreadLocal<>();
|
|
|
|
private ColorStateListInflaterCompat() {
|
|
}
|
|
|
|
@NonNull
|
|
public static ColorStateList createFromXml(@NonNull Resources resources, @NonNull XmlPullParser xmlPullParser, @Nullable Resources.Theme theme) throws XmlPullParserException, IOException {
|
|
int next;
|
|
AttributeSet asAttributeSet = Xml.asAttributeSet(xmlPullParser);
|
|
do {
|
|
next = xmlPullParser.next();
|
|
if (next == 2) {
|
|
break;
|
|
}
|
|
} while (next != 1);
|
|
if (next == 2) {
|
|
return createFromXmlInner(resources, xmlPullParser, asAttributeSet, theme);
|
|
}
|
|
throw new XmlPullParserException("No start tag found");
|
|
}
|
|
|
|
@NonNull
|
|
public static ColorStateList createFromXmlInner(@NonNull Resources resources, @NonNull XmlPullParser xmlPullParser, @NonNull AttributeSet attributeSet, @Nullable Resources.Theme theme) throws XmlPullParserException, IOException {
|
|
String name = xmlPullParser.getName();
|
|
if (name.equals("selector")) {
|
|
return inflate(resources, xmlPullParser, attributeSet, theme);
|
|
}
|
|
throw new XmlPullParserException(xmlPullParser.getPositionDescription() + ": invalid color state list tag " + name);
|
|
}
|
|
|
|
@NonNull
|
|
private static TypedValue getTypedValue() {
|
|
ThreadLocal<TypedValue> threadLocal = sTempTypedValue;
|
|
TypedValue typedValue = threadLocal.get();
|
|
if (typedValue != null) {
|
|
return typedValue;
|
|
}
|
|
TypedValue typedValue2 = new TypedValue();
|
|
threadLocal.set(typedValue2);
|
|
return typedValue2;
|
|
}
|
|
|
|
@Nullable
|
|
public static ColorStateList inflate(@NonNull Resources resources, @XmlRes int i, @Nullable Resources.Theme theme) {
|
|
try {
|
|
return createFromXml(resources, resources.getXml(i), theme);
|
|
} catch (Exception e) {
|
|
Log.e("CSLCompat", "Failed to inflate ColorStateList.", e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private static ColorStateList inflate(@NonNull Resources resources, @NonNull XmlPullParser xmlPullParser, @NonNull AttributeSet attributeSet, @Nullable Resources.Theme theme) throws XmlPullParserException, IOException {
|
|
int depth;
|
|
int i;
|
|
int i2 = 1;
|
|
int depth2 = xmlPullParser.getDepth() + 1;
|
|
int[][] iArr = new int[20][];
|
|
int[] iArr2 = new int[20];
|
|
int i3 = 0;
|
|
while (true) {
|
|
int next = xmlPullParser.next();
|
|
if (next == i2 || ((depth = xmlPullParser.getDepth()) < depth2 && next == 3)) {
|
|
break;
|
|
}
|
|
if (next == 2 && depth <= depth2 && xmlPullParser.getName().equals("item")) {
|
|
TypedArray obtainAttributes = obtainAttributes(resources, theme, attributeSet, R.styleable.ColorStateListItem);
|
|
int i4 = R.styleable.ColorStateListItem_android_color;
|
|
int resourceId = obtainAttributes.getResourceId(i4, -1);
|
|
if (resourceId == -1 || isColorInt(resources, resourceId)) {
|
|
i = obtainAttributes.getColor(i4, -65281);
|
|
} else {
|
|
try {
|
|
i = createFromXml(resources, resources.getXml(resourceId), theme).getDefaultColor();
|
|
} catch (Exception unused) {
|
|
i = obtainAttributes.getColor(R.styleable.ColorStateListItem_android_color, -65281);
|
|
}
|
|
}
|
|
float f = 1.0f;
|
|
int i5 = R.styleable.ColorStateListItem_android_alpha;
|
|
if (obtainAttributes.hasValue(i5)) {
|
|
f = obtainAttributes.getFloat(i5, 1.0f);
|
|
} else {
|
|
int i6 = R.styleable.ColorStateListItem_alpha;
|
|
if (obtainAttributes.hasValue(i6)) {
|
|
f = obtainAttributes.getFloat(i6, 1.0f);
|
|
}
|
|
}
|
|
obtainAttributes.recycle();
|
|
int attributeCount = attributeSet.getAttributeCount();
|
|
int[] iArr3 = new int[attributeCount];
|
|
int i7 = 0;
|
|
for (int i8 = 0; i8 < attributeCount; i8++) {
|
|
int attributeNameResource = attributeSet.getAttributeNameResource(i8);
|
|
if (!(attributeNameResource == 16843173 || attributeNameResource == 16843551 || attributeNameResource == R.attr.alpha)) {
|
|
int i9 = i7 + 1;
|
|
if (!attributeSet.getAttributeBooleanValue(i8, false)) {
|
|
attributeNameResource = -attributeNameResource;
|
|
}
|
|
iArr3[i7] = attributeNameResource;
|
|
i7 = i9;
|
|
}
|
|
}
|
|
int[] trimStateSet = StateSet.trimStateSet(iArr3, i7);
|
|
iArr2 = GrowingArrayUtils.append(iArr2, i3, modulateColorAlpha(i, f));
|
|
iArr = (int[][]) GrowingArrayUtils.append(iArr, i3, trimStateSet);
|
|
i3++;
|
|
}
|
|
i2 = 1;
|
|
}
|
|
int[] iArr4 = new int[i3];
|
|
int[][] iArr5 = new int[i3][];
|
|
System.arraycopy(iArr2, 0, iArr4, 0, i3);
|
|
System.arraycopy(iArr, 0, iArr5, 0, i3);
|
|
return new ColorStateList(iArr5, iArr4);
|
|
}
|
|
|
|
private static boolean isColorInt(@NonNull Resources resources, @ColorRes int i) {
|
|
TypedValue typedValue = getTypedValue();
|
|
resources.getValue(i, typedValue, true);
|
|
int i2 = typedValue.type;
|
|
return i2 >= 28 && i2 <= 31;
|
|
}
|
|
|
|
@ColorInt
|
|
private static int modulateColorAlpha(@ColorInt int i, @FloatRange(from = 0.0d, to = 1.0d) float f) {
|
|
return (i & ViewCompat.MEASURED_SIZE_MASK) | (Math.round(((float) Color.alpha(i)) * f) << 24);
|
|
}
|
|
|
|
private static TypedArray obtainAttributes(Resources resources, Resources.Theme theme, AttributeSet attributeSet, int[] iArr) {
|
|
return theme == null ? resources.obtainAttributes(attributeSet, iArr) : theme.obtainStyledAttributes(attributeSet, iArr, 0, 0);
|
|
}
|
|
}
|