package com.google.android.play.core.assetpacks;

import X.4Ms;
import X.4N2;
import X.C18518Kc;
import X.C33198r0;
import X.C33208r1;
import android.content.Context;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.ies.abmock.SettingsManager;
import com.google.android.play.core.b.b;
import com.google.android.play.core.b.bf;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

public final class ac {
    public static final b LIZ = new b("AssetPackStorage");
    public static final long LIZJ = TimeUnit.DAYS.toMillis(14);
    public static final long LIZLLL = TimeUnit.DAYS.toMillis(28);
    public final ch LIZIZ;
    public final Context LJ;

    static {
        Covode.recordClassIndex(44109);
    }

    public ac(Context context, ch chVar) {
        this.LJ = context;
        this.LIZIZ = chVar;
    }

    public static long LIZ(File file, boolean z) {
        if (!file.exists()) {
            return -1;
        }
        ArrayList arrayList = new ArrayList();
        if (z && file.listFiles().length > 1) {
            LIZ.LIZLLL("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 unused) {
            LIZ.LIZ();
        }
        if (arrayList.isEmpty()) {
            return -1;
        }
        Collections.sort(arrayList);
        return ((Long) arrayList.get(arrayList.size() - 1)).longValue();
    }

    private final File LIZ(String str, int i) {
        return new File(LIZ(str), String.valueOf(i));
    }

    public static boolean LIZIZ(File file) {
        boolean z;
        File[] listFiles = file.listFiles();
        if (listFiles != null) {
            z = true;
            for (File LIZIZ2 : listFiles) {
                z &= LIZIZ(LIZIZ2);
            }
        } else {
            z = true;
        }
        return LIZJ(file) && true == z;
    }

    private final File LIZLLL() {
        return new File(LIZJ(), "_tmp");
    }

    private final File LJII(String str, int i, long j) {
        return new File(new File(new File(LIZLLL(), str), String.valueOf(i)), String.valueOf(j));
    }

    public final File LIZ(String str) {
        return new File(LIZJ(), str);
    }

    public final File LIZ(String str, int i, long j) {
        return new File(LIZ(str, i), String.valueOf(j));
    }

    public final File LIZ(String str, int i, long j, String str2) {
        return new File(new File(new File(LJII(str, i, j), "_slices"), "_unverified"), str2);
    }

    public final File LIZIZ(String str, int i, long j) {
        return new File(LIZ(str, i, j), "_metadata");
    }

    public final File LIZIZ(String str, int i, long j, String str2) {
        return new File(new File(new File(LJII(str, i, j), "_slices"), "_verified"), str2);
    }

    public final List<File> LIZIZ() {
        ArrayList arrayList = new ArrayList();
        try {
            if (LIZJ().exists()) {
                if (LIZJ().listFiles() != null) {
                    for (File file : LIZJ().listFiles()) {
                        if (!file.getCanonicalPath().equals(LIZLLL().getCanonicalPath())) {
                            arrayList.add(file);
                        }
                    }
                    return arrayList;
                }
            }
            return arrayList;
        } catch (IOException e) {
            LIZ.LIZIZ("Could not process directory while scanning installed packs. %s", e);
        }
    }

    public final File LIZJ() {
        return new File(LIZ(this.LJ), "assetpacks");
    }

    public final File LIZJ(String str, int i, long j) {
        return new File(LJII(str, i, j), "_packs");
    }

    public final File LIZJ(String str, int i, long j, String str2) {
        return new File(LJ(str, i, j, str2), "checkpoint.dat");
    }

    public final File LIZLLL(String str, int i, long j) {
        return new File(LIZJ(str, i, j), "merge.tmp");
    }

    public final File LIZLLL(String str, int i, long j, String str2) {
        return new File(LJ(str, i, j, str2), "checkpoint_ext.dat");
    }

    public final int LJ(String str, int i, long j) {
        MethodCollector.i(17975);
        File LIZLLL2 = LIZLLL(str, i, j);
        if (!LIZLLL2.exists()) {
            MethodCollector.o(17975);
            return 0;
        }
        Properties properties = new Properties();
        FileInputStream fileInputStream = new FileInputStream(LIZLLL2);
        try {
            properties.load(fileInputStream);
            fileInputStream.close();
            if (properties.getProperty("numberOfMerges") != null) {
                try {
                    int parseInt = Integer.parseInt(properties.getProperty("numberOfMerges"));
                    MethodCollector.o(17975);
                    return parseInt;
                } catch (NumberFormatException e) {
                    au auVar = new au("Merge checkpoint file corrupt.", (Exception) e);
                    MethodCollector.o(17975);
                    throw auVar;
                }
            } else {
                au auVar2 = new au("Merge checkpoint file corrupt.");
                MethodCollector.o(17975);
                throw auVar2;
            }
        } catch (Throwable th) {
            bf.LIZ(th, th);
        }
        MethodCollector.o(17975);
        throw th;
    }

    public final File LJ(String str, int i, long j, String str2) {
        return new File(LJFF(str, i, j), str2);
    }

    public final File LJFF(String str, int i, long j) {
        return new File(new File(LJII(str, i, j), "_slices"), "_metadata");
    }

    public final void LJI(String str, int i, long j) {
        if (LJII(str, i, j).exists()) {
            LIZIZ(LJII(str, i, j));
        }
    }

    public static boolean LIZJ(File file) {
        MethodCollector.i(18084);
        try {
            C33208r1 r3 = C18518Kc.LIZIZ() ? (C33208r1) SettingsManager.LIZ().LIZ("storage_intercepter_key", C33208r1.class, 4N2.LIZ) : 4N2.LIZ;
            if (C33198r0.LIZ(file.getAbsolutePath(), r3)) {
                C33198r0.LIZ(file, new RuntimeException(), "exception_delete_log", C33198r0.LIZ(r3));
            }
            if (C33198r0.LIZJ(file.getAbsolutePath(), r3)) {
                C33198r0.LIZ(file, new RuntimeException(), "exception_handle", C33198r0.LIZ(r3));
                MethodCollector.o(18084);
                return false;
            }
        } catch (Throwable unused) {
        }
        boolean delete = file.delete();
        MethodCollector.o(18084);
        return delete;
    }

    public static File LIZ(Context context) {
        if (4Ms.LIZJ != null && 4Ms.LJ) {
            return 4Ms.LIZJ;
        }
        File filesDir = context.getFilesDir();
        4Ms.LIZJ = filesDir;
        return filesDir;
    }

    /* JADX WARNING: Removed duplicated region for block: B:22:0x00b0 A[Catch:{ IOException -> 0x00dd }] */
    /* JADX WARNING: Removed duplicated region for block: B:23:0x00b1 A[Catch:{ IOException -> 0x00dd }] */
    /* JADX WARNING: Removed duplicated region for block: B:33:0x000f A[SYNTHETIC] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public final java.util.Map<java.lang.String, com.google.android.play.core.assetpacks.b> LIZ() {
        /*
            r11 = this;
            java.util.HashMap r4 = new java.util.HashMap
            r4.<init>()
            r6 = 1
            r5 = 0
            java.util.List r0 = r11.LIZIZ()     // Catch:{ IOException -> 0x00dd }
            java.util.Iterator r10 = r0.iterator()     // Catch:{ IOException -> 0x00dd }
        L_0x000f:
            boolean r0 = r10.hasNext()     // Catch:{ IOException -> 0x00dd }
            if (r0 == 0) goto L_0x00e9
            java.lang.Object r8 = r10.next()     // Catch:{ IOException -> 0x00dd }
            java.io.File r8 = (java.io.File) r8     // Catch:{ IOException -> 0x00dd }
            java.lang.String r9 = r8.getName()     // Catch:{ IOException -> 0x00dd }
            java.io.File r2 = new java.io.File     // Catch:{ IOException -> 0x00dd }
            java.io.File r0 = r11.LIZJ()     // Catch:{ IOException -> 0x00dd }
            r2.<init>(r0, r9)     // Catch:{ IOException -> 0x00dd }
            boolean r0 = r2.exists()     // Catch:{ IOException -> 0x00dd }
            r7 = 0
            if (r0 != 0) goto L_0x003b
            com.google.android.play.core.b.b r2 = LIZ     // Catch:{ IOException -> 0x00dd }
            java.lang.Object[] r1 = new java.lang.Object[r6]     // Catch:{ IOException -> 0x00dd }
            r1[r5] = r9     // Catch:{ IOException -> 0x00dd }
            java.lang.String r0 = "Pack not found with pack name: %s"
            r2.LIZ(r0, r1)     // Catch:{ IOException -> 0x00dd }
            goto L_0x00ab
        L_0x003b:
            java.io.File r1 = new java.io.File     // Catch:{ IOException -> 0x00dd }
            com.google.android.play.core.assetpacks.ch r0 = r11.LIZIZ     // Catch:{ IOException -> 0x00dd }
            int r0 = r0.LIZ()     // Catch:{ IOException -> 0x00dd }
            java.lang.String r0 = java.lang.String.valueOf(r0)     // Catch:{ IOException -> 0x00dd }
            r1.<init>(r2, r0)     // Catch:{ IOException -> 0x00dd }
            boolean r0 = r1.exists()     // Catch:{ IOException -> 0x00dd }
            r3 = 2
            if (r0 != 0) goto L_0x0069
            com.google.android.play.core.b.b r2 = LIZ     // Catch:{ IOException -> 0x00dd }
            java.lang.Object[] r1 = new java.lang.Object[r3]     // Catch:{ IOException -> 0x00dd }
            r1[r5] = r9     // Catch:{ IOException -> 0x00dd }
            com.google.android.play.core.assetpacks.ch r0 = r11.LIZIZ     // Catch:{ IOException -> 0x00dd }
            int r0 = r0.LIZ()     // Catch:{ IOException -> 0x00dd }
            java.lang.Integer r0 = java.lang.Integer.valueOf(r0)     // Catch:{ IOException -> 0x00dd }
            r1[r6] = r0     // Catch:{ IOException -> 0x00dd }
            java.lang.String r0 = "Pack not found with pack name: %s app version: %s"
            r2.LIZ(r0, r1)     // Catch:{ IOException -> 0x00dd }
            goto L_0x00ab
        L_0x0069:
            java.io.File[] r1 = r1.listFiles()     // Catch:{ IOException -> 0x00dd }
            if (r1 == 0) goto L_0x0072
            int r0 = r1.length     // Catch:{ IOException -> 0x00dd }
            if (r0 != 0) goto L_0x008a
        L_0x0072:
            com.google.android.play.core.b.b r2 = LIZ     // Catch:{ IOException -> 0x00dd }
            java.lang.Object[] r1 = new java.lang.Object[r3]     // Catch:{ IOException -> 0x00dd }
            r1[r5] = r9     // Catch:{ IOException -> 0x00dd }
            com.google.android.play.core.assetpacks.ch r0 = r11.LIZIZ     // Catch:{ IOException -> 0x00dd }
            int r0 = r0.LIZ()     // Catch:{ IOException -> 0x00dd }
            java.lang.Integer r0 = java.lang.Integer.valueOf(r0)     // Catch:{ IOException -> 0x00dd }
            r1[r6] = r0     // Catch:{ IOException -> 0x00dd }
            java.lang.String r0 = "No pack version found for pack name: %s app version: %s"
            r2.LIZ(r0, r1)     // Catch:{ IOException -> 0x00dd }
            goto L_0x00ab
        L_0x008a:
            if (r0 <= r6) goto L_0x00a4
            com.google.android.play.core.b.b r2 = LIZ     // Catch:{ IOException -> 0x00dd }
            java.lang.Object[] r1 = new java.lang.Object[r3]     // Catch:{ IOException -> 0x00dd }
            r1[r5] = r9     // Catch:{ IOException -> 0x00dd }
            com.google.android.play.core.assetpacks.ch r0 = r11.LIZIZ     // Catch:{ IOException -> 0x00dd }
            int r0 = r0.LIZ()     // Catch:{ IOException -> 0x00dd }
            java.lang.Integer r0 = java.lang.Integer.valueOf(r0)     // Catch:{ IOException -> 0x00dd }
            r1[r6] = r0     // Catch:{ IOException -> 0x00dd }
            java.lang.String r0 = "Multiple pack versions found for pack name: %s app version: %s"
            r2.LIZIZ(r0, r1)     // Catch:{ IOException -> 0x00dd }
            goto L_0x00ab
        L_0x00a4:
            r0 = r1[r5]     // Catch:{ IOException -> 0x00dd }
            java.lang.String r1 = r0.getCanonicalPath()     // Catch:{ IOException -> 0x00dd }
            goto L_0x00ac
        L_0x00ab:
            r1 = r7
        L_0x00ac:
            if (r1 != 0) goto L_0x00b1
        L_0x00ae:
            if (r7 == 0) goto L_0x000f
            goto L_0x00d4
        L_0x00b1:
            java.io.File r3 = new java.io.File     // Catch:{ IOException -> 0x00dd }
            java.lang.String r0 = "assets"
            r3.<init>(r1, r0)     // Catch:{ IOException -> 0x00dd }
            boolean r0 = r3.isDirectory()     // Catch:{ IOException -> 0x00dd }
            if (r0 != 0) goto L_0x00ca
            com.google.android.play.core.b.b r2 = LIZ     // Catch:{ IOException -> 0x00dd }
            java.lang.Object[] r1 = new java.lang.Object[r6]     // Catch:{ IOException -> 0x00dd }
            r1[r5] = r3     // Catch:{ IOException -> 0x00dd }
            java.lang.String r0 = "Failed to find assets directory: %s"
            r2.LIZIZ(r0, r1)     // Catch:{ IOException -> 0x00dd }
            goto L_0x00ae
        L_0x00ca:
            java.lang.String r0 = r3.getCanonicalPath()     // Catch:{ IOException -> 0x00dd }
            com.google.android.play.core.assetpacks.ag r7 = new com.google.android.play.core.assetpacks.ag     // Catch:{ IOException -> 0x00dd }
            r7.<init>(r5, r1, r0)     // Catch:{ IOException -> 0x00dd }
            goto L_0x00ae
        L_0x00d4:
            java.lang.String r0 = r8.getName()     // Catch:{ IOException -> 0x00dd }
            r4.put(r0, r7)     // Catch:{ IOException -> 0x00dd }
            goto L_0x000f
        L_0x00dd:
            r0 = move-exception
            com.google.android.play.core.b.b r2 = LIZ
            java.lang.Object[] r1 = new java.lang.Object[r6]
            r1[r5] = r0
            java.lang.String r0 = "Could not process directory while scanning installed packs: %s"
            r2.LIZIZ(r0, r1)
        L_0x00e9:
            return r4
        */
        throw new UnsupportedOperationException("Method not decompiled: com.google.android.play.core.assetpacks.ac.LIZ():java.util.Map");
    }

    public static void LIZ(File file) {
        if (file.listFiles() != null && file.listFiles().length > 1) {
            long LIZ2 = LIZ(file, false);
            for (File file2 : file.listFiles()) {
                if (!file2.getName().equals(String.valueOf(LIZ2)) && !file2.getName().equals("stale.tmp")) {
                    LIZIZ(file2);
                }
            }
        }
    }

    public final int LIZIZ(String str) {
        return (int) LIZ(LIZ(str), true);
    }

    public final long LIZJ(String str) {
        return LIZ(LIZ(str, LIZIZ(str)), true);
    }
}
