package z0;

import android.content.ContentResolver;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.net.Uri;
import android.os.Bundle;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.channels.FileChannel;

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

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

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

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

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

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

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

    public e(Context context) {
        super(false);
        this.f19120u = context.getContentResolver();
    }

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

    @Override // z0.h
    public final Uri e() {
        return this.f19121v;
    }

    @Override // z0.h
    public final long g(k kVar) throws C1780d {
        int i;
        AssetFileDescriptor assetFileDescriptorOpenAssetFileDescriptor;
        try {
            try {
                Uri uriNormalizeScheme = kVar.f19138a.normalizeScheme();
                this.f19121v = uriNormalizeScheme;
                l();
                boolean zEquals = "content".equals(uriNormalizeScheme.getScheme());
                ContentResolver contentResolver = this.f19120u;
                if (zEquals) {
                    Bundle bundle = new Bundle();
                    bundle.putBoolean("android.provider.extra.ACCEPT_ORIGINAL_MEDIA_FORMAT", true);
                    assetFileDescriptorOpenAssetFileDescriptor = contentResolver.openTypedAssetFileDescriptor(uriNormalizeScheme, "*/*", bundle);
                } else {
                    assetFileDescriptorOpenAssetFileDescriptor = contentResolver.openAssetFileDescriptor(uriNormalizeScheme, "r");
                }
                this.f19122w = assetFileDescriptorOpenAssetFileDescriptor;
                if (assetFileDescriptorOpenAssetFileDescriptor == null) {
                    i = 2000;
                    try {
                        throw new C1780d(new IOException("Could not open file descriptor for: " + uriNormalizeScheme), 2000);
                    } catch (IOException e8) {
                        e = e8;
                        if (e instanceof FileNotFoundException) {
                            i = 2005;
                        }
                        throw new C1780d(e, i);
                    }
                }
                long length = assetFileDescriptorOpenAssetFileDescriptor.getLength();
                FileInputStream fileInputStream = new FileInputStream(assetFileDescriptorOpenAssetFileDescriptor.getFileDescriptor());
                this.f19123x = fileInputStream;
                long j8 = kVar.f19142e;
                if (length != -1 && j8 > length) {
                    throw new C1780d(null, 2008);
                }
                long startOffset = assetFileDescriptorOpenAssetFileDescriptor.getStartOffset();
                long jSkip = fileInputStream.skip(startOffset + j8) - startOffset;
                if (jSkip != j8) {
                    throw new C1780d(null, 2008);
                }
                if (length == -1) {
                    FileChannel channel = fileInputStream.getChannel();
                    long size = channel.size();
                    if (size == 0) {
                        this.f19124y = -1L;
                    } else {
                        long jPosition = size - channel.position();
                        this.f19124y = jPosition;
                        if (jPosition < 0) {
                            throw new C1780d(null, 2008);
                        }
                    }
                } else {
                    long j9 = length - jSkip;
                    this.f19124y = j9;
                    if (j9 < 0) {
                        throw new C1780d(null, 2008);
                    }
                }
                long j10 = kVar.f19143f;
                if (j10 != -1) {
                    long j11 = this.f19124y;
                    this.f19124y = j11 == -1 ? j10 : Math.min(j11, j10);
                }
                this.f19125z = true;
                r(kVar);
                return j10 != -1 ? j10 : this.f19124y;
            } catch (C1780d e9) {
                throw e9;
            }
        } catch (IOException e10) {
            e = e10;
            i = 2000;
        }
    }

    @Override // u0.InterfaceC1571g
    public final int p(byte[] bArr, int i, int i5) throws C1780d {
        if (i5 == 0) {
            return 0;
        }
        long j8 = this.f19124y;
        if (j8 != 0) {
            if (j8 != -1) {
                try {
                    i5 = (int) Math.min(j8, i5);
                } catch (IOException e8) {
                    throw new C1780d(e8, 2000);
                }
            }
            FileInputStream fileInputStream = this.f19123x;
            int i8 = x0.v.f18553a;
            int i9 = fileInputStream.read(bArr, i, i5);
            if (i9 != -1) {
                long j9 = this.f19124y;
                if (j9 != -1) {
                    this.f19124y = j9 - ((long) i9);
                }
                h(i9);
                return i9;
            }
        }
        return -1;
    }
}
