package g8; import android.content.Context; import android.os.Environment; import android.os.FileObserver; import android.os.StatFs; import android.util.Log; import com.vungle.warren.VungleLogger; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; /* loaded from: classes2.dex */ public class a { /* renamed from: h, reason: collision with root package name */ private static final String f9750h = "a"; /* renamed from: a, reason: collision with root package name */ private final Context f9751a; /* renamed from: b, reason: collision with root package name */ private final f f9752b; /* renamed from: d, reason: collision with root package name */ private File f9754d; /* renamed from: f, reason: collision with root package name */ private boolean f9756f; /* renamed from: c, reason: collision with root package name */ private final Set f9753c = new HashSet(); /* renamed from: e, reason: collision with root package name */ private final List f9755e = new ArrayList(); /* renamed from: g, reason: collision with root package name */ private final List f9757g = new ArrayList(); /* renamed from: g8.a$a, reason: collision with other inner class name */ class FileObserverC0146a extends FileObserver { FileObserverC0146a(String str, int i10) { super(str, i10); } @Override // android.os.FileObserver public void onEvent(int i10, String str) { stopWatching(); a.this.k(); } } class b extends FileObserver { /* renamed from: a, reason: collision with root package name */ final /* synthetic */ String f9759a; /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ b(String str, int i10, String str2) { super(str, i10); this.f9759a = str2; } @Override // android.os.FileObserver public void onEvent(int i10, String str) { stopWatching(); if (this.f9759a.equals(str)) { a.this.k(); } } } public interface c { void c(); } public a(Context context, f fVar) { this.f9751a = context; this.f9752b = fVar; fVar.b("cache_path", "cache_paths").c(); } private void c() { File file = this.f9754d; if (file != null && file.exists() && this.f9754d.isDirectory() && this.f9754d.canWrite()) { return; } k(); } private static void d(File file) { if (file.exists() && file.isFile()) { com.vungle.warren.utility.j.c(file); } } private long f(int i10) { StatFs statFs; File g10 = g(); if (g10 == null) { return -1L; } try { statFs = new StatFs(g10.getPath()); } catch (IllegalArgumentException e10) { Log.w(f9750h, "Failed to get available bytes", e10); if (i10 > 0) { return f(i10 - 1); } statFs = null; } if (statFs != null) { return statFs.getBlockSizeLong() * statFs.getAvailableBlocksLong(); } return -1L; } private synchronized void i(File file) { if (file == null) { return; } this.f9757g.clear(); this.f9757g.add(new FileObserverC0146a(file.getPath(), 1024)); while (file.getParent() != null) { this.f9757g.add(new b(file.getParent(), 256, file.getName())); file = file.getParentFile(); } Iterator it = this.f9757g.iterator(); while (it.hasNext()) { try { ((FileObserver) it.next()).startWatching(); } catch (Exception e10) { VungleLogger.k(true, f9750h, "ExceptionContext", Log.getStackTraceString(e10)); } } } /* JADX INFO: Access modifiers changed from: private */ public synchronized void k() { File file; boolean z10; File parentFile; File file2 = null; if (this.f9754d == null) { String f10 = this.f9752b.f("cache_path", null); this.f9754d = f10 != null ? new File(f10) : null; } File externalFilesDir = this.f9751a.getExternalFilesDir(null); File filesDir = this.f9751a.getFilesDir(); boolean z11 = Environment.getExternalStorageState().equals("mounted") && externalFilesDir != null; ArrayList arrayList = new ArrayList(); if (z11 && (parentFile = externalFilesDir.getParentFile()) != null) { arrayList.add(new File(parentFile, "no_backup")); } arrayList.add(this.f9751a.getNoBackupFilesDir()); if (z11) { arrayList.add(externalFilesDir); } arrayList.add(filesDir); Iterator it = arrayList.iterator(); boolean z12 = false; while (true) { if (!it.hasNext()) { break; } File file3 = new File((File) it.next(), "vungle_cache"); d(file3); if (file3.exists()) { z10 = file3.isDirectory() && file3.canWrite(); } else { z12 = file3.mkdirs(); z10 = z12; } if (z10) { file2 = file3; break; } } File cacheDir = this.f9751a.getCacheDir(); HashSet g10 = this.f9752b.g("cache_paths", new HashSet()); if (file2 != null) { com.vungle.warren.utility.d.a(g10, file2.getPath()); } com.vungle.warren.utility.d.a(g10, cacheDir.getPath()); this.f9752b.k("cache_paths", g10).c(); this.f9755e.clear(); Iterator it2 = g10.iterator(); while (it2.hasNext()) { String str = (String) it2.next(); if (file2 == null || !file2.getPath().equals(str)) { this.f9755e.add(new File(str)); } } if (z12 || ((file2 != null && !file2.equals(this.f9754d)) || ((file = this.f9754d) != null && !file.equals(file2)))) { this.f9754d = file2; if (file2 != null) { this.f9752b.j("cache_path", file2.getPath()).c(); } Iterator it3 = this.f9753c.iterator(); while (it3.hasNext()) { ((c) it3.next()).c(); } this.f9756f = true; for (File file4 : this.f9755e) { if (!file4.equals(cacheDir)) { try { com.vungle.warren.utility.j.b(file4); } catch (IOException unused) { VungleLogger.d(true, f9750h, "CacheManager", "Can't remove old cache:" + file4.getPath()); } } } } i(externalFilesDir); } public synchronized void b(c cVar) { c(); this.f9753c.add(cVar); if (this.f9756f) { cVar.c(); } } public long e() { return f(1); } public synchronized File g() { c(); return this.f9754d; } public synchronized List h() { c(); return this.f9755e; } public synchronized void j(c cVar) { this.f9753c.remove(cVar); } }