124 lines
5.6 KiB
Java
124 lines
5.6 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.Xml;
|
|
import androidx.annotation.ColorInt;
|
|
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 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);
|
|
}
|
|
|
|
@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 = 1;
|
|
int depth2 = xmlPullParser.getDepth() + 1;
|
|
int[][] iArr = new int[20][];
|
|
int[] iArr2 = new int[20];
|
|
int i2 = 0;
|
|
while (true) {
|
|
int next = xmlPullParser.next();
|
|
if (next == i || ((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 color = obtainAttributes.getColor(R.styleable.ColorStateListItem_android_color, -65281);
|
|
float f = 1.0f;
|
|
int i3 = R.styleable.ColorStateListItem_android_alpha;
|
|
if (obtainAttributes.hasValue(i3)) {
|
|
f = obtainAttributes.getFloat(i3, 1.0f);
|
|
} else {
|
|
int i4 = R.styleable.ColorStateListItem_alpha;
|
|
if (obtainAttributes.hasValue(i4)) {
|
|
f = obtainAttributes.getFloat(i4, 1.0f);
|
|
}
|
|
}
|
|
obtainAttributes.recycle();
|
|
int attributeCount = attributeSet.getAttributeCount();
|
|
int[] iArr3 = new int[attributeCount];
|
|
int i5 = 0;
|
|
for (int i6 = 0; i6 < attributeCount; i6++) {
|
|
int attributeNameResource = attributeSet.getAttributeNameResource(i6);
|
|
if (!(attributeNameResource == 16843173 || attributeNameResource == 16843551 || attributeNameResource == R.attr.alpha)) {
|
|
int i7 = i5 + 1;
|
|
if (!attributeSet.getAttributeBooleanValue(i6, false)) {
|
|
attributeNameResource = -attributeNameResource;
|
|
}
|
|
iArr3[i5] = attributeNameResource;
|
|
i5 = i7;
|
|
}
|
|
}
|
|
int[] trimStateSet = StateSet.trimStateSet(iArr3, i5);
|
|
iArr2 = GrowingArrayUtils.append(iArr2, i2, modulateColorAlpha(color, f));
|
|
iArr = (int[][]) GrowingArrayUtils.append(iArr, i2, trimStateSet);
|
|
i2++;
|
|
}
|
|
i = 1;
|
|
}
|
|
int[] iArr4 = new int[i2];
|
|
int[][] iArr5 = new int[i2][];
|
|
System.arraycopy(iArr2, 0, iArr4, 0, i2);
|
|
System.arraycopy(iArr, 0, iArr5, 0, i2);
|
|
return new ColorStateList(iArr5, iArr4);
|
|
}
|
|
|
|
@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);
|
|
}
|
|
}
|