package d.a;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.util.Log;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileFilter;
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.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/* JADX INFO: compiled from: MultiDexExtractor.java */
/* JADX INFO: loaded from: classes.dex */
final class b implements Closeable {
    private final File b;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private final long f433e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private final File f434f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private final RandomAccessFile f435g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private final FileChannel f436h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private final FileLock f437i;

    /* JADX INFO: compiled from: MultiDexExtractor.java */
    class a implements FileFilter {
        a(b bVar) {
        }

        @Override // java.io.FileFilter
        public boolean accept(File file) {
            return !file.getName().equals("MultiDex.lock");
        }
    }

    /* JADX INFO: renamed from: d.a.b$b, reason: collision with other inner class name */
    /* JADX INFO: compiled from: MultiDexExtractor.java */
    private static class C0014b extends File {
        public long b;

        public C0014b(File file, String str) {
            super(file, str);
            this.b = -1L;
        }
    }

    b(File file, File file2) throws IOException {
        Log.i("MultiDex", "MultiDexExtractor(" + file.getPath() + ", " + file2.getPath() + ")");
        this.b = file;
        this.f434f = file2;
        this.f433e = l(file);
        File file3 = new File(file2, "MultiDex.lock");
        RandomAccessFile randomAccessFile = new RandomAccessFile(file3, "rw");
        this.f435g = randomAccessFile;
        try {
            this.f436h = randomAccessFile.getChannel();
            try {
                Log.i("MultiDex", "Blocking on lock " + file3.getPath());
                this.f437i = this.f436h.lock();
                Log.i("MultiDex", file3.getPath() + " locked");
            } catch (IOException e2) {
                e = e2;
                b(this.f436h);
                throw e;
            } catch (Error e3) {
                e = e3;
                b(this.f436h);
                throw e;
            } catch (RuntimeException e4) {
                e = e4;
                b(this.f436h);
                throw e;
            }
        } catch (IOException | Error | RuntimeException e5) {
            b(this.f435g);
            throw e5;
        }
    }

    private void a() {
        File[] fileArrListFiles = this.f434f.listFiles(new a(this));
        if (fileArrListFiles == null) {
            Log.w("MultiDex", "Failed to list secondary dex dir content (" + this.f434f.getPath() + ").");
            return;
        }
        for (File file : fileArrListFiles) {
            Log.i("MultiDex", "Trying to delete old file " + file.getPath() + " of size " + file.length());
            if (file.delete()) {
                Log.i("MultiDex", "Deleted old file " + file.getPath());
            } else {
                Log.w("MultiDex", "Failed to delete old file " + file.getPath());
            }
        }
    }

    private static void b(Closeable closeable) {
        try {
            closeable.close();
        } catch (IOException e2) {
            Log.w("MultiDex", "Failed to close resource", e2);
        }
    }

    private static void d(ZipFile zipFile, ZipEntry zipEntry, File file, String str) throws IOException {
        InputStream inputStream = zipFile.getInputStream(zipEntry);
        File fileCreateTempFile = File.createTempFile("tmp-" + str, ".zip", file.getParentFile());
        Log.i("MultiDex", "Extracting " + 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 i2 = inputStream.read(bArr); i2 != -1; i2 = inputStream.read(bArr)) {
                    zipOutputStream.write(bArr, 0, i2);
                }
                zipOutputStream.closeEntry();
                zipOutputStream.close();
                if (!fileCreateTempFile.setReadOnly()) {
                    throw new IOException("Failed to mark readonly \"" + fileCreateTempFile.getAbsolutePath() + "\" (tmp of \"" + file.getAbsolutePath() + "\")");
                }
                Log.i("MultiDex", "Renaming to " + file.getPath());
                if (fileCreateTempFile.renameTo(file)) {
                    return;
                }
                throw new IOException("Failed to rename \"" + fileCreateTempFile.getAbsolutePath() + "\" to \"" + file.getAbsolutePath() + "\"");
            } catch (Throwable th) {
                zipOutputStream.close();
                throw th;
            }
        } finally {
            b(inputStream);
            fileCreateTempFile.delete();
        }
    }

    private static SharedPreferences i(Context context) {
        return context.getSharedPreferences("multidex.version", Build.VERSION.SDK_INT < 11 ? 0 : 4);
    }

    private static long j(File file) {
        long jLastModified = file.lastModified();
        return jLastModified == -1 ? jLastModified - 1 : jLastModified;
    }

    private static long l(File file) throws IOException {
        long jC = c.c(file);
        return jC == -1 ? jC - 1 : jC;
    }

    private static boolean m(Context context, File file, long j2, String str) {
        SharedPreferences sharedPreferencesI = i(context);
        if (sharedPreferencesI.getLong(str + "timestamp", -1L) == j(file)) {
            if (sharedPreferencesI.getLong(str + "crc", -1L) == j2) {
                return false;
            }
        }
        return true;
    }

    private List<C0014b> p(Context context, String str) throws IOException {
        Log.i("MultiDex", "loading existing secondary dex files");
        String str2 = this.b.getName() + ".classes";
        SharedPreferences sharedPreferencesI = i(context);
        int i2 = sharedPreferencesI.getInt(str + "dex.number", 1);
        ArrayList arrayList = new ArrayList(i2 + (-1));
        int i3 = 2;
        while (i3 <= i2) {
            C0014b c0014b = new C0014b(this.f434f, str2 + i3 + ".zip");
            if (!c0014b.isFile()) {
                throw new IOException("Missing extracted secondary dex file '" + c0014b.getPath() + "'");
            }
            c0014b.b = l(c0014b);
            long j2 = sharedPreferencesI.getLong(str + "dex.crc." + i3, -1L);
            long j3 = sharedPreferencesI.getLong(str + "dex.time." + i3, -1L);
            long jLastModified = c0014b.lastModified();
            if (j3 == jLastModified) {
                String str3 = str2;
                SharedPreferences sharedPreferences = sharedPreferencesI;
                if (j2 == c0014b.b) {
                    arrayList.add(c0014b);
                    i3++;
                    sharedPreferencesI = sharedPreferences;
                    str2 = str3;
                }
            }
            throw new IOException("Invalid extracted dex: " + c0014b + " (key \"" + str + "\"), expected modification time: " + j3 + ", modification time: " + jLastModified + ", expected crc: " + j2 + ", file crc: " + c0014b.b);
        }
        return arrayList;
    }

    private List<C0014b> q() throws IOException {
        boolean z;
        String str = this.b.getName() + ".classes";
        a();
        ArrayList arrayList = new ArrayList();
        ZipFile zipFile = new ZipFile(this.b);
        try {
            ZipEntry entry = zipFile.getEntry("classes2.dex");
            int i2 = 2;
            while (entry != null) {
                C0014b c0014b = new C0014b(this.f434f, str + i2 + ".zip");
                arrayList.add(c0014b);
                Log.i("MultiDex", "Extraction is needed for file " + c0014b);
                int i3 = 0;
                boolean z2 = false;
                while (i3 < 3 && !z2) {
                    int i4 = i3 + 1;
                    d(zipFile, entry, c0014b, str);
                    try {
                        c0014b.b = l(c0014b);
                        z = true;
                    } catch (IOException e2) {
                        Log.w("MultiDex", "Failed to read crc from " + c0014b.getAbsolutePath(), e2);
                        z = false;
                    }
                    StringBuilder sb = new StringBuilder();
                    sb.append("Extraction ");
                    sb.append(z ? "succeeded" : "failed");
                    sb.append(" '");
                    sb.append(c0014b.getAbsolutePath());
                    sb.append("': length ");
                    sb.append(c0014b.length());
                    sb.append(" - crc: ");
                    sb.append(c0014b.b);
                    Log.i("MultiDex", sb.toString());
                    if (!z) {
                        c0014b.delete();
                        if (c0014b.exists()) {
                            Log.w("MultiDex", "Failed to delete corrupted secondary dex '" + c0014b.getPath() + "'");
                        }
                    }
                    z2 = z;
                    i3 = i4;
                }
                if (!z2) {
                    throw new IOException("Could not create zip file " + c0014b.getAbsolutePath() + " for secondary dex (" + i2 + ")");
                }
                i2++;
                entry = zipFile.getEntry("classes" + i2 + ".dex");
            }
            try {
                zipFile.close();
            } catch (IOException e3) {
                Log.w("MultiDex", "Failed to close resource", e3);
            }
            return arrayList;
        } finally {
        }
    }

    private static void r(Context context, String str, long j2, long j3, List<C0014b> list) {
        SharedPreferences.Editor editorEdit = i(context).edit();
        editorEdit.putLong(str + "timestamp", j2);
        editorEdit.putLong(str + "crc", j3);
        editorEdit.putInt(str + "dex.number", list.size() + 1);
        int i2 = 2;
        for (C0014b c0014b : list) {
            editorEdit.putLong(str + "dex.crc." + i2, c0014b.b);
            editorEdit.putLong(str + "dex.time." + i2, c0014b.lastModified());
            i2++;
        }
        editorEdit.commit();
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        this.f437i.release();
        this.f436h.close();
        this.f435g.close();
    }

    List<? extends File> n(Context context, String str, boolean z) throws IOException {
        List<C0014b> listQ;
        List<C0014b> listP;
        Log.i("MultiDex", "MultiDexExtractor.load(" + this.b.getPath() + ", " + z + ", " + str + ")");
        if (!this.f437i.isValid()) {
            throw new IllegalStateException("MultiDexExtractor was closed");
        }
        if (!z && !m(context, this.b, this.f433e, str)) {
            try {
                listP = p(context, str);
            } catch (IOException e2) {
                Log.w("MultiDex", "Failed to reload existing extracted secondary dex files, falling back to fresh extraction", e2);
                listQ = q();
                r(context, str, j(this.b), this.f433e, listQ);
                listP = listQ;
            }
            Log.i("MultiDex", "load found " + listP.size() + " secondary dex files");
            return listP;
        }
        if (z) {
            Log.i("MultiDex", "Forced extraction must be performed.");
        } else {
            Log.i("MultiDex", "Detected that extraction must be performed.");
        }
        listQ = q();
        r(context, str, j(this.b), this.f433e, listQ);
        listP = listQ;
        Log.i("MultiDex", "load found " + listP.size() + " secondary dex files");
        return listP;
    }
}
