package s6;

import android.content.Context;
import android.os.Build;
import android.util.Log;
import androidx.window.core.h;
import com.getkeepsafe.relinker.MissingLibraryException;
import com.google.android.gms.measurement.internal.a0;
import defpackage.e;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Locale;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import xyz.stream.utils.e0;

/* JADX INFO: loaded from: classes2.dex */
public final class b {

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

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

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

    public b() {
        a0 a0Var = new a0(23);
        h hVar = new h(22);
        this.f32668a = new HashSet();
        this.f32669b = a0Var;
        this.f32670c = hVar;
    }

    public static void d(String str, Object... objArr) {
        String.format(Locale.US, str, objArr);
    }

    public final File a(Context context, String str, String str2) {
        this.f32669b.getClass();
        String strI = a0.i(str);
        return str2 == null || str2.length() == 0 ? new File(context.getDir("lib", 0), strI) : new File(context.getDir("lib", 0), e.j(strI, ".", str2));
    }

    public final void b(Context context, String str) {
        if (context == null) {
            throw new IllegalArgumentException("Given context is null");
        }
        if (str.length() == 0) {
            throw new IllegalArgumentException("Given library is either null or empty");
        }
        d("Beginning load of %s...", str);
        c(context, str, null);
    }

    /* JADX WARN: Code restructure failed: missing block: B:60:0x0126, code lost:
    
        ((java.util.zip.ZipFile) r2).close();
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void c(Context context, String str, String str2) throws Throwable {
        e0 e0Var;
        e0 e0VarM;
        boolean z10;
        String[] strArrO;
        Object obj;
        InputStream inputStream;
        InputStream inputStream2;
        FileOutputStream fileOutputStream;
        FileOutputStream fileOutputStream2;
        a0 a0Var = this.f32669b;
        HashSet hashSet = this.f32668a;
        if (hashSet.contains(str)) {
            d("%s already loaded previously!", str);
            return;
        }
        try {
            a0Var.getClass();
            System.loadLibrary(str);
            hashSet.add(str);
            d("%s (%s) was loaded normally!", str, str2);
        } catch (UnsatisfiedLinkError e10) {
            d("Loading the library normally failed: %s", Log.getStackTraceString(e10));
            d("%s (%s) was not loaded normally, re-linking...", str, str2);
            File fileA = a(context, str, str2);
            if (!fileA.exists()) {
                File dir = context.getDir("lib", 0);
                File fileA2 = a(context, str, str2);
                a0Var.getClass();
                File[] fileArrListFiles = dir.listFiles(new a(a0.i(str)));
                if (fileArrListFiles != null) {
                    for (File file : fileArrListFiles) {
                        if (!file.getAbsolutePath().equals(fileA2.getAbsolutePath())) {
                            file.delete();
                        }
                    }
                }
                String[] strArr = Build.SUPPORTED_ABIS;
                if (strArr.length <= 0) {
                    String str3 = Build.CPU_ABI2;
                    strArr = (str3 == null || str3.length() == 0) ? new String[]{Build.CPU_ABI} : new String[]{Build.CPU_ABI, str3};
                }
                String strI = a0.i(str);
                this.f32670c.getClass();
                try {
                    e0VarM = h.m(context, strArr, strI, this);
                    z10 = true;
                } catch (Throwable th) {
                    th = th;
                    e0Var = null;
                }
                try {
                    if (e0VarM == null) {
                        try {
                            strArrO = h.o(context, strI);
                        } catch (Exception e11) {
                            strArrO = new String[]{e11.toString()};
                        }
                        throw new MissingLibraryException(strI, strArr, strArrO);
                    }
                    int i10 = 0;
                    while (true) {
                        int i11 = i10 + 1;
                        try {
                            if (i10 < 5) {
                                d("Found %s! Extracting...", strI);
                                try {
                                    if (fileA.exists() || fileA.createNewFile()) {
                                        try {
                                            inputStream2 = ((ZipFile) e0VarM.f36128b).getInputStream((ZipEntry) e0VarM.f36129c);
                                            try {
                                                fileOutputStream2 = new FileOutputStream(fileA);
                                                try {
                                                    byte[] bArr = new byte[4096];
                                                    long j10 = 0;
                                                    while (true) {
                                                        int i12 = inputStream2.read(bArr);
                                                        if (i12 == -1) {
                                                            break;
                                                        }
                                                        fileOutputStream2.write(bArr, 0, i12);
                                                        j10 += (long) i12;
                                                        fileOutputStream2 = fileOutputStream2;
                                                        z10 = true;
                                                    }
                                                    fileOutputStream2.flush();
                                                    try {
                                                        fileOutputStream2.getFD().sync();
                                                        if (j10 != fileA.length()) {
                                                            h.k(inputStream2);
                                                            h.k(fileOutputStream2);
                                                        } else {
                                                            h.k(inputStream2);
                                                            h.k(fileOutputStream2);
                                                            fileA.setReadable(z10, false);
                                                            fileA.setExecutable(z10, false);
                                                            fileA.setWritable(z10);
                                                            obj = e0VarM.f36128b;
                                                            if (((ZipFile) obj) != null) {
                                                            }
                                                        }
                                                    } catch (FileNotFoundException unused) {
                                                        h.k(inputStream2);
                                                        h.k(fileOutputStream2);
                                                        i10 = i11;
                                                        z10 = true;
                                                    } catch (IOException unused2) {
                                                        h.k(inputStream2);
                                                        h.k(fileOutputStream2);
                                                        i10 = i11;
                                                        z10 = true;
                                                    } catch (Throwable th2) {
                                                        th = th2;
                                                        inputStream = inputStream2;
                                                        fileOutputStream = fileOutputStream2;
                                                        h.k(inputStream);
                                                        h.k(fileOutputStream);
                                                        throw th;
                                                    }
                                                } catch (FileNotFoundException unused3) {
                                                    fileOutputStream2 = fileOutputStream2;
                                                } catch (IOException unused4) {
                                                    fileOutputStream2 = fileOutputStream2;
                                                } catch (Throwable th3) {
                                                    th = th3;
                                                    fileOutputStream = fileOutputStream2;
                                                    inputStream = inputStream2;
                                                }
                                            } catch (FileNotFoundException unused5) {
                                                fileOutputStream2 = null;
                                                h.k(inputStream2);
                                                h.k(fileOutputStream2);
                                                i10 = i11;
                                                z10 = true;
                                            } catch (IOException unused6) {
                                                fileOutputStream2 = null;
                                                h.k(inputStream2);
                                                h.k(fileOutputStream2);
                                                i10 = i11;
                                                z10 = true;
                                            } catch (Throwable th4) {
                                                th = th4;
                                                inputStream = inputStream2;
                                                fileOutputStream = null;
                                                h.k(inputStream);
                                                h.k(fileOutputStream);
                                                throw th;
                                            }
                                        } catch (FileNotFoundException unused7) {
                                            inputStream2 = null;
                                        } catch (IOException unused8) {
                                            inputStream2 = null;
                                        } catch (Throwable th5) {
                                            th = th5;
                                            inputStream = null;
                                        }
                                    }
                                } catch (IOException unused9) {
                                }
                                i10 = i11;
                                z10 = true;
                            } else {
                                obj = e0VarM.f36128b;
                                if (((ZipFile) obj) != null) {
                                }
                            }
                        } catch (IOException unused10) {
                        }
                    }
                } catch (Throwable th6) {
                    th = th6;
                    e0Var = e0VarM;
                    if (e0Var != null) {
                        try {
                            Object obj2 = e0Var.f36128b;
                            if (((ZipFile) obj2) != null) {
                                ((ZipFile) obj2).close();
                            }
                        } catch (IOException unused11) {
                        }
                    }
                    throw th;
                }
            }
            String absolutePath = fileA.getAbsolutePath();
            a0Var.getClass();
            System.load(absolutePath);
            hashSet.add(str);
            d("%s (%s) was re-linked!", str, str2);
        }
    }
}
