discord-jadx/app/src/main/java/c/f/b/b/d.java
2021-08-31 09:21:49 +02:00

418 lines
13 KiB
Java

package c.f.b.b;
import android.os.StatFs;
import android.os.SystemClock;
import androidx.annotation.VisibleForTesting;
import c.f.b.a.e;
import c.f.b.a.g;
import c.f.b.b.a;
import c.f.b.b.c;
import com.airbnb.lottie.parser.AnimatableValueParser;
import com.facebook.cache.common.CacheKey;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
/* compiled from: DiskStorageCache */
public class d implements h {
public static final long a = TimeUnit.HOURS.toMillis(2);
public static final long b = TimeUnit.MINUTES.toMillis(30);
/* renamed from: c reason: collision with root package name */
public final long f359c;
public final long d;
public final CountDownLatch e;
public long f;
public final c.f.b.a.b g;
@VisibleForTesting
public final Set<String> h;
public long i;
public final c.f.d.i.a j;
public final c k;
public final g l;
public final c.f.b.a.a m;
public final boolean n;
public final b o;
public final c.f.d.k.a p;
public final Object q = new Object();
/* compiled from: DiskStorageCache */
public class a implements Runnable {
public a() {
}
@Override // java.lang.Runnable
public void run() {
synchronized (d.this.q) {
d.this.e();
}
Objects.requireNonNull(d.this);
d.this.e.countDown();
}
}
/* compiled from: DiskStorageCache */
@VisibleForTesting
public static class b {
public boolean a = false;
public long b = -1;
/* renamed from: c reason: collision with root package name */
public long f360c = -1;
public synchronized long a() {
return this.b;
}
public synchronized void b(long j, long j2) {
if (this.a) {
this.b += j;
this.f360c += j2;
}
}
}
/* compiled from: DiskStorageCache */
public static class c {
public final long a;
public final long b;
/* renamed from: c reason: collision with root package name */
public final long f361c;
public c(long j, long j2, long j3) {
this.a = j;
this.b = j2;
this.f361c = j3;
}
}
public d(c cVar, g gVar, c cVar2, c.f.b.a.b bVar, c.f.b.a.a aVar, c.f.d.a.a aVar2, Executor executor, boolean z2) {
c.f.d.i.a aVar3;
this.f359c = cVar2.b;
long j = cVar2.f361c;
this.d = j;
this.f = j;
c.f.d.i.a aVar4 = c.f.d.i.a.a;
synchronized (c.f.d.i.a.class) {
if (c.f.d.i.a.a == null) {
c.f.d.i.a.a = new c.f.d.i.a();
}
aVar3 = c.f.d.i.a.a;
}
this.j = aVar3;
this.k = cVar;
this.l = gVar;
this.i = -1;
this.g = bVar;
this.m = aVar;
this.o = new b();
this.p = c.f.d.k.c.a;
this.n = z2;
this.h = new HashSet();
if (z2) {
this.e = new CountDownLatch(1);
executor.execute(new a());
return;
}
this.e = new CountDownLatch(0);
}
public final void a(long j, int i) throws IOException {
try {
Collection<c.a> c2 = c(this.k.e());
long a2 = this.o.a() - j;
int i2 = 0;
Iterator it = ((ArrayList) c2).iterator();
long j2 = 0;
while (it.hasNext()) {
c.a aVar = (c.a) it.next();
if (j2 > a2) {
break;
}
long g = this.k.g(aVar);
this.h.remove(aVar.getId());
if (g > 0) {
i2++;
j2 += g;
i a3 = i.a();
aVar.getId();
Objects.requireNonNull((e) this.g);
a3.b();
}
}
this.o.b(-j2, (long) (-i2));
this.k.a();
} catch (IOException e) {
c.f.b.a.a aVar2 = this.m;
e.getMessage();
Objects.requireNonNull((c.f.b.a.d) aVar2);
throw e;
}
}
public c.f.a.a b(CacheKey cacheKey) {
c.f.a.a aVar;
i a2 = i.a();
a2.d = cacheKey;
try {
synchronized (this.q) {
List<String> I0 = AnimatableValueParser.I0(cacheKey);
int i = 0;
String str = null;
aVar = null;
while (true) {
ArrayList arrayList = (ArrayList) I0;
if (i >= arrayList.size()) {
break;
}
str = (String) arrayList.get(i);
aVar = this.k.d(str, cacheKey);
if (aVar != null) {
break;
}
i++;
}
if (aVar == null) {
Objects.requireNonNull((e) this.g);
this.h.remove(str);
} else {
Objects.requireNonNull(str);
Objects.requireNonNull((e) this.g);
this.h.add(str);
}
}
return aVar;
} catch (IOException unused) {
Objects.requireNonNull((c.f.b.a.d) this.m);
Objects.requireNonNull((e) this.g);
return null;
} finally {
a2.b();
}
}
public final Collection<c.a> c(Collection<c.a> collection) {
Objects.requireNonNull((c.f.d.k.c) this.p);
long currentTimeMillis = System.currentTimeMillis() + a;
ArrayList arrayList = new ArrayList(collection.size());
ArrayList arrayList2 = new ArrayList(collection.size());
for (c.a aVar : collection) {
if (aVar.a() > currentTimeMillis) {
arrayList.add(aVar);
} else {
arrayList2.add(aVar);
}
}
Collections.sort(arrayList2, this.l.get());
arrayList.addAll(arrayList2);
return arrayList;
}
public c.f.a.a d(CacheKey cacheKey, g gVar) throws IOException {
String w2;
c.f.a.a b2;
i a2 = i.a();
a2.d = cacheKey;
Objects.requireNonNull((e) this.g);
synchronized (this.q) {
try {
if (!(cacheKey instanceof c.f.b.a.c)) {
w2 = AnimatableValueParser.w2(cacheKey);
} else {
c.f.b.a.c cVar = (c.f.b.a.c) cacheKey;
throw null;
}
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
try {
c.b g = g(w2, cacheKey);
try {
a.f fVar = (a.f) g;
fVar.c(gVar, cacheKey);
synchronized (this.q) {
b2 = fVar.b(cacheKey);
this.h.add(w2);
this.o.b(b2.a(), 1);
}
b2.a();
this.o.a();
Objects.requireNonNull((e) this.g);
if (!fVar.a()) {
c.f.d.e.a.a(d.class, "Failed to delete temp file");
}
a2.b();
return b2;
} catch (Throwable th) {
if (!((a.f) g).a()) {
c.f.d.e.a.a(d.class, "Failed to delete temp file");
}
throw th;
}
} catch (IOException e2) {
Objects.requireNonNull((e) this.g);
c.f.d.e.a.b(d.class, "Failed inserting a file into the cache", e2);
throw e2;
} catch (Throwable th2) {
a2.b();
throw th2;
}
}
public final boolean e() {
boolean z2;
long j;
long j2;
Objects.requireNonNull((c.f.d.k.c) this.p);
long currentTimeMillis = System.currentTimeMillis();
b bVar = this.o;
synchronized (bVar) {
z2 = bVar.a;
}
long j3 = -1;
if (z2) {
long j4 = this.i;
if (j4 != -1 && currentTimeMillis - j4 <= b) {
return false;
}
}
Objects.requireNonNull((c.f.d.k.c) this.p);
long currentTimeMillis2 = System.currentTimeMillis();
long j5 = a + currentTimeMillis2;
Set<String> hashSet = (!this.n || !this.h.isEmpty()) ? this.n ? new HashSet<>() : null : this.h;
try {
long j6 = 0;
boolean z3 = false;
int i = 0;
for (c.a aVar : this.k.e()) {
i++;
j6 += aVar.getSize();
if (aVar.a() > j5) {
aVar.getSize();
j2 = j5;
j3 = Math.max(aVar.a() - currentTimeMillis2, j3);
z3 = true;
} else {
j2 = j5;
if (this.n) {
Objects.requireNonNull(hashSet);
hashSet.add(aVar.getId());
}
}
j5 = j2;
}
if (z3) {
Objects.requireNonNull((c.f.b.a.d) this.m);
}
b bVar2 = this.o;
synchronized (bVar2) {
j = bVar2.f360c;
}
long j7 = (long) i;
if (!(j == j7 && this.o.a() == j6)) {
if (this.n && this.h != hashSet) {
Objects.requireNonNull(hashSet);
this.h.clear();
this.h.addAll(hashSet);
}
b bVar3 = this.o;
synchronized (bVar3) {
bVar3.f360c = j7;
bVar3.b = j6;
bVar3.a = true;
}
}
this.i = currentTimeMillis2;
return true;
} catch (IOException e) {
c.f.b.a.a aVar2 = this.m;
e.getMessage();
Objects.requireNonNull((c.f.b.a.d) aVar2);
return false;
}
}
public void f(CacheKey cacheKey) {
synchronized (this.q) {
try {
List<String> I0 = AnimatableValueParser.I0(cacheKey);
int i = 0;
while (true) {
ArrayList arrayList = (ArrayList) I0;
if (i >= arrayList.size()) {
break;
}
String str = (String) arrayList.get(i);
this.k.f(str);
this.h.remove(str);
i++;
}
} catch (IOException e) {
c.f.b.a.a aVar = this.m;
e.getMessage();
Objects.requireNonNull((c.f.b.a.d) aVar);
}
}
}
public final c.b g(String str, CacheKey cacheKey) throws IOException {
synchronized (this.q) {
boolean e = e();
h();
long a2 = this.o.a();
if (a2 > this.f && !e) {
b bVar = this.o;
synchronized (bVar) {
bVar.a = false;
bVar.f360c = -1;
bVar.b = -1;
}
e();
}
long j = this.f;
if (a2 > j) {
a((j * 9) / 10, 1);
}
}
return this.k.b(str, cacheKey);
}
public final void h() {
boolean z2 = true;
char c2 = this.k.isExternal() ? (char) 2 : 1;
c.f.d.i.a aVar = this.j;
long a2 = this.d - this.o.a();
aVar.a();
aVar.a();
if (aVar.h.tryLock()) {
try {
if (SystemClock.uptimeMillis() - aVar.g > c.f.d.i.a.b) {
aVar.b();
}
} finally {
aVar.h.unlock();
}
}
StatFs statFs = c2 == 1 ? aVar.f367c : aVar.e;
long availableBlocksLong = statFs != null ? statFs.getAvailableBlocksLong() * statFs.getBlockSizeLong() : 0;
if (availableBlocksLong > 0 && availableBlocksLong >= a2) {
z2 = false;
}
if (z2) {
this.f = this.f359c;
} else {
this.f = this.d;
}
}
}