package coil.fetch;

import A4.d;
import E3.v;
import S4.p;
import S4.w;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.TypedValue;
import android.webkit.MimeTypeMap;
import coil.ImageLoader;
import coil.decode.DataSource;
import coil.decode.ImageSources;
import coil.decode.ResourceMetadata;
import coil.fetch.Fetcher;
import coil.request.Options;
import coil.util.Contexts;
import coil.util.DrawableUtils;
import coil.util.Utils;
import kotlin.jvm.internal.h;
import kotlin.jvm.internal.o;
import p5.AbstractC2732b;
import x4.AbstractC3251q;

/* JADX INFO: loaded from: classes.dex */
public final class ResourceUriFetcher implements Fetcher {
    public static final Companion Companion = new Companion(null);
    private static final String MIME_TYPE_XML = "text/xml";
    private final Uri data;
    private final Options options;

    public static final class Companion {
        public /* synthetic */ Companion(h hVar) {
            this();
        }

        private Companion() {
        }
    }

    public static final class Factory implements Fetcher.Factory<Uri> {
        private final boolean isApplicable(Uri uri) {
            return o.c(uri.getScheme(), "android.resource");
        }

        @Override // coil.fetch.Fetcher.Factory
        public Fetcher create(Uri uri, Options options, ImageLoader imageLoader) {
            if (isApplicable(uri)) {
                return new ResourceUriFetcher(uri, options);
            }
            return null;
        }
    }

    public ResourceUriFetcher(Uri uri, Options options) {
        this.data = uri;
        this.options = options;
    }

    private final Void throwInvalidUriException(Uri uri) {
        throw new IllegalStateException("Invalid android.resource URI: " + uri);
    }

    @Override // coil.fetch.Fetcher
    public Object fetch(d dVar) {
        Integer numB0;
        String authority = this.data.getAuthority();
        if (authority != null) {
            if (p.Q0(authority)) {
                authority = null;
            }
            if (authority != null) {
                String str = (String) AbstractC3251q.o0(this.data.getPathSegments());
                if (str == null || (numB0 = w.B0(str)) == null) {
                    throwInvalidUriException(this.data);
                    throw new v();
                }
                int iIntValue = numB0.intValue();
                Context context = this.options.getContext();
                Resources resources = authority.equals(context.getPackageName()) ? context.getResources() : context.getPackageManager().getResourcesForApplication(authority);
                TypedValue typedValue = new TypedValue();
                resources.getValue(iIntValue, typedValue, true);
                CharSequence charSequence = typedValue.string;
                String mimeTypeFromUrl = Utils.getMimeTypeFromUrl(MimeTypeMap.getSingleton(), charSequence.subSequence(p.T0(charSequence, '/', 0, 6), charSequence.length()).toString());
                if (!o.c(mimeTypeFromUrl, MIME_TYPE_XML)) {
                    TypedValue typedValue2 = new TypedValue();
                    return new SourceResult(ImageSources.create(AbstractC2732b.c(AbstractC2732b.k(resources.openRawResource(iIntValue, typedValue2))), context, new ResourceMetadata(authority, iIntValue, typedValue2.density)), mimeTypeFromUrl, DataSource.DISK);
                }
                Drawable drawableCompat = authority.equals(context.getPackageName()) ? Contexts.getDrawableCompat(context, iIntValue) : Contexts.getXmlDrawableCompat(context, resources, iIntValue);
                boolean zIsVector = Utils.isVector(drawableCompat);
                if (zIsVector) {
                    drawableCompat = new BitmapDrawable(context.getResources(), DrawableUtils.INSTANCE.convertToBitmap(drawableCompat, this.options.getConfig(), this.options.getSize(), this.options.getScale(), this.options.getAllowInexactSize()));
                }
                return new DrawableResult(drawableCompat, zIsVector, DataSource.DISK);
            }
        }
        throwInvalidUriException(this.data);
        throw new v();
    }
}
