package d3;

import android.content.res.AssetManager;
import android.os.Build;
import androidx.annotation.Nullable;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.Arrays;
import java.util.concurrent.Executor;

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

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

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

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

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

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public boolean f24945f = false;

    @Nullable
    private final byte[] mDesiredVersion = desiredVersion();

    @Nullable
    private b[] mProfile;

    @Nullable
    private byte[] mTranscodedProfile;

    public a(AssetManager assetManager, m.a aVar, h hVar, String str, File file) {
        this.f24940a = assetManager;
        this.f24941b = aVar;
        this.f24942c = hVar;
        this.f24944e = str;
        this.f24943d = file;
    }

    @Nullable
    private a addMetadata(b[] bVarArr, byte[] bArr) {
        InputStream inputStreamOpenStreamFromAssets;
        h hVar = this.f24942c;
        try {
            inputStreamOpenStreamFromAssets = openStreamFromAssets(this.f24940a, "dexopt/baseline.profm");
        } catch (FileNotFoundException e10) {
            hVar.onResultReceived(9, e10);
        } catch (IOException e11) {
            hVar.onResultReceived(7, e11);
        } catch (IllegalStateException e12) {
            this.mProfile = null;
            hVar.onResultReceived(8, e12);
        }
        if (inputStreamOpenStreamFromAssets == null) {
            if (inputStreamOpenStreamFromAssets != null) {
                inputStreamOpenStreamFromAssets.close();
            }
            return null;
        }
        try {
            if (!Arrays.equals(androidx.profileinstaller.a.f3655b, c.b(inputStreamOpenStreamFromAssets, 4))) {
                throw c.error("Invalid magic");
            }
            this.mProfile = androidx.profileinstaller.a.e(inputStreamOpenStreamFromAssets, c.b(inputStreamOpenStreamFromAssets, 4), bArr, bVarArr);
            inputStreamOpenStreamFromAssets.close();
            return this;
        } catch (Throwable th) {
            try {
                inputStreamOpenStreamFromAssets.close();
            } catch (Throwable th2) {
                th.addSuppressed(th2);
            }
            throw th;
        }
    }

    @Nullable
    private static byte[] desiredVersion() {
        int i10 = Build.VERSION.SDK_INT;
        if (i10 >= 31) {
            return n.f24970a;
        }
        switch (i10) {
            case 24:
            case 25:
                return n.f24974e;
            case 26:
                return n.f24973d;
            case 27:
                return n.f24972c;
            case 28:
            case 29:
            case 30:
                return n.f24971b;
            default:
                return null;
        }
    }

    @Nullable
    private InputStream getProfileInputStream(AssetManager assetManager) {
        h hVar = this.f24942c;
        try {
            return openStreamFromAssets(assetManager, "dexopt/baseline.prof");
        } catch (FileNotFoundException e10) {
            hVar.onResultReceived(6, e10);
            return null;
        } catch (IOException e11) {
            hVar.onResultReceived(7, e11);
            return null;
        }
    }

    @Nullable
    private InputStream openStreamFromAssets(AssetManager assetManager, String str) throws IOException {
        try {
            return assetManager.openFd(str).createInputStream();
        } catch (FileNotFoundException e10) {
            String message = e10.getMessage();
            if (message != null && message.contains("compressed")) {
                this.f24942c.onDiagnosticReceived(5, null);
            }
            return null;
        }
    }

    @Nullable
    private b[] readProfileInternal(InputStream inputStream) {
        h hVar = this.f24942c;
        try {
            try {
                try {
                    try {
                        if (!Arrays.equals(androidx.profileinstaller.a.f3654a, c.b(inputStream, 4))) {
                            throw c.error("Invalid magic");
                        }
                        b[] bVarArrH = androidx.profileinstaller.a.h(inputStream, c.b(inputStream, 4), this.f24944e);
                        try {
                            inputStream.close();
                            return bVarArrH;
                        } catch (IOException e10) {
                            hVar.onResultReceived(7, e10);
                            return bVarArrH;
                        }
                    } catch (IOException e11) {
                        hVar.onResultReceived(7, e11);
                        return null;
                    }
                } catch (IOException e12) {
                    hVar.onResultReceived(7, e12);
                    inputStream.close();
                    return null;
                }
            } catch (IllegalStateException e13) {
                hVar.onResultReceived(8, e13);
                inputStream.close();
                return null;
            }
        } catch (Throwable th) {
            try {
                inputStream.close();
            } catch (IOException e14) {
                hVar.onResultReceived(7, e14);
            }
            throw th;
        }
    }

    private void result(int i10, @Nullable Object obj) {
        this.f24941b.execute(new androidx.activity.n(this, i10, obj, 5));
    }

    public final boolean a() {
        if (this.mDesiredVersion == null) {
            result(3, Integer.valueOf(Build.VERSION.SDK_INT));
            return false;
        }
        File file = this.f24943d;
        if (!file.exists()) {
            try {
                if (!file.createNewFile()) {
                    result(4, null);
                    return false;
                }
            } catch (IOException unused) {
                result(4, null);
                return false;
            }
        } else if (!file.canWrite()) {
            result(4, null);
            return false;
        }
        this.f24945f = true;
        return true;
    }

    public final a b() {
        int i10;
        a aVarAddMetadata;
        if (!this.f24945f) {
            throw new IllegalStateException("This device doesn't support aot. Did you call deviceSupportsAotProfile()?");
        }
        if (this.mDesiredVersion == null) {
            return this;
        }
        InputStream profileInputStream = getProfileInputStream(this.f24940a);
        if (profileInputStream != null) {
            this.mProfile = readProfileInternal(profileInputStream);
        }
        b[] bVarArr = this.mProfile;
        return (bVarArr == null || !((i10 = Build.VERSION.SDK_INT) >= 31 || i10 == 24 || i10 == 25) || (aVarAddMetadata = addMetadata(bVarArr, this.mDesiredVersion)) == null) ? this : aVarAddMetadata;
    }

    public final void c() {
        ByteArrayOutputStream byteArrayOutputStream;
        h hVar = this.f24942c;
        b[] bVarArr = this.mProfile;
        byte[] bArr = this.mDesiredVersion;
        if (bVarArr == null || bArr == null) {
            return;
        }
        if (!this.f24945f) {
            throw new IllegalStateException("This device doesn't support aot. Did you call deviceSupportsAotProfile()?");
        }
        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            try {
                byteArrayOutputStream.write(androidx.profileinstaller.a.f3654a);
                byteArrayOutputStream.write(bArr);
            } catch (Throwable th) {
                try {
                    byteArrayOutputStream.close();
                } catch (Throwable th2) {
                    th.addSuppressed(th2);
                }
                throw th;
            }
        } catch (IOException e10) {
            hVar.onResultReceived(7, e10);
        } catch (IllegalStateException e11) {
            hVar.onResultReceived(8, e11);
        }
        if (androidx.profileinstaller.a.j(byteArrayOutputStream, bArr, bVarArr)) {
            this.mTranscodedProfile = byteArrayOutputStream.toByteArray();
            byteArrayOutputStream.close();
            this.mProfile = null;
        } else {
            hVar.onResultReceived(5, null);
            this.mProfile = null;
            byteArrayOutputStream.close();
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    public final boolean d() {
        byte[] bArr = this.mTranscodedProfile;
        if (bArr == null) {
            return false;
        }
        if (!this.f24945f) {
            throw new IllegalStateException("This device doesn't support aot. Did you call deviceSupportsAotProfile()?");
        }
        try {
            try {
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bArr);
                try {
                    FileOutputStream fileOutputStream = new FileOutputStream(this.f24943d);
                    try {
                        FileChannel channel = fileOutputStream.getChannel();
                        try {
                            FileLock fileLockTryLock = channel.tryLock();
                            try {
                                c.writeAll(byteArrayInputStream, fileOutputStream, fileLockTryLock);
                                result(1, null);
                                if (fileLockTryLock != null) {
                                    fileLockTryLock.close();
                                }
                                channel.close();
                                fileOutputStream.close();
                                byteArrayInputStream.close();
                                return true;
                            } finally {
                            }
                        } finally {
                        }
                    } finally {
                    }
                } catch (Throwable th) {
                    try {
                        byteArrayInputStream.close();
                    } catch (Throwable th2) {
                        th.addSuppressed(th2);
                    }
                    throw th;
                }
            } catch (FileNotFoundException e10) {
                result(6, e10);
                return false;
            } catch (IOException e11) {
                result(7, e11);
                return false;
            }
        } finally {
            this.mTranscodedProfile = null;
            this.mProfile = null;
        }
    }
}
