package c.i.a.c.h2.y; import android.database.SQLException; import android.os.ConditionVariable; import android.util.Log; import androidx.annotation.Nullable; import c.i.a.c.h2.y.i; import c.i.a.c.i2.p; import c.i.a.c.w1.a; import c.i.b.b.z; import com.airbnb.lottie.parser.AnimatableValueParser; import com.google.android.exoplayer2.database.DatabaseIOException; import com.google.android.exoplayer2.upstream.cache.Cache; import java.io.File; import java.io.IOException; import java.security.SecureRandom; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Objects; import java.util.Random; /* compiled from: SimpleCache */ public final class q implements Cache { public static final HashSet a = new HashSet<>(); public final File b; /* renamed from: c reason: collision with root package name */ public final d f901c; public final j d; @Nullable public final f e; public final HashMap> f; public final Random g; public final boolean h; public long i; public long j; public Cache.CacheException k; public q(File file, d dVar, a aVar) { boolean add; j jVar = new j(aVar, file, null, false, false); f fVar = new f(aVar); synchronized (q.class) { add = a.add(file.getAbsoluteFile()); } if (add) { this.b = file; this.f901c = dVar; this.d = jVar; this.e = fVar; this.f = new HashMap<>(); this.g = new Random(); this.h = true; this.i = -1; ConditionVariable conditionVariable = new ConditionVariable(); new p(this, "ExoPlayer:SimpleCacheInit", conditionVariable).start(); conditionVariable.block(); return; } throw new IllegalStateException("Another SimpleCache instance uses the folder: " + file); } public static void j(q qVar) { long j; if (!qVar.b.exists()) { try { m(qVar.b); } catch (Cache.CacheException e) { qVar.k = e; return; } } File[] listFiles = qVar.b.listFiles(); if (listFiles == null) { StringBuilder K = c.d.b.a.a.K("Failed to list cache directory files: "); K.append(qVar.b); String sb = K.toString(); Log.e("SimpleCache", sb); qVar.k = new Cache.CacheException(sb); return; } int length = listFiles.length; int i = 0; while (true) { if (i >= length) { j = -1; break; } File file = listFiles[i]; String name = file.getName(); if (name.endsWith(".uid")) { try { j = Long.parseLong(name.substring(0, name.indexOf(46)), 16); break; } catch (NumberFormatException unused) { Log.e("SimpleCache", "Malformed UID file: " + file); file.delete(); } } else { i++; } } qVar.i = j; if (j == -1) { try { qVar.i = n(qVar.b); } catch (IOException e2) { StringBuilder K2 = c.d.b.a.a.K("Failed to create cache UID: "); K2.append(qVar.b); String sb2 = K2.toString(); p.b("SimpleCache", sb2, e2); qVar.k = new Cache.CacheException(sb2, e2); return; } } try { qVar.d.e(qVar.i); f fVar = qVar.e; if (fVar != null) { fVar.b(qVar.i); Map a2 = qVar.e.a(); qVar.p(qVar.b, true, listFiles, a2); qVar.e.c(((HashMap) a2).keySet()); } else { qVar.p(qVar.b, true, listFiles, null); } j jVar = qVar.d; Iterator j2 = z.m(jVar.a.keySet()).iterator(); while (j2.hasNext()) { jVar.f((String) j2.next()); } try { qVar.d.g(); } catch (IOException e3) { p.b("SimpleCache", "Storing index file failed", e3); } } catch (IOException e4) { StringBuilder K3 = c.d.b.a.a.K("Failed to initialize cache indices: "); K3.append(qVar.b); String sb3 = K3.toString(); p.b("SimpleCache", sb3, e4); qVar.k = new Cache.CacheException(sb3, e4); } } public static void m(File file) throws Cache.CacheException { if (!file.mkdirs() && !file.isDirectory()) { String str = "Failed to create cache directory: " + file; Log.e("SimpleCache", str); throw new Cache.CacheException(str); } } public static long n(File file) throws IOException { long nextLong = new SecureRandom().nextLong(); long abs = nextLong == Long.MIN_VALUE ? 0 : Math.abs(nextLong); File file2 = new File(file, c.d.b.a.a.s(Long.toString(abs, 16), ".uid")); if (file2.createNewFile()) { return abs; } throw new IOException("Failed to create UID file: " + file2); } @Override // com.google.android.exoplayer2.upstream.cache.Cache public synchronized File a(String str, long j, long j2) throws Cache.CacheException { i iVar; File file; AnimatableValueParser.D(true); l(); iVar = this.d.a.get(str); Objects.requireNonNull(iVar); AnimatableValueParser.D(iVar.a(j, j2)); if (!this.b.exists()) { m(this.b); r(); } this.f901c.a(this, str, j, j2); file = new File(this.b, Integer.toString(this.g.nextInt(10))); if (!file.exists()) { m(file); } return r.h(file, iVar.a, j, System.currentTimeMillis()); } @Override // com.google.android.exoplayer2.upstream.cache.Cache public synchronized l b(String str) { i iVar; AnimatableValueParser.D(true); iVar = this.d.a.get(str); return iVar != null ? iVar.e : n.a; } @Override // com.google.android.exoplayer2.upstream.cache.Cache public synchronized void c(String str, m mVar) throws Cache.CacheException { AnimatableValueParser.D(true); l(); j jVar = this.d; i d = jVar.d(str); n nVar = d.e; n a2 = nVar.a(mVar); d.e = a2; if (!a2.equals(nVar)) { jVar.e.c(d); } try { this.d.g(); } catch (IOException e) { throw new Cache.CacheException(e); } } @Override // com.google.android.exoplayer2.upstream.cache.Cache public synchronized void d(h hVar) { AnimatableValueParser.D(true); q(hVar); } /* JADX WARNING: Code restructure failed: missing block: B:17:0x0049, code lost: if ((r11 + r4) <= r18) goto L_0x0059; */ /* JADX WARNING: Code restructure failed: missing block: B:21:0x0056, code lost: if ((r18 + r6) <= r11) goto L_0x0059; */ /* JADX WARNING: Removed duplicated region for block: B:26:0x0060 A[LOOP:0: B:10:0x0026->B:26:0x0060, LOOP_END] */ /* JADX WARNING: Removed duplicated region for block: B:38:0x005e A[SYNTHETIC] */ @Override // com.google.android.exoplayer2.upstream.cache.Cache @Nullable public synchronized h e(String str, long j, long j2) throws Cache.CacheException { r rVar; boolean z2; boolean z3; AnimatableValueParser.D(true); l(); r o = o(str, j, j2); if (o.l) { return s(str, o); } i d = this.d.d(str); long j3 = o.k; int i = 0; while (true) { if (i >= d.d.size()) { rVar = o; d.d.add(new i.a(j, j3)); z2 = true; break; } i.a aVar = d.d.get(i); long j4 = aVar.a; if (j4 <= j) { rVar = o; long j5 = aVar.b; if (j5 != -1) { } z3 = true; if (z3) { z2 = false; break; } i++; o = rVar; } else { rVar = o; if (j3 != -1) { } z3 = true; if (z3) { } } z3 = false; if (z3) { } } if (z2) { return rVar; } return null; } @Override // com.google.android.exoplayer2.upstream.cache.Cache public synchronized h f(String str, long j, long j2) throws InterruptedException, Cache.CacheException { h e; AnimatableValueParser.D(true); l(); while (true) { e = e(str, j, j2); if (e == null) { wait(); } } return e; } @Override // com.google.android.exoplayer2.upstream.cache.Cache public synchronized void g(File file, long j) throws Cache.CacheException { boolean z2 = true; AnimatableValueParser.D(true); if (file.exists()) { if (j == 0) { file.delete(); return; } r g = r.g(file, j, -9223372036854775807L, this.d); Objects.requireNonNull(g); i c2 = this.d.c(g.i); Objects.requireNonNull(c2); AnimatableValueParser.D(c2.a(g.j, g.k)); long a2 = k.a(c2.e); if (a2 != -1) { if (g.j + g.k > a2) { z2 = false; } AnimatableValueParser.D(z2); } if (this.e != null) { try { this.e.d(file.getName(), g.k, g.n); } catch (IOException e) { throw new Cache.CacheException(e); } } k(g); try { this.d.g(); notifyAll(); } catch (IOException e2) { throw new Cache.CacheException(e2); } } } @Override // com.google.android.exoplayer2.upstream.cache.Cache public synchronized long h() { AnimatableValueParser.D(true); return this.j; } @Override // com.google.android.exoplayer2.upstream.cache.Cache public synchronized void i(h hVar) { AnimatableValueParser.D(true); i c2 = this.d.c(hVar.i); Objects.requireNonNull(c2); long j = hVar.j; for (int i = 0; i < c2.d.size(); i++) { if (c2.d.get(i).a == j) { c2.d.remove(i); this.d.f(c2.b); notifyAll(); } } throw new IllegalStateException(); } public final void k(r rVar) { this.d.d(rVar.i).f896c.add(rVar); this.j += rVar.k; ArrayList arrayList = this.f.get(rVar.i); if (arrayList != null) { int size = arrayList.size(); while (true) { size--; if (size < 0) { break; } arrayList.get(size).d(this, rVar); } } this.f901c.d(this, rVar); } public synchronized void l() throws Cache.CacheException { Cache.CacheException cacheException = this.k; if (cacheException != null) { throw cacheException; } } public final r o(String str, long j, long j2) { r floor; long j3; i iVar = this.d.a.get(str); if (iVar == null) { return new r(str, j, j2, -9223372036854775807L, null); } while (true) { r rVar = new r(iVar.b, j, -1, -9223372036854775807L, null); floor = iVar.f896c.floor(rVar); if (floor == null || floor.j + floor.k <= j) { r ceiling = iVar.f896c.ceiling(rVar); if (ceiling != null) { long j4 = ceiling.j - j; if (j2 != -1) { j4 = Math.min(j4, j2); } j3 = j4; } else { j3 = j2; } floor = new r(iVar.b, j, j3, -9223372036854775807L, null); } if (!floor.l || floor.m.length() == floor.k) { break; } r(); } return floor; } public final void p(File file, boolean z2, @Nullable File[] fileArr, @Nullable Map map) { if (fileArr != null && fileArr.length != 0) { for (File file2 : fileArr) { String name = file2.getName(); if (z2 && name.indexOf(46) == -1) { p(file2, false, file2.listFiles(), map); } else if (!z2 || (!name.startsWith("cached_content_index.exi") && !name.endsWith(".uid"))) { long j = -1; long j2 = -9223372036854775807L; e remove = map != null ? map.remove(name) : null; if (remove != null) { j = remove.a; j2 = remove.b; } r g = r.g(file2, j, j2, this.d); if (g != null) { k(g); } else { file2.delete(); } } } } else if (!z2) { file.delete(); } } public final void q(h hVar) { boolean z2; i c2 = this.d.c(hVar.i); if (c2 != null) { if (c2.f896c.remove(hVar)) { File file = hVar.m; if (file != null) { file.delete(); } z2 = true; } else { z2 = false; } if (z2) { this.j -= hVar.k; if (this.e != null) { String name = hVar.m.getName(); try { f fVar = this.e; Objects.requireNonNull(fVar.f895c); try { fVar.b.getWritableDatabase().delete(fVar.f895c, "name = ?", new String[]{name}); } catch (SQLException e) { throw new DatabaseIOException(e); } } catch (IOException unused) { c.d.b.a.a.g0("Failed to remove file index entry for: ", name, "SimpleCache"); } } this.d.f(c2.b); ArrayList arrayList = this.f.get(hVar.i); if (arrayList != null) { int size = arrayList.size(); while (true) { size--; if (size < 0) { break; } arrayList.get(size).b(this, hVar); } } this.f901c.b(this, hVar); } } } public final void r() { ArrayList arrayList = new ArrayList(); for (i iVar : Collections.unmodifiableCollection(this.d.a.values())) { Iterator it = iVar.f896c.iterator(); while (it.hasNext()) { r next = it.next(); if (next.m.length() != next.k) { arrayList.add(next); } } } for (int i = 0; i < arrayList.size(); i++) { q((h) arrayList.get(i)); } } /* JADX WARNING: Removed duplicated region for block: B:20:0x00a0 */ public final r s(String str, r rVar) { File file; ArrayList arrayList; if (!this.h) { return rVar; } File file2 = rVar.m; Objects.requireNonNull(file2); String name = file2.getName(); long j = rVar.k; long currentTimeMillis = System.currentTimeMillis(); boolean z2 = false; f fVar = this.e; if (fVar != null) { try { fVar.d(name, j, currentTimeMillis); } catch (IOException unused) { Log.w("SimpleCache", "Failed to update index with new touch timestamp."); } } else { z2 = true; } i iVar = this.d.a.get(str); AnimatableValueParser.D(iVar.f896c.remove(rVar)); File file3 = rVar.m; Objects.requireNonNull(file3); if (z2) { File parentFile = file3.getParentFile(); Objects.requireNonNull(parentFile); File h = r.h(parentFile, iVar.a, rVar.j, currentTimeMillis); if (file3.renameTo(h)) { file = h; AnimatableValueParser.D(rVar.l); r rVar2 = new r(rVar.i, rVar.j, rVar.k, currentTimeMillis, file); iVar.f896c.add(rVar2); arrayList = this.f.get(rVar.i); if (arrayList != null) { int size = arrayList.size(); while (true) { size--; if (size < 0) { break; } arrayList.get(size).c(this, rVar, rVar2); } } this.f901c.c(this, rVar, rVar2); return rVar2; } Log.w("CachedContent", "Failed to rename " + file3 + " to " + h); } file = file3; AnimatableValueParser.D(rVar.l); r rVar2 = new r(rVar.i, rVar.j, rVar.k, currentTimeMillis, file); iVar.f896c.add(rVar2); arrayList = this.f.get(rVar.i); if (arrayList != null) { } this.f901c.c(this, rVar, rVar2); return rVar2; } }