package androidx.profileinstaller;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.os.Build;
import android.util.Log;
import defpackage.m38;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import java.util.concurrent.Executor;

/* JADX INFO: loaded from: classes.dex */
public class f {
    public static final d a = new a();
    public static final d b = new b();

    public class a implements d {
        @Override // androidx.profileinstaller.f.d
        public final void a(int i, Object obj) {
        }
    }

    public class b implements d {
        @Override // androidx.profileinstaller.f.d
        public final void a(int i, Object obj) {
            String str;
            switch (i) {
                case 1:
                    str = "RESULT_INSTALL_SUCCESS";
                    break;
                case 2:
                    str = "RESULT_ALREADY_INSTALLED";
                    break;
                case 3:
                    str = "RESULT_UNSUPPORTED_ART_VERSION";
                    break;
                case 4:
                    str = "RESULT_NOT_WRITABLE";
                    break;
                case 5:
                    str = "RESULT_DESIRED_FORMAT_UNSUPPORTED";
                    break;
                case 6:
                    str = "RESULT_BASELINE_PROFILE_NOT_FOUND";
                    break;
                case 7:
                    str = "RESULT_IO_EXCEPTION";
                    break;
                case 8:
                    str = "RESULT_PARSE_EXCEPTION";
                    break;
                case 9:
                default:
                    str = "";
                    break;
                case 10:
                    str = "RESULT_INSTALL_SKIP_FILE_SUCCESS";
                    break;
                case 11:
                    str = "RESULT_DELETE_SKIP_FILE_SUCCESS";
                    break;
            }
            if (i == 6 || i == 7 || i == 8) {
                Log.e("ProfileInstaller", str, (Throwable) obj);
            }
        }
    }

    @m38
    @Retention(RetentionPolicy.SOURCE)
    public @interface c {
    }

    public interface d {
        void a(int i, Object obj);
    }

    @m38
    @Retention(RetentionPolicy.SOURCE)
    public @interface e {
    }

    public static void a(PackageInfo packageInfo, File file) {
        try {
            DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(new File(file, "profileinstaller_profileWrittenFor_lastUpdateTime.dat")));
            try {
                dataOutputStream.writeLong(packageInfo.lastUpdateTime);
                dataOutputStream.close();
            } finally {
            }
        } catch (IOException unused) {
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:116:0x017f  */
    /* JADX WARN: Removed duplicated region for block: B:146:0x01d0  */
    /* JADX WARN: Removed duplicated region for block: B:150:0x01da  */
    /* JADX WARN: Removed duplicated region for block: B:151:0x01de  */
    /* JADX WARN: Removed duplicated region for block: B:200:0x0247  */
    /* JADX WARN: Removed duplicated region for block: B:209:0x025e A[ADDED_TO_REGION] */
    /* JADX WARN: Removed duplicated region for block: B:211:0x0262  */
    /* JADX WARN: Removed duplicated region for block: B:254:0x0191 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:27:0x0070  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static void b(Context context, Executor executor, d dVar, boolean z) {
        char c2;
        FileInputStream fileInputStreamA;
        androidx.profileinstaller.c[] cVarArrG;
        androidx.profileinstaller.c[] cVarArr;
        byte[] bArr;
        boolean z2;
        boolean z3;
        ByteArrayOutputStream byteArrayOutputStream;
        int i;
        androidx.profileinstaller.b bVar;
        FileInputStream fileInputStreamA2;
        boolean z4;
        Context applicationContext = context.getApplicationContext();
        String packageName = applicationContext.getPackageName();
        ApplicationInfo applicationInfo = applicationContext.getApplicationInfo();
        AssetManager assets = applicationContext.getAssets();
        String name = new File(applicationInfo.sourceDir).getName();
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
            File filesDir = context.getFilesDir();
            if (!z) {
                File file = new File(filesDir, "profileinstaller_profileWrittenFor_lastUpdateTime.dat");
                if (file.exists()) {
                    try {
                        DataInputStream dataInputStream = new DataInputStream(new FileInputStream(file));
                        try {
                            long j = dataInputStream.readLong();
                            dataInputStream.close();
                            z4 = j == packageInfo.lastUpdateTime;
                            if (z4) {
                                dVar.a(2, null);
                            }
                        } finally {
                        }
                    } catch (IOException unused) {
                        z4 = false;
                    }
                    if (z4) {
                    }
                } else {
                    z4 = false;
                    if (z4) {
                        context.getPackageName();
                        i.c(context, false);
                        return;
                    }
                }
            }
            context.getPackageName();
            int i2 = Build.VERSION.SDK_INT;
            File file2 = new File(new File("/data/misc/profiles/cur/0", packageName), "primary.prof");
            androidx.profileinstaller.b bVar2 = new androidx.profileinstaller.b(assets, executor, dVar, name, file2);
            byte[] bArr2 = bVar2.c;
            if (bArr2 == null) {
                bVar2.b(3, Integer.valueOf(i2));
            } else {
                if (!file2.exists()) {
                    try {
                        file2.createNewFile();
                    } catch (IOException unused2) {
                        c2 = 1;
                        bVar2.b(4, null);
                        z3 = false;
                    }
                } else if (!file2.canWrite()) {
                    bVar2.b(4, null);
                }
                bVar2.f = true;
                try {
                    fileInputStreamA = bVar2.a(assets, "dexopt/baseline.prof");
                } catch (FileNotFoundException e2) {
                    dVar.a(6, e2);
                    fileInputStreamA = null;
                } catch (IOException e3) {
                    dVar.a(7, e3);
                    fileInputStreamA = null;
                }
                byte[] bArr3 = h.a;
                c2 = '\b';
                try {
                    if (fileInputStreamA != null) {
                        try {
                        } catch (IOException e4) {
                            dVar.a(7, e4);
                            try {
                                fileInputStreamA.close();
                            } catch (IOException e5) {
                                dVar.a(7, e5);
                            }
                            cVarArrG = null;
                        } catch (IllegalStateException e6) {
                            dVar.a(8, e6);
                            fileInputStreamA.close();
                            cVarArrG = null;
                        }
                        if (!Arrays.equals(bArr3, androidx.profileinstaller.d.b(fileInputStreamA, 4))) {
                            throw new IllegalStateException("Invalid magic");
                        }
                        cVarArrG = h.g(fileInputStreamA, androidx.profileinstaller.d.b(fileInputStreamA, 4), bVar2.e);
                        try {
                            fileInputStreamA.close();
                        } catch (IOException e7) {
                            dVar.a(7, e7);
                        }
                        bVar2.g = cVarArrG;
                    }
                    androidx.profileinstaller.c[] cVarArr2 = bVar2.g;
                    if (cVarArr2 != null && (i = Build.VERSION.SDK_INT) <= 34) {
                        switch (i) {
                            case 31:
                            case 32:
                            case 33:
                            case 34:
                                try {
                                    fileInputStreamA2 = bVar2.a(assets, "dexopt/baseline.profm");
                                } catch (FileNotFoundException e8) {
                                    dVar.a(9, e8);
                                } catch (IOException e9) {
                                    dVar.a(7, e9);
                                } catch (IllegalStateException e10) {
                                    bVar2.g = null;
                                    dVar.a(8, e10);
                                }
                                if (fileInputStreamA2 != null) {
                                    try {
                                        if (!Arrays.equals(h.b, androidx.profileinstaller.d.b(fileInputStreamA2, 4))) {
                                            throw new IllegalStateException("Invalid magic");
                                        }
                                        bVar2.g = h.d(fileInputStreamA2, androidx.profileinstaller.d.b(fileInputStreamA2, 4), bArr2, cVarArr2);
                                        fileInputStreamA2.close();
                                        bVar = bVar2;
                                        if (bVar != null) {
                                            bVar2 = bVar;
                                        }
                                    } finally {
                                    }
                                    break;
                                } else {
                                    if (fileInputStreamA2 != null) {
                                        fileInputStreamA2.close();
                                    }
                                    bVar = null;
                                    if (bVar != null) {
                                    }
                                }
                            default:
                                d dVar2 = bVar2.b;
                                cVarArr = bVar2.g;
                                byte[] bArr4 = bVar2.c;
                                if (cVarArr != null && bArr4 != null) {
                                    if (bVar2.f) {
                                        throw new IllegalStateException("This device doesn't support aot. Did you call deviceSupportsAotProfile()?");
                                    }
                                    try {
                                        byteArrayOutputStream = new ByteArrayOutputStream();
                                        try {
                                            byteArrayOutputStream.write(bArr3);
                                            byteArrayOutputStream.write(bArr4);
                                        } finally {
                                        }
                                    } catch (IOException e11) {
                                        dVar2.a(7, e11);
                                    } catch (IllegalStateException e12) {
                                        dVar2.a(8, e12);
                                    }
                                    if (h.i(byteArrayOutputStream, bArr4, cVarArr)) {
                                        bVar2.h = byteArrayOutputStream.toByteArray();
                                        byteArrayOutputStream.close();
                                        bVar2.g = null;
                                    } else {
                                        dVar2.a(5, null);
                                        bVar2.g = null;
                                        byteArrayOutputStream.close();
                                    }
                                }
                                bArr = bVar2.h;
                                if (bArr != null) {
                                    z2 = false;
                                    c2 = 1;
                                } else {
                                    try {
                                        if (!bVar2.f) {
                                            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(bVar2.d);
                                                    try {
                                                        try {
                                                            byte[] bArr5 = new byte[512];
                                                            while (true) {
                                                                int i3 = byteArrayInputStream.read(bArr5);
                                                                if (i3 > 0) {
                                                                    fileOutputStream.write(bArr5, 0, i3);
                                                                } else {
                                                                    c2 = 1;
                                                                    try {
                                                                        bVar2.b(1, null);
                                                                        fileOutputStream.close();
                                                                        byteArrayInputStream.close();
                                                                        bVar2.h = null;
                                                                        bVar2.g = null;
                                                                        z2 = true;
                                                                    } catch (Throwable th) {
                                                                        th = th;
                                                                        Throwable th2 = th;
                                                                        try {
                                                                            fileOutputStream.close();
                                                                            throw th2;
                                                                        } catch (Throwable th3) {
                                                                            th2.addSuppressed(th3);
                                                                            throw th2;
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        } catch (Throwable th4) {
                                                            th = th4;
                                                            Throwable th5 = th;
                                                            try {
                                                                byteArrayInputStream.close();
                                                                throw th5;
                                                            } catch (Throwable th6) {
                                                                th5.addSuppressed(th6);
                                                                throw th5;
                                                            }
                                                        }
                                                    } catch (Throwable th7) {
                                                        th = th7;
                                                    }
                                                } catch (Throwable th8) {
                                                    th = th8;
                                                }
                                            } catch (FileNotFoundException e13) {
                                                e = e13;
                                                c2 = 1;
                                                bVar2.b(6, e);
                                                z2 = false;
                                                if (z2) {
                                                }
                                                z3 = z2;
                                                i.c(context, (z3 && z) ? c2 : 0);
                                            } catch (IOException e14) {
                                                e = e14;
                                                c2 = 1;
                                                bVar2.b(7, e);
                                                z2 = false;
                                                if (z2) {
                                                }
                                                z3 = z2;
                                                i.c(context, (z3 && z) ? c2 : 0);
                                            }
                                        } catch (FileNotFoundException e15) {
                                            e = e15;
                                            bVar2.b(6, e);
                                            z2 = false;
                                            if (z2) {
                                            }
                                            z3 = z2;
                                            i.c(context, (z3 && z) ? c2 : 0);
                                        } catch (IOException e16) {
                                            e = e16;
                                            bVar2.b(7, e);
                                            z2 = false;
                                            if (z2) {
                                            }
                                            z3 = z2;
                                            i.c(context, (z3 && z) ? c2 : 0);
                                        }
                                    } finally {
                                        bVar2.h = null;
                                        bVar2.g = null;
                                    }
                                }
                                if (z2) {
                                    a(packageInfo, filesDir);
                                }
                                z3 = z2;
                                i.c(context, (z3 && z) ? c2 : 0);
                        }
                    }
                    d dVar22 = bVar2.b;
                    cVarArr = bVar2.g;
                    byte[] bArr42 = bVar2.c;
                    if (cVarArr != null) {
                        if (bVar2.f) {
                        }
                    }
                    bArr = bVar2.h;
                    if (bArr != null) {
                    }
                    if (z2) {
                    }
                    z3 = z2;
                    i.c(context, (z3 && z) ? c2 : 0);
                } finally {
                }
            }
            c2 = 1;
            z3 = false;
            i.c(context, (z3 && z) ? c2 : 0);
        } catch (PackageManager.NameNotFoundException e17) {
            dVar.a(7, e17);
            i.c(context, false);
        }
    }
}
