package com.google.android.play.core.assetpacks; import android.content.Context; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.concurrent.TimeUnit; /* loaded from: classes2.dex */ final class e0 { /* renamed from: c, reason: collision with root package name */ private static final r3.h0 f4831c = new r3.h0("AssetPackStorage"); /* renamed from: d, reason: collision with root package name */ private static final long f4832d; /* renamed from: e, reason: collision with root package name */ private static final long f4833e; /* renamed from: a, reason: collision with root package name */ private final Context f4834a; /* renamed from: b, reason: collision with root package name */ private final r2 f4835b; static { TimeUnit timeUnit = TimeUnit.DAYS; f4832d = timeUnit.toMillis(14L); f4833e = timeUnit.toMillis(28L); } e0(Context context, r2 r2Var) { this.f4834a = context; this.f4835b = r2Var; } private static long g(File file, boolean z10) { if (!file.exists()) { return -1L; } ArrayList arrayList = new ArrayList(); if (z10 && file.listFiles().length > 1) { f4831c.e("Multiple pack versions found, using highest version code.", new Object[0]); } try { for (File file2 : file.listFiles()) { if (!file2.getName().equals("stale.tmp")) { arrayList.add(Long.valueOf(file2.getName())); } } } catch (NumberFormatException e10) { f4831c.c(e10, "Corrupt asset pack directories.", new Object[0]); } if (arrayList.isEmpty()) { return -1L; } Collections.sort(arrayList); return ((Long) arrayList.get(arrayList.size() - 1)).longValue(); } private final File h(String str) { return new File(k(), str); } private final File i(String str, int i10, long j10) { return new File(v(str, i10, j10), "merge.tmp"); } private final File j(String str, int i10, long j10) { return new File(new File(new File(l(), str), String.valueOf(i10)), String.valueOf(j10)); } private final File k() { return new File(this.f4834a.getFilesDir(), "assetpacks"); } private final File l() { return new File(k(), "_tmp"); } private final List m() { ArrayList arrayList = new ArrayList(); try { if (k().exists() && k().listFiles() != null) { for (File file : k().listFiles()) { if (!file.getCanonicalPath().equals(l().getCanonicalPath())) { arrayList.add(file); } } } } catch (IOException e10) { f4831c.b("Could not process directory while scanning installed packs. %s", e10); } return arrayList; } private static boolean n(File file) { File[] listFiles = file.listFiles(); boolean z10 = true; if (listFiles != null) { for (File file2 : listFiles) { z10 &= n(file2); } } if (file.delete()) { return z10; } return false; } final File A(String str, int i10, long j10, String str2) { return new File(x(str, i10, j10), str2); } final File B(String str, int i10, long j10, String str2) { return new File(new File(new File(j(str, i10, j10), "_slices"), "_unverified"), str2); } final File C(String str, int i10, long j10, String str2) { return new File(new File(new File(j(str, i10, j10), "_slices"), "_verified"), str2); } final String D(String str) { int length; File file = new File(k(), str); if (!file.exists()) { f4831c.a("Pack not found with pack name: %s", str); return null; } File file2 = new File(file, String.valueOf(this.f4835b.a())); if (!file2.exists()) { f4831c.a("Pack not found with pack name: %s app version: %s", str, Integer.valueOf(this.f4835b.a())); return null; } File[] listFiles = file2.listFiles(); if (listFiles == null || (length = listFiles.length) == 0) { f4831c.a("No pack version found for pack name: %s app version: %s", str, Integer.valueOf(this.f4835b.a())); return null; } if (length <= 1) { return listFiles[0].getCanonicalPath(); } f4831c.b("Multiple pack versions found for pack name: %s app version: %s", str, Integer.valueOf(this.f4835b.a())); return null; } final Map E() { HashMap hashMap = new HashMap(); Iterator it = m().iterator(); while (it.hasNext()) { String name = ((File) it.next()).getName(); int g10 = (int) g(h(name), true); long g11 = g(s(name, g10), true); if (t(name, g10, g11).exists()) { hashMap.put(name, Long.valueOf(g11)); } } return hashMap; } final Map F() { HashMap hashMap = new HashMap(); for (String str : G().keySet()) { hashMap.put(str, Long.valueOf(q(str))); } return hashMap; } final Map G() { HashMap hashMap = new HashMap(); try { for (File file : m()) { b r10 = r(file.getName()); if (r10 != null) { hashMap.put(file.getName(), r10); } } } catch (IOException e10) { f4831c.b("Could not process directory while scanning installed packs: %s", e10); } return hashMap; } final void H() { n(k()); } final void a(String str, int i10, long j10, int i11) { File i12 = i(str, i10, j10); Properties properties = new Properties(); properties.put("numberOfMerges", String.valueOf(i11)); i12.getParentFile().mkdirs(); i12.createNewFile(); FileOutputStream fileOutputStream = new FileOutputStream(i12); properties.store(fileOutputStream, (String) null); fileOutputStream.close(); } final void b(String str, int i10, long j10) { File h10 = h(str); if (h10.exists()) { for (File file : h10.listFiles()) { if (!file.getName().equals(String.valueOf(i10)) && !file.getName().equals("stale.tmp")) { n(file); } else if (file.getName().equals(String.valueOf(i10))) { for (File file2 : file.listFiles()) { if (!file2.getName().equals(String.valueOf(j10))) { n(file2); } } } } } } final void c(List list) { int a10 = this.f4835b.a(); for (File file : m()) { if (!list.contains(file.getName()) && g(file, true) != a10) { n(file); } } } final boolean d(String str, int i10, long j10) { if (j(str, i10, j10).exists()) { return n(j(str, i10, j10)); } return true; } final boolean e(String str, int i10, long j10) { if (t(str, i10, j10).exists()) { return n(t(str, i10, j10)); } return true; } final boolean f(String str) { return D(str) != null; } final int o(String str) { return (int) g(h(str), true); } final int p(String str, int i10, long j10) { File i11 = i(str, i10, j10); if (!i11.exists()) { return 0; } Properties properties = new Properties(); FileInputStream fileInputStream = new FileInputStream(i11); try { properties.load(fileInputStream); fileInputStream.close(); if (properties.getProperty("numberOfMerges") == null) { throw new d1("Merge checkpoint file corrupt."); } try { return Integer.parseInt(properties.getProperty("numberOfMerges")); } catch (NumberFormatException e10) { throw new d1("Merge checkpoint file corrupt.", e10); } } catch (Throwable th) { try { fileInputStream.close(); } catch (Throwable th2) { th.addSuppressed(th2); } throw th; } } final long q(String str) { return g(s(str, (int) g(h(str), true)), true); } final b r(String str) { String D = D(str); if (D == null) { return null; } File file = new File(D, "assets"); if (file.isDirectory()) { return new i0(0, D, file.getCanonicalPath()); } f4831c.b("Failed to find assets directory: %s", file); return null; } final File s(String str, int i10) { return new File(h(str), String.valueOf(i10)); } final File t(String str, int i10, long j10) { return new File(s(str, i10), String.valueOf(j10)); } final File u(String str, int i10, long j10) { return new File(t(str, i10, j10), "_metadata"); } final File v(String str, int i10, long j10) { return new File(j(str, i10, j10), "_packs"); } final File w(String str, int i10, long j10) { return new File(u(str, i10, j10), "properties.dat"); } final File x(String str, int i10, long j10) { return new File(new File(j(str, i10, j10), "_slices"), "_metadata"); } final File y(String str, int i10, long j10, String str2) { return new File(A(str, i10, j10, str2), "checkpoint_ext.dat"); } final File z(String str, int i10, long j10, String str2) { return new File(A(str, i10, j10, str2), "checkpoint.dat"); } }