package a.g;

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 {

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: a.g.b$b, reason: collision with other inner class name */
    /* JADX INFO: compiled from: MultiDexExtractor.java */
    private static class C0018b extends File {

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public long f306c;

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

    b(File file, File file2) throws IOException {
        Log.i("MultiDex", "MultiDexExtractor(" + file.getPath() + ", " + file2.getPath() + ")");
        this.f300c = file;
        this.f302e = file2;
        this.f301d = i(file);
        File file3 = new File(file2, "MultiDex.lock");
        RandomAccessFile randomAccessFile = new RandomAccessFile(file3, "rw");
        this.f303f = randomAccessFile;
        try {
            FileChannel channel = randomAccessFile.getChannel();
            this.f304g = channel;
            try {
                Log.i("MultiDex", "Blocking on lock " + file3.getPath());
                this.h = channel.lock();
                Log.i("MultiDex", file3.getPath() + " locked");
            } catch (IOException e2) {
                e = e2;
                c(this.f304g);
                throw e;
            } catch (Error e3) {
                e = e3;
                c(this.f304g);
                throw e;
            } catch (RuntimeException e4) {
                e = e4;
                c(this.f304g);
                throw e;
            }
        } catch (IOException | Error | RuntimeException e5) {
            c(this.f303f);
            throw e5;
        }
    }

    private void b() {
        File[] fileArrListFiles = this.f302e.listFiles(new a());
        if (fileArrListFiles == null) {
            Log.w("MultiDex", "Failed to list secondary dex dir content (" + this.f302e.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 c(Closeable closeable) {
        try {
            closeable.close();
        } catch (IOException e2) {
            Log.w("MultiDex", "Failed to close resource", e2);
        }
    }

    private static void e(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 i = inputStream.read(bArr); i != -1; i = inputStream.read(bArr)) {
                    zipOutputStream.write(bArr, 0, i);
                }
                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 {
            c(inputStream);
            fileCreateTempFile.delete();
        }
    }

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

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

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

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

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

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

    private static void p(Context context, String str, long j, long j2, List<C0018b> list) {
        SharedPreferences.Editor editorEdit = g(context).edit();
        editorEdit.putLong(str + "timestamp", j);
        editorEdit.putLong(str + "crc", j2);
        editorEdit.putInt(str + "dex.number", list.size() + 1);
        int i = 2;
        for (C0018b c0018b : list) {
            editorEdit.putLong(str + "dex.crc." + i, c0018b.f306c);
            editorEdit.putLong(str + "dex.time." + i, c0018b.lastModified());
            i++;
        }
        editorEdit.commit();
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        this.h.release();
        this.f304g.close();
        this.f303f.close();
    }

    List<? extends File> l(Context context, String str, boolean z) throws IOException {
        List<C0018b> listO;
        List<C0018b> listN;
        Log.i("MultiDex", "MultiDexExtractor.load(" + this.f300c.getPath() + ", " + z + ", " + str + ")");
        if (!this.h.isValid()) {
            throw new IllegalStateException("MultiDexExtractor was closed");
        }
        if (!z && !k(context, this.f300c, this.f301d, str)) {
            try {
                listN = n(context, str);
            } catch (IOException e2) {
                Log.w("MultiDex", "Failed to reload existing extracted secondary dex files, falling back to fresh extraction", e2);
                listO = o();
                p(context, str, h(this.f300c), this.f301d, listO);
                listN = listO;
            }
            Log.i("MultiDex", "load found " + listN.size() + " secondary dex files");
            return listN;
        }
        if (z) {
            Log.i("MultiDex", "Forced extraction must be performed.");
        } else {
            Log.i("MultiDex", "Detected that extraction must be performed.");
        }
        listO = o();
        p(context, str, h(this.f300c), this.f301d, listO);
        listN = listO;
        Log.i("MultiDex", "load found " + listN.size() + " secondary dex files");
        return listN;
    }
}
