package j2;

import android.content.ContentResolver;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.VectorDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.util.Xml;
import android.webkit.MimeTypeMap;
import com.google.android.gms.internal.measurement.AbstractC0707u1;
import com.google.android.gms.internal.measurement.X1;
import d2.q;
import d5.AbstractC0844a;
import g2.C0973a;
import g2.EnumC0977e;
import g2.n;
import g2.o;
import java.io.InputStream;
import java.util.List;
import n4.u0;
import org.xmlpull.v1.XmlPullParserException;
import p2.C1423m;
import q2.C1458a;

/* JADX INFO: loaded from: classes.dex */
public final class b implements g {

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Uri f13671b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final C1423m f13672c;

    public /* synthetic */ b(Uri uri, C1423m c1423m, int i) {
        this.f13670a = i;
        this.f13671b = uri;
        this.f13672c = c1423m;
    }

    /* JADX WARN: Removed duplicated region for block: B:53:0x0122  */
    /* JADX WARN: Removed duplicated region for block: B:96:0x0232  */
    @Override // j2.g
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object a(E6.d dVar) throws Throwable {
        InputStream inputStreamOpenInputStream;
        List<String> pathSegments;
        int size;
        Bundle bundle;
        Integer numE;
        Drawable drawable;
        Drawable eVar;
        boolean z8 = true;
        EnumC0977e enumC0977e = EnumC0977e.f12899s;
        C1423m c1423m = this.f13672c;
        Uri uri = this.f13671b;
        switch (this.f13670a) {
            case u0.f15495q /* 0 */:
                String strM0 = B6.k.m0(B6.k.e0(uri.getPathSegments()), "/", null, null, null, 62);
                return new m(new o(s7.l.c(s7.l.z(c1423m.f16093a.getAssets().open(strM0))), new C0973a()), u2.f.b(MimeTypeMap.getSingleton(), strM0), enumC0977e);
            case 1:
                ContentResolver contentResolver = c1423m.f16093a.getContentResolver();
                if (P6.g.a(uri.getAuthority(), "com.android.contacts") && P6.g.a(uri.getLastPathSegment(), "display_photo")) {
                    AssetFileDescriptor assetFileDescriptorOpenAssetFileDescriptor = contentResolver.openAssetFileDescriptor(uri, "r");
                    inputStreamOpenInputStream = assetFileDescriptorOpenAssetFileDescriptor != null ? assetFileDescriptorOpenAssetFileDescriptor.createInputStream() : null;
                    if (inputStreamOpenInputStream == null) {
                        throw new IllegalStateException(("Unable to find a contact photo associated with '" + uri + "'.").toString());
                    }
                } else if (Build.VERSION.SDK_INT >= 29 && P6.g.a(uri.getAuthority(), "media") && (size = (pathSegments = uri.getPathSegments()).size()) >= 3 && P6.g.a(pathSegments.get(size - 3), "audio") && P6.g.a(pathSegments.get(size - 2), "albums")) {
                    q2.h hVar = c1423m.f16096d;
                    AbstractC0707u1 abstractC0707u1 = hVar.f16240a;
                    C1458a c1458a = abstractC0707u1 instanceof C1458a ? (C1458a) abstractC0707u1 : null;
                    if (c1458a != null) {
                        AbstractC0707u1 abstractC0707u12 = hVar.f16241b;
                        C1458a c1458a2 = abstractC0707u12 instanceof C1458a ? (C1458a) abstractC0707u12 : null;
                        if (c1458a2 != null) {
                            bundle = new Bundle(1);
                            bundle.putParcelable("android.content.extra.SIZE", new Point(c1458a.i, c1458a2.i));
                        } else {
                            bundle = null;
                        }
                        AssetFileDescriptor assetFileDescriptorOpenTypedAssetFile = contentResolver.openTypedAssetFile(uri, "image/*", bundle, null);
                        inputStreamOpenInputStream = assetFileDescriptorOpenTypedAssetFile != null ? assetFileDescriptorOpenTypedAssetFile.createInputStream() : null;
                        if (inputStreamOpenInputStream == null) {
                            throw new IllegalStateException(("Unable to find a music thumbnail associated with '" + uri + "'.").toString());
                        }
                    }
                } else {
                    inputStreamOpenInputStream = contentResolver.openInputStream(uri);
                    if (inputStreamOpenInputStream == null) {
                        throw new IllegalStateException(("Unable to open '" + uri + "'.").toString());
                    }
                }
                return new m(new o(s7.l.c(s7.l.z(inputStreamOpenInputStream)), new C0973a()), contentResolver.getType(uri), enumC0977e);
            default:
                String authority = uri.getAuthority();
                if (authority != null) {
                    String str = X6.e.V(authority) ? null : authority;
                    if (str != null) {
                        String str2 = (String) B6.k.o0(uri.getPathSegments());
                        if (str2 == null || (numE = X6.m.E(str2)) == null) {
                            throw new IllegalStateException("Invalid android.resource URI: " + uri);
                        }
                        int iIntValue = numE.intValue();
                        Context context = c1423m.f16093a;
                        Resources resources = str.equals(context.getPackageName()) ? context.getResources() : context.getPackageManager().getResourcesForApplication(str);
                        TypedValue typedValue = new TypedValue();
                        resources.getValue(iIntValue, typedValue, true);
                        CharSequence charSequence = typedValue.string;
                        String strB = u2.f.b(MimeTypeMap.getSingleton(), charSequence.subSequence(X6.e.W(charSequence, '/', 0, 6), charSequence.length()).toString());
                        if (!P6.g.a(strB, "text/xml")) {
                            TypedValue typedValue2 = new TypedValue();
                            return new m(new o(s7.l.c(s7.l.z(resources.openRawResource(iIntValue, typedValue2))), new n(typedValue2.density)), strB, enumC0977e);
                        }
                        if (str.equals(context.getPackageName())) {
                            drawable = android.support.v4.media.session.a.l(context, iIntValue);
                            if (drawable == null) {
                                throw new IllegalStateException(AbstractC0844a.j("Invalid resource ID: ", iIntValue).toString());
                            }
                        } else {
                            XmlResourceParser xml = resources.getXml(iIntValue);
                            int next = xml.next();
                            while (next != 2 && next != 1) {
                                next = xml.next();
                            }
                            if (next != 2) {
                                throw new XmlPullParserException("No start tag found.");
                            }
                            if (Build.VERSION.SDK_INT < 24) {
                                String name = xml.getName();
                                if (P6.g.a(name, "vector")) {
                                    AttributeSet attributeSetAsAttributeSet = Xml.asAttributeSet(xml);
                                    Resources.Theme theme = context.getTheme();
                                    eVar = new q();
                                    eVar.inflate(resources, xml, attributeSetAsAttributeSet, theme);
                                } else {
                                    if (P6.g.a(name, "animated-vector")) {
                                        AttributeSet attributeSetAsAttributeSet2 = Xml.asAttributeSet(xml);
                                        Resources.Theme theme2 = context.getTheme();
                                        eVar = new d2.e(context);
                                        eVar.inflate(resources, xml, attributeSetAsAttributeSet2, theme2);
                                    }
                                    Resources.Theme theme3 = context.getTheme();
                                    ThreadLocal threadLocal = E.j.f1954a;
                                    drawable = resources.getDrawable(iIntValue, theme3);
                                    if (drawable == null) {
                                        throw new IllegalStateException(AbstractC0844a.j("Invalid resource ID: ", iIntValue).toString());
                                    }
                                }
                                drawable = eVar;
                            } else {
                                Resources.Theme theme32 = context.getTheme();
                                ThreadLocal threadLocal2 = E.j.f1954a;
                                drawable = resources.getDrawable(iIntValue, theme32);
                                if (drawable == null) {
                                }
                            }
                        }
                        if (!(drawable instanceof VectorDrawable) && !(drawable instanceof q)) {
                            z8 = false;
                        }
                        if (z8) {
                            drawable = new BitmapDrawable(context.getResources(), X1.j(drawable, c1423m.f16094b, c1423m.f16096d, c1423m.f16097e, c1423m.f16098f));
                        }
                        return new d(drawable, z8, enumC0977e);
                    }
                }
                throw new IllegalStateException("Invalid android.resource URI: " + uri);
        }
    }
}
