package com.bytedance.crash.nativecrash;

import X.C23818bs;
import X.C23838bu;
import X.C27468hl;
import X.C27798iI;
import X.C28338jA;
import android.content.Context;
import android.os.Build;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import java.io.File;

public class NativeImpl {
    public static volatile boolean LIZ;
    public static long LIZIZ = 100;
    public static boolean LIZJ;
    public static volatile boolean LIZLLL;
    public static String LJ;

    public static native boolean doCheckNativeCrash();

    public static native int doCreateCallbackThread();

    public static native void doDelayCheck();

    public static native String doGetCrashHeader(String str);

    public static native long doGetNpthCatchAddr();

    public static native void doRebuildTombstone(String str, String str2, String str3);

    public static native void doSetAlogFlushAddr(long j);

    public static native void doSetLocalCoreInfo(int i);

    public static native void doSetMallocInfoFunctionAddress(long j);

    public static native void doSetNativeCallbackAddr(long j);

    public static native void doSetOnlineCoreInfo(int i);

    public static native void doSetUploadEnd();

    public static native int doStartNativeCrashMonitor(int i, String str, String str2, String str3);

    static {
        Covode.recordClassIndex(25145);
    }

    public static boolean LIZIZ() {
        MethodCollector.i(18808);
        if (!LIZ) {
            MethodCollector.o(18808);
            return false;
        }
        try {
            boolean doCheckNativeCrash = doCheckNativeCrash();
            MethodCollector.o(18808);
            return doCheckNativeCrash;
        } catch (Throwable unused) {
            MethodCollector.o(18808);
            return false;
        }
    }

    public static void LIZJ() {
        MethodCollector.i(18843);
        if (!LIZ) {
            MethodCollector.o(18843);
            return;
        }
        doSetUploadEnd();
        MethodCollector.o(18843);
    }

    public static boolean LIZ() {
        if (LIZLLL) {
            return LIZ;
        }
        LIZLLL = true;
        if (!LIZ) {
            boolean LIZ2 = C23838bu.LIZ("npth");
            LIZ = LIZ2;
            if (LIZ2) {
                LIZ = C23838bu.LIZ("npth_tools");
            }
        }
        return LIZ;
    }

    public static void handleNativeCrash(String str) {
        NativeCrashCollector.onNativeCrash(str);
    }

    public static String LIZIZ(Context context) {
        String str = LJ;
        if (str != null) {
            return str;
        }
        if (new File(context.getApplicationInfo().nativeLibraryDir, "libnpth_dumper.so").exists()) {
            LJ = context.getApplicationInfo().nativeLibraryDir;
        } else {
            LJ = C23818bs.LIZ();
            LIZJ = true;
            C23818bs.LIZ("npth_dumper", "3.1.6-rc.17-ttpfix3");
        }
        return LJ;
    }

    public static boolean LIZ(Context context) {
        MethodCollector.i(16667);
        if (LIZ()) {
            try {
                int doStartNativeCrashMonitor = doStartNativeCrashMonitor(Build.VERSION.SDK_INT, LIZIZ(context), C27468hl.LJIIIZ(context) + "/npth", C27798iI.LJ());
                if (doStartNativeCrashMonitor != 0) {
                    C28338jA.LIZ.LIZ("start Native Failed with code ".concat(String.valueOf(doStartNativeCrashMonitor)), (Throwable) new RuntimeException());
                }
            } catch (Throwable unused) {
                C28338jA.LIZ.LIZ("start Native Failed!!!", (Throwable) new RuntimeException());
                MethodCollector.o(16667);
                return false;
            }
        } else {
            C28338jA.LIZ.LIZ("LOAD lib Failed!!!", (Throwable) new RuntimeException());
        }
        MethodCollector.o(16667);
        return true;
    }
}
