package com.applovin.impl; import android.content.ContentResolver; import android.content.Context; import android.content.res.AssetFileDescriptor; import android.media.ApplicationMediaCapabilities; 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; /* loaded from: classes.dex */ public final class s4 extends a2 { private final ContentResolver e; private Uri f; private AssetFileDescriptor g; private FileInputStream h; private long i; private boolean j; @Override // com.applovin.impl.i5 public Uri c() { return this.f; } public static class b extends j5 { public b(IOException iOException, int i) { super(iOException, i); } } public s4(Context context) { super(false); this.e = context.getContentResolver(); } @Override // com.applovin.impl.i5 public long a(l5 l5Var) { AssetFileDescriptor openAssetFileDescriptor; try { Uri uri = l5Var.a; this.f = uri; b(l5Var); if ("content".equals(l5Var.a.getScheme())) { Bundle bundle = new Bundle(); if (xp.a >= 31) { a.a(bundle); } openAssetFileDescriptor = this.e.openTypedAssetFileDescriptor(uri, "*/*", bundle); } else { openAssetFileDescriptor = this.e.openAssetFileDescriptor(uri, "r"); } this.g = openAssetFileDescriptor; if (openAssetFileDescriptor != null) { long length = openAssetFileDescriptor.getLength(); FileInputStream fileInputStream = new FileInputStream(openAssetFileDescriptor.getFileDescriptor()); this.h = fileInputStream; if (length != -1 && l5Var.g > length) { throw new b(null, 2008); } long startOffset = openAssetFileDescriptor.getStartOffset(); long skip = fileInputStream.skip(l5Var.g + startOffset) - startOffset; if (skip == l5Var.g) { if (length == -1) { FileChannel channel = fileInputStream.getChannel(); long size = channel.size(); if (size == 0) { this.i = -1L; } else { long position = size - channel.position(); this.i = position; if (position < 0) { throw new b(null, 2008); } } } else { long j = length - skip; this.i = j; if (j < 0) { throw new b(null, 2008); } } long j2 = l5Var.h; if (j2 != -1) { long j3 = this.i; if (j3 != -1) { j2 = Math.min(j3, j2); } this.i = j2; } this.j = true; c(l5Var); long j4 = l5Var.h; return j4 != -1 ? j4 : this.i; } throw new b(null, 2008); } throw new b(new IOException("Could not open file descriptor for: " + uri), 2000); } catch (b e) { throw e; } catch (IOException e2) { throw new b(e2, e2 instanceof FileNotFoundException ? 2005 : 2000); } } @Override // com.applovin.impl.i5 public void close() { this.f = null; try { try { FileInputStream fileInputStream = this.h; if (fileInputStream != null) { fileInputStream.close(); } this.h = null; try { try { AssetFileDescriptor assetFileDescriptor = this.g; if (assetFileDescriptor != null) { assetFileDescriptor.close(); } } catch (IOException e) { throw new b(e, 2000); } } finally { this.g = null; if (this.j) { this.j = false; g(); } } } catch (IOException e2) { throw new b(e2, 2000); } } catch (Throwable th) { this.h = null; try { try { AssetFileDescriptor assetFileDescriptor2 = this.g; if (assetFileDescriptor2 != null) { assetFileDescriptor2.close(); } this.g = null; if (this.j) { this.j = false; g(); } throw th; } catch (IOException e3) { throw new b(e3, 2000); } } finally { this.g = null; if (this.j) { this.j = false; g(); } } } } public static final class a { public static void a(Bundle bundle) { ApplicationMediaCapabilities.Builder addSupportedVideoMimeType; ApplicationMediaCapabilities.Builder addSupportedHdrType; ApplicationMediaCapabilities.Builder addSupportedHdrType2; ApplicationMediaCapabilities.Builder addSupportedHdrType3; ApplicationMediaCapabilities.Builder addSupportedHdrType4; ApplicationMediaCapabilities build; addSupportedVideoMimeType = s4$a$$ExternalSyntheticApiModelOutline3.m().addSupportedVideoMimeType("video/hevc"); addSupportedHdrType = addSupportedVideoMimeType.addSupportedHdrType("android.media.feature.hdr.dolby_vision"); addSupportedHdrType2 = addSupportedHdrType.addSupportedHdrType("android.media.feature.hdr.hdr10"); addSupportedHdrType3 = addSupportedHdrType2.addSupportedHdrType("android.media.feature.hdr.hdr10_plus"); addSupportedHdrType4 = addSupportedHdrType3.addSupportedHdrType("android.media.feature.hdr.hlg"); build = addSupportedHdrType4.build(); bundle.putParcelable("android.provider.extra.MEDIA_CAPABILITIES", build); } } @Override // com.applovin.impl.g5 public int a(byte[] bArr, int i, int i2) { if (i2 == 0) { return 0; } long j = this.i; if (j == 0) { return -1; } if (j != -1) { try { i2 = (int) Math.min(j, i2); } catch (IOException e) { throw new b(e, 2000); } } int read = ((FileInputStream) xp.a((Object) this.h)).read(bArr, i, i2); if (read == -1) { return -1; } long j2 = this.i; if (j2 != -1) { this.i = j2 - read; } d(read); return read; } }