package defpackage;

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 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.io.InputStream;
import java.io.Serializable;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.Arrays;
import java.util.concurrent.Executor;

/* JADX INFO: renamed from: ᵳᵿˑᵩ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public abstract class AbstractC4620 {

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public static final ᵤᵮᵨ f17569 = new ᵤᵮᵨ(3);

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:116:0x0180  */
    /* JADX WARN: Removed duplicated region for block: B:124:0x0198  */
    /* JADX WARN: Removed duplicated region for block: B:154:0x01f1  */
    /* JADX WARN: Removed duplicated region for block: B:158:0x01fb  */
    /* JADX WARN: Removed duplicated region for block: B:159:0x01ff  */
    /* JADX WARN: Removed duplicated region for block: B:237:0x02b1  */
    /* JADX WARN: Removed duplicated region for block: B:246:0x02c5 A[ADDED_TO_REGION] */
    /* JADX WARN: Removed duplicated region for block: B:248:0x02c9  */
    /* JADX WARN: Removed duplicated region for block: B:263:0x014c A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:27:0x0070  */
    /* JADX WARN: Removed duplicated region for block: B:299:0x00e1 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:300:0x01b1 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Type inference failed for: r7v10 */
    /* JADX WARN: Type inference failed for: r7v16 */
    /* JADX WARN: Type inference failed for: r7v18 */
    /* JADX WARN: Type inference failed for: r7v19 */
    /* JADX WARN: Type inference failed for: r7v2 */
    /* JADX WARN: Type inference failed for: r7v21 */
    /* JADX WARN: Type inference failed for: r7v23, types: [int] */
    /* JADX WARN: Type inference failed for: r7v24 */
    /* JADX WARN: Type inference failed for: r7v25 */
    /* JADX WARN: Type inference failed for: r7v3, types: [java.io.FileInputStream, java.io.InputStream] */
    /* JADX WARN: Type inference failed for: r7v30 */
    /* JADX WARN: Type inference failed for: r7v31 */
    /* JADX WARN: Type inference failed for: r7v35 */
    /* JADX WARN: Type inference failed for: r7v36 */
    /* JADX WARN: Type inference failed for: r7v37 */
    /* JADX WARN: Type inference failed for: r7v38 */
    /* JADX WARN: Type inference failed for: r7v39 */
    /* JADX WARN: Type inference failed for: r7v4 */
    /* JADX WARN: Type inference failed for: r7v40 */
    /* JADX WARN: Type inference failed for: r7v41 */
    /* JADX WARN: Type inference failed for: r7v42 */
    /* JADX WARN: Type inference failed for: r7v43 */
    /* JADX WARN: Type inference failed for: r7v44 */
    /* JADX WARN: Type inference failed for: r7v45 */
    /* JADX WARN: Type inference failed for: r7v46 */
    /* JADX WARN: Type inference failed for: r7v47 */
    /* JADX WARN: Type inference failed for: r7v48 */
    /* JADX WARN: Type inference failed for: r7v49 */
    /* JADX WARN: Type inference failed for: r7v5 */
    /* JADX WARN: Type inference failed for: r7v50 */
    /* JADX WARN: Type inference failed for: r7v51 */
    /* JADX WARN: Type inference failed for: r7v52 */
    /* JADX WARN: Type inference failed for: r7v53 */
    /* JADX WARN: Type inference failed for: r7v6 */
    /* JADX WARN: Type inference failed for: r7v7 */
    /* JADX WARN: Type inference failed for: r7v8 */
    /* JADX WARN: Type inference failed for: r7v9 */
    /* JADX WARN: Type inference failed for: r9v7 */
    /* JADX WARN: Type inference failed for: r9v8, types: [boolean] */
    /* JADX WARN: Type inference failed for: r9v9 */
    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static void m9750(Context context, Executor executor, InterfaceC5272 interfaceC5272, boolean z) {
        boolean z2;
        ?? r7;
        C2394[] c2394Arr;
        C2394[] c2394Arr2;
        C2394[] c2394Arr3;
        byte[] bArr;
        ?? r72;
        boolean z3;
        Throwable th;
        Throwable th2;
        ?? r73;
        boolean z4;
        boolean z5;
        ?? r74;
        ByteArrayOutputStream byteArrayOutputStream;
        ᵯᵿᵦ r0;
        ?? r75;
        String str;
        FileInputStream fileInputStream;
        ?? r76;
        boolean z6;
        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();
                            z6 = j == packageInfo.lastUpdateTime;
                            if (z6) {
                                interfaceC5272.m10869(2, null);
                            }
                        } finally {
                        }
                    } catch (IOException unused) {
                        z6 = false;
                    }
                    if (z6) {
                    }
                } else {
                    z6 = false;
                    if (z6) {
                        context.getPackageName();
                        AbstractC3354.m7708(context, false);
                        return;
                    }
                }
            }
            context.getPackageName();
            byte[] bArr2 = ᵻᵕᵭ.ːˏᵟᵯ;
            File file2 = new File(new File("/data/misc/profiles/cur/0", packageName), "primary.prof");
            ᵯᵿᵦ r2 = new ᵯᵿᵦ(assets, executor, interfaceC5272, name, file2);
            byte[] bArr3 = (byte[]) r2.ᵷᵺʾᵖ;
            if (bArr3 != null) {
                if (file2.exists()) {
                    if (!file2.canWrite()) {
                        r2.ᵟᵹᵙ(4, (Serializable) null);
                    }
                    r2.ᵲᵶᵳˈ = true;
                    try {
                        r7 = r2.ᵝᵶ(assets, "dexopt/baseline.prof");
                    } catch (FileNotFoundException e) {
                        interfaceC5272.m10869(6, e);
                        r7 = 0;
                    } catch (IOException e2) {
                        interfaceC5272.m10869(7, e2);
                        r7 = 0;
                    }
                    try {
                        if (r7 != 0) {
                            try {
                            } catch (IOException e3) {
                                interfaceC5272.m10869(7, e3);
                                try {
                                    r7.close();
                                } catch (IOException e4) {
                                    interfaceC5272.m10869(7, e4);
                                }
                                c2394Arr = null;
                            } catch (IllegalStateException e5) {
                                interfaceC5272.m10869(8, e5);
                                r7.close();
                                c2394Arr = null;
                            }
                            if (!Arrays.equals(bArr2, ᵲᵧˇ.ᵨᵼⁱ((InputStream) r7, 4))) {
                                throw new IllegalStateException("Invalid magic");
                            }
                            c2394Arr = ᵻᵕᵭ.ˁˌᵩ((FileInputStream) r7, ᵲᵧˇ.ᵨᵼⁱ((InputStream) r7, 4), (String) r2.ᵵᵽ);
                            try {
                                r7.close();
                            } catch (IOException e6) {
                                interfaceC5272.m10869(7, e6);
                            }
                            r2.ˊᵫᵗ = c2394Arr;
                        }
                        c2394Arr2 = (C2394[]) r2.ˊᵫᵗ;
                        if (c2394Arr2 != null && (r7 = Build.VERSION.SDK_INT) >= 24 && (r7 >= 31 || r7 == 24 || r7 == 25)) {
                            try {
                                str = "dexopt/baseline.profm";
                                fileInputStream = r2.ᵝᵶ(assets, "dexopt/baseline.profm");
                                r75 = str;
                            } catch (FileNotFoundException e7) {
                                interfaceC5272.m10869(9, e7);
                                r75 = r7;
                            } catch (IOException e8) {
                                interfaceC5272.m10869(7, e8);
                                r75 = r7;
                            } catch (IllegalStateException e9) {
                                r2.ˊᵫᵗ = null;
                                interfaceC5272.m10869(8, e9);
                                r75 = r7;
                            }
                            if (fileInputStream == null) {
                                try {
                                    if (!Arrays.equals(ᵻᵕᵭ.ᵷᵺʾᵖ, ᵲᵧˇ.ᵨᵼⁱ(fileInputStream, 4))) {
                                        throw new IllegalStateException("Invalid magic");
                                    }
                                    byte[] bArr4 = ᵲᵧˇ.ᵨᵼⁱ(fileInputStream, 4);
                                    r2.ˊᵫᵗ = ᵻᵕᵭ.ʿٴˇ(fileInputStream, bArr4, bArr3, c2394Arr2);
                                    fileInputStream.close();
                                    r0 = r2;
                                    r7 = bArr4;
                                    if (r0 != null) {
                                        r2 = r0;
                                    }
                                } finally {
                                }
                            } else {
                                if (fileInputStream != null) {
                                    fileInputStream.close();
                                    r75 = str;
                                }
                                r0 = null;
                                r7 = r75;
                                if (r0 != null) {
                                }
                            }
                        }
                        InterfaceC5272 interfaceC52722 = (InterfaceC5272) r2.ːˏᵟᵯ;
                        c2394Arr3 = (C2394[]) r2.ˊᵫᵗ;
                        byte[] bArr5 = (byte[]) r2.ᵷᵺʾᵖ;
                        ?? r77 = r7;
                        r77 = r7;
                        if (c2394Arr3 != null && bArr5 != null) {
                            z5 = r2.ᵲᵶᵳˈ;
                            if (z5) {
                                ᵕᵩʽᵲ.ᵪᵹᵥᵻ("This device doesn't support aot. Did you call deviceSupportsAotProfile()?");
                                return;
                            }
                            try {
                                byteArrayOutputStream = new ByteArrayOutputStream();
                                try {
                                    byteArrayOutputStream.write(bArr2);
                                    byteArrayOutputStream.write(bArr5);
                                } finally {
                                }
                            } catch (IOException e10) {
                                interfaceC52722.m10869(7, e10);
                                r74 = z5;
                            } catch (IllegalStateException e11) {
                                interfaceC52722.m10869(8, e11);
                                r74 = z5;
                            }
                            if (ᵻᵕᵭ.ᵨˈʼﹶ(byteArrayOutputStream, bArr5, c2394Arr3)) {
                                r2.ᵨᵣ = byteArrayOutputStream.toByteArray();
                                byteArrayOutputStream.close();
                                r74 = byteArrayOutputStream;
                                r2.ˊᵫᵗ = null;
                                r77 = r74;
                            } else {
                                interfaceC52722.m10869(5, null);
                                r2.ˊᵫᵗ = null;
                                byteArrayOutputStream.close();
                                r77 = byteArrayOutputStream;
                            }
                        }
                        bArr = (byte[]) r2.ᵨᵣ;
                        if (bArr != null) {
                            z3 = false;
                            r73 = 1;
                        } else {
                            try {
                                if (!r2.ᵲᵶᵳˈ) {
                                    ᵕᵩʽᵲ.ᵪᵹᵥᵻ("This device doesn't support aot. Did you call deviceSupportsAotProfile()?");
                                    return;
                                }
                                try {
                                    try {
                                        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bArr);
                                        try {
                                            try {
                                                FileOutputStream fileOutputStream = new FileOutputStream((File) r2.ᵻˈᵦʼ);
                                                try {
                                                    try {
                                                        FileChannel channel = fileOutputStream.getChannel();
                                                        try {
                                                            FileLock fileLockTryLock = channel.tryLock();
                                                            try {
                                                                try {
                                                                    if (fileLockTryLock != null) {
                                                                        try {
                                                                            if (fileLockTryLock.isValid()) {
                                                                                byte[] bArr6 = new byte[512];
                                                                                while (true) {
                                                                                    int i = byteArrayInputStream.read(bArr6);
                                                                                    if (i <= 0) {
                                                                                        break;
                                                                                    } else {
                                                                                        fileOutputStream.write(bArr6, 0, i);
                                                                                    }
                                                                                }
                                                                                r73 = 1;
                                                                                r2.ᵟᵹᵙ(1, (Serializable) null);
                                                                                fileLockTryLock.close();
                                                                                channel.close();
                                                                                fileOutputStream.close();
                                                                                byteArrayInputStream.close();
                                                                                r2.ᵨᵣ = null;
                                                                                r2.ˊᵫᵗ = null;
                                                                                z3 = true;
                                                                            }
                                                                        } catch (Throwable th3) {
                                                                            th = th3;
                                                                            Throwable th4 = th;
                                                                            if (fileLockTryLock == null) {
                                                                                throw th4;
                                                                            }
                                                                            try {
                                                                                fileLockTryLock.close();
                                                                                throw th4;
                                                                            } catch (Throwable th5) {
                                                                                th4.addSuppressed(th5);
                                                                                throw th4;
                                                                            }
                                                                        }
                                                                    }
                                                                    throw new IOException("Unable to acquire a lock on the underlying file channel.");
                                                                } catch (Throwable th6) {
                                                                    th = th6;
                                                                    Throwable th7 = th;
                                                                    if (channel == null) {
                                                                        throw th7;
                                                                    }
                                                                    try {
                                                                        channel.close();
                                                                        throw th7;
                                                                    } catch (Throwable th8) {
                                                                        th7.addSuppressed(th8);
                                                                        throw th7;
                                                                    }
                                                                }
                                                            } catch (Throwable th9) {
                                                                th = th9;
                                                            }
                                                        } catch (Throwable th10) {
                                                            th = th10;
                                                        }
                                                    } catch (Throwable th11) {
                                                        th = th11;
                                                        th2 = th;
                                                        try {
                                                            fileOutputStream.close();
                                                            throw th2;
                                                        } catch (Throwable th12) {
                                                            th2.addSuppressed(th12);
                                                            throw th2;
                                                        }
                                                    }
                                                } catch (Throwable th13) {
                                                    th = th13;
                                                    th2 = th;
                                                    fileOutputStream.close();
                                                    throw th2;
                                                }
                                            } catch (Throwable th14) {
                                                th = th14;
                                                th = th;
                                                try {
                                                    byteArrayInputStream.close();
                                                    throw th;
                                                } catch (Throwable th15) {
                                                    th.addSuppressed(th15);
                                                    throw th;
                                                }
                                            }
                                        } catch (Throwable th16) {
                                            th = th16;
                                            th = th;
                                            byteArrayInputStream.close();
                                            throw th;
                                        }
                                    } catch (FileNotFoundException e12) {
                                        e = e12;
                                        r77 = 1;
                                        r2.ᵟᵹᵙ(6, e);
                                        r72 = r77;
                                        z3 = false;
                                        r73 = r72;
                                        if (z3) {
                                        }
                                        z4 = z3;
                                        r76 = r73;
                                        AbstractC3354.m7708(context, (z4 || !z) ? 0 : r76);
                                    } catch (IOException e13) {
                                        e = e13;
                                        r77 = 1;
                                        r2.ᵟᵹᵙ(7, e);
                                        r72 = r77;
                                        z3 = false;
                                        r73 = r72;
                                        if (z3) {
                                        }
                                        z4 = z3;
                                        r76 = r73;
                                        AbstractC3354.m7708(context, (z4 || !z) ? 0 : r76);
                                    }
                                } catch (FileNotFoundException e14) {
                                    e = e14;
                                    r2.ᵟᵹᵙ(6, e);
                                    r72 = r77;
                                    z3 = false;
                                    r73 = r72;
                                    if (z3) {
                                    }
                                    z4 = z3;
                                    r76 = r73;
                                    AbstractC3354.m7708(context, (z4 || !z) ? 0 : r76);
                                } catch (IOException e15) {
                                    e = e15;
                                    r2.ᵟᵹᵙ(7, e);
                                    r72 = r77;
                                    z3 = false;
                                    r73 = r72;
                                    if (z3) {
                                    }
                                    z4 = z3;
                                    r76 = r73;
                                    AbstractC3354.m7708(context, (z4 || !z) ? 0 : r76);
                                }
                            } finally {
                                r2.ᵨᵣ = null;
                                r2.ˊᵫᵗ = null;
                            }
                        }
                        if (z3) {
                            m9751(packageInfo, filesDir);
                        }
                        z4 = z3;
                        r76 = r73;
                    } finally {
                    }
                } else {
                    try {
                        if (!file2.createNewFile()) {
                            r2.ᵟᵹᵙ(4, (Serializable) null);
                        }
                        r2.ᵲᵶᵳˈ = true;
                        r7 = r2.ᵝᵶ(assets, "dexopt/baseline.prof");
                        if (r7 != 0) {
                        }
                        c2394Arr2 = (C2394[]) r2.ˊᵫᵗ;
                        if (c2394Arr2 != null) {
                            str = "dexopt/baseline.profm";
                            fileInputStream = r2.ᵝᵶ(assets, "dexopt/baseline.profm");
                            r75 = str;
                            if (fileInputStream == null) {
                            }
                        }
                        InterfaceC5272 interfaceC527222 = (InterfaceC5272) r2.ːˏᵟᵯ;
                        c2394Arr3 = (C2394[]) r2.ˊᵫᵗ;
                        byte[] bArr52 = (byte[]) r2.ᵷᵺʾᵖ;
                        ?? r772 = r7;
                        r772 = r7;
                        if (c2394Arr3 != null) {
                            z5 = r2.ᵲᵶᵳˈ;
                            if (z5) {
                            }
                        }
                        bArr = (byte[]) r2.ᵨᵣ;
                        if (bArr != null) {
                        }
                        if (z3) {
                        }
                        z4 = z3;
                        r76 = r73;
                    } catch (IOException unused2) {
                        z2 = true;
                        r2.ᵟᵹᵙ(4, (Serializable) null);
                    }
                }
                AbstractC3354.m7708(context, (z4 || !z) ? 0 : r76);
            }
            r2.ᵟᵹᵙ(3, Integer.valueOf(Build.VERSION.SDK_INT));
            z2 = true;
            z4 = false;
            r76 = z2;
            AbstractC3354.m7708(context, (z4 || !z) ? 0 : r76);
        } catch (PackageManager.NameNotFoundException e16) {
            interfaceC5272.m10869(7, e16);
            AbstractC3354.m7708(context, false);
        }
    }

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public static void m9751(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) {
        }
    }
}
