package com.applovin.impl.sdk; import android.content.Context; import android.net.Uri; import android.support.v4.media.session.PlaybackStateCompat; import com.applovin.impl.ca; import com.applovin.impl.jn; import com.applovin.impl.la; import com.applovin.impl.sdk.utils.CollectionUtils; import com.applovin.impl.sdk.utils.StringUtils; import com.applovin.impl.sj; import com.applovin.impl.tm; import com.applovin.impl.u2; import com.applovin.impl.yl; import com.applovin.impl.yp; import com.facebook.share.internal.ShareConstants; import com.google.firebase.perf.network.FirebasePerfUrlConnection; import com.mbridge.msdk.foundation.entity.CampaignEx; import csdk.gluads.Consts; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; /* loaded from: classes2.dex */ public class l { private final j b; private final n c; private final String a = "FileManager"; private final Object d = new Object(); private final Set e = new HashSet(); public l(j jVar) { this.b = jVar; this.c = jVar.J(); } private boolean f(File file) { if (n.a()) { this.c.a("FileManager", "Removing file " + file.getName() + " from filesystem..."); } try { c(file); boolean delete = file.delete(); if (!delete) { this.b.E().a(la.K, "removeFile", (Map) CollectionUtils.hashMap("path", file.getAbsolutePath())); } g(file); return delete; } catch (Throwable th) { try { if (n.a()) { this.c.a("FileManager", "Failed to remove file " + file.getName() + " from filesystem!", th); } this.b.E().a("FileManager", "removeFile", th); g(file); return false; } catch (Throwable th2) { g(file); throw th2; } } } private void g(File file) { String absolutePath = file.getAbsolutePath(); synchronized (this.d) { try { if (!this.e.remove(absolutePath)) { this.b.E().a(la.K, "unlockFile", (Map) CollectionUtils.hashMap("path", absolutePath)); } this.d.notifyAll(); } catch (Throwable th) { throw th; } } } public void b(Context context) { if (this.b.x0()) { if (n.a()) { this.c.a("FileManager", "Compacting cache..."); } a(a(context), context); } } public void c(final com.applovin.impl.sdk.ad.b bVar, final Context context) { this.b.j0().a((yl) new jn(this.b, false, "removeCachedResourcesForAd", new Runnable() { // from class: com.applovin.impl.sdk.l$$ExternalSyntheticLambda1 @Override // java.lang.Runnable public final void run() { l.this.a(bVar, context); } }), tm.b.CACHING); } public void d(final com.applovin.impl.sdk.ad.b bVar, final Context context) { this.b.j0().a((yl) new jn(this.b, false, "removeCachedVideoResourceForAd", new Runnable() { // from class: com.applovin.impl.sdk.l$$ExternalSyntheticLambda0 @Override // java.lang.Runnable public final void run() { l.this.b(bVar, context); } }), tm.b.CACHING); } /* JADX WARN: Type inference failed for: r1v4, types: [boolean, java.io.Closeable] */ public String e(File file) { Throwable th; InputStream inputStream; IOException e; FileNotFoundException e2; if (file == null) { return null; } if (n.a()) { this.c.a("FileManager", "Reading resource from filesystem: " + file.getName()); } ?? booleanValue = ((Boolean) this.b.a(sj.D)).booleanValue(); boolean z = true; try { try { try { try { if (booleanValue != 0) { try { try { FileInputStream fileInputStream = new FileInputStream(file); try { c(file); String a = a(fileInputStream); r9 = a == null; fileInputStream.close(); if (r9 && ((Boolean) this.b.a(sj.f1)).booleanValue()) { a(file, "removeFileAfterReadFail"); } g(file); return a; } catch (Throwable th2) { try { fileInputStream.close(); } catch (Throwable th3) { th2.addSuppressed(th3); } throw th2; } } catch (FileNotFoundException e3) { if (n.a()) { this.c.d("FileManager", "File not found. " + e3); } this.c.a("FileManager", e3); this.b.E().a("FileManager", "readFileNotFound", e3); if (0 != 0 && ((Boolean) this.b.a(sj.f1)).booleanValue()) { a(file, "removeFileAfterReadFail"); } g(file); return null; } } catch (IOException e4) { if (n.a()) { this.c.a("FileManager", "Failed to read file: " + file.getName() + e4); } this.c.a("FileManager", e4); this.b.E().a("FileManager", "readFileIO", e4); if (((Boolean) this.b.a(sj.f1)).booleanValue()) { a(file, "removeFileAfterReadFail"); } g(file); return null; } catch (Throwable th4) { if (n.a()) { this.c.a("FileManager", "Unknown failure to read file.", th4); } this.c.a("FileManager", th4); this.b.E().a("FileManager", "readFile", th4); if (((Boolean) this.b.a(sj.f1)).booleanValue()) { a(file, "removeFileAfterReadFail"); } g(file); return null; } } try { c(file); inputStream = new FileInputStream(file); } catch (FileNotFoundException e5) { e2 = e5; inputStream = null; } catch (IOException e6) { e = e6; inputStream = null; } catch (Throwable th5) { th = th5; inputStream = null; } try { String a2 = a(inputStream); r9 = a2 == null; yp.a(inputStream, this.b); if (r9 && ((Boolean) this.b.a(sj.f1)).booleanValue()) { a(file, "removeFileAfterReadFail"); } g(file); return a2; } catch (FileNotFoundException e7) { e2 = e7; if (n.a()) { this.c.d("FileManager", "File not found. " + e2); } this.b.E().a("FileManager", "readFileNotFound", e2); yp.a(inputStream, this.b); g(file); return null; } catch (IOException e8) { e = e8; if (n.a()) { this.c.a("FileManager", "Failed to read file: " + file.getName() + e); } this.b.E().a("FileManager", "readFileIO", e); yp.a(inputStream, this.b); if (((Boolean) this.b.a(sj.f1)).booleanValue()) { a(file, "removeFileAfterReadFail"); } g(file); return null; } catch (Throwable th6) { th = th6; if (n.a()) { this.c.a("FileManager", "Unknown failure to read file.", th); } this.b.E().a("FileManager", "readFile", th); yp.a(inputStream, this.b); if (((Boolean) this.b.a(sj.f1)).booleanValue()) { a(file, "removeFileAfterReadFail"); } g(file); return null; } } catch (Throwable th7) { th = th7; yp.a((Closeable) booleanValue, this.b); if (r9 && ((Boolean) this.b.a(sj.f1)).booleanValue()) { a(file, "removeFileAfterReadFail"); } g(file); throw th; } } catch (Throwable th8) { th = th8; r9 = true; yp.a((Closeable) booleanValue, this.b); if (r9) { a(file, "removeFileAfterReadFail"); } g(file); throw th; } } catch (Throwable th9) { th = th9; z = false; if (z && ((Boolean) this.b.a(sj.f1)).booleanValue()) { a(file, "removeFileAfterReadFail"); } g(file); throw th; } } catch (Throwable th10) { th = th10; if (z) { a(file, "removeFileAfterReadFail"); } g(file); throw th; } } private void c(File file) { String absolutePath = file.getAbsolutePath(); synchronized (this.d) { boolean add = this.e.add(absolutePath); while (!add) { try { this.d.wait(); add = this.e.add(absolutePath); } catch (InterruptedException e) { if (n.a()) { this.c.a("FileManager", "Lock '" + absolutePath + "' interrupted", e); } throw new RuntimeException(e); } } } } private boolean d(File file) { if (file == null) { return false; } String absolutePath = file.getAbsolutePath(); synchronized (this.d) { try { if (this.e.contains(absolutePath)) { return false; } c(file); return true; } catch (Throwable th) { throw th; } } } /* JADX INFO: Access modifiers changed from: private */ public /* synthetic */ void b(com.applovin.impl.sdk.ad.b bVar, Context context) { f(a(bVar.t0().getLastPathSegment(), context)); } private boolean b(File file) { boolean contains; String absolutePath = file.getAbsolutePath(); synchronized (this.d) { contains = this.e.contains(absolutePath); } return contains; } private File d(Context context) { return new File(context.getFilesDir(), CampaignEx.JSON_KEY_AD_AL); } public boolean b(String str, Context context) { return a(a(str, false, context)); } private List c(Context context) { File[] listFiles; File d = d(context); if (d.isDirectory() && (listFiles = d.listFiles()) != null) { return Arrays.asList(listFiles); } return Collections.emptyList(); } public boolean c(String str, Context context) { boolean z = false; File a = a(str, false, context); if (!d(a)) { return false; } if (a.exists() && !a.isDirectory()) { z = true; } g(a); return z; } public void e(Context context) { try { a(".nomedia", context); File file = new File(d(context), ".nomedia"); if (a(file)) { return; } if (n.a()) { this.c.a("FileManager", "Creating .nomedia file at " + file.getAbsolutePath()); } if (file.createNewFile()) { return; } if (n.a()) { this.c.b("FileManager", "Failed to create .nomedia file"); } this.b.E().a(la.K, "createNoMediaFile"); } catch (IOException e) { if (n.a()) { this.c.a("FileManager", "Failed to create .nomedia file", e); } } } public String a(Context context, String str, String str2, List list, boolean z, u2 u2Var, int i) { return a(context, str, str2, list, z, false, u2Var, i); } public String a(Context context, String str, String str2, List list, boolean z, boolean z2, u2 u2Var, int i) { if (!StringUtils.isValidString(str)) { if (n.a()) { this.c.a("FileManager", "Nothing to cache, skipping..."); } this.b.E().a(la.K, "cacheResource"); return null; } String a = yp.a(Uri.parse(str), str2, this.b); File a2 = a(a, context); if (!a(a2, str, list, z, u2Var, i)) { return null; } if (n.a()) { this.c.a("FileManager", "Caching succeeded for file " + a); } return z2 ? Uri.fromFile(a2).toString() : a; } public boolean a(File file, String str, List list, u2 u2Var, int i) { return a(file, str, list, true, u2Var, i); } private boolean a(File file, String str, List list, boolean z, u2 u2Var) { InputStream inputStream; if (a(file)) { if (n.a()) { this.c.a("FileManager", "File exists for " + str); } if (u2Var == null) { return true; } u2Var.a(file.length()); return true; } if (((Boolean) this.b.a(sj.D)).booleanValue()) { try { InputStream a = a(str, list, z, u2Var); try { boolean a2 = a(a, file); if (a != null) { a.close(); } return a2; } finally { } } catch (Throwable th) { this.c.a("FileManager", th); this.b.E().a("FileManager", "loadAndCacheResource", th); return false; } } try { inputStream = a(str, list, z, u2Var); try { boolean a3 = a(inputStream, file); yp.a(inputStream, this.b); return a3; } catch (Throwable th2) { th = th2; yp.a(inputStream, this.b); throw th; } } catch (Throwable th3) { th = th3; inputStream = null; } } private boolean a(File file, String str, List list, boolean z, u2 u2Var, int i) { for (int i2 = 1; i2 <= i; i2++) { if (a(file, str, list, z, u2Var)) { if (i2 > 1) { HashMap hashMap = new HashMap(3); hashMap.put(ShareConstants.FEED_SOURCE_PARAM, "assetSuccessfullyRedownloadedAndCached"); hashMap.put("details", String.valueOf(i2)); hashMap.put("url", str); this.b.E().a(la.P, (Map) hashMap); } return true; } } return false; } public InputStream a(String str, List list, boolean z, u2 u2Var) { HttpURLConnection httpURLConnection; if (z && !yp.a(str, list)) { if (n.a()) { this.c.a("FileManager", "Domain is not whitelisted, skipping precache for url: " + str); } return null; } if (((Boolean) this.b.a(sj.m3)).booleanValue() && !str.contains("https://")) { if (n.a()) { this.c.k("FileManager", "Plaintext HTTP operation requested; upgrading to HTTPS due to universal SSL setting..."); } str = str.replace("http://", "https://"); } if (n.a()) { this.c.a("FileManager", "Loading " + str + "..."); } try { httpURLConnection = (HttpURLConnection) ((URLConnection) FirebasePerfUrlConnection.instrument(new URL(str).openConnection())); try { httpURLConnection.setConnectTimeout(((Integer) this.b.a(sj.k3)).intValue()); httpURLConnection.setReadTimeout(((Integer) this.b.a(sj.l3)).intValue()); httpURLConnection.setDefaultUseCaches(true); httpURLConnection.setUseCaches(true); httpURLConnection.setAllowUserInteraction(false); httpURLConnection.setInstanceFollowRedirects(true); int responseCode = httpURLConnection.getResponseCode(); u2Var.a(responseCode); this.b.E().a("loadResource", str, responseCode); if (responseCode >= 200 && responseCode < 300) { if (n.a()) { this.c.a("FileManager", "Opened stream to resource " + str); } InputStream inputStream = httpURLConnection.getInputStream(); if (((Boolean) this.b.a(sj.J3)).booleanValue()) { yp.a(httpURLConnection, this.b); } return inputStream; } return null; } catch (Throwable th) { th = th; try { if (n.a()) { this.c.a("FileManager", "Error loading " + str, th); } this.b.E().a("FileManager", "loadResource", th, CollectionUtils.hashMap("url", str)); u2Var.a(th); if (((Boolean) this.b.a(sj.J3)).booleanValue()) { yp.a(httpURLConnection, this.b); } return null; } finally { if (((Boolean) this.b.a(sj.J3)).booleanValue()) { yp.a(httpURLConnection, this.b); } } } } catch (Throwable th2) { th = th2; httpURLConnection = null; } } public File a(String str, Context context) { return a(str, true, context); } private File a(String str, boolean z, Context context) { if (!StringUtils.isValidString(str)) { if (n.a()) { this.c.a("FileManager", "Nothing to look up, skipping..."); } return null; } if (n.a()) { this.c.a("FileManager", "Looking up cached resource: " + str); } String replace = str.contains("icon") ? str.replace("/", "_").replace(Consts.STRING_PERIOD, "_") : str; File d = d(context); File file = new File(d, replace); if (yp.a(sj.h1, this.b)) { boolean z2 = file.length() == 0; boolean equals = str.equals(".nomedia"); if (file.exists() && z2 && !equals) { this.b.E().a(la.K, "removeEmptyCachedResource", (Map) CollectionUtils.hashMap("path", file.getAbsolutePath())); f(file); } } if (z) { try { d.mkdirs(); } catch (Throwable th) { if (n.a()) { this.c.a("FileManager", "Unable to make cache directory at " + d, th); } this.b.E().a("FileManager", "createCacheDir", th); return null; } } return file; } public String a(InputStream inputStream) { if (((Boolean) this.b.a(sj.D)).booleanValue()) { try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try { byte[] bArr = new byte[8192]; while (true) { int read = inputStream.read(bArr, 0, 8192); if (read >= 0) { byteArrayOutputStream.write(bArr, 0, read); } else { String byteArrayOutputStream2 = byteArrayOutputStream.toString("UTF-8"); byteArrayOutputStream.close(); return byteArrayOutputStream2; } } } finally { } } catch (Throwable th) { this.c.a("FileManager", th); this.b.E().a("FileManager", "readInputStreamAsString", th); return null; } } else { ByteArrayOutputStream byteArrayOutputStream3 = new ByteArrayOutputStream(); byte[] bArr2 = new byte[8192]; while (true) { int read2 = inputStream.read(bArr2, 0, 8192); if (read2 >= 0) { try { byteArrayOutputStream3.write(bArr2, 0, read2); } catch (Throwable th2) { yp.a(byteArrayOutputStream3, this.b); this.b.E().a("FileManager", "readInputStreamAsString", th2); return null; } } else { return byteArrayOutputStream3.toString("UTF-8"); } } } } /* JADX WARN: Removed duplicated region for block: B:126:0x01ca A[Catch: all -> 0x01d0, TRY_LEAVE, TryCatch #6 {all -> 0x01d0, blocks: (B:124:0x01c4, B:126:0x01ca), top: B:123:0x01c4 }] */ /* JADX WARN: Removed duplicated region for block: B:46:0x00f3 A[Catch: all -> 0x00f9, TryCatch #13 {all -> 0x00f9, blocks: (B:44:0x00ed, B:46:0x00f3, B:47:0x00fc), top: B:43:0x00ed }] */ /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct add '--show-bad-code' argument */ public boolean a(java.io.InputStream r17, java.io.File r18, boolean r19) { /* Method dump skipped, instructions count: 505 To view this dump add '--comments-level debug' option */ throw new UnsupportedOperationException("Method not decompiled: com.applovin.impl.sdk.l.a(java.io.InputStream, java.io.File, boolean):boolean"); } public boolean a(InputStream inputStream, File file) { if (file == null) { return false; } if (n.a()) { this.c.a("FileManager", "Caching " + file.getAbsolutePath() + "..."); } if (!a(inputStream, file, false)) { if (n.a()) { this.c.b("FileManager", "Unable to cache " + file.getAbsolutePath()); } return false; } if (!n.a()) { return true; } this.c.a("FileManager", "Caching completed for " + file); return true; } public int a(String str, com.applovin.impl.sdk.ad.b bVar) { List Y = bVar.Y(); if (bVar.S0() || Y.contains(str)) { return bVar.F(); } return 1; } private long a(Context context) { long a = a(); boolean z = a != -1; long seconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()); List c = this.b.c(sj.a1); long j = 0; for (File file : c(context)) { if (z && !c.contains(file.getName()) && !b(file) && seconds - TimeUnit.MILLISECONDS.toSeconds(file.lastModified()) > a) { if (n.a()) { this.c.a("FileManager", "File " + file.getName() + " has expired, removing..."); } if (f(file)) { this.b.D().c(ca.j); } } j += file.length(); } return j; } private void a(long j, Context context) { long intValue = ((Integer) this.b.a(sj.W0)).intValue(); if (intValue == -1) { if (n.a()) { this.c.a("FileManager", "Cache has no maximum size set; skipping drop..."); } } else { if (a(j) > intValue) { if (n.a()) { this.c.a("FileManager", "Cache has exceeded maximum size; dropping..."); } Iterator it = c(context).iterator(); while (it.hasNext()) { f((File) it.next()); } this.b.D().c(ca.k); return; } if (n.a()) { this.c.a("FileManager", "Cache is present but under size limit; not dropping..."); } } } /* JADX INFO: Access modifiers changed from: private */ public /* synthetic */ void a(com.applovin.impl.sdk.ad.b bVar, Context context) { ArrayList arrayList = new ArrayList(bVar.h()); arrayList.add(bVar.t0()); Iterator it = arrayList.iterator(); while (it.hasNext()) { f(a(((Uri) it.next()).getLastPathSegment(), context)); } } public void a(File file, String str) { if (n.a()) { this.c.a("FileManager", "Removing file " + file.getName() + " for source " + str + Consts.STRING_PERIOD); } try { if (file.delete()) { return; } this.b.E().a(la.K, str, (Map) CollectionUtils.hashMap("path", file.getAbsolutePath())); } catch (Throwable th) { if (n.a()) { this.c.a("FileManager", "Failed to remove file " + file.getName() + " from filesystem after failed operation.", th); } this.b.E().a("FileManager", str, th); } } private long a() { long longValue = ((Long) this.b.a(sj.V0)).longValue(); if (longValue >= 0) { return longValue; } return -1L; } private long a(long j) { return j / PlaybackStateCompat.ACTION_SET_CAPTIONING_ENABLED; } public boolean a(File file) { if (!yp.a(sj.k1, this.b)) { return (file == null || !file.exists() || file.isDirectory()) ? false : true; } if (file == null) { return false; } yp.a(); c(file); boolean z = file.exists() && !file.isDirectory(); g(file); return z; } }