package com.google.android.material.shape;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.util.AttributeSet;
import android.util.StateSet;
import android.util.TypedValue;
import android.util.Xml;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleableRes;
import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

/* JADX INFO: loaded from: classes2.dex */
public final class q0 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public int f23146a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public o0 f23147b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public int[][] f23148c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public o0[] f23149d;

    @Nullable
    public static q0 create(@NonNull Context context, @NonNull TypedArray typedArray, @StyleableRes int i10) {
        int next;
        int resourceId = typedArray.getResourceId(i10, 0);
        if (resourceId == 0 || !context.getResources().getResourceTypeName(resourceId).equals("xml")) {
            return null;
        }
        try {
            XmlResourceParser xml = context.getResources().getXml(resourceId);
            try {
                q0 q0Var = new q0();
                q0Var.f23148c = new int[10][];
                q0Var.f23149d = new o0[10];
                AttributeSet attributeSetAsAttributeSet = Xml.asAttributeSet(xml);
                do {
                    next = xml.next();
                    if (next == 2) {
                        break;
                    }
                } while (next != 1);
                if (next != 2) {
                    throw new XmlPullParserException("No start tag found");
                }
                if (xml.getName().equals("selector")) {
                    q0Var.loadSizeChangeFromItems(context, xml, attributeSetAsAttributeSet, context.getTheme());
                }
                xml.close();
                return q0Var;
            } catch (Throwable th) {
                if (xml != null) {
                    try {
                        xml.close();
                    } catch (Throwable th2) {
                        th.addSuppressed(th2);
                    }
                }
                throw th;
            }
        } catch (Resources.NotFoundException | IOException | XmlPullParserException unused) {
            return null;
        }
    }

    @Nullable
    private p0 getSizeChangeAmount(@NonNull TypedArray typedArray, int i10, @Nullable p0 p0Var) {
        TypedValue typedValuePeekValue = typedArray.peekValue(i10);
        if (typedValuePeekValue == null) {
            return p0Var;
        }
        int i11 = typedValuePeekValue.type;
        return i11 == 5 ? new p0(StateListSizeChange$SizeChangeType.f23040b, TypedValue.complexToDimensionPixelSize(typedValuePeekValue.data, typedArray.getResources().getDisplayMetrics())) : i11 == 6 ? new p0(StateListSizeChange$SizeChangeType.f23039a, typedValuePeekValue.getFraction(1.0f, 1.0f)) : p0Var;
    }

    private void loadSizeChangeFromItems(@NonNull Context context, @NonNull XmlPullParser xmlPullParser, @NonNull AttributeSet attributeSet, @Nullable Resources.Theme theme) throws XmlPullParserException, IOException {
        int depth = xmlPullParser.getDepth() + 1;
        while (true) {
            int next = xmlPullParser.next();
            if (next == 1) {
                return;
            }
            int depth2 = xmlPullParser.getDepth();
            if (depth2 < depth && next == 3) {
                return;
            }
            if (next == 2 && depth2 <= depth && xmlPullParser.getName().equals("item")) {
                TypedArray typedArrayObtainAttributes = theme == null ? context.getResources().obtainAttributes(attributeSet, x8.m.StateListSizeChange) : theme.obtainStyledAttributes(attributeSet, x8.m.StateListSizeChange, 0, 0);
                p0 sizeChangeAmount = getSizeChangeAmount(typedArrayObtainAttributes, x8.m.StateListSizeChange_widthChange, null);
                typedArrayObtainAttributes.recycle();
                int attributeCount = attributeSet.getAttributeCount();
                int[] iArr = new int[attributeCount];
                int i10 = 0;
                for (int i11 = 0; i11 < attributeCount; i11++) {
                    int attributeNameResource = attributeSet.getAttributeNameResource(i11);
                    if (attributeNameResource != x8.c.widthChange) {
                        int i12 = i10 + 1;
                        if (!attributeSet.getAttributeBooleanValue(i11, false)) {
                            attributeNameResource = -attributeNameResource;
                        }
                        iArr[i10] = attributeNameResource;
                        i10 = i12;
                    }
                }
                int[] iArrTrimStateSet = StateSet.trimStateSet(iArr, i10);
                o0 o0Var = new o0(sizeChangeAmount);
                int i13 = this.f23146a;
                if (i13 == 0 || iArrTrimStateSet.length == 0) {
                    this.f23147b = o0Var;
                }
                int[][] iArr2 = this.f23148c;
                if (i13 >= iArr2.length) {
                    int i14 = i13 + 10;
                    int[][] iArr3 = new int[i14][];
                    System.arraycopy(iArr2, 0, iArr3, 0, i13);
                    this.f23148c = iArr3;
                    o0[] o0VarArr = new o0[i14];
                    System.arraycopy(this.f23149d, 0, o0VarArr, 0, i13);
                    this.f23149d = o0VarArr;
                }
                int[][] iArr4 = this.f23148c;
                int i15 = this.f23146a;
                iArr4[i15] = iArrTrimStateSet;
                this.f23149d[i15] = o0Var;
                this.f23146a = i15 + 1;
            }
        }
    }
}
