package z0;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.net.Uri;
import android.text.TextUtils;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public final class w extends AbstractC1779c {

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public final Context f19175u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public k f19176v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public AssetFileDescriptor f19177w;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public FileInputStream f19178x;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    public long f19179y;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public boolean f19180z;

    public w(Context context) {
        super(false);
        this.f19175u = context.getApplicationContext();
    }

    @Deprecated
    public static Uri buildRawResourceUri(int i) {
        return Uri.parse("rawresource:///" + i);
    }

    @Override // z0.h
    public final void close() {
        this.f19176v = null;
        try {
            try {
                FileInputStream fileInputStream = this.f19178x;
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                this.f19178x = null;
                try {
                    try {
                        AssetFileDescriptor assetFileDescriptor = this.f19177w;
                        if (assetFileDescriptor != null) {
                            assetFileDescriptor.close();
                        }
                    } catch (IOException e8) {
                        throw new v(null, e8, 2000);
                    }
                } finally {
                    this.f19177w = null;
                    if (this.f19180z) {
                        this.f19180z = false;
                        j();
                    }
                }
            } catch (IOException e9) {
                throw new v(null, e9, 2000);
            }
        } catch (Throwable th) {
            this.f19178x = null;
            try {
                try {
                    AssetFileDescriptor assetFileDescriptor2 = this.f19177w;
                    if (assetFileDescriptor2 != null) {
                        assetFileDescriptor2.close();
                    }
                    this.f19177w = null;
                    if (this.f19180z) {
                        this.f19180z = false;
                        j();
                    }
                    throw th;
                } catch (IOException e10) {
                    throw new v(null, e10, 2000);
                }
            } finally {
                this.f19177w = null;
                if (this.f19180z) {
                    this.f19180z = false;
                    j();
                }
            }
        }
    }

    @Override // z0.h
    public final Uri e() {
        k kVar = this.f19176v;
        if (kVar != null) {
            return kVar.f19138a;
        }
        return null;
    }

    @Override // z0.h
    public final long g(k kVar) throws v {
        Resources resourcesForApplication;
        int identifier;
        int i;
        Resources resources;
        this.f19176v = kVar;
        l();
        Uri uriNormalizeScheme = kVar.f19138a.normalizeScheme();
        boolean zEquals = TextUtils.equals("rawresource", uriNormalizeScheme.getScheme());
        Context context = this.f19175u;
        if (zEquals) {
            resources = context.getResources();
            List<String> pathSegments = uriNormalizeScheme.getPathSegments();
            if (pathSegments.size() != 1) {
                throw new v("rawresource:// URI must have exactly one path element, found " + pathSegments.size(), null, 2000);
            }
            try {
                i = Integer.parseInt(pathSegments.get(0));
            } catch (NumberFormatException unused) {
                throw new v("Resource identifier must be an integer.", null, 1004);
            }
        } else {
            if (!TextUtils.equals("android.resource", uriNormalizeScheme.getScheme())) {
                throw new v("Unsupported URI scheme (" + uriNormalizeScheme.getScheme() + "). Only android.resource is supported.", null, 1004);
            }
            String path = uriNormalizeScheme.getPath();
            path.getClass();
            if (path.startsWith("/")) {
                path = path.substring(1);
            }
            String packageName = TextUtils.isEmpty(uriNormalizeScheme.getHost()) ? context.getPackageName() : uriNormalizeScheme.getHost();
            if (packageName.equals(context.getPackageName())) {
                resourcesForApplication = context.getResources();
            } else {
                try {
                    resourcesForApplication = context.getPackageManager().getResourcesForApplication(packageName);
                } catch (PackageManager.NameNotFoundException e8) {
                    throw new v("Package in android.resource:// URI not found. Check http://g.co/dev/packagevisibility.", e8, 2005);
                }
            }
            if (path.matches("\\d+")) {
                try {
                    identifier = Integer.parseInt(path);
                } catch (NumberFormatException unused2) {
                    throw new v("Resource identifier must be an integer.", null, 1004);
                }
            } else {
                identifier = resourcesForApplication.getIdentifier(packageName + ":" + path, "raw", null);
                if (identifier == 0) {
                    throw new v("Resource not found.", null, 2005);
                }
            }
            i = identifier;
            resources = resourcesForApplication;
        }
        try {
            AssetFileDescriptor assetFileDescriptorOpenRawResourceFd = resources.openRawResourceFd(i);
            if (assetFileDescriptorOpenRawResourceFd == null) {
                throw new v("Resource is compressed: " + uriNormalizeScheme, null, 2000);
            }
            this.f19177w = assetFileDescriptorOpenRawResourceFd;
            long length = assetFileDescriptorOpenRawResourceFd.getLength();
            FileInputStream fileInputStream = new FileInputStream(this.f19177w.getFileDescriptor());
            this.f19178x = fileInputStream;
            long j8 = kVar.f19142e;
            try {
                if (length != -1 && j8 > length) {
                    throw new v(null, null, 2008);
                }
                long startOffset = this.f19177w.getStartOffset();
                long jSkip = fileInputStream.skip(startOffset + j8) - startOffset;
                if (jSkip != j8) {
                    throw new v(null, null, 2008);
                }
                if (length == -1) {
                    FileChannel channel = fileInputStream.getChannel();
                    if (channel.size() == 0) {
                        this.f19179y = -1L;
                    } else {
                        long size = channel.size() - channel.position();
                        this.f19179y = size;
                        if (size < 0) {
                            throw new v(null, null, 2008);
                        }
                    }
                } else {
                    long j9 = length - jSkip;
                    this.f19179y = j9;
                    if (j9 < 0) {
                        throw new i(2008);
                    }
                }
                long j10 = kVar.f19143f;
                if (j10 != -1) {
                    long j11 = this.f19179y;
                    this.f19179y = j11 == -1 ? j10 : Math.min(j11, j10);
                }
                this.f19180z = true;
                r(kVar);
                return j10 != -1 ? j10 : this.f19179y;
            } catch (v e9) {
                throw e9;
            } catch (IOException e10) {
                throw new v(null, e10, 2000);
            }
        } catch (Resources.NotFoundException e11) {
            throw new v(null, e11, 2005);
        }
    }

    @Override // u0.InterfaceC1571g
    public final int p(byte[] bArr, int i, int i5) throws v {
        if (i5 == 0) {
            return 0;
        }
        long j8 = this.f19179y;
        if (j8 != 0) {
            if (j8 != -1) {
                try {
                    i5 = (int) Math.min(j8, i5);
                } catch (IOException e8) {
                    throw new v(null, e8, 2000);
                }
            }
            FileInputStream fileInputStream = this.f19178x;
            int i8 = x0.v.f18553a;
            int i9 = fileInputStream.read(bArr, i, i5);
            if (i9 != -1) {
                long j9 = this.f19179y;
                if (j9 != -1) {
                    this.f19179y = j9 - ((long) i9);
                }
                h(i9);
                return i9;
            }
            if (this.f19179y != -1) {
                throw new v("End of stream reached having not read sufficient data.", new EOFException(), 2000);
            }
        }
        return -1;
    }
}
