package androidx.multidex;

import android.content.Context;
import android.content.SharedPreferences;
import androidx.emoji2.text.l0;
import com.google.android.gms.measurement.internal.u1;
import defpackage.e;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import ua.b;

/* JADX INFO: loaded from: classes.dex */
public final class a implements Closeable {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final File f3535a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final long f3536b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final File f3537c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final RandomAccessFile f3538d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final FileChannel f3539e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final FileLock f3540f;

    public a(File file, File file2) throws Throwable {
        file.getPath();
        file2.getPath();
        this.f3535a = file;
        this.f3537c = file2;
        this.f3536b = g(file);
        File file3 = new File(file2, "MultiDex.lock");
        RandomAccessFile randomAccessFile = new RandomAccessFile(file3, "rw");
        this.f3538d = randomAccessFile;
        try {
            try {
                FileChannel channel = randomAccessFile.getChannel();
                this.f3539e = channel;
                try {
                    file3.getPath();
                    this.f3540f = channel.lock();
                    file3.getPath();
                } catch (IOException e10) {
                    e = e10;
                    try {
                        this.f3539e.close();
                    } catch (IOException unused) {
                    }
                    throw e;
                } catch (Error e11) {
                    e = e11;
                    this.f3539e.close();
                    throw e;
                } catch (RuntimeException e12) {
                    e = e12;
                    this.f3539e.close();
                    throw e;
                }
            } catch (IOException e13) {
                e = e13;
                try {
                    this.f3538d.close();
                } catch (IOException unused2) {
                }
                throw e;
            }
        } catch (Error e14) {
            e = e14;
            this.f3538d.close();
            throw e;
        } catch (RuntimeException e15) {
            e = e15;
            this.f3538d.close();
            throw e;
        }
    }

    public static void e(ZipFile zipFile, ZipEntry zipEntry, File file, String str) throws IOException {
        InputStream inputStream = zipFile.getInputStream(zipEntry);
        File fileCreateTempFile = File.createTempFile(e.i("tmp-", str), ".zip", file.getParentFile());
        fileCreateTempFile.getPath();
        try {
            ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(fileCreateTempFile)));
            try {
                ZipEntry zipEntry2 = new ZipEntry("classes.dex");
                zipEntry2.setTime(zipEntry.getTime());
                zipOutputStream.putNextEntry(zipEntry2);
                byte[] bArr = new byte[16384];
                for (int i10 = inputStream.read(bArr); i10 != -1; i10 = inputStream.read(bArr)) {
                    zipOutputStream.write(bArr, 0, i10);
                }
                zipOutputStream.closeEntry();
                zipOutputStream.close();
                if (!fileCreateTempFile.setReadOnly()) {
                    throw new IOException("Failed to mark readonly \"" + fileCreateTempFile.getAbsolutePath() + "\" (tmp of \"" + file.getAbsolutePath() + "\")");
                }
                file.getPath();
                if (fileCreateTempFile.renameTo(file)) {
                    try {
                        inputStream.close();
                    } catch (IOException unused) {
                    }
                    fileCreateTempFile.delete();
                    return;
                }
                throw new IOException("Failed to rename \"" + fileCreateTempFile.getAbsolutePath() + "\" to \"" + file.getAbsolutePath() + "\"");
            } catch (Throwable th) {
                zipOutputStream.close();
                throw th;
            }
        } catch (Throwable th2) {
            try {
                inputStream.close();
            } catch (IOException unused2) {
            }
            fileCreateTempFile.delete();
            throw th2;
        }
    }

    public static long g(File file) throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
        try {
            l0 l0VarU = b.u(randomAccessFile);
            CRC32 crc32 = new CRC32();
            long j10 = l0VarU.f2248c;
            randomAccessFile.seek(l0VarU.f2247b);
            byte[] bArr = new byte[16384];
            int i10 = randomAccessFile.read(bArr, 0, (int) Math.min(16384L, j10));
            while (i10 != -1) {
                crc32.update(bArr, 0, i10);
                j10 -= (long) i10;
                if (j10 == 0) {
                    break;
                }
                i10 = randomAccessFile.read(bArr, 0, (int) Math.min(16384L, j10));
            }
            long value = crc32.getValue();
            randomAccessFile.close();
            return value == -1 ? value - 1 : value;
        } catch (Throwable th) {
            randomAccessFile.close();
            throw th;
        }
    }

    public static void x(Context context, long j10, long j11, ArrayList arrayList) {
        SharedPreferences.Editor editorEdit = context.getSharedPreferences("multidex.version", 4).edit();
        editorEdit.putLong("timestamp", j10);
        editorEdit.putLong("crc", j11);
        editorEdit.putInt("dex.number", arrayList.size() + 1);
        Iterator it = arrayList.iterator();
        int i10 = 2;
        while (it.hasNext()) {
            MultiDexExtractor$ExtractedDex multiDexExtractor$ExtractedDex = (MultiDexExtractor$ExtractedDex) it.next();
            editorEdit.putLong(u1.g("dex.crc.", i10), multiDexExtractor$ExtractedDex.crc);
            editorEdit.putLong("dex.time." + i10, multiDexExtractor$ExtractedDex.lastModified());
            i10++;
        }
        editorEdit.commit();
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final void close() throws IOException {
        this.f3540f.release();
        this.f3539e.close();
        this.f3538d.close();
    }

    /* JADX WARN: Removed duplicated region for block: B:21:0x0056  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final ArrayList k(Context context, boolean z10) {
        ArrayList arrayListP;
        ArrayList arrayListM;
        File file = this.f3535a;
        file.getPath();
        if (!this.f3540f.isValid()) {
            throw new IllegalStateException("MultiDexExtractor was closed");
        }
        if (!z10) {
            SharedPreferences sharedPreferences = context.getSharedPreferences("multidex.version", 4);
            long j10 = sharedPreferences.getLong("timestamp", -1L);
            long jLastModified = file.lastModified();
            if (jLastModified == -1) {
                jLastModified--;
            }
            if (j10 == jLastModified && sharedPreferences.getLong("crc", -1L) == this.f3536b) {
                try {
                    arrayListM = m(context);
                } catch (IOException unused) {
                    arrayListP = p();
                    long jLastModified2 = file.lastModified();
                    if (jLastModified2 == -1) {
                        jLastModified2--;
                    }
                    x(context, jLastModified2, this.f3536b, arrayListP);
                    arrayListM = arrayListP;
                }
            } else {
                arrayListP = p();
                long jLastModified3 = file.lastModified();
                if (jLastModified3 == -1) {
                    jLastModified3--;
                }
                x(context, jLastModified3, this.f3536b, arrayListP);
            }
            arrayListM = arrayListP;
        }
        arrayListM.size();
        return arrayListM;
    }

    public final ArrayList m(Context context) throws IOException {
        String str = this.f3535a.getName() + ".classes";
        SharedPreferences sharedPreferences = context.getSharedPreferences("multidex.version", 4);
        int i10 = sharedPreferences.getInt("dex.number", 1);
        ArrayList arrayList = new ArrayList(i10 - 1);
        for (int i11 = 2; i11 <= i10; i11++) {
            MultiDexExtractor$ExtractedDex multiDexExtractor$ExtractedDex = new MultiDexExtractor$ExtractedDex(this.f3537c, str + i11 + ".zip");
            if (!multiDexExtractor$ExtractedDex.isFile()) {
                throw new IOException("Missing extracted secondary dex file '" + multiDexExtractor$ExtractedDex.getPath() + "'");
            }
            multiDexExtractor$ExtractedDex.crc = g(multiDexExtractor$ExtractedDex);
            long j10 = sharedPreferences.getLong("dex.crc." + i11, -1L);
            long j11 = sharedPreferences.getLong("dex.time." + i11, -1L);
            long jLastModified = multiDexExtractor$ExtractedDex.lastModified();
            if (j11 != jLastModified || j10 != multiDexExtractor$ExtractedDex.crc) {
                StringBuilder sb = new StringBuilder("Invalid extracted dex: ");
                sb.append(multiDexExtractor$ExtractedDex);
                sb.append(" (key \"\"), expected modification time: ");
                sb.append(j11);
                e.B(sb, ", modification time: ", jLastModified, ", expected crc: ");
                sb.append(j10);
                sb.append(", file crc: ");
                sb.append(multiDexExtractor$ExtractedDex.crc);
                throw new IOException(sb.toString());
            }
            arrayList.add(multiDexExtractor$ExtractedDex);
        }
        return arrayList;
    }

    public final ArrayList p() {
        StringBuilder sb = new StringBuilder();
        File file = this.f3535a;
        sb.append(file.getName());
        sb.append(".classes");
        String string = sb.toString();
        z2.b bVar = new z2.b();
        File file2 = this.f3537c;
        File[] fileArrListFiles = file2.listFiles(bVar);
        if (fileArrListFiles == null) {
            file2.getPath();
        } else {
            for (File file3 : fileArrListFiles) {
                file3.getPath();
                file3.length();
                if (file3.delete()) {
                    file3.getPath();
                } else {
                    file3.getPath();
                }
            }
        }
        ArrayList arrayList = new ArrayList();
        ZipFile zipFile = new ZipFile(file);
        try {
            StringBuilder sb2 = new StringBuilder();
            sb2.append("classes");
            int i10 = 2;
            sb2.append(2);
            sb2.append(".dex");
            ZipEntry entry = zipFile.getEntry(sb2.toString());
            while (entry != null) {
                MultiDexExtractor$ExtractedDex multiDexExtractor$ExtractedDex = new MultiDexExtractor$ExtractedDex(file2, string + i10 + ".zip");
                arrayList.add(multiDexExtractor$ExtractedDex);
                multiDexExtractor$ExtractedDex.toString();
                int i11 = 0;
                boolean z10 = false;
                while (i11 < 3 && !z10) {
                    i11++;
                    e(zipFile, entry, multiDexExtractor$ExtractedDex, string);
                    try {
                        multiDexExtractor$ExtractedDex.crc = g(multiDexExtractor$ExtractedDex);
                        z10 = true;
                    } catch (IOException unused) {
                        multiDexExtractor$ExtractedDex.getAbsolutePath();
                        z10 = false;
                    }
                    multiDexExtractor$ExtractedDex.getAbsolutePath();
                    multiDexExtractor$ExtractedDex.length();
                    if (!z10) {
                        multiDexExtractor$ExtractedDex.delete();
                        if (multiDexExtractor$ExtractedDex.exists()) {
                            multiDexExtractor$ExtractedDex.getPath();
                        }
                    }
                }
                if (!z10) {
                    throw new IOException("Could not create zip file " + multiDexExtractor$ExtractedDex.getAbsolutePath() + " for secondary dex (" + i10 + ")");
                }
                i10++;
                entry = zipFile.getEntry("classes" + i10 + ".dex");
            }
            return arrayList;
        } finally {
            try {
                zipFile.close();
            } catch (IOException unused2) {
            }
        }
    }
}
